00001 /********************************************************************
00002 Description: class for managing simple config files
00003 part of the 3Dsia project
00004 created: StonedBones, 16.2.2000
00005
00006 History:
00007 date, name, changes, in funtion
00008 160200 StonedBones created source
00009 260200 StonedBones made minor bugfixes setVariable (char*, int)
00010 setVariable (char*, char*)
00011
00012 ********************************************************************/
00013
00014 #include "config.h"
00015
00016 #include "intToChar.h"
00017
00018 #include <fstream>
00019
00020 void Config::readConfigFile () {
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 };
00049
00050 /*...................................................................
00051 Description: Constructor
00052 Args: <none>
00053 Returns: <none>
00054 Created: StonedBones, 16.2.2000
00055 [ToDo:]
00056 [Comments:]
00057 Changes:
00058 -------------------------------------------------------------------*/
00059
00060 Config::Config () {
00061 confFile = "";
00062 actTag = "";
00063 };
00064
00065 /*...................................................................
00066 Description: Destructor
00067 Args: <none>
00068 Returns: <none>
00069 Created: StonedBones, 16.2.2000
00070 [ToDo:]
00071 [Comments:]
00072 Changes:
00073 -------------------------------------------------------------------*/
00074
00075 Config::~Config () {
00076 };
00077
00078 /*...................................................................
00079 Description: opens a config file, creates it if not existant
00080 Args: char* filename: filename of config file
00081 Returns: true on success
00082 Created: StonedBones, 16.2.2000
00083 [ToDo:]
00084 [Comments:]
00085 Changes:
00086 -------------------------------------------------------------------*/
00087
00088 bool Config::openConfig (char* filename) {
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 };
00105
00106 /*...................................................................
00107 Description: searches a tag ("[example_tag]")
00108 Args: char* tag: tag-name
00109 Returns: true if found
00110 Created: StonedBones, 16.2.2000
00111 [ToDo:]
00112 [Comments:]
00113 - positions the cursor right after the tag (use 'searchTag (actTag)' for
00114 resetting file cursor to actual tag)
00115 Changes:
00116 -------------------------------------------------------------------*/
00117
00118 bool Config::searchTag (char* tag) {
00119 if (confMap.find (tag) != confMap.end ()) {
00120 actTag = tag;
00121 return true;
00122 } else {
00123 return false;
00124 };
00125 };
00126
00127 /*...................................................................
00128 Description: creates a tag if it doesn't exist
00129 Args: char* tag: tag-name
00130 Returns: true on success
00131 Created: StonedBones, 16.2.2000
00132 [ToDo:]
00133 [Comments:]
00134 Changes:
00135 -------------------------------------------------------------------*/
00136
00137 bool Config::createTag (char* tag) {
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 };
00149
00150 /*...................................................................
00151 Description: returns integer value of variable
00152 Args: char* var: variable name
00153 Returns: -111 on error (because -1 should be a legal number)
00154 Created: StonedBones, 16.2.2000
00155 [ToDo:]
00156 [Comments:]
00157 Changes:
00158 -------------------------------------------------------------------*/
00159
00160 int Config::getIntVariable (char* var) {
00161 if (confFile.length () > 0) {
00162 return charToInt (const_cast<char*> (confMap[actTag][var].c_str ()));
00163 };
00164
00165 return -111;
00166 };
00167
00168 /*...................................................................
00169 Description: returns string of a variable
00170 Args: char* var: variable name
00171 Returns: NULL on error, pointer to string on success
00172 Created: StonedBones, 16.2.2000
00173 [ToDo:]
00174 [Comments:]
00175 Changes:
00176 -------------------------------------------------------------------*/
00177
00178 char* Config::getCharVariable (char* var) {
00179 if (confFile.length () > 0) {
00180 return const_cast<char*> (confMap[actTag][var].c_str ());
00181 };
00182
00183 return NULL;
00184 };
00185
00186 /*...................................................................
00187 Description: sets value of a variable and creates it if it doesn't exist
00188 Args: char* var: variable-name; int val: value
00189 Returns: true on success
00190 Created: StonedBones, 16.2.2000
00191 [ToDo:]
00192 [Comments:]
00193 Changes:
00194 -------------------------------------------------------------------*/
00195
00196 bool Config::setVariable (char* var, int val) {
00197 return setVariable (var, intToChar (val));
00198 };
00199
00200 /*...................................................................
00201 Description: sets string of a variable and creates it if it doesn't exist
00202 Args: char* var: variable name; char* str: string
00203 Returns: true on succes
00204 Created: StonedBones, 16.2.2000
00205 [ToDo:]
00206 [Comments:]
00207 Changes:
00208 -------------------------------------------------------------------*/
00209
00210 bool Config::setVariable (char* var, char* str) {
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 };