include/configfile.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *            configfile.h
00003  *
00004  *  Sun Oct  2 18:34:24 2005
00005  *  Copyright  2005  Joe Venzon
00006  *  joe@venzon.net
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024  
00025 #ifndef _CONFIGFILE_H
00026 #define _CONFIGFILE_H
00027 
00028 #include <stdio.h>                      // Header File For Standard Input/Output
00029 #include <string>
00030 #include <iostream>
00031 #include <fstream>
00032 #include <cmath>
00033 #include <cassert>
00034 
00035 using namespace std;
00036 
00037 //see the user's guide at the bottom of the file
00038 
00039 class CONFIGVARIABLE
00040 {
00041 public:
00042         CONFIGVARIABLE();       
00043 
00044         string section;
00045         string name;
00046 
00047         string GetFullName();
00048 
00049         string val_s;
00050         int val_i;
00051         float val_f;
00052         float val_v[3];
00053         bool val_b;
00054 
00055         CONFIGVARIABLE * next;
00056 
00057         void Set(string newval);
00058 
00059         void DebugPrint();
00060 
00061         string strLTrim(string instr);
00062         string strRTrim(string instr);
00063         string strTrim(string instr);
00064         string strLCase(string instr);
00065 
00066         bool written;
00067 };
00068 
00069 class CONFIGFILE
00070 {
00071 private:
00072         string filename;
00073         CONFIGVARIABLE * vars;
00074         void Add(CONFIGVARIABLE * newvar);
00075         string Trim(string instr);
00076         void ProcessLine(string & cursection, string linestr);
00077         string Strip(string instr, char stripchar);
00078         string LCase(string instr);
00079         bool SUPPRESS_ERROR;
00080 
00081 public:
00082         CONFIGFILE();
00083         CONFIGFILE(string fname);
00084         ~CONFIGFILE();
00085 
00086         void Load(string fname);
00087         void Clear();
00088 
00089         //returns true if the param was found
00090         bool ClearParam(string param);
00091 
00092         //returns true if the param was found
00093         bool GetParam(string param, string & outvar);
00094         bool GetParam(string param, int & outvar);
00095         bool GetParam(string param, float & outvar);
00096         bool GetParam(string param, float * outvar); //for float[3]
00097         bool GetParam(string param, bool & outvar);
00098 
00099         //always returns true at the moment
00100         bool SetParam(string param, string invar);
00101         bool SetParam(string param, int invar);
00102         bool SetParam(string param, float invar);
00103         bool SetParam(string param, float * invar);
00104         bool SetParam(string param, bool invar);
00105 
00106         void DebugPrint();
00107 
00108         bool Write();
00109         bool Write(bool with_brackets);
00110         bool Write(bool with_brackets, string save_as);
00111         
00112         void SuppressError(bool newse) {SUPPRESS_ERROR = newse;}
00113         
00114         CONFIGVARIABLE * GetHead() {return vars;}
00115 };
00116 
00117 #endif /* _CONFIGFILE_H */
00118 
00119 /* USER GUIDE
00120 
00121 Paste the file included below somewhere, then run this code:
00122 
00123         CONFIGFILE testconfig("/home/joe/.vdrift/test.cfg");
00124         testconfig.DebugPrint();
00125         string tstr = "notfound";
00126         cout << "!!! test vectors: " << endl;
00127         testconfig.GetParam("variable outside of", tstr);
00128         cout << tstr << endl;
00129         tstr = "notfound";
00130         testconfig.GetParam(".variable outside of", tstr);
00131         cout << tstr << endl;
00132         float vec[3];
00133         testconfig.GetParam("what about.even vectors", vec);
00134         cout << vec[0] << "," << vec[1] << "," << vec[2] << endl;
00135 
00136 your output should be the debug print of all variables, then:
00137 
00138         !!! test vectors: 
00139         a section
00140         a section
00141         2.1,0.9,0
00142 
00143 
00144 */
00145 
00146 /* EXAMPLE (not a good example -- tons of errors -- to demonstrate robustness)
00147 
00148 #comment on the FIRST LINE??
00149 
00150                 variable outside of=a section  
00151 
00152 test section numero UNO
00153 look at me = 23.4
00154 
00155 i'm so great=   BANANA
00156 #break!
00157 [ section    dos??]
00158  why won't you = breeeak #trying to break it
00159 
00160 what about ] # this malformed thing???
00161 nope works = fine.
00162 even vectors = 2.1,0.9,GAMMA
00163 this is a duplicate = 0
00164 this is a duplicate = 1
00165 random = intermediary
00166 this is a duplicate = 2
00167 
00168 
00169 */

Generated on Thu Oct 19 04:05:47 2006 by  doxygen 1.4.6