OpenAlbum 1.0.b

/home/manuel/PFC/open-album/OpenAlbum-Unix/parser.cpp

00001 #include <iostream>
00002 #include "parser.h"
00003 #include "error.h"
00004 #include <QWidget>
00005 
00006 using namespace std;
00007 
00008 Parser::Parser(const string& path, QWidget *parent_): document(path.c_str()), parent(parent_)
00009 {
00010         try{
00011                 document.LoadFile();
00012         }
00013         catch(ticpp::Exception& e){
00014             
00015                 Error *err=new Error(3, e.what(), this->parent);
00016                 err->LanzarDialogo();
00017                 delete err;
00018                 exit(1);
00019         }
00020 }
00021 
00022 ticpp::Element* Parser::root()
00023 {
00024         return document.FirstChildElement();
00025 }
00026 
00027 std::string Parser::get_content(const ticpp::Element* element) const
00028 {
00029         try{
00030                 return element->GetText();
00031         }
00032         catch(ticpp::Exception& e){
00033         }
00034         return string();
00035 }
00036 
00037 std::string Parser::get_attribute(const std::string& name, const ticpp::Element* element) const
00038 {
00039         try{
00040                 return element->GetAttribute(name);
00041         }
00042         catch(ticpp::Exception& e){
00043         }
00044         return string();
00045 }
00046 
00047 void Parser::find_aux(const std::string& name, ticpp::Element* father, ticpp::Element*& element, bool& stop)
00048 {
00049         if(father && !stop){
00050                 ticpp::Iterator<ticpp::Element> son;
00051                 for (son = son.begin(father); son != son.end() && !stop; ++son){
00052                         if(son->Value() == name){
00053                                 element = son.Get();
00054                                 stop = true;
00055                         }
00056                         else
00057                                 find_aux(name, son.Get(), element, stop);
00058                 }
00059         }
00060 }
00061 
00062 ticpp::Element* Parser::find(const std::string& name, ticpp::Element* element)
00063 {
00064         ticpp::Element* father = element;
00065         if(!father)
00066                 father = document.FirstChildElement(false);
00067         
00068         bool stop = false;
00069         ticpp::Element* e = 0;
00070         
00071         if(father){
00072                 if(father->Value() == name)
00073                         return father;
00074                 else
00075                         find_aux(name, father, e, stop);
00076         }
00077         
00078         return e;
00079 }
00080 
00081 void Parser::find_aux(const std::string& name, ticpp::Element* father, std::vector<ticpp::Element*>& v)
00082 {
00083         if(father){
00084                 ticpp::Iterator<ticpp::Element> son;
00085                 for (son = son.begin(father); son != son.end(); ++son){
00086                         if(son->Value() == name)
00087                                 v.push_back(son.Get());
00088                         find_aux(name, son.Get(), v);
00089                 }
00090         }
00091 }
00092 
00093 bool Parser::find(const std::string& name, std::vector<ticpp::Element*>& v, ticpp::Element* element)
00094 {
00095         ticpp::Element* father = element;
00096         if(!father)
00097                 father = document.FirstChildElement(false);
00098         
00099         size_t size = v.size();
00100         
00101         if(father){
00102                 if(father->Value() == name)
00103                         v.push_back(father);
00104                 find_aux(name, father, v);
00105         }
00106         
00107         return size < v.size();
00108 }
00109 
00110 bool Parser::add_element(const std::string& name, ticpp::Element* father)
00111 {
00112         ticpp::Element* root = father ? father : document.FirstChildElement(false);
00113 
00114         if(root){
00115                 ticpp::Node* node = root->LinkEndChild(new ticpp::Element(name));
00116         if(node)
00117                 return node->ToElement();
00118         }
00119 
00120         return 0;
00121 }
00122 
00123 bool Parser::save_document(const char* path)
00124 {
00125         try{
00126                 document.SaveFile(path);
00127         }
00128         catch(ticpp::Exception& e){
00129                 return false;
00130         }
00131         return true;    
00132 }
00133 
 Todo Clases Archivos Funciones Variables