1 #include "filezilla.h"
2 
3 #include "../include/xmlutils.h"
4 
5 #include <cstring>
6 #include <cassert>
7 
AddTextElement(pugi::xml_node node,const char * name,std::string const & value,bool overwrite)8 pugi::xml_node AddTextElement(pugi::xml_node node, const char* name, std::string const& value, bool overwrite)
9 {
10 	return AddTextElementUtf8(node, name, fz::to_utf8(value), overwrite);
11 }
12 
AddTextElement(pugi::xml_node node,const char * name,std::wstring const & value,bool overwrite)13 pugi::xml_node AddTextElement(pugi::xml_node node, const char* name, std::wstring const& value, bool overwrite)
14 {
15 	return AddTextElementUtf8(node, name, fz::to_utf8(value), overwrite);
16 }
17 
AddTextElement(pugi::xml_node node,const char * name,int64_t value,bool overwrite)18 void AddTextElement(pugi::xml_node node, const char* name, int64_t value, bool overwrite)
19 {
20 	if (overwrite) {
21 		node.remove_child(name);
22 	}
23 	auto child = node.append_child(name);
24 	child.text().set(static_cast<long long>(value));
25 }
26 
AddTextElementUtf8(pugi::xml_node node,const char * name,std::string const & value,bool overwrite)27 pugi::xml_node AddTextElementUtf8(pugi::xml_node node, const char* name, std::string const& value, bool overwrite)
28 {
29 	assert(node);
30 
31 	if (overwrite) {
32 		node.remove_child(name);
33 	}
34 
35 	auto element = node.append_child(name);
36 	if (!value.empty()) {
37 		element.text().set(value.c_str());
38 	}
39 
40 	return element;
41 }
42 
AddTextElement(pugi::xml_node node,std::string const & value)43 void AddTextElement(pugi::xml_node node, std::string const& value)
44 {
45 	AddTextElementUtf8(node, fz::to_utf8(value));
46 }
47 
AddTextElement(pugi::xml_node node,std::wstring const & value)48 void AddTextElement(pugi::xml_node node, std::wstring const& value)
49 {
50 	AddTextElementUtf8(node, fz::to_utf8(value));
51 }
52 
AddTextElement(pugi::xml_node node,int64_t value)53 void AddTextElement(pugi::xml_node node, int64_t value)
54 {
55 	assert(node);
56 	node.text().set(static_cast<long long>(value));
57 }
58 
AddTextElementUtf8(pugi::xml_node node,std::string const & value)59 void AddTextElementUtf8(pugi::xml_node node, std::string const& value)
60 {
61 	assert(node);
62 	node.text().set(value.c_str());
63 }
64 
GetTextElement_Trimmed(pugi::xml_node node,const char * name)65 std::wstring GetTextElement_Trimmed(pugi::xml_node node, const char* name)
66 {
67 	return fz::trimmed(GetTextElement(node, name));
68 }
69 
GetTextElement(pugi::xml_node node,const char * name)70 std::wstring GetTextElement(pugi::xml_node node, const char* name)
71 {
72 	assert(node);
73 
74 	return fz::to_wstring_from_utf8(node.child_value(name));
75 }
76 
GetTextElement_Trimmed(pugi::xml_node node)77 std::wstring GetTextElement_Trimmed(pugi::xml_node node)
78 {
79 	return fz::trimmed(GetTextElement(node));
80 }
81 
GetTextElement(pugi::xml_node node)82 std::wstring GetTextElement(pugi::xml_node node)
83 {
84 	assert(node);
85 	return fz::to_wstring_from_utf8(node.child_value());
86 }
87 
GetTextElementInt(pugi::xml_node node,const char * name,int defValue)88 int64_t GetTextElementInt(pugi::xml_node node, const char* name, int defValue /*=0*/)
89 {
90 	assert(node);
91 	return static_cast<int64_t>(node.child(name).text().as_llong(defValue));
92 }
93 
GetTextElementBool(pugi::xml_node node,const char * name,bool defValue)94 bool GetTextElementBool(pugi::xml_node node, const char* name, bool defValue /*=false*/)
95 {
96 	assert(node);
97 	return node.child(name).text().as_bool(defValue);
98 }
99 
SetTextAttribute(pugi::xml_node node,char const * name,std::string const & value)100 void SetTextAttribute(pugi::xml_node node, char const* name, std::string const& value)
101 {
102 	SetTextAttributeUtf8(node, name, fz::to_utf8(value));
103 }
104 
SetTextAttribute(pugi::xml_node node,char const * name,std::wstring const & value)105 void SetTextAttribute(pugi::xml_node node, char const* name, std::wstring const& value)
106 {
107 	SetTextAttributeUtf8(node, name, fz::to_utf8(value));
108 }
109 
SetTextAttributeUtf8(pugi::xml_node node,char const * name,std::string const & utf8)110 void SetTextAttributeUtf8(pugi::xml_node node, char const* name, std::string const& utf8)
111 {
112 	assert(node);
113 	auto attribute = node.attribute(name);
114 	if (!attribute) {
115 		attribute = node.append_attribute(name);
116 	}
117 	attribute.set_value(utf8.c_str());
118 }
119 
GetTextAttribute(pugi::xml_node node,char const * name)120 std::wstring GetTextAttribute(pugi::xml_node node, char const* name)
121 {
122 	assert(node);
123 
124 	const char* value = node.attribute(name).value();
125 	return fz::to_wstring_from_utf8(value);
126 }
127 
FindElementWithAttribute(pugi::xml_node node,const char * element,const char * attribute,const char * value)128 pugi::xml_node FindElementWithAttribute(pugi::xml_node node, const char* element, const char* attribute, const char* value)
129 {
130 	pugi::xml_node child = element ? node.child(element) : node.first_child();
131 	while (child) {
132 		const char* nodeVal = child.attribute(attribute).value();
133 		if (nodeVal && !strcmp(value, nodeVal)) {
134 			return child;
135 		}
136 
137 		child = element ? child.next_sibling(element) : child.next_sibling();
138 	}
139 
140 	return child;
141 }
142 
GetAttributeInt(pugi::xml_node node,const char * name)143 int GetAttributeInt(pugi::xml_node node, const char* name)
144 {
145 	return node.attribute(name).as_int();
146 }
147 
SetAttributeInt(pugi::xml_node node,const char * name,int value)148 void SetAttributeInt(pugi::xml_node node, const char* name, int value)
149 {
150 	auto attribute = node.attribute(name);
151 	if (!attribute) {
152 		attribute = node.append_attribute(name);
153 	}
154 	attribute.set_value(value);
155 }
156