Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
XmlToParameterList.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Teuchos_Version.hpp"
48 
49 #include <fstream>
50 
51 int main( int argc, char* argv[] )
52 {
53 
54  using Teuchos::inoutArg;
55 
56  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
57 
58  std::cout << std::endl << Teuchos::Teuchos_Version() << std::endl;
59 
60  bool success = true;
61 
62  try {
63 
64  std::string xmlInFileName = "";
65  std::string extraXmlFile = "";
66  std::string xmlOutFileName = "paramList.out";
67 
68  Teuchos::CommandLineProcessor clp(false); // Don't throw exceptions
69  clp.setOption("xml-in-file",&xmlInFileName,"The XML file to read into a parameter list");
70  clp.setOption("extra-xml-file",&extraXmlFile,"File with extra XML text that will modify the initial XML read in");
71  clp.setOption("xml-out-file",&xmlOutFileName,"The XML file to write the final parameter list to");
72  clp.setDocString(
73  "This example program shows how to read in a parameter list from an"
74  " XML file (given by --xml-in-file=xmlInFileName) and then modify it"
75  " given some XML specified on the command-line (given by --extra-xml=extrXmlStr)."
76  " The final parameter list is then written back to an XML file."
77  " (given by --xml-out-file=xmlOutFileName)."
78  );
80  parse_return = clp.parse(argc,argv);
82  std::cout << "\nEnd Result: TEST FAILED" << std::endl;
83  return parse_return;
84  }
85 
86  Teuchos::ParameterList paramList;
87 
88  if(xmlInFileName.length()) {
89  std::cout << "\nReading a parameter list from the XML file \""<<xmlInFileName<<"\" ...\n";
90  Teuchos::updateParametersFromXmlFile(xmlInFileName, inoutArg(paramList));
91  std::cout << "\nParameter list read from the XML file \""<<xmlInFileName<<"\":\n\n";
92  paramList.print(std::cout,2,true,true);
93  }
94 
95  std::string line("");
96  if(extraXmlFile.length()) {
97  std::ifstream myfile(extraXmlFile.c_str());
98  if (myfile.is_open())
99  {
100  getline (myfile,line);
101  std::cout << line << "\n";
102  myfile.close();
103  }
104  std::cout << "\nUpdating the parameter list given the extra XML std::string:\n\n"<<line<<"\n";
105  Teuchos::updateParametersFromXmlString(line, inoutArg(paramList));
106  std::cout << "\nParameter list after ammending extra XML std::string:\n\n";
107  paramList.print(std::cout,2,true,true);
108  }
109 
110  std::cout << "\nWriting the final parameter list back to the XML file \""<<xmlOutFileName<<"\" ... \n";
111  Teuchos::writeParameterListToXmlFile(paramList,xmlOutFileName);
112 
113  }
114  TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
115 
116  if(success)
117  std::cout << "\nEnd Result: TEST PASSED" << std::endl;
118  else
119  std::cout << "\nEnd Result: TEST FAILED" << std::endl;
120 
121  return ( success ? 0 : 1 );
122 
123 }
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.
void print() const
Print function to use in debugging in a debugger.
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.
int main(int argc, char *argv[])
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
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.
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.