| 1 |
/* |
| 2 |
* This file is part of libcxxsupport. |
| 3 |
* |
| 4 |
* libcxxsupport is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* libcxxsupport is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with libcxxsupport; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik |
| 21 |
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt |
| 22 |
* (DLR). |
| 23 |
*/ |
| 24 |
|
| 25 |
/* |
| 26 |
* Class for parsing parameter files |
| 27 |
* |
| 28 |
* Copyright (C) 2003-2011 Max-Planck-Society |
| 29 |
* Authors: Martin Reinecke, Reinhard Hell |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <iostream> |
| 33 |
#include "paramfile.h" |
| 34 |
#include "string_utils.h" |
| 35 |
|
| 36 |
using namespace std; |
| 37 |
|
| 38 |
string paramfile::get_valstr(const string &key) const |
| 39 |
{ |
| 40 |
params_type::const_iterator loc=params.find(key); |
| 41 |
if (loc!=params.end()) return loc->second; |
| 42 |
planck_fail ("Cannot find the key '" + key + "'."); |
| 43 |
} |
| 44 |
|
| 45 |
bool paramfile::param_unread (const string &key) const |
| 46 |
{ return (read_params.find(key)==read_params.end()); } |
| 47 |
|
| 48 |
paramfile::paramfile (const string &filename, bool verbose_) |
| 49 |
: verbose(verbose_) |
| 50 |
{ parse_file (filename, params); } |
| 51 |
|
| 52 |
paramfile::paramfile (const params_type &par, bool verbose_) |
| 53 |
: params (par), verbose(verbose_) |
| 54 |
{} |
| 55 |
|
| 56 |
paramfile::~paramfile() |
| 57 |
{ |
| 58 |
if (verbose) |
| 59 |
for (params_type::const_iterator loc=params.begin(); |
| 60 |
loc!=params.end(); ++loc) |
| 61 |
if (param_unread(loc->first)) |
| 62 |
cout << "Parser warning: unused parameter '" |
| 63 |
<< loc->first << "'" << endl; |
| 64 |
} |
| 65 |
|
| 66 |
bool paramfile::param_present(const string &key) const |
| 67 |
{ return (params.find(key)!=params.end()); } |
| 68 |
|
| 69 |
void paramfile::findhelper (const string &key, const string &value, NDT type, |
| 70 |
bool deflt) const |
| 71 |
{ |
| 72 |
string output = value; |
| 73 |
if (type==NAT_STRING) output = "'"+output+"'"; |
| 74 |
if (verbose && param_unread(key)) |
| 75 |
cout << "Parser: " << key << " = " << output |
| 76 |
<< (deflt ? " <default>" : "") << endl; |
| 77 |
read_params.insert(key); |
| 78 |
} |
| 79 |
|
| 80 |
template<typename T> T paramfile::find (const string &key) const |
| 81 |
{ |
| 82 |
T result = stringToData<T>(get_valstr(key)); |
| 83 |
findhelper (key, dataToString(result), nativeType<T>(), false); |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
template unsigned char paramfile::find (const string &key) const; |
| 88 |
template signed char paramfile::find (const string &key) const; |
| 89 |
template unsigned short paramfile::find (const string &key) const; |
| 90 |
template short paramfile::find (const string &key) const; |
| 91 |
template unsigned int paramfile::find (const string &key) const; |
| 92 |
template int paramfile::find (const string &key) const; |
| 93 |
template unsigned long paramfile::find (const string &key) const; |
| 94 |
template long paramfile::find (const string &key) const; |
| 95 |
template unsigned long long paramfile::find (const string &key) const; |
| 96 |
template long long paramfile::find (const string &key) const; |
| 97 |
template float paramfile::find (const string &key) const; |
| 98 |
template double paramfile::find (const string &key) const; |
| 99 |
template long double paramfile::find (const string &key) const; |
| 100 |
template bool paramfile::find (const string &key) const; |
| 101 |
template string paramfile::find (const string &key) const; |
| 102 |
|
| 103 |
template<typename T> T paramfile::find (const string &key, const T &deflt) |
| 104 |
{ |
| 105 |
if (param_present(key)) return find<T>(key); |
| 106 |
string sdeflt=dataToString(deflt); |
| 107 |
findhelper (key, sdeflt, nativeType<T>(), true); |
| 108 |
params[key]=sdeflt; |
| 109 |
return deflt; |
| 110 |
} |
| 111 |
|
| 112 |
template unsigned char paramfile::find (const string &key, |
| 113 |
const unsigned char &deflt); |
| 114 |
template signed char paramfile::find (const string &key, |
| 115 |
const signed char &deflt); |
| 116 |
template unsigned short paramfile::find (const string &key, |
| 117 |
const unsigned short &deflt); |
| 118 |
template short paramfile::find (const string &key, const short &deflt); |
| 119 |
template unsigned int paramfile::find (const string &key, |
| 120 |
const unsigned int &deflt); |
| 121 |
template int paramfile::find (const string &key, const int &deflt); |
| 122 |
template unsigned long paramfile::find (const string &key, |
| 123 |
const unsigned long &deflt); |
| 124 |
template long paramfile::find (const string &key, const long &deflt); |
| 125 |
template unsigned long long paramfile::find (const string &key, |
| 126 |
const unsigned long long &deflt); |
| 127 |
template long long paramfile::find (const string &key, const long long &deflt); |
| 128 |
template float paramfile::find (const string &key, const float &deflt); |
| 129 |
template double paramfile::find (const string &key, const double &deflt); |
| 130 |
template long double paramfile::find (const string &key, |
| 131 |
const long double &deflt); |
| 132 |
template bool paramfile::find (const string &key, const bool &deflt); |
| 133 |
template string paramfile::find (const string &key, const string &deflt); |
| 134 |
|
| 135 |
void paramfile::setParamString (const string &key, const string &value) |
| 136 |
{ |
| 137 |
if (param_present(key)) |
| 138 |
{ |
| 139 |
if (params[key]!=value) |
| 140 |
{ |
| 141 |
if (verbose) |
| 142 |
cout << "Parser: altering value of key '"<<key<<"' to '"<<value<<"'." |
| 143 |
<< endl; |
| 144 |
params[key]=value; |
| 145 |
} |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
if (verbose) |
| 150 |
cout << "Parser: setting new key '"<<key<<"' to '"<<value<<"'."<<endl; |
| 151 |
params[key]=value; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
paramfile getParamsFromCmdline (int argc, const char **argv, bool verbose) |
| 156 |
{ |
| 157 |
planck_assert(argc>=2,"incorrect command line format"); |
| 158 |
if ((argc==2) && (string(argv[1]).find("=")==string::npos)) |
| 159 |
return paramfile(argv[1],verbose); |
| 160 |
map<string,string> pmap; |
| 161 |
parse_cmdline_equalsign(argc,argv,pmap); |
| 162 |
return paramfile(pmap,verbose); |
| 163 |
} |