Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XmlToParameterList.cpp
Go to the documentation of this file.
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 
10 #include "Teuchos_Version.hpp"
16 
17 #include <fstream>
18 
19 int main( int argc, char* argv[] )
20 {
21 
22  using Teuchos::inoutArg;
23 
24  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
25 
26  std::cout << std::endl << Teuchos::Teuchos_Version() << std::endl;
27 
28  bool success = true;
29 
30  try {
31 
32  std::string xmlInFileName = "";
33  std::string extraXmlFile = "";
34  std::string xmlOutFileName = "paramList.out";
35 
36  Teuchos::CommandLineProcessor clp(false); // Don't throw exceptions
37  clp.setOption("xml-in-file",&xmlInFileName,"The XML file to read into a parameter list");
38  clp.setOption("extra-xml-file",&extraXmlFile,"File with extra XML text that will modify the initial XML read in");
39  clp.setOption("xml-out-file",&xmlOutFileName,"The XML file to write the final parameter list to");
40  clp.setDocString(
41  "This example program shows how to read in a parameter list from an"
42  " XML file (given by --xml-in-file=xmlInFileName) and then modify it"
43  " given some XML specified on the command-line (given by --extra-xml=extrXmlStr)."
44  " The final parameter list is then written back to an XML file."
45  " (given by --xml-out-file=xmlOutFileName)."
46  );
48  parse_return = clp.parse(argc,argv);
50  std::cout << "\nEnd Result: TEST FAILED" << std::endl;
51  return parse_return;
52  }
53 
54  Teuchos::ParameterList paramList;
55 
56  if(xmlInFileName.length()) {
57  std::cout << "\nReading a parameter list from the XML file \""<<xmlInFileName<<"\" ...\n";
58  Teuchos::updateParametersFromXmlFile(xmlInFileName, inoutArg(paramList));
59  std::cout << "\nParameter list read from the XML file \""<<xmlInFileName<<"\":\n\n";
60  paramList.print(std::cout,2,true,true);
61  }
62 
63  std::string line("");
64  if(extraXmlFile.length()) {
65  std::ifstream myfile(extraXmlFile.c_str());
66  if (myfile.is_open())
67  {
68  getline (myfile,line);
69  std::cout << line << "\n";
70  myfile.close();
71  }
72  std::cout << "\nUpdating the parameter list given the extra XML std::string:\n\n"<<line<<"\n";
73  Teuchos::updateParametersFromXmlString(line, inoutArg(paramList));
74  std::cout << "\nParameter list after ammending extra XML std::string:\n\n";
75  paramList.print(std::cout,2,true,true);
76  }
77 
78  std::cout << "\nWriting the final parameter list back to the XML file \""<<xmlOutFileName<<"\" ... \n";
79  Teuchos::writeParameterListToXmlFile(paramList,xmlOutFileName);
80 
81  }
82  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
83 
84  if(success)
85  std::cout << "\nEnd Result: TEST PASSED" << std::endl;
86  else
87  std::cout << "\nEnd Result: TEST FAILED" << std::endl;
88 
89  return ( success ? 0 : 1 );
90 
91 }
void print() const
Print function to use in debugging in a debugger.
Initialize, finalize, and query the global MPI session.
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
Templated Parameter List class.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
Ptr< T > inoutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
std::string Teuchos_Version()
A list of parameters of arbitrary type.
int main(int argc, char *argv[])
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
void setDocString(const char doc_string[])
Set a documentation sting for the entire program printed when –help is specified. ...
Class that helps parse command line input arguments from (argc,argv[]) and set options.