Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Config Class Reference

The configuration Class Class for managing simple configuration files. More...

#include <config.h>

List of all members.

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...


Detailed Description

The configuration Class Class for managing simple configuration files.

Author(s):
StonedBones
Date:
160200

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 }

Constructor & Destructor Documentation

Config::Config ()

The Constructor.

Definition at line 60 of file config.cpp.

00060 {
00061     confFile = "";
00062     actTag = "";
00063 }

Config::~Config ()

The Deconstructor.

Definition at line 75 of file config.cpp.

00075 {
00076 }

Member Function Documentation

bool Config::createTag ( char * tag)

Creates a specific tag if it doesn't exist yet.

Parameters:
tag   name of the tag
Returns:
true on success, false on failure

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 }

char * Config::getCharVariable ( char * var)

Returns string of a variable.

Parameters:
var   name of variable
Returns:
0 on error, pointer to string on success

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 }

int Config::getIntVariable ( char * var)

Returns integer value of a variable.

Parameters:
var   name of variable
Returns:
-111 on error (because -1 should be a legal number
Warning:
does not work when the value of the variable is -111
Bugs and limitations:
the problem with -111 can be considered a bug

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 }

bool Config::openConfig ( char * filename)

Open A Configuration File.

Parameters:
filename   name of the config file
Returns:
true on success, false on failure

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 }

void Config::readConfigFile () [private]

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 }

bool Config::searchTag ( char * tag)

Searches a specific tag ("[example_tag]").

Parameters:
tag   name of the tag
Returns:
true if found, false if not found

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 }

bool Config::setVariable ( char * var,
char * str)

Sets the string value of a variable and creates it if it doesn't extist.

Parameters:
var   the name of the variable
str   the string
Returns:
true on success, false on failure

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 }

bool Config::setVariable ( char * var,
int val)

Sets the integer value of a variable and creates it if it doesn't extist.

Parameters:
var   the name of the variable
val   the value of the variable
Returns:
true on success, false on failure

Definition at line 196 of file config.cpp.

00196 {
00197     return setVariable (var, intToChar (val));
00198 }

Member Data Documentation

string Config::actTag [private]

The tag used at the moment.

Definition at line 77 of file config.h.

string Config::confFile [private]

the configuration file name.

Definition at line 76 of file config.h.

map<string,map<string,string>> Config::confMap [private]

the internal configuration "database".

Definition at line 80 of file config.h.


The documentation for this class was generated from the following files:
Generated at Sat May 13 13:50:24 2000 for 3Dsia by doxygen 1.1.2 written by Dimitri van Heesch, © 1997-2000