![]() |
OpenAlbum 1.0.b
|
00001 /* 00002 Copyright © 2011 Manuel Jesús de la Calle Brihuega 00003 00004 This file is part of open Album & Granny's Bloodbath. 00005 00006 Open Album is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 Open Album is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with Open Album. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #ifndef _PARSER_ 00021 #define _PARSER_ 00022 00023 #include <vector> 00024 #include <string> 00025 #include "ticpp/ticpp.h" 00026 #include <QWidget> 00027 00029 00097 class Parser{ 00098 public: 00106 Parser(const std::string& path, QWidget *parent_); 00107 00115 ticpp::Element* root(); 00116 00125 std::string get_content(const ticpp::Element* element) const; 00126 00136 std::string get_attribute(const std::string& name, const ticpp::Element* element) const; 00137 00148 template <typename T> 00149 bool get_attribute(const std::string& name, const ticpp::Element *element, T* value) const{ 00150 if(element){ 00151 try{ 00152 element->GetAttribute(name, value); 00153 } 00154 catch(ticpp::Exception& e){ 00155 return false; 00156 } 00157 return true; 00158 } 00159 return false; 00160 } 00161 00168 ticpp::Element* find(const std::string& name, ticpp::Element* element = 0); 00169 00178 bool find(const std::string& name, std::vector<ticpp::Element*>& v, ticpp::Element* element = 0); 00179 00187 bool add_element(const std::string& name, ticpp::Element* father = 0); 00188 00197 template<typename T> 00198 bool set_attribute(const std::string& name, const T& value, ticpp::Element* element){ 00199 if(element){ 00200 try{ 00201 element->SetAttribute(name, value); 00202 }catch(ticpp::Exception& e){ 00203 return false; 00204 } 00205 return true; 00206 } 00207 return false; 00208 } 00209 00217 template<typename T> 00218 bool set_content(const T& value, ticpp::Element* element){ 00219 if(element){ 00220 try{ 00221 element->SetText(value); 00222 }catch(ticpp::Exception& e){ 00223 return false; 00224 } 00225 return true; 00226 } 00227 return false; 00228 } 00229 00236 bool save_document(const char* path); 00237 00238 private: 00239 ticpp::Document document; 00240 QWidget *parent; 00241 void find_aux(const std::string& name, ticpp::Element* father, ticpp::Element*& element, bool& stop); 00242 void find_aux(const std::string& name, ticpp::Element* father, std::vector<ticpp::Element*>& v); 00243 }; 00244 00245 #endif