UniSet  2.6.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 ) 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  // Добавить следующий узел
163  static xmlNode* createNext(xmlNode* node, const std::string& title, const std::string& text);
164 
165  // Удалить указанный узел и все вложенные узлы
166  static void removeNode(xmlNode* node);
167 
168  // копировать указанный узел и все вложенные узлы
169  static xmlNode* copyNode(xmlNode* node, int recursive = 1);
170 
171  // Сохранить в файл, если параметр не указан, сохраняет в тот файл
172  // который был загружен последним.
173  bool save(const std::string& filename = "", int level = 2);
174 
175  // Переместить указатель к следующему узлу
176  static xmlNode* nextNode(xmlNode* node);
177 
178  // После проверки исправить рекурсивный алгоритм на обычный,
179  // используя ->parent
180  xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const;
181 
182  // ??
183  //width means number of nodes of the same level as node in 1-st parameter (width number includes first node)
184  //depth means number of times we can go to the children, if 0 we can't go only to elements of the same level
185  xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top = true ) const;
186 
187  protected:
188  std::string filename;
189  std::shared_ptr<xmlDoc> doc = { nullptr };
190  };
191  // -------------------------------------------------------------------------
192 } // end of uniset namespace
193 // --------------------------------------------------------------------------
194 #endif