| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef CONFIGURE_H |
| 21 |
#define CONFIGURE_H |
| 22 |
|
| 23 |
const unsigned MaxConfLineLen = 1024; |
| 24 |
|
| 25 |
#include <cstring> |
| 26 |
#include <cstdlib> |
| 27 |
#include <iosfwd> |
| 28 |
|
| 29 |
#include <string> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
|
| 33 |
class ConfElem |
| 34 |
{ |
| 35 |
public: |
| 36 |
|
| 37 |
const std::string Name; |
| 38 |
|
| 39 |
std::string Value; |
| 40 |
|
| 41 |
public: |
| 42 |
|
| 43 |
ConfElem() = default; |
| 44 |
|
| 45 |
ConfElem ( const std::string& n, const std::string& v ) : Name (n), Value (v) {} |
| 46 |
|
| 47 |
|
| 48 |
void Save ( std::ostream& o ) const; |
| 49 |
|
| 50 |
|
| 51 |
long GetLong ( void ) const { return std::stol(Value); } |
| 52 |
double GetDouble ( void ) const { return atof ( Value.c_str() ); } |
| 53 |
const char* GetString ( void ) const { return Value.c_str(); } |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
class ConfSection |
| 58 |
{ |
| 59 |
protected: |
| 60 |
const std::string Name; |
| 61 |
|
| 62 |
typedef std::vector <ConfElem*> ConfBase; |
| 63 |
ConfBase Base; |
| 64 |
|
| 65 |
public: |
| 66 |
explicit ConfSection ( const std::string& pc ) : Name ( pc ) {} |
| 67 |
~ConfSection(); |
| 68 |
|
| 69 |
bool operator == ( const std::string& pc ) const { return ( Name == pc ); } |
| 70 |
bool operator != ( const std::string& pc ) const { return ( Name != pc ); } |
| 71 |
|
| 72 |
void addEntry ( const std::string& Name, const std::string& Value ); |
| 73 |
|
| 74 |
|
| 75 |
ConfElem* FindByName ( const std::string& name ) const; |
| 76 |
|
| 77 |
void Save ( std::ostream& o ) const; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
class Configuration |
| 82 |
{ |
| 83 |
protected: |
| 84 |
std::string fileName; |
| 85 |
char Line [MaxConfLineLen+1]; |
| 86 |
bool isLoaded = false; |
| 87 |
bool isSaved = false; |
| 88 |
|
| 89 |
|
| 90 |
void loadString ( std::istream& ); |
| 91 |
bool isComment ( void ) const; |
| 92 |
bool isSection ( void ) const |
| 93 |
{ return ( Line [0] == '[' && Line [strlen(Line)-1] == ']' ); } |
| 94 |
void loadSection ( void ); |
| 95 |
|
| 96 |
int SplitLine ( char*& pName, char*& pValue ); |
| 97 |
|
| 98 |
protected: |
| 99 |
typedef std::vector <ConfSection*> ConfSectBase; |
| 100 |
ConfSectBase Base; |
| 101 |
ConfSection* Section = nullptr; |
| 102 |
ConfElem* Element = nullptr; |
| 103 |
|
| 104 |
|
| 105 |
ConfSection* FindSection ( const std::string& pc ) const; |
| 106 |
|
| 107 |
public: |
| 108 |
|
| 109 |
Configuration() = default; |
| 110 |
|
| 111 |
Configuration ( const Configuration& ) = delete; |
| 112 |
|
| 113 |
Configuration& operator = ( const Configuration& ) = delete; |
| 114 |
|
| 115 |
~Configuration(); |
| 116 |
|
| 117 |
|
| 118 |
bool Load ( const char* Filename ); |
| 119 |
|
| 120 |
bool Save ( const char* Filename ); |
| 121 |
|
| 122 |
bool Save ( void ) { return isLoaded ? Save(fileName.c_str()) : true; } |
| 123 |
|
| 124 |
|
| 125 |
bool Loaded ( void ) const { return isLoaded; } |
| 126 |
bool Saved ( void ) const { return isSaved; } |
| 127 |
|
| 128 |
void createSection ( const std::string& name ); |
| 129 |
|
| 130 |
bool checkValue ( const std::string& Section, const std::string& Element ); |
| 131 |
|
| 132 |
bool checkValue ( const std::string& Element ); |
| 133 |
|
| 134 |
bool setValue ( const std::string& Element, const std::string& Value ); |
| 135 |
|
| 136 |
|
| 137 |
std::string getValue ( void ) const { return Element->Value; } |
| 138 |
long getLong ( void ) const { return Element->GetLong(); } |
| 139 |
double getDouble ( void ) const { return Element->GetDouble(); } |
| 140 |
const char* getString ( void ) const { return Element->GetString(); } |
| 141 |
|
| 142 |
bool useSection ( const std::string& name ) |
| 143 |
{ Section = FindSection(name); return !Section; } |
| 144 |
}; |
| 145 |
|
| 146 |
#endif |