UniSet  2.24.2
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 // --------------------------------------------------------------------------
39 namespace 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;
59  int getIntProp( const std::string& name ) const noexcept;
61  int getPIntProp( const std::string& name, int def ) const noexcept;
62  void setProp( const std::string& name, const std::string& text ) noexcept;
63 
64  bool findName( const std::string& node, const std::string& searchname, bool deepfind = true ) noexcept;
65  bool find( const std::string& searchnode, bool deepfind = true) noexcept;
66  xmlNode* findX( xmlNode* root, const std::string& searchnode, bool deepfind = true ) const noexcept;
67 
69  bool goNext() noexcept;
70 
72  bool goThrowNext() noexcept;
73 
75  bool goPrev() noexcept;
76 
77  bool canPrev() const noexcept;
78  bool canNext() const noexcept;
79 
80  // Перейти к следующему узлу
81  UniXML_iterator& operator+(int) noexcept;
82  UniXML_iterator operator++(int) noexcept;
83  UniXML_iterator& operator+=(int) noexcept;
84  UniXML_iterator& operator++() noexcept;
85 
86  // Перейти к предыдущему узлу
87  UniXML_iterator& operator-(int) noexcept;
88  UniXML_iterator operator--(int) noexcept;
89  UniXML_iterator& operator--() noexcept;
90  UniXML_iterator& operator-=(int) noexcept;
91 
95  bool goParent() noexcept;
96 
100  bool goChildren() noexcept;
101 
102  // Получить текущий узел
103  xmlNode* getCurrent() noexcept;
104 
105  // Получить название текущего узла
106  const std::string getName() const noexcept;
107  const std::string getContent() const noexcept;
108 
109  operator xmlNode* () const noexcept;
110 
111  void goBegin() noexcept;
112  void goEnd() noexcept;
113 
114  UniXMLPropList getPropList() const;
115 
116  private:
117 
118  xmlNode* curNode;
119  };
120  // --------------------------------------------------------------------------
121  class UniXML
122  {
123  public:
124 
125  typedef UniXML_iterator iterator;
126  typedef UniXMLPropList PropList;
127 
128  UniXML( const std::string& filename );
129  UniXML();
130  ~UniXML();
131 
132  xmlNode* getFirstNode() noexcept;
133  xmlNode* getFirstNode() const noexcept;
134 
136  iterator begin() noexcept;
137  iterator end() noexcept;
138 
139  // Загружает указанный файл
140  void open( const std::string& filename );
141  bool isOpen() const noexcept;
142 
143  void close();
144 
145  std::string getFileName() const noexcept;
146 
147  void createFromText( const std::string& text );
148 
149 
150  // Создать новый XML-документ
151  void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0");
152 
153  // Получить свойство name указанного узла node
154  static std::string getProp(const xmlNode* node, const std::string& name) noexcept;
155  static std::string getProp2(const xmlNode* node, const std::string& name, const std::string& defval = "" ) noexcept;
156 
157  static int getIntProp(const xmlNode* node, const std::string& name) noexcept;
158 
160  static int getPIntProp(const xmlNode* node, const std::string& name, int def) noexcept;
161 
162  // Установить свойство name указанного узла node
163  static void setProp(xmlNode* node, const std::string& name, const std::string& text);
164 
165  static UniXMLPropList getPropList( xmlNode* node );
166 
167  // Добавить новый дочерний узел
168  static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text);
169 
170  // Добавить следующий узел (добавление в конец списка узлов на уровне node)
171  static xmlNode* createNext(xmlNode* node, const std::string& title, const std::string& text);
172 
173  // Создать новый узел следующим за node
174  static xmlNode* insertNext(xmlNode* node, const std::string& title, const std::string& text);
175 
176  // Удалить указанный узел и все вложенные узлы
177  static void removeNode(xmlNode* node);
178 
179  // копировать указанный узел и все вложенные узлы
180  static xmlNode* copyNode(xmlNode* node, int recursive = 1);
181 
182  // Сохранить в файл, если параметр не указан, сохраняет в тот файл
183  // который был загружен последним.
184  bool save(const std::string& filename = "", int level = 2);
185 
186  // Переместить указатель к следующему узлу
187  static xmlNode* nextNode(xmlNode* node);
188 
189  // После проверки исправить рекурсивный алгоритм на обычный,
190  // используя ->parent
191  xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
192 
193  // ??
194  //width means number of nodes of the same level as node in 1-st parameter (width number includes first node)
195  //depth means number of times we can go to the children, if 0 we can't go only to elements of the same level
196  xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top = true ) const;
197 
198  // Функция поиска по текущему уровню (без рекурсии для дочерних узлов)
199  // root указывается исходный, внутри функции осуществляется переход к списку дочерних узлов
200  // (другими словами делать goChildren() не надо)
201  xmlNode* findNodeLevel1( xmlNode* root, const std::string& nodename, const std::string& nm = "" ) const;
202 
203 
204  protected:
205  std::string filename;
206 
208  {
209  void operator()(xmlDoc* doc) const noexcept
210  {
211  if( doc )
212  xmlFreeDoc(doc);
213  }
214  };
215 
216  std::unique_ptr<xmlDoc, UniXMLDocDeleter> doc;
217  };
218  // -------------------------------------------------------------------------
219 } // end of uniset namespace
220 // --------------------------------------------------------------------------
221 #endif
Definition: UniXML.h:44
bool goParent() noexcept
Definition: UniXML.cc:506
bool goThrowNext() noexcept
Definition: UniXML.cc:456
bool goPrev() noexcept
Definition: UniXML.cc:474
bool goChildren() noexcept
Definition: UniXML.cc:518
bool goNext() noexcept
Definition: UniXML.cc:440
int getPIntProp(const std::string &name, int def) const noexcept
if value if not positive ( <= 0 ), returns def
Definition: UniXML.cc:612
Definition: UniXML.h:122
static int getPIntProp(const xmlNode *node, const std::string &name, int def) noexcept
if value if not positive ( <= 0 ), returns def
Definition: UniXML.cc:212
iterator begin() noexcept
Definition: UniXML.cc:100
static xmlNode * copyNode(xmlNode *node, int recursive=1)
Definition: UniXML.cc:278
static void removeNode(xmlNode *node)
Удаление указанного узла со всеми вложенными
Definition: UniXML.cc:272
Definition: CommonEventLoop.h:15
Definition: UniXML.h:208