KSquare Utilities
Configuration.h
Go to the documentation of this file.
1 /* Configuration.h -- Generic Configuration file manager.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_CONFIGURATION_)
6 #define _CONFIGURATION_
7 
8 #include "KKQueue.h"
9 #include "KKStr.h"
10 #include "RunLog.h"
11 
12 
13 namespace KKB
14 {
15  /**
16  *@brief General purpose Configuration File manager class.
17  *@details This class will read and write configuration files. It understands the concept of Logical Sections and
18  * Variables. You will be able to pragmatically define Sections and Settings in these sections. Each Setting
19  * will have a related value stored with it.
20  *
21  *@code
22  *Example Configuration File
23  *===========================================================
24  * [Header]
25  * ClassifierType = SVM
26  * Parameters = -K RBF -S SVC -G 1.76 -C 12
27  * RootDir = ${LarcosHmeDir}\\Classifier\\TrainingLibraries\\
28  *
29  * [Class]
30  * Name = Shrimp_01
31  *
32  * [Class]
33  * Name = Shrimp_02
34  *===========================================================
35  *@endcode
36  *
37  * There are three sections defined in the example above; Header, Class, and Class. Note
38  * that Section names do not have to be unique. You can access sections by name or index.
39  * If the name is not unique the first instance will be returned. Index values start at
40  * the top of the file, that is the first section to appear in the file is section index
41  * 0. The Setting names are of the format "SettingName = Value". Again you will be able
42  * to access Setting by either name or index.
43  */
44 
46  {
47  public:
48  Configuration (const KKB::KKStr& _fileName,
49  RunLog& _log
50  );
51 
52  Configuration ();
53 
54  Configuration (const Configuration& c);
55 
56  virtual ~Configuration ();
57 
58  virtual
59  void Load (const KKB::KKStr& _fileName,
60  RunLog& _log
61  );
62 
63 
64  bool FormatGood () const {return formatGood;}
65 
66  void FormatGood (bool _formatGood) {formatGood = _formatGood;}
67 
68  const VectorKKStr& FormatErrors () const {return formatErrors;}
69  const VectorInt& FormatErrorsLineNums () const {return formatErrorsLineNums;}
70 
71 
73 
74  void FormatErrorsAdd (kkint32 lineNum,
75  const KKStr& error
76  );
77 
78  void FormatErrorsClear (); /**< @brief Call this to clear all format error messages. */
79 
80  void LoadFile (RunLog& log);
81 
82  virtual kkint32 MemoryConsumedEstimated () const;
83 
85 
86  kkint32 NumOfSettings (const KKB::KKStr& sectionName) const;
87 
88  kkint32 NumOfSettings (kkint32 sectionNum) const; /**< @brief Returns number of settings for the specified section, */
89 
90  void PrintFormatErrors (std::ostream& o);
91 
92  bool SectionDefined (const KKB::KKStr& sectionName) const; /**< @brief Returns true if the section is defined. */
93 
94 
95  // Access Methods.
96  const KKB::KKStr& FileName () const {return fileName;}
97 
98  KKStrConstPtr SectionName (kkint32 sectionNum) const; /**< @brief Returns the name of the section for specified index, if index not defined will return NULL. */
99 
100  kkint32 SectionNum (const KKB::KKStr& sectionName) const;
101 
102  kkint32 SectionLineNum (kkint32 sectionNum) const;
103 
104  KKStrConstPtr SettingName (const KKB::KKStr& sectionName,
105  kkint32 settingNum
106  ) const;
107 
108  KKStrConstPtr SettingName (kkint32 sectionNum,
109  kkint32 settingNum
110  ) const;
111 
112 
113  KKStrConstPtr SettingValue (const KKB::KKStr& sectionName,
114  const KKB::KKStr& settingName,
115  kkint32& lineNum
116  ) const;
117 
118 
119  KKStr SettingValueToStr (const KKB::KKStr& sectionName,
120  const KKB::KKStr& settingName,
121  kkint32& lineNum
122  ) const;
123 
124 
125  KKStrConstPtr SettingValue (kkint32 sectionNum,
126  const KKB::KKStr& settingName,
127  kkint32& lineNum
128  ) const;
129 
130 
131  KKStr SettingValueToStr (kkint32 sectionNum,
132  const KKB::KKStr& settingName,
133  kkint32& lineNum
134  ) const;
135 
136 
137  KKStrConstPtr SettingValue (kkint32 sectionNum,
138  kkint32 settingNum,
139  kkint32& lineNum
140  ) const;
141 
142 
143  void GetSetting (const char* sectiopnName,
144  kkint32 settingNum,
145  KKStrConstPtr& name,
146  KKStrConstPtr& value,
147  kkint32& lineNum
148  );
149 
150  private:
151  class Setting;
152  class SettingList;
153  class ConfSection;
154  class ConfSectionList;
155  typedef Setting* SettingPtr;
156  typedef ConfSection* ConfSectionPtr;
157  typedef ConfSectionList* ConfSectionListPtr;
158 
159  KKB::KKStr curSectionName;
160  KKB::KKStr fileName;
161  bool formatGood;
162  VectorKKStr formatErrors; /**< Configuration Format Errors will be recorder here. */
163  VectorInt formatErrorsLineNums;
164  ConfSectionListPtr sections;
165  }; /* Configuration */
166 
168 
169 #define _Configuration_Defined_
170 
171 }
172 #endif
kkint32 SectionNum(const KKB::KKStr &sectionName) const
kkint32 NumOfSettings(kkint32 sectionNum) const
Returns number of settings for the specified section,.
KKStrConstPtr SectionName(kkint32 sectionNum) const
Returns the name of the section for specified index, if index not defined will return NULL...
__int32 kkint32
Definition: KKBaseTypes.h:88
bool SectionDefined(const KKB::KKStr &sectionName) const
Returns true if the section is defined.
VectorKKStr FormatErrorsWithLineNumbers() const
virtual void Load(const KKB::KKStr &_fileName, RunLog &_log)
std::vector< int > VectorInt
Definition: KKBaseTypes.h:138
KKStrConstPtr SettingValue(kkint32 sectionNum, const KKB::KKStr &settingName, kkint32 &lineNum) const
virtual kkint32 MemoryConsumedEstimated() const
KKStrConstPtr SettingName(kkint32 sectionNum, kkint32 settingNum) const
KKStrConstPtr SettingValue(kkint32 sectionNum, kkint32 settingNum, kkint32 &lineNum) const
KKTHread * KKTHreadPtr
const VectorKKStr & FormatErrors() const
Definition: Configuration.h:68
kkint32 SectionLineNum(kkint32 sectionNum) const
KKStr SettingValueToStr(kkint32 sectionNum, const KKB::KKStr &settingName, kkint32 &lineNum) const
KKStrConstPtr SettingValue(const KKB::KKStr &sectionName, const KKB::KKStr &settingName, kkint32 &lineNum) const
kkint32 NumOfSettings(const KKB::KKStr &sectionName) const
void PrintFormatErrors(std::ostream &o)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
Configuration * ConfigurationPtr
KKStr SettingValueToStr(const KKB::KKStr &sectionName, const KKB::KKStr &settingName, kkint32 &lineNum) const
General purpose Configuration File manager class.
Definition: Configuration.h:45
const KKB::KKStr & FileName() const
Definition: Configuration.h:96
void GetSetting(const char *sectiopnName, kkint32 settingNum, KKStrConstPtr &name, KKStrConstPtr &value, kkint32 &lineNum)
bool FormatGood() const
Definition: Configuration.h:64
Configuration(const Configuration &c)
const VectorInt & FormatErrorsLineNums() const
Definition: Configuration.h:69
Used for logging messages.
Definition: RunLog.h:49
void LoadFile(RunLog &log)
void FormatErrorsAdd(kkint32 lineNum, const KKStr &error)
KKStrConstPtr SettingName(const KKB::KKStr &sectionName, kkint32 settingNum) const
void FormatGood(bool _formatGood)
Definition: Configuration.h:66
Configuration(const KKB::KKStr &_fileName, RunLog &_log)
void FormatErrorsClear()
Call this to clear all format error messages.