VParameterNode.cc

00001 //Copyright 2013 Ben Loer
00002 //This file is part of the ConfigHandler library
00003 //It is released under the terms of the GNU General Public License v3
00004 
00005 #include "VParameterNode.hh"
00006 #include "Message.hh"
00007 #include <fstream>
00008 #include <typeinfo>
00009 bool VParameterNode::SaveToFile(const char *fname, bool showhelp)
00010 {
00011   std::ofstream fout(fname);
00012   if(fout.fail()){
00013     Message(ERROR)<<"Unable to open file "<<fname<<" for writing."<<std::endl;
00014     return false;
00015   }
00016   return WriteTo(fout, showhelp);
00017 }
00018 
00019 bool VParameterNode::ReadFromFile(const char *fname, const std::string& key,
00020                                   bool suppress_errs)
00021 {
00022   if(key != "")
00023     Message(DEBUG)<<"Searching for '"<<key<<"' in file "<<fname<<std::endl;
00024   std::ifstream fin(fname);
00025   if(fin.fail()){
00026     if(!suppress_errs)
00027       Message(ERROR)<<"Unable to open file "<<fname<<" for reading."<<std::endl;
00028     return false;
00029   }
00030   ReadFromByKey(fin,key,suppress_errs);
00031   //eof seems to trip the failbit, so don't test directly
00032   //note that this test will allow an empty file to pass without problems!
00033   //similarly an incomplete  non-list Parameter
00034   bool fail = !(fin.good() || fin.eof());
00035   fin.close();
00036   return fail;
00037 }
00038 
00039 std::istream& VParameterNode::ReadFromByKey(std::istream& in, 
00040                                             const std::string& key,
00041                                             bool suppress_errs)
00042 {
00043   if(key == "")
00044     return ReadFrom(in);
00045   std::string next("empty");
00046   while( in >> next && next != key ) {}
00047   if (in.eof() && !suppress_errs){
00048     Message(ERROR)<<"Key "<<key<<" was not found.\n";
00049     return in;
00050   }
00051   else if(in.fail() && !suppress_errs){
00052     Message(ERROR)<<"Problem encountered reading '"<<key<<"' from stream.\n";
00053     return in;
00054   }
00055   
00056   return ReadFrom(in);
00057 }
00058 
00059 int VParameterNode::PrintHelp(const std::string& myname) const
00060 {
00061   std::cout<<"--------------------------------------------------------\n";
00062   std::cout<<"Parameter name: "<<myname<<"\n"
00063            <<"Default name:   "<<_default_key<<"\n";
00064   if(_helptext == "")
00065     std::cout<<"No description available for this parameter.\n";
00066   else
00067     std::cout<<"Description:    "<<_helptext<<std::endl;
00068 
00069   return 0;
00070 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1