44 string trim (
const string &orig)
46 string::size_type p1=orig.find_first_not_of(
" \t");
47 if (p1==string::npos)
return "";
48 string::size_type p2=orig.find_last_not_of(
" \t");
49 return orig.substr(p1,p2-p1+1);
54 ostringstream strstrm;
56 return trim(strstrm.str());
60 {
return x ?
"T" :
"F"; }
65 ostringstream strstrm;
66 strstrm << setprecision(8) << x;
67 return trim(strstrm.str());
71 ostringstream strstrm;
72 strstrm << setprecision(16) << x;
73 return trim(strstrm.str());
77 ostringstream strstrm;
78 strstrm << setprecision(25) << x;
79 return trim(strstrm.str());
91 template string dataToString (
const unsigned long long &x);
95 ostringstream strstrm;
96 (x>=0) ? strstrm << setw(width) << setfill(
'0') << x
97 : strstrm <<
"-" << setw(width-1) << setfill(
'0') << -x;
98 string res = strstrm.str();
105 void end_stringToData (
const string &x,
const char *tn, istringstream &strstrm)
107 string error = string(
"conversion error in stringToData<")+tn+
">(\""+x+
"\")";
117 template<
typename T>
void stringToData (
const string &x, T &value)
119 istringstream strstrm(x);
121 end_stringToData (x,type2typename<T>(),strstrm);
124 template<>
void stringToData (
const string &x,
string &value)
127 template<>
void stringToData (
const string &x,
bool &value)
129 const char *fval[] = {
"f",
"n",
"false",
".false."};
130 const char *tval[] = {
"t",
"y",
"true",
".true."};
131 for (
tsize i=0; i<
sizeof(fval)/
sizeof(fval[0]); ++i)
133 for (
tsize i=0; i<
sizeof(tval)/
sizeof(tval[0]); ++i)
135 planck_fail(
"conversion error in stringToData<bool>(\""+x+
"\")");
138 template void stringToData (
const string &x,
signed char &value);
139 template void stringToData (
const string &x,
unsigned char &value);
140 template void stringToData (
const string &x,
short &value);
141 template void stringToData (
const string &x,
unsigned short &value);
142 template void stringToData (
const string &x,
int &value);
143 template void stringToData (
const string &x,
unsigned int &value);
144 template void stringToData (
const string &x,
long &value);
145 template void stringToData (
const string &x,
unsigned long &value);
146 template void stringToData (
const string &x,
long long &value);
147 template void stringToData (
const string &x,
unsigned long long &value);
148 template void stringToData (
const string &x,
float &value);
149 template void stringToData (
const string &x,
double &value);
150 template void stringToData (
const string &x,
long double &value);
154 if (a.size()!=b.size())
return false;
155 for (
tsize m=0; m<a.size(); ++m)
160 string tolower(
const string &input)
163 for (
tsize m=0; m<result.size(); ++m)
164 result[m]=
char(
tolower(result[m]));
168 void parse_file (
const string &filename, map<string,string> &dict)
172 ifstream inp(filename.c_str());
173 planck_assert (inp,
"Could not open parameter file '"+filename+
"'.");
180 line=line.substr(0,line.find(
"\r"));
181 line=line.substr(0,line.find(
"#"));
185 string::size_type eqpos=line.find(
"=");
186 if (eqpos!=string::npos)
188 string key=
trim(line.substr(0,eqpos)),
189 value=
trim(line.substr(eqpos+1,string::npos));
191 cerr <<
"Warning: empty key in '" << filename <<
"', line " 195 if (dict.find(key)!=dict.end())
196 cerr <<
"Warning: key '" << key <<
"' multiply defined in '" 197 << filename <<
"', line " << lineno << endl;
202 cerr <<
"Warning: unrecognized format in '" << filename <<
"', line " 203 << lineno <<
":\n" << line << endl;
210 bool isParam (
const string &s)
212 if (s.size()<2)
return false;
213 if (s[0]!=
'-')
return false;
214 return !(isdigit(s[1]) || (s[1]==
'.'));
219 void parse_cmdline_classic (
int argc,
const char **argv,
220 const vector<string> &leading_args, map<string,string> &dict)
224 for (
tsize i=0; i<leading_args.size(); ++i)
225 dict[leading_args[i]] = argv[i+1];
226 int curarg=leading_args.size()+1;
229 string param=argv[curarg];
230 planck_assert(isParam(param),
"unrecognized command line format");
231 if ((curarg==argc-1) || isParam(argv[curarg+1]))
233 dict[param.substr(1)]=
"true";
238 dict[param.substr(1)]=argv[curarg+1];
244 void parse_cmdline_classic (
int argc,
const char **argv,
245 map<string,string> &dict)
246 { parse_cmdline_classic (argc, argv, vector<string>(), dict); }
248 void parse_cmdline_equalsign (
int argc,
const char **argv,
249 const vector<string> &leading_args, map<string,string> &dict)
253 for (
tsize i=0; i<leading_args.size(); ++i)
254 dict[leading_args[i]] = argv[i+1];
255 for (
int i=leading_args.size()+1; i<argc; ++i)
257 string arg=
trim(argv[i]);
260 string::size_type eqpos=arg.find(
"=");
261 if (eqpos!=string::npos)
263 string key=
trim(arg.substr(0,eqpos)),
264 value=
trim(arg.substr(eqpos+1,string::npos));
266 cerr <<
"Warning: empty key in argument'" << arg <<
"'" << endl;
269 if (dict.find(key)!=dict.end())
270 cerr <<
"Warning: key '" << key <<
"' multiply defined" << endl;
275 cerr <<
"Warning: unrecognized format in argument '" << arg <<
"'" 281 void parse_cmdline_equalsign (
int argc,
const char **argv,
282 map<string,string> &dict)
283 { parse_cmdline_equalsign (argc, argv, vector<string>(), dict); }
287 template<
typename T>
void split (istream &stream, vector<T> &list)
295 string(
"error while splitting stream into ") + type2typename<T>()
297 if (stream) list.push_back(stringToData<T>(word));
303 template<
typename T>
void split (
const string &inp, vector<T> &list)
305 istringstream stream(inp);
309 template void split (
const string &inp, vector<string> &list);
310 template void split (
const string &inp, vector<float> &list);
311 template void split (
const string &inp, vector<double> &list);
312 template void split (
const string &inp, vector<int> &list);
313 template void split (
const string &inp, vector<long> &list);
315 void tokenize (
const string &inp,
char delim, vector<string> &list)
317 istringstream stream(inp);
320 while (getline(stream,token,delim))
321 list.push_back(token);
327 ifstream inp(filename.c_str());
334 if (word!=
"") words.push_back(word);
void tokenize(const std::string &inp, char delim, std::vector< std::string > &list)
string intToString(int64 x, tsize width)
void parse_words_from_file(const std::string &filename, std::vector< std::string > &words)
bool equal_nocase(const std::string &a, const std::string &b)
std::string trim(const std::string &orig)
void parse_file(const std::string &filename, std::map< std::string, std::string > &dict)
void stringToData(const std::string &x, T &value)
string dataToString(const T &x)
#define planck_assert(testval, msg)
void split(const std::string &inp, std::vector< T > &list)
std::string tolower(const std::string &input)