ls1-MarDyn
ls1-MarDyn molecular dynamics code
xmlfile.h
Go to the documentation of this file.
1
22#ifndef XMLFILE_H
23#define XMLFILE_H
24
25#include <string>
26#include <iostream>
27#include <list>
28#include <vector>
29#include <set>
30#include <stack>
31
32//#define RAPIDXML_NO_EXCEPTIONS
33#include "rapidxml/rapidxml.hpp"
34
35#ifdef ENABLE_MPI
36#include <mpi.h>
37#endif
38
39//+XMLfile======================================================================
47{
48 //typedef rapidxml::xml_node<> t_XMLnode;
49 typedef rapidxml::xml_base<> t_XMLnode;
50 typedef rapidxml::xml_node<> t_XMLelement;
51 typedef rapidxml::xml_attribute<> t_XMLattribute;
52 typedef rapidxml::xml_document<> t_XMLdocument;
53
54public:
55 static const char *const includeattrtag;
56 static const char *const queryattrtag;
57
58 class Query;
59 //class Query::const_iterator; forward declaration not possible...
60
61
62//+XMLfile::Node----------------------------------------------------------------
63 class Node
64 {
65 friend class Query;
66 friend class XMLfile;
67 // using member function friends is more specific about where private access happens, but is difficult to handle for a one-pass-compiler...
68 //friend Node Query::operator [](unsigned long idx) const;
69 //friend Node Query::const_iterator::operator *() const;
70 //friend bool changecurrentnode(const Query::const_iterator& pos);
71 //friend unsigned long changecurrentnode(const std::string& nodepath);
72 //friend void expandincludes();
73 //friend unsigned long query(std::list<Node>& nodeselection, const char* querystr, Node startnode) const;
74 //friend bool initfile_local(const std::string& filepath);
75 //friend void initstring_local(const char* xmlstring);
76
77 public:
82 enum NodeType { Unknown_Node=-1,
83 Invalid_Node=0,
84 ELEMENT_Node=1,
85 ATTRIBUTE_Node=2,
86 TEXT_Node=3,
87 CDATA_SECTION_Node=4,
88 ENTITY_REFERENCE_Node=5,
89 ENTITY_Node=6,
90 PROCESSING_INSTRUCTION_Node=7,
91 COMMENT_Node=8,
92 DOCUMENT_Node=9,
93 DOCUMENT_TYPE_Node=10,
94 DOCUMENT_FRAGMENT_Node=11,
95 NOTATION_Node=12 };
96
100 : m_xmlnode(NULL), m_nodepath(std::string()), m_type(Unknown_Node)
101 {}
102
106 Node(const Node& n)
107 : m_xmlnode(n.m_xmlnode), m_nodepath(n.m_nodepath), m_type(n.m_type)
108 {}
109
113 const std::string& nodepath() const
114 { return m_nodepath; }
115
119 { return (m_xmlnode) ? std::string(m_xmlnode->name()) : std::string(); }
120
124 { m_xmlnode=NULL; m_nodepath=std::string(); }
125
130 { return m_type; }
131
135 bool isRootNode() const
136 { if(m_xmlnode && ! m_xmlnode->parent()) return true; else return false; }
140 bool isLeafNode() const;
141
146 template<typename T> bool getValue(T& value) const;
147
152 std::string value_string(std::string defaultvalue=std::string()) const;
157 int value_int(int defaultvalue=0) const;
162 long value_long(long defaultvalue=0) const;
167 float value_float(float defaultvalue=0.) const;
172 double value_double(double defaultvalue=0.) const;
177 bool value_bool(bool defaultvalue=false) const;
178
184 { m_xmlnode=n.m_xmlnode; m_nodepath=n.m_nodepath; return *this; }
187 template<typename T> operator T() const
188 { T value; getValue(value); return value; }
191 operator bool() const
192 { return m_xmlnode!=NULL && m_type!=Invalid_Node; }
193
197 void printXML(std::ostream& ostrm=std::cout) const;
201 void print(std::ostream& ostrm=std::cout) const;
202
203 Node& operator=(Node& node){
204 m_xmlnode = node.m_xmlnode;
205 m_nodepath = node.m_nodepath;
206 m_type = node.m_type;
207 return *this;
208 }
209 private:
210 Node(const t_XMLnode* xmlnode, std::string nodepath=std::string())
211 : m_xmlnode(xmlnode), m_nodepath(nodepath), m_type(Unknown_Node)
212 { if(!xmlnode) m_type=Invalid_Node; }
213 Node(const t_XMLelement* xmlelement, std::string nodepath=std::string());
214 Node(const t_XMLattribute* xmlattribute, std::string nodepath=std::string())
215 : m_xmlnode(xmlattribute), m_nodepath(nodepath), m_type(ATTRIBUTE_Node)
216 { if(!xmlattribute) m_type=Invalid_Node; }
217 /* rapidxml::xml_document is derived from rapidxml::xml_node => t_XMLelement* constructor will handle this
218 Node(const t_XMLdocument* xmldocument, std::string nodepath=std::string())
219 : m_xmlnode(xmldocument), m_nodepath(nodepath), m_type(DOCUMENT_Node)
220 { if(!xmldocument) m_type=Invalid_Node; }
221 */
222
223 const t_XMLnode* m_xmlnode;
224 std::string m_nodepath;
225 NodeType m_type;
226 };
227//-XMLfile::Node----------------------------------------------------------------
228
229
230//+XMLfile::Query---------------------------------------------------------------
231 class Query
232 {
233 friend class XMLfile;
234 //friend Query XMLfile::query(const char* querystr) const;
235
236 public:
237
238//+XMLfile::Query::const_iterator...............................................
240 {
241 friend class Query;
242
243 public:
247 : m_query(NULL), m_nodesidx(0) {}
248 long index() const { return m_nodesidx; }
251 operator bool() const
252 { return m_query!=NULL && m_nodesidx>=0 && m_nodesidx<long(m_query->card()); }
257 { if(m_nodesidx<long(m_query->card())) ++m_nodesidx; return *this; }
262 { if(m_nodesidx<long(m_query->card())) m_nodesidx++; return *this; }
266 const_iterator& operator --() // prefix decrement
267 { if(m_nodesidx>=0) --m_nodesidx; return *this; }
271 const_iterator operator --(int) // postfix decrement
272 { if(m_nodesidx>=0) m_nodesidx--; return *this; }
277 { if(*this) return (*m_query)[m_nodesidx]; else return Node(); }
278
279 private:
280 const Query* m_query;
281 long m_nodesidx;
282
283 const_iterator(const Query* query, long idx)
284 : m_query(query), m_nodesidx(idx) {}
285 };
286//-XMLfile::Query::const_iterator...............................................
287
290 Query() : m_xmlfile(NULL), m_nodes()
291 {}
295 Query(const Query& q) : m_xmlfile(q.m_xmlfile), m_nodes(q.m_nodes)
296 { xmlfile_register(); }
300 { xmlfile_unregister(); }
301
305 unsigned long card() const { return m_nodes.size(); }
309 bool empty() const
310 { return card()==0; }
314 { xmlfile_unregister(); m_xmlfile=NULL; m_nodes.clear(); }
318 Node front() const
319 { return (*this)[0]; }
320
325 Query& operator =(const Query& q);
328 operator unsigned long() const
329 { return card(); }
332 operator bool() const
333 { return m_xmlfile!=NULL; }
338 Node operator [](unsigned long idx) const
339 { if(idx<m_nodes.size()) return Node(m_nodes[idx]); else return Node(); }
340
345 { return const_iterator(this,0); }
350 { return const_iterator(this,m_nodes.size()-1); }
355 bool end() const
356 { return false; }
357 /*
362 bool rend() const
363 { return false; }
364 */
365
370 template<typename T> unsigned long getNodeValue(T& value) const;
376 { std::string value(defaultvalue); getNodeValue(value); return value; }
381 int getNodeValue_int(int defaultvalue=0) const
382 { int value=defaultvalue; getNodeValue(value); return value; }
387 long getNodeValue_long(long defaultvalue=0) const
388 { long value=defaultvalue; getNodeValue(value); return value; }
393 float getNodeValue_float(float defaultvalue=0.) const
394 { float value=defaultvalue; getNodeValue(value); return value; }
399 double getNodeValue_double(double defaultvalue=0.) const
400 { double value=defaultvalue; getNodeValue(value); return value; }
405 bool getNodeValue_bool(bool defaultvalue=false) const
406 { bool value=defaultvalue; getNodeValue(value); return value; }
407
411 void printXML(std::ostream& ostrm=std::cout) const;
415 void print(std::ostream& ostrm=std::cout) const;
416
417 private:
418 const XMLfile* m_xmlfile;
419 std::vector<Node> m_nodes; // nodes within the actual query set
420
421 Query(const XMLfile* xmlfile)
422 : m_xmlfile(xmlfile)
423 { m_nodes.clear(); }
424
425 void xmlfile_register()
426 { if(m_xmlfile) m_xmlfile->registerQuery(this); }
427 void xmlfile_unregister()
428 { if(m_xmlfile) m_xmlfile->unregisterQuery(this); }
429 };
430//-XMLfile::Query---------------------------------------------------------------
431
432 friend void Query::xmlfile_register();
433 friend void Query::xmlfile_unregister();
434
436 XMLfile();
438 virtual ~XMLfile() { clear(); }
439
443 XMLfile(const std::string& filepath);
444
448 bool initfile(const std::string& filepath);
449
453 void initstring(const char* xmlstring);
454
458 const std::string getDir() const { return m_filedir; }
462 const std::string getFilename() const { return m_filename; }
463
468 long changecurrentnode(const std::string& nodepath=std::string("/"));
477 std::string getcurrentnodepath() const { return m_currentnode.nodepath(); }
478
484 template<typename T> unsigned long getNodeValue(const std::string& nodepath, T& value) const
485 //{ Query q=query(nodepath.c_str()); if(!q.empty()) q.front().getValue(value); return q.card(); }
486 { return query(nodepath).getNodeValue(value); }
492 template<typename T> unsigned long getNodeValue(const char* nodepath, T& value) const
493 { return getNodeValue(std::string(nodepath),value); }
499 std::string getNodeValue_string(const std::string& nodepath, const std::string defaultvalue=std::string()) const
500 //{ std::string value(defaultvalue); getNodeValue(nodepath,value); return value; }
501 { return query(nodepath).getNodeValue_string(defaultvalue); }
507 std::string getNodeValue_string(const char* nodepath, std::string defaultvalue=std::string()) const
508 { return getNodeValue_string(std::string(nodepath),defaultvalue); }
514 int getNodeValue_int(const std::string& nodepath, int defaultvalue=0) const
515 //{ int value=defaultvalue; getNodeValue(nodepath,value); return value; }
516 { return query(nodepath).getNodeValue_int(defaultvalue); }
522 int getNodeValue_int(const char* nodepath, int defaultvalue=0) const
523 { return getNodeValue_int(std::string(nodepath),defaultvalue); }
529 long getNodeValue_long(const std::string& nodepath, long defaultvalue=0) const
530 //{ long value=defaultvalue; getNodeValue(nodepath,value); return value; }
531 { return query(nodepath.c_str()).getNodeValue_long(defaultvalue); }
537 long getNodeValue_long(const char* nodepath, long defaultvalue=0) const
538 { return getNodeValue_long(std::string(nodepath),defaultvalue); }
544 float getNodeValue_float(const std::string& nodepath, float defaultvalue=0.) const
545 //{ float value=defaultvalue; getNodeValue(nodepath,value); return value; }
546 { return query(nodepath).getNodeValue_float(defaultvalue); }
552 float getNodeValue_float(const char* nodepath, float defaultvalue=0.) const
553 { return getNodeValue_float(std::string(nodepath),defaultvalue); }
559 double getNodeValue_double(const std::string& nodepath, double defaultvalue=0.) const
560 //{ double value=defaultvalue; getNodeValue(nodepath,value); return value; }
561 { return query(nodepath).getNodeValue_double(defaultvalue); }
567 double getNodeValue_double(const char* nodepath, double defaultvalue=0.) const
568 { return getNodeValue_double(std::string(nodepath),defaultvalue); }
575 bool getNodeValue_bool(const std::string& nodepath, bool defaultvalue=false) const
576 //{ bool value=defaultvalue; getNodeValue(nodepath,value); return value; }
577 { return query(nodepath).getNodeValue_bool(defaultvalue); }
583 bool getNodeValue_bool(const char* nodepath, bool defaultvalue=false) const
584 { return getNodeValue_bool(std::string(nodepath),defaultvalue); }
585
589 void printXML(std::ostream& ostrm=std::cout) const;
593 void print(std::ostream& ostrm=std::cout) const;
597 void save(std::string filepath=std::string());
598
603 Query query(const std::string& querystr) const;
604
607 operator std::string() const;
608
612 size_t numqueries() const
613 { return m_queries.size(); }
614
615#ifdef ENABLE_MPI
616 void setMPIdefaults(int mpirootrank=0, MPI_Comm mpicomm=MPI_COMM_WORLD);
617#endif
618
619//protected:
620private:
621 std::string m_filedir;
622 std::string m_filename;
623 t_XMLdocument m_xmldoc;
624 Node m_currentnode;
625 mutable std::set<Query*> m_queries;
626 //std::stack<Node> m_lastnodes;
627
628 void clear();
629 bool initfile_local(const std::string& filepath);
630 void initstring_local(const char* xmlstring);
631 void expandincludes();
632 unsigned long query(std::list<Node>& nodeselection, const std::string& querystring, Node startnode=Node()) const;
633 void insertcloneelement(const t_XMLelement* src, t_XMLelement* dest_after);
634
635 void registerQuery(Query* q) const
636 { if(q) m_queries.insert(q); }
637 void unregisterQuery(Query* q) const
638 { m_queries.erase(q); }
639 void invalidateQueries()
640 { for(std::set<Query*>::iterator pos=m_queries.begin();pos!=m_queries.end();++pos) (*pos)->invalidate(); m_queries.clear(); }
641
642#ifdef ENABLE_MPI
643 int m_mpi_rootrank;
644 MPI_Comm m_mpi_comm;
645 int m_mpi_myrank;
646 //bool m_mpi_isroot;
647
648 bool distributeXMLstring();
649#endif
650
651};
652//-XMLfile======================================================================
653
659inline std::ostream& operator << (std::ostream& ostrm, const XMLfile::Node& xmlnode)
660{
661 xmlnode.printXML(ostrm);
662 return ostrm;
663}
664
670inline std::ostream& operator << (std::ostream& ostrm, const XMLfile::Query& xmlquery)
671{
672 xmlquery.printXML(ostrm);
673 return ostrm;
674}
675
681inline std::ostream& operator << (std::ostream& ostrm, const XMLfile& xmlfile)
682{
683 xmlfile.printXML(ostrm);
684 return ostrm;
685}
686
687/*
688// explicit instantiation
689template bool XMLfile::Node::getValue<std::string>(std::string& value)const;
690template bool XMLfile::Node::getValue<int>(int& value)const;
691template bool XMLfile::Node::getValue<long>(long& value)const;
692template bool XMLfile::Node::getValue<float>(float& value)const;
693template bool XMLfile::Node::getValue<double>(double& value)const;
694template bool XMLfile::Node::getValue<bool>(bool& value)const;
695*/
696
697#endif
Definition: xmlfile.h:64
void print(std::ostream &ostrm=std::cout) const
print data to stream print the node data
Definition: xmlfile.cpp:689
void invalidate()
invalidate the node set the internal state to invalid
Definition: xmlfile.h:123
bool isRootNode() const
check, if node is root node return true, if node is a valid root node - false otherwise
Definition: xmlfile.h:135
const std::string & nodepath() const
get nodepath returns the full path of the node
Definition: xmlfile.h:113
NodeType type() const
get node type determine the node type (enumeration)
Definition: xmlfile.h:129
float value_float(float defaultvalue=0.) const
get the node float value returns the node value as float value or default value, if node is invalid
Definition: xmlfile.cpp:644
void printXML(std::ostream &ostrm=std::cout) const
print XML data to stream print the node content using XML syntax
Definition: xmlfile.cpp:674
int value_int(int defaultvalue=0) const
get the node int value returns the node value as int value or default value, if node is invalid
Definition: xmlfile.cpp:624
std::string value_string(std::string defaultvalue=std::string()) const
get the node string value returns the node value as string value or default value,...
Definition: xmlfile.cpp:614
Node()
XMLfile::Node constructor sets up an invalid node.
Definition: xmlfile.h:99
Node(const Node &n)
XMLfile::Node copy constructor duplicate a node.
Definition: xmlfile.h:106
bool isLeafNode() const
check, if node is leaf node return true, if node is a valid leaf node - false otherwise
Definition: xmlfile.cpp:524
NodeType
Node types enumeration associates a number to each XML node type.
Definition: xmlfile.h:82
double value_double(double defaultvalue=0.) const
get the node double value returns the node value as double value or default value,...
Definition: xmlfile.cpp:654
Node & operator=(const Node &n)
assignment operator copy/duplicate other node content to node
Definition: xmlfile.h:183
std::string name() const
get node name returns the name of the node, if node is valid - otherwise an empty string
Definition: xmlfile.h:118
long value_long(long defaultvalue=0) const
get the node long value returns the node value as long value or default value, if node is invalid
Definition: xmlfile.cpp:634
bool value_bool(bool defaultvalue=false) const
get the node bool value returns the node value as bool value or default value, if node is invalid
Definition: xmlfile.cpp:664
bool getValue(T &value) const
get the node value returns the node value converted to a given type
Definition: xmlfile.cpp:532
Definition: xmlfile.h:240
const_iterator & operator--()
prefix decrement operator decrement the index
Definition: xmlfile.h:266
Node operator*() const
access operator return the node, the iterator (index) actually points to or an invalid Node,...
Definition: xmlfile.h:276
const_iterator()
XMLfile::Query::const_iterator constructor sets up an invalid iterator.
Definition: xmlfile.h:246
const_iterator & operator++()
prefix increment operator put the index to the next position
Definition: xmlfile.h:256
Definition: xmlfile.h:232
Node operator[](unsigned long idx) const
indexing operator return the Node within the query at given index, or empty node, if query or index i...
Definition: xmlfile.h:338
void invalidate()
invalidate query unregister from XMLfile and invalidate the query
Definition: xmlfile.h:313
double getNodeValue_double(double defaultvalue=0.) const
get node value as double get the node content and convert it to a double
Definition: xmlfile.h:399
bool getNodeValue_bool(bool defaultvalue=false) const
get node value as bool get the node content and convert it to a bool
Definition: xmlfile.h:405
Query()
XMLfile::Query constructor sets up an invalid query.
Definition: xmlfile.h:290
long getNodeValue_long(long defaultvalue=0) const
get node value as long get the node content and convert it to a long
Definition: xmlfile.h:387
const_iterator begin() const
get starting iterator return an iterator to the first node
Definition: xmlfile.h:344
void printXML(std::ostream &ostrm=std::cout) const
print XML data to stream print the query content using XML syntax
Definition: xmlfile.cpp:762
int getNodeValue_int(int defaultvalue=0) const
get node value as int get the node content and convert it to an int
Definition: xmlfile.h:381
void print(std::ostream &ostrm=std::cout) const
print data to stream print the query content
Definition: xmlfile.cpp:768
bool end() const
get (dummy) iteration end for comparison return false (as dummy) to enable e.g. for(it=query....
Definition: xmlfile.h:355
const_iterator rbegin() const
get reverse starting iterator return an iterator to the last node
Definition: xmlfile.h:349
unsigned long card() const
get cardinality of query set return the size of the query set
Definition: xmlfile.h:305
bool empty() const
check ,if query set is empty return true if the query set is empty
Definition: xmlfile.h:309
float getNodeValue_float(float defaultvalue=0.) const
get node value as float get the node content and convert it to a float
Definition: xmlfile.h:393
Node front() const
get first query entry return the first Node of the query set (invalid Node, if there's none)
Definition: xmlfile.h:318
std::string getNodeValue_string(const std::string defaultvalue=std::string()) const
get node value as string get the node content
Definition: xmlfile.h:375
Query(const Query &q)
copy constructor duplicate a given query and register the new created one at the XMLfile
Definition: xmlfile.h:295
~Query()
XMLfile::Query destructor unregister the query.
Definition: xmlfile.h:299
Query & operator=(const Query &q)
assignment operator copy/duplicate other query content to query
Definition: xmlfile.cpp:738
unsigned long getNodeValue(T &value) const
get node value get the node content and convert it to a given type
Definition: xmlfile.cpp:747
XML file abstraction.
Definition: xmlfile.h:47
Query query(const std::string &querystr) const
perform a query return a query to a given query expression
void printXML(std::ostream &ostrm=std::cout) const
print node content as XML print node content to stream using XML
Definition: xmlfile.cpp:159
bool getNodeValue_bool(const std::string &nodepath, bool defaultvalue=false) const
get node value as bool get the node content and convert it to a bool (an alternative of using <tag>tr...
Definition: xmlfile.h:575
unsigned long getNodeValue(const char *nodepath, T &value) const
get node value get the node content and convert it to a given type
Definition: xmlfile.h:492
void print(std::ostream &ostrm=std::cout) const
print node content print node content and debug information to stream
Definition: xmlfile.cpp:164
long changecurrentnode(const std::string &nodepath=std::string("/"))
set current node set a node, relative queries start with
void save(std::string filepath=std::string())
save save node content as XML-file
Definition: xmlfile.cpp:113
long getNodeValue_long(const char *nodepath, long defaultvalue=0) const
get node value as long get the node content and convert it to a long
Definition: xmlfile.h:537
int getNodeValue_int(const std::string &nodepath, int defaultvalue=0) const
get node value as int get the node content and convert it to an integer
Definition: xmlfile.h:514
bool getNodeValue_bool(const char *nodepath, bool defaultvalue=false) const
get node value as bool get the node content and convert it to a bool
Definition: xmlfile.h:583
std::string getNodeValue_string(const std::string &nodepath, const std::string defaultvalue=std::string()) const
get node value as string get the node content
Definition: xmlfile.h:499
long getNodeValue_long(const std::string &nodepath, long defaultvalue=0) const
get node value as long get the node content and convert it to a long
Definition: xmlfile.h:529
const std::string getFilename() const
get XML filename if instantiated with a XML-file, return the filename (without directory part of path...
Definition: xmlfile.h:462
float getNodeValue_float(const char *nodepath, float defaultvalue=0.) const
get node value as float get the node content and convert it to a float
Definition: xmlfile.h:552
virtual ~XMLfile()
XMLfile default destructor.
Definition: xmlfile.h:438
bool initfile(const std::string &filepath)
initialize with XML-file instantiating with XML file
Definition: xmlfile.cpp:54
double getNodeValue_double(const char *nodepath, double defaultvalue=0.) const
get node value as double get the node content and convert it to a double
Definition: xmlfile.h:567
unsigned long getNodeValue(const std::string &nodepath, T &value) const
get node value get the node content and convert it to a given type
Definition: xmlfile.h:484
float getNodeValue_float(const std::string &nodepath, float defaultvalue=0.) const
get node value as float get the node content and convert it to a float
Definition: xmlfile.h:544
int getNodeValue_int(const char *nodepath, int defaultvalue=0) const
get node value as int get the node content and convert it to an integer
Definition: xmlfile.h:522
XMLfile(const std::string &filepath)
constructor for XML-file constructor calls initfile
XMLfile()
XMLfile default constructor.
Definition: xmlfile.cpp:37
double getNodeValue_double(const std::string &nodepath, double defaultvalue=0.) const
get node value as double get the node content and convert it to a double
Definition: xmlfile.h:559
const std::string getDir() const
get XML file directory if instantiated with a XML-file, return the directory
Definition: xmlfile.h:458
std::string getNodeValue_string(const char *nodepath, std::string defaultvalue=std::string()) const
get node value as string get the node content
Definition: xmlfile.h:507
size_t numqueries() const
number of registered queries return the number of active queries
Definition: xmlfile.h:612
void initstring(const char *xmlstring)
initialize with XML-string instantiating with XML-string
Definition: xmlfile.cpp:72
std::string getcurrentnodepath() const
get current node path
Definition: xmlfile.h:477
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
Definition: vtk-punstructured.h:270
Definition: FakedOptFFT.h:23
std::ostream & operator<<(std::ostream &ostrm, const XMLfile::Node &xmlnode)
write a node to a stream write XML node data to an output stream
Definition: xmlfile.h:659