UniSet  2.12.1
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  void createFromText( const std::string& text );
143 
144 
145  // Создать новый XML-документ
146  void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0");
147 
148  // Получить свойство name указанного узла node
149  static std::string getProp(const xmlNode* node, const std::string& name) noexcept;
150  static std::string getProp2(const xmlNode* node, const std::string& name, const std::string& defval = "" ) noexcept;
151 
152  static int getIntProp(const xmlNode* node, const std::string& name) noexcept;
153 
155  static int getPIntProp(const xmlNode* node, const std::string& name, int def) noexcept;
156 
157  // Установить свойство name указанного узла node
158  static void setProp(xmlNode* node, const std::string& name, const std::string& text);
159 
160  static UniXMLPropList getPropList( xmlNode* node );
161 
162  // Добавить новый дочерний узел
163  static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text);
164 
165  // Добавить следующий узел (добавление в конец списка узлов на уровне node)
166  static xmlNode* createNext(xmlNode* node, const std::string& title, const std::string& text);
167 
168  // Создать новый узел следующим за node
169  static xmlNode* insertNext(xmlNode* node, const std::string& title, const std::string& text);
170 
171  // Удалить указанный узел и все вложенные узлы
172  static void removeNode(xmlNode* node);
173 
174  // копировать указанный узел и все вложенные узлы
175  static xmlNode* copyNode(xmlNode* node, int recursive = 1);
176 
177  // Сохранить в файл, если параметр не указан, сохраняет в тот файл
178  // который был загружен последним.
179  bool save(const std::string& filename = "", int level = 2);
180 
181  // Переместить указатель к следующему узлу
182  static xmlNode* nextNode(xmlNode* node);
183 
184  // После проверки исправить рекурсивный алгоритм на обычный,
185  // используя ->parent
186  xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
187 
188  // ??
189  //width means number of nodes of the same level as node in 1-st parameter (width number includes first node)
190  //depth means number of times we can go to the children, if 0 we can't go only to elements of the same level
191  xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top = true ) const;
192 
193  // Функция поиска по текущему уровню (без рекурсии для дочерних узлов)
194  // root указывается исходный, внутри функции осуществляется переход к списку дочерних узлов
195  // (другими словами делать goChildren() не надо)
196  xmlNode* findNodeLevel1( xmlNode* root, const std::string& nodename, const std::string& nm = "" ) const;
197 
198 
199  protected:
200  std::string filename;
201 
203  {
204  void operator()(xmlDoc* doc) const noexcept
205  {
206  if( doc )
207  xmlFreeDoc(doc);
208  }
209  };
210 
211  std::unique_ptr<xmlDoc, UniXMLDocDeleter> doc;
212  };
213  // -------------------------------------------------------------------------
214 } // end of uniset namespace
215 // --------------------------------------------------------------------------
216 #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:456
Definition: UniXML.h:202
bool goChildren() noexcept
Definition: UniXML.cc:518
int getPIntProp(const std::string &name, int def) const noexcept
if value if not positive ( <= 0 ), returns def
Definition: UniXML.cc:612
bool goNext() noexcept
Definition: UniXML.cc:440
bool goParent() noexcept
Definition: UniXML.cc:506
static void removeNode(xmlNode *node)
Удаление указанного узла со всеми вложенными
Definition: UniXML.cc:272
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
static xmlNode * copyNode(xmlNode *node, int recursive=1)
Definition: UniXML.cc:278
bool goPrev() noexcept
Definition: UniXML.cc:474