UniSet @VERSION@
UniXML.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// --------------------------------------------------------------------------
23// --------------------------------------------------------------------------
24
25// Класс для работы с данными в XML, выборки и перекодирования
26
27#ifndef UniXML_H_
28#define UniXML_H_
29
30#include <assert.h>
31#include <string>
32#include <cstddef>
33#include <memory>
34#include <vector>
35
36#include <libxml/parser.h>
37#include <libxml/tree.h>
38// --------------------------------------------------------------------------
39namespace uniset
40{
41 typedef std::vector< std::pair<const std::string, const std::string> > UniXMLPropList;
42
44 {
45 public:
46 using iterator_category = std::bidirectional_iterator_tag;
47 using value_type = xmlNode;
48 using difference_type = ptrdiff_t;
49 using pointer = const value_type*;
50 using reference = const value_type&;
51
52 UniXML_iterator(xmlNode* node) noexcept:
53 curNode(node)
54 {}
55 UniXML_iterator() noexcept: curNode(0) {}
56
57 std::string getProp2( const std::string& name, const std::string& defval = "" ) const noexcept;
58 std::string getProp( const std::string& name ) const noexcept;
60 std::string getPropOrProp( const std::string& name1, const std::string& name2 ) const noexcept;
61 int getIntProp( const std::string& name ) const noexcept;
63 int getPIntProp( const std::string& name, int def ) const noexcept;
64 void setProp( const std::string& name, const std::string& text ) noexcept;
65
66 bool findName( const std::string& node, const std::string& searchname, bool deepfind = true ) noexcept;
67 bool find( const std::string& searchnode, bool deepfind = true) noexcept;
68 xmlNode* findX( xmlNode* root, const std::string& searchnode, bool deepfind = true ) const noexcept;
69
71 bool goNext() noexcept;
72
74 bool goThrowNext() noexcept;
75
77 bool goPrev() noexcept;
78
79 bool canPrev() const noexcept;
80 bool canNext() const noexcept;
81
82 // Перейти к следующему узлу
83 UniXML_iterator& operator+(int) noexcept;
84 UniXML_iterator operator++(int) noexcept;
85 UniXML_iterator& operator+=(int) noexcept;
86 UniXML_iterator& operator++() noexcept;
87
88 // Перейти к предыдущему узлу
89 UniXML_iterator& operator-(int) noexcept;
90 UniXML_iterator operator--(int) noexcept;
91 UniXML_iterator& operator--() noexcept;
92 UniXML_iterator& operator-=(int) noexcept;
93
97 bool goParent() noexcept;
98
102 bool goChildren() noexcept;
103
104 // Получить текущий узел
105 xmlNode* getCurrent() noexcept;
106
107 // Получить название текущего узла
108 const std::string getName() const noexcept;
109 const std::string getContent() const noexcept;
110
111 operator xmlNode* () const noexcept;
112
113 void goBegin() noexcept;
114 void goEnd() noexcept;
115
116 UniXMLPropList getPropList() const;
117
118 private:
119
120 xmlNode* curNode;
121 };
122 // --------------------------------------------------------------------------
123 class UniXML
124 {
125 public:
126
128 typedef UniXMLPropList PropList;
129
130 UniXML( const std::string& filename );
131 UniXML();
132 ~UniXML();
133
134 xmlNode* getFirstNode() noexcept;
135 xmlNode* getFirstNode() const noexcept;
136
138 iterator begin() noexcept;
139 iterator end() noexcept;
140
141 // Загружает указанный файл
142 void open( const std::string& filename );
143 bool isOpen() const noexcept;
144
145 void close();
146
147 std::string getFileName() const noexcept;
148
149 void createFromText( const std::string& text );
150
151
152 // Создать новый XML-документ
153 void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0");
154
155 // Получить свойство name указанного узла node
156 static std::string getProp(const xmlNode* node, const std::string& name) noexcept;
157 static std::string getProp2(const xmlNode* node, const std::string& name, const std::string& defval = "" ) noexcept;
159 static std::string getPropOrProp(const xmlNode* node, const std::string& name1, const std::string& name2) noexcept;
160
161 static int getIntProp(const xmlNode* node, const std::string& name) noexcept;
162
164 static int getPIntProp(const xmlNode* node, const std::string& name, int def) noexcept;
165
166 // Установить свойство name указанного узла node
167 static void setProp(xmlNode* node, const std::string& name, const std::string& text);
168
169 static UniXMLPropList getPropList( xmlNode* node );
170
171 // Добавить новый дочерний узел
172 static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text);
173
174 // Добавить следующий узел (добавление в конец списка узлов на уровне node)
175 static xmlNode* createNext(xmlNode* node, const std::string& title, const std::string& text);
176
177 // Создать новый узел следующим за node
178 static xmlNode* insertNext(xmlNode* node, const std::string& title, const std::string& text);
179
180 // Удалить указанный узел и все вложенные узлы
181 static void removeNode(xmlNode* node);
182
183 // копировать указанный узел и все вложенные узлы
184 static xmlNode* copyNode(xmlNode* node, int recursive = 1);
185
186 // Сохранить в файл, если параметр не указан, сохраняет в тот файл
187 // который был загружен последним.
188 bool save(const std::string& filename = "", int level = 2);
189
190 // Переместить указатель к следующему узлу
191 static xmlNode* nextNode(xmlNode* node);
192
193 // После проверки исправить рекурсивный алгоритм на обычный,
194 // используя ->parent
195 xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
196
197 // ??
198 //width means number of nodes of the same level as node in 1-st parameter (width number includes first node)
199 //depth means number of times we can go to the children, if 0 we can't go only to elements of the same level
200 xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top = true ) const;
201
202 // Функция поиска по текущему уровню (без рекурсии для дочерних узлов)
203 // root указывается исходный, внутри функции осуществляется переход к списку дочерних узлов
204 // (другими словами делать goChildren() не надо)
205 xmlNode* findNodeLevel1( xmlNode* root, const std::string& nodename, const std::string& nm = "" ) const;
206
207
208 protected:
209 std::string filename;
210
212 {
213 void operator()(xmlDoc* doc) const noexcept
214 {
215 if( doc )
216 xmlFreeDoc(doc);
217 }
218 };
219
220 std::unique_ptr<xmlDoc, UniXMLDocDeleter> doc;
221 };
222 // -------------------------------------------------------------------------
223} // end of uniset namespace
224// --------------------------------------------------------------------------
225#endif
Definition UniXML.h:44
bool goParent() noexcept
Definition UniXML.cc:529
std::string getPropOrProp(const std::string &name1, const std::string &name2) const noexcept
Get property by name1, if empty - try name2.
Definition UniXML.cc:596
bool goThrowNext() noexcept
Definition UniXML.cc:473
bool goPrev() noexcept
Definition UniXML.cc:494
bool goChildren() noexcept
Definition UniXML.cc:541
bool goNext() noexcept
Definition UniXML.cc:454
int getPIntProp(const std::string &name, int def) const noexcept
if value if not positive ( <= 0 ), returns def
Definition UniXML.cc:642
Definition UniXML.h:124
static std::string getPropOrProp(const xmlNode *node, const std::string &name1, const std::string &name2) noexcept
Get property by name1, if empty - try name2.
Definition UniXML.cc:226
static int getPIntProp(const xmlNode *node, const std::string &name, int def) noexcept
if value if not positive ( <= 0 ), returns def
Definition UniXML.cc:216
iterator begin() noexcept
Definition UniXML.cc:100
static xmlNode * copyNode(xmlNode *node, int recursive=1)
Definition UniXML.cc:292
static void removeNode(xmlNode *node)
Удаление указанного узла со всеми вложенными
Definition UniXML.cc:286
Definition AccessConfig.h:30
Definition UniXML.h:212