Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

conf.cpp

00001 /***************************************************************************
00002                           conf.cpp    -  description
00003         $Id: conf.cpp 84 2006-11-06 18:58:40Z neoneurone $
00004                              -------------------
00005     begin                : dim 1 août 2004
00006     copyright            : (C) 2004 by Duong-Khang NGUYEN
00007     email                : neoneurone @ users sourceforge net
00008  ***************************************************************************/
00009 
00010 /***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   any later version.                                                    *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "conf.h"
00020 
00021 #include <fstream>      // for file IO
00022 #include <cerrno>
00023 #include <cctype>       // for isspace()
00024 #include <cstring>      // for C string functions
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 // is the file opened ?
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 // read the first line
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 // now process the file
00056     while ( inFile.good() ) {
00057         if ( (strlen(strTemp) != 0)
00058           && (strTemp[0] != '#') ) {
00059             strNew = strTemp;
00060         // get the first token, it's the name of the parameter
00061             strFirst = strtok( strNew, "=" );
00062         // get the second token, it's the value of the parameter
00063             strSecond = strtok( NULL, "=" );
00064         // Trim out spaces from the datas if applicable
00065             (strFirst != NULL) ? strFirst = Conf::RTrim( strFirst ) : strFirst = strEmpty;
00066             (strSecond != NULL) ? strSecond = Conf::LTrim( strSecond ) : strSecond = strEmpty;
00067 
00068         // Add the pair to the _mapData if the key is not empty
00069             if (strlen(strFirst) > 0)
00070                 _mapData[ strFirst ] = strSecond;
00071 
00072 //debug cout << "StrFirst/StrSecond: " << strFirst << "/" << strSecond << endl;
00073 //debug
00074 /*
00075 __gnu_cxx::hash<const char *> H;
00076 cout << "'" << (string) strFirst << "' = '" << (string) strSecond << "'" << endl;
00077 cout << "hash : " << H(strFirst) << " / " << H(strSecond) << endl;
00078 cout << (long) strFirst << "=" << (long) strSecond << endl;
00079 cout << "new" << (long) strNew << endl;
00080             delete strNew;
00081 */
00082         }
00083     // read the next line
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 //debug
00105 /*
00106 __gnu_cxx::hash_map<string, string, myHash>::iterator iter;
00107 for ( iter = _mapData.begin(); iter != _mapData.end(); iter++ ) {
00108     cout << "first '" << iter->first << "', second '" << iter->second << "'" << endl;
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 //debug
00122 /*cout << "key is : '" << key << "', data is : '" << _mapData[ key ] << "'" << endl;
00123     __gnu_cxx::hash_map<string, string, myHash>::iterator
00124         iter = _mapData.find( key );
00125     return iter != _mapData.end() ? iter->second : "";
00126 */
00127 
00128 // IF the key is not in the hash_map THEN return the default value
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 // IF the key is not in the hash_map THEN return the default value
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 /* debug
00176 for (__gnu_cxx::hash_map<string, string, myHash>::iterator i = _mapData.begin();
00177 i != _mapData.end(); i++) {
00178     cout << "Map key: " << i->first << "/ value: " << i->second << endl;
00179 }
00180 */
00181 
00182 // IF the key is not in the hash_map THEN return the default value
00183     if (_mapData.find( key ) == _mapData.end()) {
00184 //debug cout << "key: " << key << "/ default: " << def << endl;
00185         rlint = def;
00186         return OC_ERR_FREE;
00187     }
00188 
00189     rlint = strtol(_mapData[key].c_str(), NULL, 0);
00190 
00191 //debug cout << __PRETTY_FUNCTION__ << "read: " << (long int)rlint << endl;
00192 
00193 /* FIXME: better check
00194     int myerr = errno;
00195     if (myerr != 0) {
00196 //debug cout << "Errno: " << myerr << "/ Str: " << strerror(myerr) << endl;
00197         assert(0);
00198         return OC_ERR_INVALID;
00199     }
00200     else {
00201         return OC_ERR_FREE;
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 // IF the key is not in the hash_map THEN return the default value
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    /*                             STATIC     METHOD                       */
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 

Generated on Sat Nov 11 10:21:09 2006 for OpenCity by  doxygen 1.4.2