00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "conf.h"
00020
00021 #include <fstream>
00022 #include <cerrno>
00023 #include <cctype>
00024 #include <cstring>
00025
00026
00027
00028 const OPENCITY_ERR_CODE
00029 Conf::Open(
00030 const string& fname )
00031 {
00032 std::ifstream inFile( fname.c_str() );
00033
00034
00035 if ( inFile == NULL ) {
00036 OPENCITY_DEBUG( "WARNING: File open error, see below: " );
00037 OPENCITY_DEBUG( fname.c_str() );
00038 return OC_ERR_FILE;
00039 }
00040
00041 OC_CHAR strTemp[ OC_MAX_CONF_LINE ];
00042 OC_CHAR* strNew;
00043 OC_CHAR* strFirst;
00044 OC_CHAR* strSecond;
00045 OC_CHAR* strEmpty = "";
00046
00047
00048 inFile.getline( strTemp, OC_MAX_CONF_LINE );
00049 if ( !inFile.good() ) {
00050 inFile.close();
00051 OPENCITY_DEBUG( "WARNING: File read error" );
00052 return OC_ERR_FILE;
00053 }
00054
00055
00056 while ( inFile.good() ) {
00057 if ( (strlen(strTemp) != 0)
00058 && (strTemp[0] != '#') ) {
00059 strNew = strTemp;
00060
00061 strFirst = strtok( strNew, "=" );
00062
00063 strSecond = strtok( NULL, "=" );
00064
00065 (strFirst != NULL) ? strFirst = Conf::RTrim( strFirst ) : strFirst = strEmpty;
00066 (strSecond != NULL) ? strSecond = Conf::LTrim( strSecond ) : strSecond = strEmpty;
00067
00068
00069 if (strlen(strFirst) > 0)
00070 _mapData[ strFirst ] = strSecond;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 }
00083
00084 inFile.getline( strTemp, OC_MAX_CONF_LINE );
00085 }
00086
00087 if (inFile.eof()) {
00088 inFile.close();
00089 return OC_ERR_FREE;
00090 }
00091 else {
00092 inFile.close();
00093 OPENCITY_DEBUG("FATAL: out of buffer ?");
00094 assert( 0 );
00095 return OC_ERR_SOMETHING;
00096 }
00097 }
00098
00099
00100
00101 void
00102 Conf::Close()
00103 {
00104
00105
00106
00107
00108
00109
00110
00111 _mapData.clear();
00112 }
00113
00114
00115
00116 const string &
00117 Conf::GetValue(
00118 const string& key,
00119 const string def )
00120 {
00121
00122
00123
00124
00125
00126
00127
00128
00129 if (_mapData.find( key ) == _mapData.end())
00130 return def;
00131 else
00132 return _mapData[ key ];
00133 }
00134
00135
00136
00137 const OPENCITY_ERR_CODE
00138 Conf::GetBool(
00139 const string & key,
00140 bool & rbool,
00141 const bool def )
00142 {
00143
00144 if (_mapData.find( key ) == _mapData.end()) {
00145 rbool = def;
00146 return OC_ERR_FREE;
00147 }
00148
00149 if (_mapData[key] == "") {
00150 return OC_ERR_INVALID;
00151 }
00152
00153 if ((strcasecmp(_mapData[key].c_str(), "no") == 0)
00154 || (strcasecmp(_mapData[key].c_str(), "n") == 0)
00155 || (strcasecmp(_mapData[key].c_str(), "false") == 0)
00156 || (strcasecmp(_mapData[key].c_str(), "off") == 0)
00157 || (strcasecmp(_mapData[key].c_str(), "0") == 0)) {
00158 rbool = false;
00159 }
00160 else {
00161 rbool = true;
00162 }
00163
00164 return OC_ERR_FREE;
00165 }
00166
00167
00168
00169 const OPENCITY_ERR_CODE
00170 Conf::GetLint(
00171 const string & key,
00172 OC_LINT & rlint,
00173 const OC_LINT def )
00174 {
00175
00176
00177
00178
00179
00180
00181
00182
00183 if (_mapData.find( key ) == _mapData.end()) {
00184
00185 rlint = def;
00186 return OC_ERR_FREE;
00187 }
00188
00189 rlint = strtol(_mapData[key].c_str(), NULL, 0);
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 return OC_ERR_FREE;
00206 }
00207
00208
00209
00210 const OPENCITY_ERR_CODE
00211 Conf::GetFloat(
00212 const string& key,
00213 float& rfloat,
00214 const float def )
00215 {
00216
00217 if (_mapData.find( key ) == _mapData.end()) {
00218 rfloat = def;
00219 return OC_ERR_FREE;
00220 }
00221
00222 rfloat = strtof(_mapData[key].c_str(), NULL);
00223
00224 return OC_ERR_FREE;
00225 }
00226
00227
00228
00229
00230
00231 OC_CHAR* const
00232 Conf::RTrim( OC_CHAR* const str )
00233 {
00234 OC_CHAR* strSpace = NULL;
00235
00236 if (str != NULL) {
00237 strSpace = str + strlen( str ) - 1;
00238 while ((strSpace >= str) && (isspace(*strSpace) != 0))
00239 *strSpace-- = '\0';
00240 }
00241
00242 return str;
00243 }
00244
00245
00246
00247 OC_CHAR* const
00248 Conf::LTrim( OC_CHAR* const str )
00249 {
00250 OC_CHAR* strSpace = NULL;
00251 OC_CHAR* strEnd = NULL;
00252
00253 if (str != NULL) {
00254 strSpace = str;
00255 strEnd = str;
00256 strEnd = strEnd + strlen( str );
00257 while ((strSpace < strEnd) && (isspace(*strSpace) != 0))
00258 *strSpace++ = '\0';
00259 }
00260
00261 return strSpace;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275