00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __TOOLBOX_CONFIG_H
00010 #define __TOOLBOX_CONFIG_H
00011
00012
00013 #define _CF_TOKENTYPE_OPENING 1
00014 #define _CF_TOKENTYPE_CLOSING 2
00015 #define _CF_TOKENTYPE_PLAIN 3
00016 #define _CF_TOKENTYPE_IGNORE 4
00017
00018
00023 #define CF_TNONE 0
00024
00029 #define CF_TBOOL 1
00030
00035 #define CF_TDOUBLE 2
00036
00041 #define CF_TLONG 3
00042
00047 #define CF_TSTRING 4
00048
00053 #define CF_SWBUFFERINCREASE 512
00054
00055
00056 namespace toolbox
00057 {
00066 #if defined(_DEBUG) && (defined(_AFX) || defined(_AFXDLL))
00067 class Config : public CObject
00068 #else
00069 class Config
00070 #endif
00071 {
00072 friend ConfigSection;
00073 friend ConfigParameter;
00074
00075 public:
00076
00081 #if defined(_DEBUG) && (defined(_AFX) || defined(_AFXDLL))
00082 class Writer : public CObject
00083 #else
00084 class Writer
00085 #endif
00086 {
00087 public:
00088
00092 virtual ~Writer();
00093
00100 virtual bool Write(char character) = NULL;
00101
00108 virtual bool Write(const char * string) = NULL;
00109 };
00110
00114 class FileWriter : public Writer
00115 {
00116 private:
00117
00121 FILE * File;
00122
00123 public:
00124
00128 FileWriter();
00129
00134 virtual ~FileWriter();
00135
00142 bool Open(const char * fileName);
00143
00150 virtual bool Write(char character);
00151
00158 virtual bool Write(const char * string);
00159 };
00160
00164 class StringWriter : public Writer
00165 {
00166 private:
00167
00171 char * Buffer;
00172
00177 unsigned int BufferLen;
00178
00183 unsigned int BufferSize;
00184
00188 void IncreaseBuffer();
00189
00190 public:
00191
00195 StringWriter();
00196
00200 virtual ~StringWriter();
00201
00208 inline const char * GetString();
00209
00216 virtual bool Write(char character);
00217
00224 virtual bool Write(const char * string);
00225 };
00226
00227 private:
00228
00233 ConfigSection * Content;
00234
00235 #ifdef _TOOLBOX_TEST
00236
00239 static int InstanceCount;
00240 #endif
00241
00250 static bool WriteCorrectedForCfgFile(class Config::Writer * Writer, const char * str);
00251
00252 public:
00253
00257 Config();
00258
00262 ~Config();
00263
00268 void Clear();
00269
00278 bool ExportIni();
00279
00287 bool ExportIni(const char * fileName);
00288
00293 inline ConfigSection * GetContent();
00294
00305 bool ImportIni();
00306
00316 bool ImportIni(const char * fileName);
00317
00325 bool Parse(char * text);
00326
00337 bool Read();
00338
00348 bool Read(const char * fileName);
00349
00350 #ifdef _TOOLBOX_TEST
00351
00357 static void RunTestSuite(int * performedTests, int * failedTests);
00358 #endif
00359
00371 bool Write(bool withBacking = false, bool indent = true);
00372
00383 bool Write(const char * fileName, bool withBacking = false, bool indent = true);
00384
00392 bool Write(class Config::Writer * Writer, bool indent = true);
00393 };
00394
00395
00401 #if defined(_DEBUG) && (defined(_AFX) || defined(_AFXDLL))
00402 class ConfigSection : public CObject
00403 #else
00404 class ConfigSection
00405 #endif
00406 {
00407 friend Config;
00408 friend HashMap<ConfigSection>;
00409
00410 private:
00411
00412 #ifdef _TOOLBOX_TEST
00413
00416 static int InstanceCount;
00417 #endif
00418
00422 char * Name;
00423
00427 StringKeyHashMap<ConfigParameter> * Parameters;
00428
00432 StringKeyHashMap<ConfigSection> * Sections;
00433
00438 ConfigSection(const char * name);
00439
00443 ~ConfigSection();
00444
00452 static bool WriteIndentation(class Config::Writer * Writer, int depth);
00453
00462 bool WriteTo(class Config::Writer * Writer, int depth, bool indent);
00463
00464 public:
00465
00471 bool ContainsParameter(const char * name);
00472
00478 bool ContainsSection(const char * name);
00479
00483 void DeleteAllParameters();
00484
00488 void DeleteAllSections();
00489
00494 void DeleteParameter(const char * name);
00495
00500 void DeleteSection(const char * name);
00501
00510 inline bool GetBoolValue(const char * name);
00511
00522 bool GetBoolValue(const char * name, bool defaultBoolValue);
00523
00532 inline double GetDoubleValue(const char * name);
00533
00544 double GetDoubleValue(const char * name, double defaultDoubleValue);
00545
00556 inline int GetIntValue(const char * name);
00557
00570 int GetIntValue(const char * name, int defaultIntValue);
00571
00580 inline long GetLongValue(const char * name);
00581
00592 long GetLongValue(const char * name, long defaultLongValue);
00593
00598 inline const char * GetName();
00599
00606 ConfigParameter * GetParameter(const char * name);
00607
00615 inline PointeredList * GetParameterNames();
00616
00624 ConfigSection * GetSection(const char * name);
00625
00633 inline PointeredList * GetSectionNames();
00634
00642 inline PointeredList * GetSections();
00643
00657 inline const char * GetStringValue(const char * name);
00658
00675 const char * GetStringValue(const char * name, const char * defaultStringValue);
00676
00682 inline int GetType(const char * name);
00683
00689 bool HasParameter(const char * name);
00690
00696 bool HasSection(const char * name);
00697
00698 #ifdef _TOOLBOX_TEST
00699
00705 static void RunTestSuite(int * performedTests, int * failedTests);
00706 #endif
00707
00713 inline void SetBoolValue(const char * name, bool boolValue);
00714
00720 inline void SetDoubleValue(const char * name, double doubleValue);
00721
00729 inline void SetIntValue(const char * name, int intValue);
00730
00736 inline void SetLongValue(const char * name, long longValue);
00737
00745 inline void SetStringValue(const char * name, const char * stringValue);
00746 };
00747
00748
00755 #if defined(_DEBUG) && (defined(_AFX) || defined(_AFXDLL))
00756 class ConfigParameter : public CObject
00757 #else
00758 class ConfigParameter
00759 #endif
00760 {
00761 friend ConfigSection;
00762 friend HashMap<ConfigParameter>;
00763
00764 private:
00765
00766 union
00767 {
00772 bool BoolValue;
00773
00778 double DoubleValue;
00779
00784 long LongValue;
00785
00790 char * StringValue;
00791 };
00792
00793 #ifdef _TOOLBOX_TEST
00794
00797 static int InstanceCount;
00798 #endif
00799
00803 int Type;
00804
00808 ConfigParameter();
00809
00814 ConfigParameter(bool boolValue);
00815
00820 ConfigParameter(double doubleValue);
00821
00826 ConfigParameter(long longValue);
00827
00834 ConfigParameter(const char * stringValue);
00835
00839 ~ConfigParameter();
00840
00848 bool WriteCorrectedForCfgFile(class Config::Writer * Writer);
00849
00850 public:
00851
00859 bool GetBoolValue();
00860
00868 double GetDoubleValue();
00869
00877 long GetLongValue();
00878
00891 char * GetStringValue();
00892
00897 inline int GetType();
00898
00899 #ifdef _TOOLBOX_TEST
00900
00906 static void RunTestSuite(int * performedTests, int * failedTests);
00907 #endif
00908
00914 void SetBoolValue(bool boolValue);
00915
00921 void SetDoubleValue(double doubleValue);
00922
00928 void SetLongValue(long longValue);
00929
00937 void SetStringValue(const char * stringValue);
00938 };
00939 }
00940
00941
00942 #endif