Blender V4.5
ExtraTags.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2002-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstddef>
10#include <cstdlib>
11
12#include <regex>
13
14#include "ExtraTags.h"
15
16ExtraTags::ExtraTags(std::string profile)
17{
18 this->profile = profile;
19 this->tags = std::map<std::string, std::string>();
20}
21
22ExtraTags::~ExtraTags() = default;
23
24bool ExtraTags::isProfile(std::string profile)
25{
26 return this->profile == profile;
27}
28
29bool ExtraTags::addTag(std::string tag, std::string data)
30{
31 tags[tag] = data;
32
33 return true;
34}
35
36int ExtraTags::asInt(std::string tag, bool *r_ok)
37{
38 if (tags.find(tag) == tags.end()) {
39 *r_ok = false;
40 return -1;
41 }
42 *r_ok = true;
43 return atoi(tags[tag].c_str());
44}
45
46float ExtraTags::asFloat(std::string tag, bool *r_ok)
47{
48 if (tags.find(tag) == tags.end()) {
49 *r_ok = false;
50 return -1.0f;
51 }
52 *r_ok = true;
53 return float(atof(tags[tag].c_str()));
54}
55
56std::string ExtraTags::asString(std::string tag, bool *r_ok)
57{
58 if (tags.find(tag) == tags.end()) {
59 *r_ok = false;
60 return "";
61 }
62 *r_ok = true;
63 return tags[tag];
64}
65
66bool ExtraTags::setData(std::string tag, short *data)
67{
68 bool ok = false;
69 int tmp = asInt(tag, &ok);
70 if (ok) {
71 *data = short(tmp);
72 }
73 return ok;
74}
75
76bool ExtraTags::setData(std::string tag, int *data)
77{
78 bool ok = false;
79 int tmp = asInt(tag, &ok);
80 if (ok) {
81 *data = tmp;
82 }
83 return ok;
84}
85
86bool ExtraTags::setData(std::string tag, float *data)
87{
88 bool ok = false;
89 float tmp = asFloat(tag, &ok);
90 if (ok) {
91 *data = tmp;
92 }
93 return ok;
94}
95
96bool ExtraTags::setData(std::string tag, char *data)
97{
98 bool ok = false;
99 int tmp = asInt(tag, &ok);
100 if (ok) {
101 *data = char(tmp);
102 }
103 return ok;
104}
105
106std::string ExtraTags::setData(std::string tag, std::string &data)
107{
108 bool ok = false;
109 std::string tmp = asString(tag, &ok);
110 return (ok) ? tmp : data;
111}
112
113std::vector<std::string> ExtraTags::dataSplitString(const std::string &tag)
114{
115 bool ok = false;
116 const std::string value = asString(tag, &ok);
117 if (!ok) {
118 return std::vector<std::string>();
119 }
120
121 std::vector<std::string> values;
122
123 const std::regex newline_re("[^\\s][^\\r\\n]+");
124 const std::sregex_token_iterator end;
125 std::sregex_token_iterator iter(value.begin(), value.end(), newline_re);
126 for (; iter != end; iter++) {
127 values.push_back(*iter);
128 }
129
130 return values;
131}
BMesh const char void * data
ExtraTags(const std::string profile)
Definition ExtraTags.cpp:16
virtual ~ExtraTags()
bool isProfile(std::string profile)
Definition ExtraTags.cpp:24
std::vector< std::string > dataSplitString(const std::string &tag)
bool setData(std::string tag, short *data)
Definition ExtraTags.cpp:66
bool addTag(std::string tag, std::string data)
Definition ExtraTags.cpp:29