1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 //                    Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
9 //                    Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 //
11 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #ifndef OHMMS_XMLDATA_H
16 #define OHMMS_XMLDATA_H
17 /**@file OhmmsElementBase.h
18  *@brief Declaration of OhmmsElementBase and define xml-related macros.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "OhmmsData/libxmldefs.h"
25 
26 /**\class OhmmsElementBase
27  *\brief Abstract class to provide xml-compatible I/O interfaces for the derived classes.
28  *
29  *Generic interfaces using std::iostream are much preferred. However,
30  *there isn't any pure c++ xml parser that is based on std::iostream alone.
31  *After evaluating several xml parsers, JK chose libxml
32  *(The XML C parser and toolkit of gnome, http://www.xmlsoft.org)
33  *based on its performance and availability on many platforms.
34  *
35  *The base class is written to be able to handle DTD or Schema in
36  *future.  Current implementation assumes that each OhmmsElementBase
37  *object handles a node and its child nodes. However, it does not
38  *specify how the derived classes hanlde the child nodes.
39  */
40 class OhmmsElementBase
41 {
42 public:
43   ///enumeration to choose the xml parser
44   enum
45   {
46     useLIBXML = 0, /*!< using libxml2 library */
47     useLIBXMLPP,   /*!< using libxml++ library */
48     usePLAIN       /*!< using ascii parser */
49   };
50 
51   ///constructor with a name
myIOMode(useLIBXML)52   OhmmsElementBase(const char* aname = "none") : myIOMode(useLIBXML), myName(aname) {}
53 
54   ///destructor
~OhmmsElementBase()55   virtual ~OhmmsElementBase() {}
56 
57   ///return the name
getName()58   inline const std::string& getName() const { return myName; }
59 
60   ///set name
setName(const std::string & aname)61   inline void setName(const std::string& aname) { myName = aname; }
62 
63   ///set iomode
setIOMode(int imode)64   inline void setIOMode(int imode) { myIOMode = imode; }
65 
66   ///write to a std::ostream
67   virtual bool get(std::ostream&) const = 0;
68 
69   ///read from std::istream
70   virtual bool put(std::istream&) = 0;
71 
72   ///read from an xmlNode
73   virtual bool put(xmlNodePtr cur) = 0;
74 
75   ///reset member data
76   virtual void reset() = 0;
77 
78   ///add a xmlNode to the children list of parent
add(xmlNodePtr parent)79   virtual bool add(xmlNodePtr parent) { return true; }
80 
81   ///read from string
put(const std::string & s)82   void put(const std::string& s)
83   {
84     std::istringstream stream(s);
85     put(stream);
86   }
87 
88   ///write the start of a node
begin_node(std::ostream & os)89   virtual void begin_node(std::ostream& os) const {}
90 
91   ///write the end of a node
end_node(std::ostream & os)92   virtual void end_node(std::ostream& os) const {}
93 
94 protected:
95   ///the type of IO mode: default is useLIBXML
96   int myIOMode;
97 
98   ///the name of the node, corresponds to the xml tag
99   std::string myName;
100 };
101 
102 //add tolower function here
103 
tolower(std::string & s)104 inline void tolower(std::string& s)
105 {
106   for (int i = 0; i < s.size(); ++i)
107     s[i] = tolower(s[i]);
108   //std::transform(s.begin(), s.end(), s.begin(), std::tolower);
109 }
110 
111 #endif
112