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
00034 #ifndef PARAMETERFILE_H
00035 #define PARAMETERFILE_H
00036
00037
00038 #include <string>
00039 #include <iostream>
00040 #include <sstream>
00041 #include <fstream>
00042
00043 #include <vector>
00044
00045 #include <complex>
00046 #include <cmath>
00047 #include <typeinfo>
00048
00049 #include <simthetic/misc.h>
00050 #include <simthetic/exceptions.h>
00051 #include <simthetic/checkedvector.h>
00052
00053 namespace simth
00054 {
00055
00056
00057 template<class T>
00058 class checkedVector;
00059
00060
00063
00064 bool isComment(const std::string& token);
00065 void nextLine(std::istream& inputStream);
00066 bool seekToToken(const std::string& token, std::istream& parameterStream,bool ignoreComments=true);
00067 bool isToken(const std::string& token, std::istream& parameterStream,bool ignoreComments=true);
00068 std::string getStream(const std::string& token, std::istream& is);
00069 std::string getStream(const std::string& region, const std::string& token, std::istream& is);
00070
00071 std::string getString(const std::string& token, std::istream& parameterStream);
00072
00073 std::string getPathname(const std::string& token, std::istream& parameterStream);
00074 simth::checkedVector<std::string> getPathnameSeq(const std::string& token, std::istream& istr);
00075 bool getBool(const std::string& token, std::istream& parameterStream);
00076 int getInt(const std::string& token, std::istream& parameterStream);
00077 unsigned getOctal(const std::string& istring);
00078 int getOctal(const std::string& token, std::istream& parameterStream);
00079 double getDouble(const std::string& token, std::istream& parameterStream);
00080 template<class T>
00081 checkedVector<T> getDecSeq(const std::string& token, std::istream& is);
00082 checkedVector<int> getDecSeq(const std::string& token, std::istream& is);
00083 std::vector<int> getOctalSeq(const std::string& helpString);
00084 checkedVector<int> getOctalSeq(const std::string& token, std::istream& is);
00085 template<class T>
00086 checkedVector<std::complex<T> > getComplSeq(const std::string& token, std::istream& is);
00087
00088 std::string getString(const std::string& token, std::istream& parameterStream, std::string defaultStr);
00089 std::string getLine(const std::string& token, std::istream& parameterStream, std::string defaultStr);
00090
00091 std::string getPathname(const std::string& token, std::istream& parameterStream, std::string defaultStr);
00092 bool getBool(const std::string& token, std::istream& parameterStream, bool defaultStatus);
00093 int getInt(const std::string& token, std::istream& parameterStream, int defaultVal);
00094 int getOctal(const std::string& token, std::istream& parameterStream, int defaultVal);
00095 double getDouble(const std::string& token, std::istream& parameterStream, double defaultVal);
00096
00097
00098 bool seekToToken(const std::string& region, const std::string& token,
00099 std::istream& parameterStream, const std::string endQualifier = "[end]");
00100 bool isToken(const std::string& region, const std::string& token, std::istream& parameterStream);
00101
00102 std::string getString(const std::string& region, const std::string& token, std::istream& parameterStream);
00103
00104
00105 std::string getPathname(const std::string& region, const std::string& token, std::istream& parameterStream);
00106 bool getBool(const std::string& region, const std::string& token, std::istream& parameterStream);
00107 int getInt(const std::string& region, const std::string& token, std::istream& parameterStream);
00108 int getOctal(const std::string& region, const std::string& token, std::istream& parameterStream);
00109 double getDouble(const std::string& region, const std::string& token, std::istream& parameterStream);
00110 Complex getComplex(const std::string& region, const std::string& token, std::istream& is);
00111 checkedVector<int> getDecSeq(const std::string& region, const std::string& token, std::istream& is);
00112 checkedVector<int> getOctalSeq(const std::string& region, const std::string& token, std::istream& is);
00113
00114 template<class T> std::vector<T>
00115 getSeq(const std::string&);
00116
00117 template<class T>
00118 checkedVector<T> getSeq(const std::string& region, const std::string& token, std::istream& is);
00119
00120 template<class T>
00121 checkedVector<T> getSeq(const std::string& region, const std::string& token, std::istream& is,checkedVector<T> defaultSeq);
00122
00123 template<class T>
00124 checkedVector<std::complex<T> > getComplSeq(const std::string& region, const std::string& token, std::istream& is);
00125
00126 std::string getString(const std::string& region, const std::string& token, std::istream& parameterStream, std::string defaultStr);
00127 std::string getLine(const std::string& region, const std::string& token, std::istream& parameterStream, std::string defaultStr);
00128
00129 std::string getPathname(const std::string& region, const std::string& token, std::istream& parameterStream, std::string defaultStr);
00130 bool getBool(const std::string& region, const std::string& token, std::istream& parameterStream, bool defaultStatus);
00131 int getInt(const std::string& region, const std::string& token, std::istream& parameterStream, int defaultVal);
00132 int getOctal(const std::string& region, const std::string& token, std::istream& parameterStream, int defaultVal);
00133 double getDouble(const std::string& region, const std::string& token, std::istream& parameterStream, double defaultVal);
00134
00135
00142 size_t getSequenceLength(const std::string& token, std::istream& parameterStream);
00143
00148 size_t getSequenceLength(const std::string& token, std::istream& parameterStream, int defaultVal);
00149
00153 size_t getSequenceLength(const std::string& region, const std::string& token, std::istream& parameterStream);
00154
00158 size_t getSequenceLength(const std::string& region, const std::string& token, std::istream& parameterStream, int defaultVal);
00159
00160
00165 template<class T>
00166 inline std::ostream& printParameter(const std::string& name, T value,std::ostream &os )
00167 {
00168
00169 std::ios::fmtflags oldSettings = os.flags();
00170 os.width(0);
00171 os<<"# ";
00172 os.width(15);
00173 os<<name.c_str();
00174 os.width(0);
00175 os<<": "<<value;
00176 os.setf(oldSettings);
00177 return os;
00178 }
00179
00180
00186 bool isFilemaskMatched(const char *mask, const char *s);
00187
00189
00190 }
00191
00192
00193 #ifndef DOXYGEN_IGNORE
00194
00195 template<class T>
00196 simth::checkedVector<T> simth::getDecSeq(const std::string& token, std::istream& is)
00197 {
00198
00199 std::ios::fmtflags oldSettings = is.flags();
00200 std::string helpString;
00201 std::string testString;
00202 T helpValue;
00203 checkedVector<T> helpPol(0);
00204 if(seekToToken(token, is)) {
00205 std::streampos currPos = is.tellg();
00206 while(is >> testString) {
00207 if(isComment(testString)) {
00208 break;
00209 }
00210 is.seekg(currPos);
00211
00212
00213 if(is >> helpValue) {
00214 helpPol.push_back(helpValue);
00215 }
00216 else {
00217 throw std::runtime_error(testString+" is no valid value to"
00218 " specify a "+typedid(helpValue).name()+" sequence for '"+token+"'");
00219 }
00220 currPos = is.tellg();
00221 }
00222 if(helpPol.size() > 0) {
00223 is.clear();
00224 is.setf(oldSettings);
00225 return helpPol;
00226 }
00227 else {
00228 is.clear();
00229 is.setf(oldSettings);
00230 throw std::runtime_error(token+" has no valid value");
00231 }
00232 }
00233 else {
00234 throw simth::token_not_found(token);
00235 }
00236 }
00237
00238
00239 template<class T> std::vector<T>
00240 simth::getSeq(const std::string& helpString)
00241 {
00242 std::istringstream is(helpString);
00243
00244 std::string testString;
00245 T helpValue;
00246 std::vector<T> helpPol(0);
00247
00248 std::streampos currPos = is.tellg();
00249 while(is >> testString) {
00250 if(isComment(testString)) {
00251 break;
00252 }
00253 is.seekg(currPos);
00254
00255
00256 if(is >> helpValue) {
00257 helpPol.push_back(helpValue);
00258 }
00259 else {
00260 throw std::runtime_error(testString+" is no valid value to"
00261 " specify a "+typeid(helpValue).name()+" sequence");
00262 }
00263 currPos = is.tellg();
00264 }
00265 return helpPol;
00266 }
00267
00268 template<class T>
00269 inline simth::checkedVector<T> simth::getSeq(const std::string& region, const std::string& token, std::istream& is)
00270 {
00271
00272
00273 std::ios::fmtflags oldSettings = is.flags();
00274 std::string helpString;
00275 std::string testString;
00276 T helpValue;
00277 checkedVector<T> helpPol(0);
00278 if(seekToToken(region, token, is)) {
00279 std::streampos currPos = is.tellg();
00280 while(is >> testString) {
00281 if(isComment(testString)) {
00282 break;
00283 }
00284 is.seekg(currPos);
00285
00286
00287 if(is >> helpValue) {
00288 helpPol.push_back(helpValue);
00289 }
00290 else {
00291 throw std::runtime_error(testString+" is no valid value to"
00292 " specify a "+typeid(helpValue).name()+" sequence for '"+token+"'");
00293 }
00294 currPos = is.tellg();
00295 }
00296 if(helpPol.size() > 0) {
00297 is.clear();
00298 is.setf(oldSettings);
00299 return helpPol;
00300 }
00301 else {
00302 is.clear();
00303 is.setf(oldSettings);
00304 throw std::runtime_error(token+" has no valid value");
00305 }
00306 }
00307 else {
00308 throw simth::token_not_found(token);
00309 }
00310 }
00311
00312
00313
00314
00315 template<class T>
00316 inline simth::checkedVector<T> simth::getSeq(const std::string& region, const std::string& token, std::istream& is,simth::checkedVector<T> defaultSeq)
00317 {
00318 try {
00319 return simth::getSeq<T>(region, token, is);
00320 } catch (simth::token_not_found) {
00321 return defaultSeq;
00322 }
00323 }
00324
00325
00326 namespace simth {
00327
00328 template<class T>
00329 inline checkedVector<std::complex<T> > getComplSeq(const std::string& token, std::istream& is)
00330 {
00331
00332 std::ios::fmtflags oldSettings = is.flags();
00333 std::string helpString;
00334 std::string testString;
00335 std::complex<T> helpValue;
00336 checkedVector<std::complex<T> > helpPol(0);
00337 if(seekToToken(token, is)) {
00338 std::streampos currPos = is.tellg();
00339 while(is >> testString) {
00340 if(isComment(testString)) {
00341 break;
00342 }
00343 is.seekg(currPos);
00344
00345
00346 if(std::operator>>(is,helpValue)) {
00347 helpPol.push_back(helpValue);
00348 }
00349 else {
00350 throw std::runtime_error(testString+" is no valid value to"
00351 " specify a complex sequence");
00352 }
00353 currPos = is.tellg();
00354 }
00355 is.clear();
00356 is.setf(oldSettings);
00357 return helpPol;
00358 }
00359 else {
00360 throw simth::token_not_found(token);
00361 }
00362 }
00363
00364
00365 template<class T>
00366 inline checkedVector<std::complex<T> > getComplSeq(const std::string& region, const std::string& token, std::istream& is)
00367 {
00368
00369 std::ios::fmtflags oldSettings = is.flags();
00370 std::string helpString;
00371 std::string testString;
00372 std::complex<T> helpValue;
00373 checkedVector<std::complex<T> > helpPol(0);
00374 if(seekToToken(region, token, is)) {
00375 std::streampos currPos = is.tellg();
00376 while(is >> testString) {
00377 if(isComment(testString)) {
00378 break;
00379 }
00380 is.seekg(currPos);
00381
00382
00383 if(operator>>(is,helpValue)) {
00384 helpPol.push_back(helpValue);
00385 }
00386 else {
00387 throw std::runtime_error(testString+" is no valid value to"
00388 " specify a complex sequence");
00389 }
00390 currPos = is.tellg();
00391 }
00392 is.clear();
00393 is.setf(oldSettings);
00394 return helpPol;
00395 }
00396 else {
00397 throw simth::token_not_found(token);
00398 }
00399 }
00400 }
00401
00402 #endif // DOXYGEN_IGNORE
00403
00404
00405 namespace simth {
00406 class PropertyList;
00414 void readProperties(const std::string& region,
00415 std::istream& parameterStream,
00416 PropertyList& properties);
00417
00418
00419 }
00420
00421 #endif // PARAMETERFILE_H
00422