Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_XMLParameterListCoreHelpers.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #include <fstream>
16 
17 void Teuchos::updateParametersFromXmlFile(
18  const std::string &xmlFileName,
19  const Ptr<ParameterList> &paramList
20  )
21 {
22  XMLParameterListReader xmlPLReader;
23  xmlPLReader.setAllowsDuplicateSublists( false );
24  FileInputSource xmlFile(xmlFileName);
25  XMLObject xmlParams = xmlFile.getObject();
26  paramList->setParameters(xmlPLReader.toParameterList(xmlParams));
27 }
28 
29 
31 Teuchos::getParametersFromXmlFile(const std::string &xmlFileName)
32 {
33  RCP<ParameterList> pl = parameterList();
34  updateParametersFromXmlFile(xmlFileName, pl.ptr());
35  return pl;
36 }
37 
38 
40 Teuchos::getParametersFromXmlFile(
41  const std::string &xmlFileName,
42  RCP<DependencySheet> depSheet)
43 {
44  XMLParameterListReader xmlPLReader;
45  xmlPLReader.setAllowsDuplicateSublists( false );
46  FileInputSource xmlFile(xmlFileName);
47  XMLObject xmlParams = xmlFile.getObject();
48  return xmlPLReader.toParameterList(xmlParams, depSheet);
49 }
50 
51 
52 void Teuchos::updateParametersFromXmlString(
53  const std::string &xmlStr,
54  const Ptr<ParameterList> &paramList,
55  bool overwrite
56  )
57 {
58  XMLParameterListReader xmlPLReader;
59  xmlPLReader.setAllowsDuplicateSublists( false );
60  StringInputSource xmlStrSrc(xmlStr);
61  XMLObject xmlParams = xmlStrSrc.getObject();
62  if(overwrite) paramList->setParameters(xmlPLReader.toParameterList(xmlParams));
63  else paramList->setParametersNotAlreadySet(xmlPLReader.toParameterList(xmlParams));
64 }
65 
66 
68 Teuchos::getParametersFromXmlString(const std::string &xmlStr)
69 {
70  RCP<ParameterList> pl = parameterList();
71  updateParametersFromXmlString(xmlStr, pl.ptr());
72  return pl;
73 }
74 
75 
77 Teuchos::getParametersFromXmlString(const std::string &xmlStr,
78  RCP<DependencySheet> depSheet)
79 {
80  XMLParameterListReader xmlPLReader;
81  xmlPLReader.setAllowsDuplicateSublists( false );
82  StringInputSource xmlStrSrc(xmlStr);
83  XMLObject xmlParams = xmlStrSrc.getObject();
84  return xmlPLReader.toParameterList(xmlParams, depSheet);
85 }
86 
87 
88 void Teuchos::writeParameterListToXmlOStream(
89  const ParameterList &paramList,
90  std::ostream &xmlOut,
91  RCP<const DependencySheet> depSheet
92  )
93 {
94  XMLParameterListWriter plWriter;
95  XMLObject xml = plWriter.toXML(paramList, depSheet);
96  xmlOut << xml << std::endl;
97 }
98 
99 
100 void Teuchos::writeParameterListToXmlFile(
101  const ParameterList &paramList,
102  const std::string &xmlFileName,
103  RCP<const DependencySheet> depSheet
104  )
105 {
106  std::ofstream ofs(xmlFileName.c_str());
107  writeParameterListToXmlOStream(paramList,ofs, depSheet);
108 }
Writes an XML object to a parameter list.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromXmlFile(const std::string &xmlFileName, const Ptr< ParameterList > &paramList)
Reads XML parameters from a file and updates those already in the given parameter list...
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
RCP< ParameterList > parameterList()
Nonmember constructor.
Writes a ParameterList to an XML object.
Definition of XMLInputSource derived class for reading XML from a std::string.
Definition of XMLInputSource derived class for reading XML from a file.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void writeParameterListToXmlOStream(const ParameterList &paramList, std::ostream &xmlOut, RCP< const DependencySheet > depSheet=null)
Write parameters and sublists in XML format to an std::ostream.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromXmlString(const std::string &xmlStr, const Ptr< ParameterList > &paramList, bool overwrite=true)
Reads XML parameters from a std::string and updates those already in the given parameter list...