paramfile.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <iostream>
00033 #include "paramfile.h"
00034 #include "string_utils.h"
00035
00036 using namespace std;
00037
00038 string paramfile::get_valstr(const string &key) const
00039 {
00040 params_type::const_iterator loc=params.find(key);
00041 if (loc!=params.end()) return loc->second;
00042 planck_fail ("Cannot find the key '" + key + "'.");
00043 }
00044
00045 bool paramfile::param_unread (const string &key) const
00046 { return (read_params.find(key)==read_params.end()); }
00047
00048 paramfile::paramfile (const string &filename, bool verbose_)
00049 : verbose(verbose_)
00050 { parse_file (filename, params); }
00051
00052 paramfile::paramfile (const params_type &par, bool verbose_)
00053 : params (par), verbose(verbose_)
00054 {}
00055
00056 paramfile::~paramfile()
00057 {
00058 if (verbose)
00059 for (params_type::const_iterator loc=params.begin();
00060 loc!=params.end(); ++loc)
00061 if (param_unread(loc->first))
00062 cout << "Parser warning: unused parameter '"
00063 << loc->first << "'" << endl;
00064 }
00065
00066 bool paramfile::param_present(const string &key) const
00067 { return (params.find(key)!=params.end()); }
00068
00069 void paramfile::findhelper (const string &key, const string &value, NDT type,
00070 bool deflt) const
00071 {
00072 string output = value;
00073 if (type==NAT_STRING) output = "'"+output+"'";
00074 if (verbose && param_unread(key))
00075 cout << "Parser: " << key << " = " << output
00076 << (deflt ? " <default>" : "") << endl;
00077 read_params.insert(key);
00078 }
00079
00080 template<typename T> T paramfile::find (const string &key) const
00081 {
00082 T result = stringToData<T>(get_valstr(key));
00083 findhelper (key, dataToString(result), nativeType<T>(), false);
00084 return result;
00085 }
00086
00087 template unsigned char paramfile::find (const string &key) const;
00088 template signed char paramfile::find (const string &key) const;
00089 template unsigned short paramfile::find (const string &key) const;
00090 template short paramfile::find (const string &key) const;
00091 template unsigned int paramfile::find (const string &key) const;
00092 template int paramfile::find (const string &key) const;
00093 template unsigned long paramfile::find (const string &key) const;
00094 template long paramfile::find (const string &key) const;
00095 template unsigned long long paramfile::find (const string &key) const;
00096 template long long paramfile::find (const string &key) const;
00097 template float paramfile::find (const string &key) const;
00098 template double paramfile::find (const string &key) const;
00099 template long double paramfile::find (const string &key) const;
00100 template bool paramfile::find (const string &key) const;
00101 template string paramfile::find (const string &key) const;
00102
00103 template<typename T> T paramfile::find (const string &key, const T &deflt)
00104 {
00105 if (param_present(key)) return find<T>(key);
00106 string sdeflt=dataToString(deflt);
00107 findhelper (key, sdeflt, nativeType<T>(), true);
00108 params[key]=sdeflt;
00109 return deflt;
00110 }
00111
00112 template unsigned char paramfile::find (const string &key,
00113 const unsigned char &deflt);
00114 template signed char paramfile::find (const string &key,
00115 const signed char &deflt);
00116 template unsigned short paramfile::find (const string &key,
00117 const unsigned short &deflt);
00118 template short paramfile::find (const string &key, const short &deflt);
00119 template unsigned int paramfile::find (const string &key,
00120 const unsigned int &deflt);
00121 template int paramfile::find (const string &key, const int &deflt);
00122 template unsigned long paramfile::find (const string &key,
00123 const unsigned long &deflt);
00124 template long paramfile::find (const string &key, const long &deflt);
00125 template unsigned long long paramfile::find (const string &key,
00126 const unsigned long long &deflt);
00127 template long long paramfile::find (const string &key, const long long &deflt);
00128 template float paramfile::find (const string &key, const float &deflt);
00129 template double paramfile::find (const string &key, const double &deflt);
00130 template long double paramfile::find (const string &key,
00131 const long double &deflt);
00132 template bool paramfile::find (const string &key, const bool &deflt);
00133 template string paramfile::find (const string &key, const string &deflt);
00134
00135 void paramfile::setParamString (const string &key, const string &value)
00136 {
00137 if (param_present(key))
00138 {
00139 if (params[key]!=value)
00140 {
00141 if (verbose)
00142 cout << "Parser: altering value of key '"<<key<<"' to '"<<value<<"'."
00143 << endl;
00144 params[key]=value;
00145 }
00146 }
00147 else
00148 {
00149 if (verbose)
00150 cout << "Parser: setting new key '"<<key<<"' to '"<<value<<"'."<<endl;
00151 params[key]=value;
00152 }
00153 }
00154
00155 paramfile getParamsFromCmdline (int argc, const char **argv, bool verbose)
00156 {
00157 planck_assert(argc>=2,"incorrect command line format");
00158 if ((argc==2) && (string(argv[1]).find("=")==string::npos))
00159 return paramfile(argv[1],verbose);
00160 map<string,string> pmap;
00161 parse_cmdline_equalsign(argc,argv,pmap);
00162 return paramfile(pmap,verbose);
00163 }