sprint 1-alpha
sprint/xml/uxml.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::xml
00003 *
00004 *   Copyright (C) 2004-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library 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 GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SPRINT_XML_H
00023 #define _SPRINT_XML_H
00024 
00029 #if defined (_MSC_VER)
00030 // avoid bogus warning....
00031 # define _CRT_SECURE_NO_WARNINGS
00032 #endif
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <sprint/smart_cast.h>
00037 
00038 #ifndef NO_USE_XSTREAM
00039 # include <sprint/io/xstream/xstream.h>
00040 #else
00041 # include <iostream>
00042 #endif
00043 
00044 namespace sprint {
00045 
00047 namespace xml {
00048 
00049 // a forward declaration for the following typedef
00050 struct node;
00051 
00053 typedef std::pair<std::string, std::string> attr_t;
00054 
00056 typedef std::vector< attr_t > attr_list_t;
00057 
00059 typedef std::vector< node > node_list_t;
00060 
00063 struct node {
00065       std::string id;
00067       std::string text;
00069       attr_list_t attr;
00071       node_list_t child;
00072       
00073   public:
00074     
00075       typedef node_list_t::const_iterator const_iterator;
00076       typedef node_list_t::iterator iterator;      
00077       
00078   private:
00082       bool add_attr(const std::string & key, const std::string & value);
00083   
00084   public:      
00086       node();
00088       node(const char *p_id);
00089       ~node();
00090       
00092       const_iterator cbegin() const { return child.begin(); }
00094       const_iterator cend() const { return child.end(); }
00095 
00097       void add(const node & n) { child.push_back(n); }
00098       
00100       bool find_attr(const std::string & key, std::string & value) const;
00101 
00109       template<class T>
00110       void set_attr(const std::string & key, T value)
00111       {
00112             // TODO: check multiple
00113           add_attr( key, smart_cast<std::string>(value) );
00114       }
00115       
00123       template<class T>
00124       T get_attr(const std::string & key, T _default) const
00125       {
00126         std::string value;
00127         if(find_attr(key, value))
00128          return smart_cast<T>(value);
00129          else
00130          return _default;
00131       }
00132       
00134       inline bool has_child() const { return !child.empty(); }
00135       
00141       inline const node & first_child() const { return child[0]; }
00143       inline node & first_child() { return child[0]; }
00144       
00152       node * find_child_by_class(const char *classid);
00153       const node * find_child_by_class(const char *classid) const;      
00154       
00160       inline node & set_text(const char *txt) { text = txt; return *this; }
00161       inline node & set_text(const std::string & txt) { text = txt; return *this; }
00162       };
00163 
00165 class document: public node {
00166 public:
00167       document();
00168       
00174       document(const char *file);     
00175       
00176 #ifndef NO_USE_XSTREAM
00177 
00178       document(sprint::io::xstream & file);     
00179 #else
00180       document(std::istream & file);     
00181 #endif
00182       
00184       ~document();
00185 
00190       bool load(const char *file);
00191 
00199       bool save(const char *file) const;
00200 
00201 #ifndef NO_USE_XSTREAM
00202 
00203       bool load(sprint::io::xstream & file);
00205       bool save(sprint::io::xstream & file) const;
00206 #else
00207 
00208       bool load(std::istream & file);
00210       bool save(std::ostream & file) const;
00211 #endif
00212 
00213 };
00214 
00215 } // xml namespace closed       
00216 
00217 } // sprint namespace closed
00218 
00219 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines