QMCPACK
XMLParsingString.h
Go to the documentation of this file.
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) 2021 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 /** @file XMLParsingString.h
15  *
16  * XMLNodeString convert xmlNode contents into a std::string
17  * XMLAttrString convert one xmlNode attribute into a std::string
18  */
19 #ifndef QMCPLUSPLUS_XMLSTRING_H
20 #define QMCPLUSPLUS_XMLSTRING_H
21 
22 #include <string>
23 #include <string_view>
24 #include <libxml/xmlmemory.h>
25 #include <libxml/tree.h>
26 
27 /** convert xmlNode contents into a std::string
28  * \todo this should just be a function that takes a cur
29  * and returns a std::string.
30  * setXMLNodeContent is OOP without reason.
31  */
32 class XMLNodeString : public std::string
33 {
34 public:
35  /// construct a string from an xmlNode
36  XMLNodeString(const xmlNodePtr cur)
37  {
38  xmlChar* node_char = xmlNodeListGetString(cur->doc, cur->xmlChildrenNode, 1);
39  if (node_char)
40  {
41  assign((const char*)node_char);
42  xmlFree(node_char);
43  }
44  }
45 
46  /// expose base class constructors
47  XMLNodeString(const std::string& in) : std::string(in) {}
48 
49  XMLNodeString(const char* in) : std::string(in) {}
50 
51  /// write a string to an xmlNode
52  void setXMLNodeContent(xmlNodePtr cur) const { xmlNodeSetContent(cur, (const xmlChar*)(this->c_str())); }
53 };
54 
55 /** get the value string for attribute name
56  * if name is unfound in cur you get an empty string back
57  * this is the same behavior XMLAttrString provides. Without the complication
58  * of a composite type you don't need.
59  */
60 std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name);
61 
62 #endif
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
XMLNodeString(const char *in)
Matrix< T1, C1 > & assign(Matrix< T1, C1 > &lhs, const RHS &rhs)
void setXMLNodeContent(xmlNodePtr cur) const
write a string to an xmlNode
XMLNodeString(const xmlNodePtr cur)
construct a string from an xmlNode
convert xmlNode contents into a std::string
XMLNodeString(const std::string &in)
expose base class constructors