#include <config.h>
Public Methods | |
| Config () | |
| The Constructor. More... | |
| ~Config () | |
| The Deconstructor. More... | |
| bool | openConfig (char* filename) |
| Open A Configuration File. More... | |
| bool | searchTag (char* tag) |
| Searches a specific tag ("[example_tag]"). More... | |
| bool | createTag (char* tag) |
| Creates a specific tag if it doesn't exist yet. More... | |
| int | getIntVariable (char* var) |
| Returns integer value of a variable. More... | |
| char* | getCharVariable (char* var) |
| Returns string of a variable. More... | |
| bool | setVariable (char* var, int val) |
| Sets the integer value of a variable and creates it if it doesn't extist. More... | |
| bool | setVariable (char* var, char* str) |
| Sets the string value of a variable and creates it if it doesn't extist. More... | |
Private Methods | |
| void | readConfigFile () |
| reads the file and stores it in confMap. More... | |
Private Attributes | |
| string | confFile |
| the configuration file name. More... | |
| string | actTag |
| The tag used at the moment. More... | |
| map<string, map <string, string> > | confMap |
| the internal configuration "database". More... | |
Definition at line 16 of file config.h.
00016 {
00017 public:
00019 Config ();
00021 ~Config ();
00022
00024
00028 bool openConfig (char* filename);
00029
00031
00035 bool searchTag (char* tag);
00037
00041 bool createTag (char* tag);
00042
00044
00050 int getIntVariable (char* var);
00052
00056 char* getCharVariable (char* var);
00057
00059
00064 bool setVariable (char* var, int val);
00066
00071 bool setVariable (char* var, char* str);
00072
00073
00074
00075 private:
00076 string confFile;
00077 string actTag;
00078
00080 map <string, map <string, string> > confMap;
00081
00082 void readConfigFile ();
00083 }
|
The Constructor.
Definition at line 60 of file config.cpp.
00060 {
00061 confFile = "";
00062 actTag = "";
00063 }
|
|
Creates a specific tag if it doesn't exist yet.
| tag | name of the tag |
Definition at line 137 of file config.cpp.
00137 {
00138 ofstream out (confFile.c_str (), ofstream::app);
00139
00140 if (!out) return false;
00141
00142 actTag = tag;
00143 out << "[" << tag << "]\n";
00144
00145 out.close ();
00146
00147 return true;
00148 }
|
Returns string of a variable.
| var | name of variable |
Definition at line 178 of file config.cpp.
00178 {
00179 if (confFile.length () > 0) {
00180 return const_cast<char*> (confMap[actTag][var].c_str ());
00181 };
00182
00183 return NULL;
00184 }
|
Returns integer value of a variable.
| var | name of variable |
Definition at line 160 of file config.cpp.
00160 {
00161 if (confFile.length () > 0) {
00162 return charToInt (const_cast<char*> (confMap[actTag][var].c_str ()));
00163 };
00164
00165 return -111;
00166 }
|
Open A Configuration File.
| filename | name of the config file |
Definition at line 88 of file config.cpp.
00088 {
00089 confFile = filename;
00090
00091 ifstream in(confFile.c_str ());
00092 if (!in) {
00093 ofstream out (confFile.c_str ());
00094 if (!out) return false;
00095 out.close ();
00096
00097 in.open (confFile.c_str ());
00098 if (!in) return false;
00099 };
00100
00101 readConfigFile ();
00102
00103 return true;
00104 }
|
reads the file and stores it in confMap.
Definition at line 20 of file config.cpp.
00020 {
00021 ifstream in(confFile.c_str ());
00022 string str, str2;
00023
00024 try {
00025 if (!in) return;
00026
00027 while (!in.eof ()) {
00028 in >> str;
00029
00030 if (str.find ("[") == 0) {
00031 str2 = str.substr (1, str.length () - 2);
00032 cerr << "Found tag: " << str2 << "\n";
00033 } else if (str2.length () > 0) {
00034 string tmp;
00035 in >> tmp;
00036
00037 if (tmp == "=") {
00038 in >> confMap[str2][str];
00039 };
00040
00041 cerr << str << " = " << confMap[str2][str] << "\n";
00042 };
00043 };
00044 } catch (...) {
00045 cerr << "Exception in Config::readConfigFile !\n";
00046 };
00047
00048 }
|
Searches a specific tag ("[example_tag]").
| tag | name of the tag |
Definition at line 118 of file config.cpp.
00118 {
00119 if (confMap.find (tag) != confMap.end ()) {
00120 actTag = tag;
00121 return true;
00122 } else {
00123 return false;
00124 };
00125 }
|
Sets the string value of a variable and creates it if it doesn't extist.
| var | the name of the variable |
| str | the string |
Definition at line 210 of file config.cpp.
00210 {
00211 if (confFile.length () < 1) return false;
00212
00213 try {
00214 ifstream in(confFile.c_str ());
00215 string st, strTag, buffer1, buffer2;
00216 char ch;
00217
00218 strTag = "[";
00219 strTag += actTag;
00220 strTag += "]";
00221
00222
00223 buffer1 = buffer2 = "";
00224
00225 while ((buffer1.find (strTag.c_str ()) != 0) && (!in.eof ())) {
00226 in.get (ch);
00227 if (!in.eof ()) buffer1 += ch;
00228 };
00229
00230 while (!in.eof ()) {
00231 in.get (ch);
00232 if (!in.eof ()) buffer2 += ch;
00233 };
00234
00235 in.close ();
00236
00237 ofstream out (confFile.c_str ());
00238
00239 out << buffer1;
00240 out << "\n";
00241 out << var;
00242 out << " = ";
00243 out << str;
00244 out << buffer2;
00245
00246 out.close ();
00247
00248 readConfigFile ();
00249
00250 return true;
00251 } catch (...) {
00252 cerr << "Exception in Config::setVariable (char*, char*)\n";
00253 return false;
00254 };
00255
00256 return false;
00257 }
|
Sets the integer value of a variable and creates it if it doesn't extist.
| var | the name of the variable |
| val | the value of the variable |
Definition at line 196 of file config.cpp.
00196 {
00197 return setVariable (var, intToChar (val));
00198 }
|
|
|
1.1.2 written by Dimitri van Heesch,
© 1997-2000