![]() |
Gowdy solver
|
00001 /** 00002 * @file paramsHolder.h 00003 * @brief Access to the configuration files. 00004 * @authors Mikica Kocic, Wilkinson & Reinsch, Press et al 00005 * @copyright GNU General Public License (GPLv3). 00006 */ 00007 00008 #include <map> 00009 00010 /** Contains `key = value` pairs that are obtained from a configuration file. 00011 */ 00012 class Parameters 00013 : std::map<std::string, std::string> 00014 { 00015 public: 00016 00017 /** Checks wether the given key already exists. 00018 */ 00019 bool exists( const std::string& key ) const { 00020 return count( key ) > 0; 00021 } 00022 00023 /** Loads the configuration parameters from file. 00024 * The expected format is `key = value` (where value can be empty). 00025 */ 00026 Parameters( const std::string& fileName ) 00027 { 00028 FILE* inf = fileName == "stdin" ? stdin 00029 : fopen( fileName.c_str(), "r" ); 00030 if( ! inf ) { 00031 std::cerr << "Error: Could not open " << fileName << std::endl; 00032 return; 00033 } 00034 00035 char line[ 1024 ] = {0}; 00036 while( fgets( line, sizeof(line)-1, inf ) ) 00037 { 00038 char key[ 64 ] = {0}; 00039 int eqpos = -1, value = -1; 00040 if ( 1 != sscanf( line, " %s %n= %n", key, &eqpos, &value ) ) { 00041 // empty line 00042 } 00043 else if ( eqpos < 0 || line[eqpos] != '=' ) { 00044 std::cerr << "Error: Not 'key = value' pair:" << std::endl << line; 00045 } 00046 else { 00047 char* chp = line + value; 00048 while( *chp ) ++chp; 00049 while( isspace( chp[-1] ) ) { *--chp = 0; } 00050 if ( exists( key ) ) { 00051 std::cerr << "Warning: Redefining " << key << std::endl; 00052 } 00053 operator[]( key ) = std::string( line + value ); 00054 } 00055 }; 00056 00057 fclose( inf ); 00058 } 00059 00060 void get( const std::string& key, Real& var, const Real defaultValue ) 00061 { 00062 var = !exists( key ) ? defaultValue 00063 : strToReal( operator[]( key ).c_str(), NULL ); 00064 } 00065 00066 void get( const std::string& key, Int& var, const Int defaultValue ) 00067 { 00068 var = !exists( key ) ? defaultValue 00069 : std::atoi( operator[]( key ).c_str() ); 00070 } 00071 00072 void get( const std::string& key, std::string& var, const char* defaultValue ) 00073 { 00074 var = !exists( key ) ? defaultValue : operator[]( key ); 00075 } 00076 00077 void get( const std::string& key, bool& var, const bool defaultValue ) 00078 { 00079 var = !exists( key ) ? defaultValue 00080 : std::atoi( operator[]( key ).c_str() ) != 0; 00081 } 00082 00083 std::string get( const std::string& key, Int& var, const Int defaultValue, 00084 std::map<std::string, int>& enumMap ) 00085 { 00086 if( !exists( key ) || enumMap.count( operator[]( key ) ) == 0 ) { 00087 var = defaultValue; 00088 return "(unknown)"; 00089 } 00090 std::string verboseName = operator[]( key ); 00091 var = enumMap[ verboseName ]; 00092 return verboseName; 00093 } 00094 00095 /** Dumps all the keys that were loaded. 00096 */ 00097 void dump () 00098 { 00099 for ( Parameters::iterator i = begin(); i != end(); ++i ) { 00100 std::cout << "(" << i->first << ")=(" << i->second << ")" << std::endl; 00101 } 00102 } 00103 };