cgv
Loading...
Searching...
No Matches
query.h
1#pragma once
2
3// This file contains implementation of helper methods and classes to perform query operations on a tinyxml2::XMLDocument.
4
5#include <cgv/media/color.h>
6#include <cgv/math/fvec.h>
7#include <cgv/utils/scan.h>
8
9#include "../tinyxml2/tinyxml2.h"
10
11namespace cgv {
12namespace xml {
13
27class FindElementByNameVisitor : public tinyxml2::XMLVisitor {
28private:
29 std::string name = "";
30 const tinyxml2::XMLElement* result = nullptr;
31
32public:
34
35 FindElementByNameVisitor(const std::string& name) : name(name) {}
36
39 void SetQueryName(const std::string& name) {
40
41 this->name = name;
42 }
43
46 const tinyxml2::XMLElement* Result() const {
47
48 return result;
49 }
50
51 bool VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* attribute) override {
52
53 if(strcmp(element.Name(), name.c_str()) == 0) {
54 result = &element;
55 return false;
56 }
57
58 return true;
59 }
60};
61
67static tinyxml2::XMLError QueryStringAttribute(const tinyxml2::XMLElement& elem, const std::string& name, std::string& value) {
68
69 const char* c = "";
70 tinyxml2::XMLError result = elem.QueryStringAttribute(name.c_str(), &c);
71 if (result == tinyxml2::XML_SUCCESS)
72 value = std::string(c);
73
74 return result;
75};
76
87static tinyxml2::XMLError QueryBoolAttribute(const tinyxml2::XMLElement& elem, const std::string& name, bool& value) {
88
89 std::string str = "";
90 tinyxml2::XMLError result = QueryStringAttribute(elem, name, str);
91 if(result == tinyxml2::XML_SUCCESS) {
92 if(str.length() > 0) {
93 str = cgv::utils::to_lower(str);
94
95 if(str[0] == '0' || str == "false") {
96 value = false;
97 return result;
98 } else if(str[0] == '1' || str == "true") {
99 value = true;
100 return result;
101 }
102
103 return tinyxml2::XMLError::XML_WRONG_ATTRIBUTE_TYPE;
104 } else {
105 value = false;
106 return result;
107 }
108 }
109
110 return result;
111};
112
126template <typename T, cgv::type::uint32_type N>
127static tinyxml2::XMLError QueryVecAttribute(const tinyxml2::XMLElement& elem, const std::string& name, cgv::math::fvec<T, N>& value) {
128
129 std::string str = "";
130 tinyxml2::XMLError result = QueryStringAttribute(elem, name, str);
131 if(result == tinyxml2::XML_SUCCESS) {
132 cgv::utils::remove(cgv::utils::trim(str, "()"), ',');
133
134 if(!cgv::math::from_string(str, value))
135 return tinyxml2::XMLError::XML_WRONG_ATTRIBUTE_TYPE;
136 }
137
138 return result;
139};
140
150static tinyxml2::XMLError QueryRGBAttribute(const tinyxml2::XMLElement& elem, const std::string& name, cgv::media::color<float, cgv::media::RGB>& value) {
151
153 tinyxml2::XMLError result = QueryVecAttribute(elem, name, vec);
154 if(result == tinyxml2::XML_SUCCESS)
155 value = cgv::media::color<float, cgv::media::RGB>(vec[0], vec[1], vec[2]);
156
157 return result;
158};
159
160}
161}
A vector with zero based index.
Definition fvec.h:27
represent a color with components of given type and color and alpha model as specified.
Definition color.h:574
Finds a tinyxml2::XMLElement by name.
Definition query.h:27
void SetQueryName(const std::string &name)
Changes the name used to find a matching element.
Definition query.h:39
const tinyxml2::XMLElement * Result() const
Get the query result.
Definition query.h:46
std::string & trim(std::string &str, const std::string &chars)
trim white space or other characters from start and end of string
Definition scan.cxx:709
char to_lower(char c)
convert char to lower case
Definition scan.cxx:39
std::string & remove(std::string &s, char c)
remove char c from the given string s and return a reference to the same string object
Definition scan.cxx:127
the cgv namespace
Definition print.h:11
Helper functions to process strings.