DSL
config.h
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #ifndef __UNIVERSAL_CONFIG_H__
12 #define __UNIVERSAL_CONFIG_H__
13 
14 enum DS_VALUE_TYPE {
15  DS_TYPE_UNKNOWN,
16  DS_TYPE_BOOL,
17  DS_TYPE_INT,
18  DS_TYPE_FLOAT,
19  DS_TYPE_STRING,
20  DS_TYPE_BINARY,
21  DS_TYPE_LONG = DS_TYPE_INT, // for legacy code
22 };
23 
24 #pragma pack(1)
25 struct DS_VALUE {
26  DS_VALUE_TYPE type;
27  union {
28  char * pString;
29  void * pBinary;
30  union {
31  uint32 uLong;
32  int32 lLong;
33  double lFloat;
34  };
35  };
36 };
37 
39  char name[64];
40  DS_VALUE value;
41  DS_CONFIG_VALUE *Prev,*Next;
42 };
43 
45  char name[64];
46  DS_CONFIG_VALUE * values;
47  DS_CONFIG_SECTION * sections; // sub-sections
48  DS_CONFIG_SECTION *Prev,*Next,*Parent;
49 };
50 #pragma pack()
51 
52 typedef std::vector<DS_CONFIG_SECTION *> scanStackType;
53 
54 class DSL_API_CLASS Universal_Config {
55  private:
56  DS_CONFIG_SECTION *fSection, *lSection;
57 
58  scanStackType scanStack;
59  //DS_CONFIG_SECTION *LastScan[20];
60 
61  void FreeSection(DS_CONFIG_SECTION * Scan);
62  void WriteSection(FILE * fp, DS_CONFIG_SECTION * sec, int level);
63  void WriteBinarySection(FILE * fp, DS_CONFIG_SECTION * sec);
64  void PrintSection(DS_CONFIG_SECTION * Scan, int level);
65  bool IsLong(const char * text);
66  bool IsFloat(const char * text);
67  DS_CONFIG_SECTION * GetSectionFromString(const char * sec, bool create=false);
68 
69  DS_CONFIG_SECTION * PopScan();
70  void PushScan(DS_CONFIG_SECTION * section);
71 
72  public:
75 
76  bool LoadConfig(const char * filename, DS_CONFIG_SECTION * Scan=NULL);
77  bool LoadConfig(FILE * fp, const char * filename, DS_CONFIG_SECTION * Scan=NULL);
78  bool LoadBinaryConfig(const char * filename);
79  bool LoadBinaryConfig(FILE * fp);
80 
81  bool WriteConfig(const char * filename, DS_CONFIG_SECTION * Start=NULL, bool Single=false);
82  bool WriteConfig(FILE * fp, DS_CONFIG_SECTION * Start=NULL, bool Single=false);
83  bool WriteBinaryConfig(const char * filename, DS_CONFIG_SECTION * Start=NULL, bool Single=false);
84  bool WriteBinaryConfig(FILE * fp, DS_CONFIG_SECTION * Start=NULL, bool Single=false);
85 
86  void FreeConfig();
87 
88  DS_CONFIG_SECTION * GetSection(DS_CONFIG_SECTION * parent, const char * name);
89  DS_VALUE * GetSectionValue(DS_CONFIG_SECTION * sec, const char * name);
90 
91  DS_VALUE * GetValue(const char * sec, const char * name);
92  const char * GetValueString(const char * sec, const char * name);
93  int32 GetValueLong(const char * sec, const char * name);
94  double GetValueFloat(const char * sec, const char * name);
95  void SetValue(const char * sec, const char * name, DS_VALUE * val);
96  void SetValueString(const char * sec, const char * name, const char * val);
97  void SetValueLong(const char * sec, const char * name, int32 val);
98  void SetValueFloat(const char * sec, const char * name, double val);
99 
100  DS_VALUE * SetSectionValue(DS_CONFIG_SECTION * sec, const char * name, DS_VALUE * val);
101 
102  // advanced commands
103  DS_CONFIG_SECTION *FindOrAddSection(DS_CONFIG_SECTION * parent, const char * name);
104 
105  void ClearSection(DS_CONFIG_SECTION * Scan);
106  void PrintConfigTree();
107 };
108 
109 #endif // __UNIVERSAL_CONFIG_H__