| 1 | /*************************************************************************** |
| 2 | conf.h - description |
| 3 | ------------------- |
| 4 | begin : august 1st, 2004 |
| 5 | copyright : (C) 2004-2010 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: conf.h 450 2010-11-21 19:11:43Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | #ifndef _OPENCITY_CONF_H_ |
| 21 | #define _OPENCITY_CONF_H_ 1 |
| 22 | |
| 23 | #include "main.h" |
| 24 | |
| 25 | #include <map> |
| 26 | |
| 27 | using std::map; |
| 28 | |
| 29 | // Contain the maximum length of each line in the .conf files. |
| 30 | #define OC_MAX_CONF_LINE 1024 |
| 31 | |
| 32 | // Contain the key-value pair separator string. |
| 33 | #define OC_CONF_KEY_VALUE_SEPARATOR "=" |
| 34 | |
| 35 | // Contain the comment start character. |
| 36 | #define OC_CONF_COMMENT_START '#' |
| 37 | |
| 38 | //======================================================================== |
| 39 | /** Manage the ".conf" file. The parser automatically trim out the spaces |
| 40 | around the "=" sign. A ".conf" file is a text file with things |
| 41 | like following: |
| 42 | \li bla = 1 |
| 43 | \li foo = coco kiki |
| 44 | \li etc... |
| 45 | \n |
| 46 | \n |
| 47 | Code sample: |
| 48 | \code |
| 49 | Conf* pConf = new Conf(); |
| 50 | pConf->Open("opencity.conf"); |
| 51 | cout << "one is : '" << pConf->GetValue("one") << "'" << endl |
| 52 | << "two is : '" << pConf->GetValue("two") << "'" << endl |
| 53 | << "three is : '" << pConf->GetValue("three") << "'" << endl |
| 54 | << "moo is : '" << pConf->GetValue("moo") << "'" << endl |
| 55 | << "coco : '" << pConf->GetValue("coco") << "'" << endl; |
| 56 | pConf->Close(); |
| 57 | delete pConf; |
| 58 | \endcode |
| 59 | */ |
| 60 | class Conf |
| 61 | { |
| 62 | |
| 63 | public: |
| 64 | Conf() { OPENCITY_DEBUG("ctor"); } |
| 65 | |
| 66 | ~Conf() { OPENCITY_DEBUG("dtor"); } |
| 67 | |
| 68 | |
| 69 | //======================================================================== |
| 70 | /** Open the .conf file specified by its filename then load its content. |
| 71 | \param fileName The name of the .conf file that you want to open |
| 72 | \return the error code |
| 73 | \sa main.h |
| 74 | */ |
| 75 | const OPENCITY_ERR_CODE |
| 76 | Open( |
| 77 | const string& fileName ); |
| 78 | |
| 79 | //======================================================================== |
| 80 | /** Close the previously opened .conf file and clear its content from |
| 81 | memory. |
| 82 | */ |
| 83 | void |
| 84 | Close(); |
| 85 | |
| 86 | |
| 87 | //======================================================================== |
| 88 | /** Return the string which represents the value part of the (key-value) |
| 89 | pair. |
| 90 | \param key The key of the element that you what to retrieve the value |
| 91 | \param defaultValue The default value |
| 92 | \return the value pointed by the given "key" |
| 93 | */ |
| 94 | const string& |
| 95 | GetValue( |
| 96 | const string& key, |
| 97 | const string& defaultValue = ""); |
| 98 | |
| 99 | //======================================================================== |
| 100 | /** Empty value causes the method to return OC_ERR_INVALID and |
| 101 | the rbool variable contains an undefied value. |
| 102 | The following values are interpreted as false: 0, no, n, off. |
| 103 | Anything other than space is interpreted as true |
| 104 | \param key The key of the element that you what to retrieve the value |
| 105 | \param rbool The reference to the boolean variable that holds the value |
| 106 | \param defaultValue The default value |
| 107 | \return the value pointed by the given "key" |
| 108 | */ |
| 109 | const OPENCITY_ERR_CODE |
| 110 | GetBool( |
| 111 | const string& key, |
| 112 | bool& rbool, |
| 113 | const bool defaultValue = false ); |
| 114 | |
| 115 | |
| 116 | //======================================================================== |
| 117 | /** Convert the string specified by the key "key" to long int. This method |
| 118 | uses the function "strtol()" |
| 119 | */ |
| 120 | const OPENCITY_ERR_CODE |
| 121 | GetLint( |
| 122 | const string& key, |
| 123 | OC_LINT& rlint, |
| 124 | const OC_LINT defaultValue = 0 ); |
| 125 | |
| 126 | |
| 127 | //======================================================================== |
| 128 | /** Convert the string specified by the key "key" to float. This method |
| 129 | uses the function "strtol()" |
| 130 | */ |
| 131 | const OPENCITY_ERR_CODE |
| 132 | GetFloat( |
| 133 | const string& key, |
| 134 | float& rfloat, |
| 135 | const float defaultValue = 0 ); |
| 136 | |
| 137 | |
| 138 | //======================================================================== |
| 139 | /** Static right string trim function. |
| 140 | */ |
| 141 | static char* const |
| 142 | RTrim( char* const str ); |
| 143 | |
| 144 | /** Static left string trim function. |
| 145 | */ |
| 146 | static char* const |
| 147 | LTrim( char* const str ); |
| 148 | |
| 149 | |
| 150 | private: |
| 151 | |
| 152 | /* TOKILL, kept for future reference |
| 153 | //======================================================================== |
| 154 | This is our hash function. In fact, it's just a wrapper |
| 155 | for the standard hash<const char*> |
| 156 | |
| 157 | struct myHash : public std::unary_function< const string &, size_t > |
| 158 | { |
| 159 | public: |
| 160 | size_t operator()( const string & x ) const |
| 161 | { |
| 162 | return stdHash( x.c_str() ); |
| 163 | } |
| 164 | |
| 165 | private: |
| 166 | __gnu_cxx::hash<const char*> stdHash; |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | __gnu_cxx::hash_map<string, string, myHash> mapData; |
| 171 | */ |
| 172 | |
| 173 | map<string, string> _mapData; |
| 174 | }; // class |
| 175 | |
| 176 | #endif |