1 /////////////////////////////////////////
2 //
3 //             OpenLieroX
4 //
5 // code under LGPL, based on JasonBs work,
6 // enhanced by Dark Charlie and Albert Zeyer
7 //
8 //
9 /////////////////////////////////////////
10 
11 
12 // Useful functions for XML/HTML parsing
13 // Created 24/7/08
14 // Karel Petranek
15 
16 #include "XMLutils.h"
17 
18 #include "StringUtils.h"
19 
20 ///////////////////
21 // Get an integer from the specified node
xmlGetInt(xmlNodePtr node,const std::string & name,int def)22 int xmlGetInt(xmlNodePtr node, const std::string& name, int def)
23 {
24 	// Read
25 	xmlChar *sValue = xmlGetProp(node, (const xmlChar *)name.c_str());
26 	if(!sValue)
27 		return def;
28 
29 	// Convert
30 	bool fail = false;
31 	int result = from_string<int>((const char *)sValue, fail);
32 	if (fail)
33 		result = def;
34 
35 	// Cleanup
36 	xmlFree(sValue);
37 	return result;
38 }
39 
40 ///////////////////
41 // Get a float from the specified node
xmlGetFloat(xmlNodePtr node,const std::string & name,float def)42 float xmlGetFloat(xmlNodePtr node, const std::string& name, float def)
43 {
44 	// Read
45 	xmlChar *sValue = xmlGetProp(node, (const xmlChar *)name.c_str());
46 	if (!sValue)
47 		return def;
48 
49 	// Convert
50 	bool fail = false;
51 	float result = from_string<float>((const char *)sValue, fail);
52 	if (fail)
53 		result = def;
54 
55 	// Cleanup
56 	xmlFree(sValue);
57 	return result;
58 }
59 
60 ///////////////////
61 // Get a colour from the specified node
xmlGetColour(xmlNodePtr node,const std::string & name,const Color & def)62 Color xmlGetColour(xmlNodePtr node, const std::string& name, const Color& def)
63 {
64 	// Get the value
65 	xmlChar *sValue = xmlGetProp(node, (const xmlChar *)name.c_str());
66 	if (!sValue)
67 		return def;
68 
69 	// Convert
70 	bool fail = false;
71 	Color result = StrToCol((const char *)sValue, fail);
72 	if (fail)
73 		return def;
74 
75 	//Cleanup
76 	xmlFree(sValue);
77 	return result;
78 }
79 
80 ///////////////////
81 // Get a string from the specified node
xmlGetString(xmlNodePtr node,const std::string & name,const std::string & def)82 std::string xmlGetString(xmlNodePtr node, const std::string& name, const std::string& def)
83 {
84 	// Get the value
85 	xmlChar *sValue = xmlGetProp(node, (const xmlChar *)name.c_str());
86 	if (!sValue)
87 		return def;
88 
89 	// Convert
90 	std::string result((const char *)sValue);
91 
92 	//Cleanup
93 	xmlFree(sValue);
94 	return result;
95 }
96 
97 ///////////////////
98 // Get a boolean from the specified node
xmlGetBool(xmlNodePtr node,const std::string & name,bool def)99 bool xmlGetBool(xmlNodePtr node, const std::string& name, bool def)
100 {
101 	// Get the value
102 	xmlChar *sValue = xmlGetProp(node, (const xmlChar *)name.c_str());
103 	if (!sValue)
104 		return def;
105 
106 	// Convert
107 	bool result = def;
108 	if (!xmlStrcasecmp(sValue, (const xmlChar *)"true"))
109 		result = true;
110 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"yes"))
111 		result = true;
112 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"1"))
113 		result = true;
114 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"on"))
115 		result = true;
116 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"false"))
117 		result = false;
118 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"no"))
119 		result = false;
120 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"0"))
121 		result = false;
122 	else if (!xmlStrcasecmp(sValue, (const xmlChar *)"off"))
123 		result = false;
124 
125 	//Cleanup
126 	xmlFree(sValue);
127 	return result;
128 }
129 
130 ////////////////
131 // Get a text from the node
xmlNodeText(xmlNodePtr node,const std::string & def)132 std::string xmlNodeText(xmlNodePtr node, const std::string& def)
133 {
134 	if (node->doc && node->children)  {
135 		const char *str = (const char *)xmlNodeListGetString(node->doc, node->children, 1);
136 		if (str)  {
137 			std::string res = str;
138 			xmlFree((xmlChar *)str);
139 			return res;
140 		} else {
141 			return def;
142 		}
143 	} else
144 		return def;
145 }
146 
147 //////////////////
148 // Get the base URL for the given node
xmlGetBaseURL(xmlNodePtr node)149 std::string xmlGetBaseURL(xmlNodePtr node)
150 {
151 	if (node)
152 		if (node->doc)
153 			if (node->doc->URL)  {
154 				std::string res((const char *)node->doc->URL);
155 				return res;
156 			}
157 	return "";
158 }
159 
160 /////////////////
161 // Replaces all the escape characters with html entities
xmlEntityText(std::string & text)162 void xmlEntityText(std::string& text)
163 {
164 	//replace(text,"\"","&quot;",text);  // " - should not be escaped for libxml
165 	//replace(text,"'", "&apos;",text);  // ' - should not be escaped for libxml
166 	replace(text,"&", "&amp;", text);  // &
167 	replace(text,"<", "&lt;",  text);  // <
168 	replace(text,">", "&gt;",  text);  // >
169 }
170 
xmlEntities(const std::string & text)171 std::string xmlEntities(const std::string& text)
172 {
173 	std::string res = text;
174 	xmlEntityText(res);
175 	return res;
176 }
177