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