|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // -------------------------------------------------------------------------- 00023 // -------------------------------------------------------------------------- 00024 00025 // Класс для работы с данными в XML, выборки и перекодирования 00026 00027 #ifndef UniXML_H_ 00028 #define UniXML_H_ 00029 00030 #include <assert.h> 00031 #include <string> 00032 #include <cstddef> 00033 #include <memory> 00034 00035 #include <libxml/parser.h> 00036 #include <libxml/tree.h> 00037 // -------------------------------------------------------------------------- 00038 namespace uniset 00039 { 00040 00041 class UniXML_iterator: 00042 public std::iterator<std::bidirectional_iterator_tag, xmlNode, ptrdiff_t, xmlNode*, xmlNode&> 00043 { 00044 public: 00045 UniXML_iterator(xmlNode* node) noexcept: 00046 curNode(node) 00047 {} 00048 UniXML_iterator() noexcept: curNode(0) {} 00049 00050 std::string getProp2( const std::string& name, const std::string& defval = "" ) const noexcept; 00051 std::string getProp( const std::string& name ) const noexcept; 00052 int getIntProp( const std::string& name ) const noexcept; 00054 int getPIntProp( const std::string& name, int def ) const noexcept; 00055 void setProp( const std::string& name, const std::string& text ) noexcept; 00056 00057 bool findName( const std::string& node, const std::string& searchname, bool deepfind = true ) noexcept; 00058 bool find( const std::string& searchnode, bool deepfind = true) noexcept; 00059 xmlNode* findX( xmlNode* root, const std::string& searchnode, bool deepfind = true ) noexcept; 00060 00062 bool goNext() noexcept; 00063 00065 bool goThrowNext() noexcept; 00066 00068 bool goPrev() noexcept; 00069 00070 bool canPrev() const noexcept; 00071 bool canNext() const noexcept; 00072 00073 // Перейти к следующему узлу 00074 UniXML_iterator& operator+(int) noexcept; 00075 UniXML_iterator& operator++(int) noexcept; 00076 UniXML_iterator& operator+=(int) noexcept; 00077 UniXML_iterator& operator++() noexcept; 00078 00079 // Перейти к предыдущему узлу 00080 UniXML_iterator& operator-(int) noexcept; 00081 UniXML_iterator& operator--(int) noexcept; 00082 UniXML_iterator& operator--() noexcept; 00083 UniXML_iterator& operator-=(int) noexcept; 00084 00088 bool goParent() noexcept; 00089 00093 bool goChildren() noexcept; 00094 00095 // Получить текущий узел 00096 xmlNode* getCurrent() noexcept; 00097 00098 // Получить название текущего узла 00099 const std::string getName() const noexcept; 00100 const std::string getContent() const noexcept; 00101 00102 operator xmlNode* () const noexcept; 00103 00104 void goBegin() noexcept; 00105 void goEnd() noexcept; 00106 00107 private: 00108 00109 xmlNode* curNode; 00110 }; 00111 // -------------------------------------------------------------------------- 00112 class UniXML 00113 { 00114 public: 00115 00116 typedef UniXML_iterator iterator; 00117 00118 UniXML( const std::string& filename ); 00119 UniXML(); 00120 ~UniXML(); 00121 00122 xmlNode* getFirstNode() noexcept; 00123 xmlNode* getFirstNode() const noexcept; 00124 00126 iterator begin() noexcept; 00127 iterator end() noexcept; 00128 00129 // Загружает указанный файл 00130 void open( const std::string& filename ); 00131 bool isOpen() const noexcept; 00132 00133 void close(); 00134 00135 std::string getFileName() const noexcept; 00136 00137 // Создать новый XML-документ 00138 void newDoc( const std::string& root_node, const std::string& xml_ver = "1.0"); 00139 00140 // Получить свойство name указанного узла node 00141 static std::string getProp(const xmlNode* node, const std::string& name) noexcept; 00142 static std::string getProp2(const xmlNode* node, const std::string& name, const std::string& defval = "" ) noexcept; 00143 00144 static int getIntProp(const xmlNode* node, const std::string& name) noexcept; 00145 00147 static int getPIntProp(const xmlNode* node, const std::string& name, int def) noexcept; 00148 00149 // Установить свойство name указанного узла node 00150 static void setProp(xmlNode* node, const std::string& name, const std::string& text); 00151 00152 // Добавить новый дочерний узел 00153 static xmlNode* createChild(xmlNode* node, const std::string& title, const std::string& text); 00154 00155 // Добавить следующий узел 00156 static xmlNode* createNext(xmlNode* node, const std::string& title, const std::string& text); 00157 00158 // Удалить указанный узел и все вложенные узлы 00159 static void removeNode(xmlNode* node); 00160 00161 // копировать указанный узел и все вложенные узлы 00162 static xmlNode* copyNode(xmlNode* node, int recursive = 1); 00163 00164 // Сохранить в файл, если параметр не указан, сохраняет в тот файл 00165 // который был загружен последним. 00166 bool save(const std::string& filename = "", int level = 2); 00167 00168 // Переместить указатель к следующему узлу 00169 static xmlNode* nextNode(xmlNode* node); 00170 00171 // После проверки исправить рекурсивный алгоритм на обычный, 00172 // используя ->parent 00173 xmlNode* findNode( xmlNode* node, const std::string& searchnode, const std::string& name = "") const; 00174 00175 // ?? 00176 //width means number of nodes of the same level as node in 1-st parameter (width number includes first node) 00177 //depth means number of times we can go to the children, if 0 we can't go only to elements of the same level 00178 xmlNode* extFindNode( xmlNode* node, int depth, int width, const std::string& searchnode, const std::string& name = "", bool top = true ) const; 00179 00180 protected: 00181 std::string filename; 00182 std::shared_ptr<xmlDoc> doc = { nullptr }; 00183 }; 00184 // ------------------------------------------------------------------------- 00185 } // end of uniset namespace 00186 // -------------------------------------------------------------------------- 00187 #endif
1.7.6.1