-
Notifications
You must be signed in to change notification settings - Fork 9
/
cxprofile.h
445 lines (299 loc) · 13.5 KB
/
cxprofile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifndef CXPROFILE_H
#define CXPROFILE_H
#include "cxglobal.h"
#include "cxcontext.h"
#include "cxstring.h"
#include "cxfile.h"
#include "cxcontainer.h"
// skv == section key value
// loo == line object overload / override
class CxProfileLoo
{
public:
};
// skv == section key value
//attention : do not support multi thread share one CxSkver
class CxSkverBase;
// CxSkvBase : to set value to cache
class GM_CCXX_CORE_API CxSkv : public CxIGetSkv, public CxISetSkv
{
public:
CxSkv();
CxSkv(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues);
virtual ~CxSkv();
///*** *** *** get value by key *** *** ***
void prepareGet(const std::string & sSection);
int getValue(const std::string& sEntry, int iDefaultValue) const ;
bool getValue(const std::string& sEntry, bool bDefaultValue) const ;
double getValue(const std::string& sEntry, double dDefaultValue) const ;
///attention error with : msepoch_t l = getValue(sSection, l);
msepoch_t getValue(const std::string& sEntry, const msepoch_t& dtDefaultValue) const ;
///attention error with : std::string s = getValue(sEntry, s);
std::string getValue(const std::string& sEntry, const std::string& sDefaultValue) const ;
///--- --- --- end --- --- ---
///*** *** *** get value by section and key *** *** ***
int getValue(const std::string& sSection, const std::string& sEntry, int iDefaultValue) const ;
bool getValue(const std::string& sSection, const std::string& sEntry, bool bDefaultValue) const ;
double getValue(const std::string& sSection, const std::string& sEntry, double dDefaultValue) const ;
///attention error with : std::string s = getValue(sSection, sEntry, s);
std::string getValue(const std::string& sSection, const std::string& sEntry, const std::string& sDefaultValue) const ;
///attention error with : msepoch_t l = getValue(sSection, sEntry, l);
msepoch_t getValue(const std::string& sSection, const std::string& sEntry, const msepoch_t& dtDefaultValue) const ;
///--- --- --- end --- --- ---
///*** *** *** set value by key *** *** ***
void prepareSet(const std::string & sSection);
void setValue(const std::string& sEntry, int iValue);
void setValue(const std::string& sEntry, bool bValue);
void setValue(const std::string& sEntry, double dValue);
void setValue(const std::string& sEntry, msepoch_t dtValue);
void setValue(const std::string& sEntry, std::string sValue);
///--- --- --- end --- --- ---
///*** *** *** set value by setion and key *** *** ***
void setValue(const std::string& sSection, const std::string& sEntry, int iValue);
void setValue(const std::string& sSection, const std::string& sEntry, bool bValue);
void setValue(const std::string& sSection, const std::string& sEntry, double dValue);
void setValue(const std::string& sSection, const std::string& sEntry, msepoch_t dtValue);
void setValue(const std::string& sSection, const std::string& sEntry, std::string sValue);
///--- --- --- end --- --- ---
inline size_t sectionCount() const { return _sectionEntryValues.size(); }
inline bool empty() const { return _sectionEntryValues.size() == 0; }
bool hasSection(const std::string& sSection) const;
bool hasEntry(const std::string& sSection, const std::string& sEntry) const ;
bool hasEntry(const std::string& sEntry) const ;
void removeSection(const std::string& sSection);
void removeEntry(const std::string& sSection, const std::string& sEntry);
std::vector<std::string> sectionNames(int iSort = 0) const;
std::vector<std::string> entryNames(const std::string& sSection) const;
const std::map<std::string, std::string> & getEntryValues(const std::string& sSection, bool * ok = NULL) const;
inline const std::map<std::string, std::map<std::string, std::string> > & getSectionEntryValues() const { return _sectionEntryValues; }
void appendEntryValues(const std::string& sSection, const std::map<std::string, std::string>& entryValues);
void updateSectionEntryValues(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues);
int upateEntryValues(const std::string & sSection, const std::map<std::string, std::string> & entryValues);
protected:
std::map<std::string, std::string> * findEntryValues(const std::string& sSection) ;
std::map<std::string, std::map<std::string, std::string> > _sectionEntryValues;
std::map<std::string, std::string> * _defaultEntryValues;
};
class CxSkverFactoryBase : public CxFactoryTemplate<CxSkverBase>
{
};
class GM_CCXX_CORE_API CxSkverBase : public CxSkv
{
public:
static std::vector<CxFactoryTemplate<CxSkverBase>*>* factoriesContainer();
static void factoriesCreateAndRegister();
public:
CxSkverBase();
CxSkverBase(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues);
~CxSkverBase();
bool load(const std::string & sFilePath);
inline bool hadLoaded() const { return _filePath.empty(); }
bool save(const std::string & sFilePath = std::string());
inline const std::string & skvType() const { return _skvType; }
inline void setSkvType(const std::string & value) { _skvType = value; }
inline const std::string & skvPath() const { return _skvPath; }
inline void setSkvPath(const std::string & value) { _skvPath = value; }
inline const std::string & skvName() const { return _skvName; }
inline void setSkvName(const std::string & value) { _skvName = value; }
inline const std::string & skvParam() const { return _skvParam; }
inline void setSkvParam(const std::string & value) { _skvParam = value; }
inline const std::string & filePath() const { return _filePath; }
inline const std::string & fileName() const { return _fileName; }
inline const std::string & path() const { return _path; }
inline const std::string & fileSuffixName() const { return _fileSuffixName; }
inline std::string defaultSaveFilePath() { return _defaultSaveFilePath; }
inline void setDefaultSaveFilePath(const std::string & value) { _defaultSaveFilePath = value; }
protected:
virtual bool loadImpl(std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath) = 0;
virtual bool saveImpl(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath, bool bRenew = true) const = 0;
private:
inline int useCount() const { return m_useCount; }
inline int useSaveCount() const { return m_useSaveCount; }
inline void useBegin() { m_useCount++; }
inline void useEnd(bool bSave) { m_useCount--; if (bSave) m_useSaveCount++; }
friend class CxSkverManager;
private:
std::string _skvType;
std::string _skvPath;
std::string _skvName;
std::string _skvParam;
std::string _filePath;
std::string _path;
std::string _fileName;
std::string _fileSuffixName;
std::string _defaultSaveFilePath;
int m_useCount;
int m_useSaveCount;
};
class CxSkverFactoryManager : public CxFactoryManagerTemplate<CxSkverBase>
{
};
class GM_CCXX_CORE_API CxSkverManager
{
public:
static const std::vector<CxSkverBase*>& skvers();
static CxSkverBase* beginUse(const std::string& sFilePath, bool bLoad = true);
static CxSkverBase* beginUse(const std::string& sFilePath, const std::string& sDataType, const std::string& sDataParam, bool bLoad = true);
static int endUse(CxSkverBase * oProfile, bool bSave = false);
static void closeDeleteAll();
static CxSkverBase* skver(const std::string& sFilePath);
static int load(std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath);
static int save(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath, bool bRenew = true);
//fromContext(const CxISetSkv &)
template<class ObjectDataBase>
static std::vector<ObjectDataBase*> loadObjects(const std::string& sFilePath)
{
std::vector<ObjectDataBase*> objectList;
CxSkverBase * oProfile = CxSkverManager::beginUse(sFilePath, true);
if (oProfile) {
std::vector<std::string> oSections = oProfile->sectionNames();
for (size_t i = 0; i < oSections.size(); ++i)
{
ObjectDataBase* oObject = new ObjectDataBase();
oProfile->prepareGet(oSections.at(i));
oObject->fromContext( *oProfile );
objectList.push_back( oObject );
}
CxSkverManager::endUse(oProfile, false);
}
return objectList;
}
//toContext(CxISetSkv &)
template<class ObjectDataBase>
static void saveObjects(const std::string& sFilePath, std::vector<ObjectDataBase*>* oObjectList)
{
if ( ! CxFileSystem::isExist(sFilePath) ) {
if ( ! CxFileSystem::createFile(sFilePath) )
return;
}
CxSkverBase * oProfile = CxSkverManager::beginUse(sFilePath, false);
if (oProfile){
for (size_t i = 0; i < oObjectList->size(); ++i) {
ObjectDataBase* oObject = oObjectList->at(i);
oProfile->prepareSet(CxString::format("%06d",i));
oObject->toContext( *oProfile );
}
CxSkverManager::endUse(oProfile, true);
}
}
//fromContext(const CxISetSkv &)
template<class StructDataBase>
static std::vector<StructDataBase> loadStructs(const std::string& sFilePath)
{
std::vector<StructDataBase> mStructList;
CxSkverBase * oProfile = CxSkverManager::beginUse(sFilePath, true);
if (oProfile) {
std::vector<std::string> oSections = oProfile->sectionNames();
for (size_t i = 0; i < oSections.size(); ++i)
{
StructDataBase m;
oProfile->prepareGet(oSections.at(i));
m.fromContext( *oProfile );
mStructList.push_back( m );
}
CxSkverManager::endUse(oProfile, false);
}
return mStructList;
}
//toContext(CxISetSkv &)
template<class StructDataBase>
static void saveStructs(const std::string& sFilePath, const std::vector<StructDataBase> & mStructList)
{
if ( ! CxFileSystem::isExist(sFilePath) )
{
if ( ! CxFileSystem::createFile(sFilePath) )
return;
}
CxSkverBase * oProfile = CxSkverManager::beginUse(sFilePath, false);
if (oProfile)
{
for (size_t i = 0; i < mStructList.size(); ++i)
{
const StructDataBase & m = mStructList.at(i);
oProfile->prepareSet(CxString::format("%06d",i));
m.toContext( *oProfile );
}
CxSkverManager::endUse(oProfile, true);
}
}
};
class GM_CCXX_CORE_API CxSkverTxt : public CxSkverBase
{
public:
CxSkverTxt();
CxSkverTxt(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues);
CxSkverTxt( const std::string & sFilePath );
~CxSkverTxt();
protected:
bool loadImpl(std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath);
bool saveImpl(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath, bool bRenew = true) const;
private:
std::string _currentSection;
std::string _currentEntry;
std::string _currentValue;
};
class GM_CCXX_CORE_API CxSkverTxtFactory : public CxSkverFactoryBase
{
public:
std::string factoryName()
{
return ".txt";
}
CxSkverBase * createObject()
{
return new CxSkverTxt();
}
//private:
// static CxSkverTxtFactory * s_instance;
// static CxSkverTxtFactory * createSingleton()
// {
// CxSkverFactoryManager::createAndRegister<CxSkverTxtFactory>();
// static CxSkverTxtFactory mSingleton;
// return & mSingleton;
// }
};
class GM_CCXX_CORE_API CxSkverIni : public CxSkverBase
{
public:
static std::vector<std::vector<std::string> > split(const std::vector<std::string> & ss, char cHeadCharacter, char cIgnore);
public:
CxSkverIni();
CxSkverIni(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues);
CxSkverIni( const std::string & sFilePath );
~CxSkverIni();
int dealLoad(int iLength, const char * pData);
protected:
bool loadImpl(std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath);
bool saveImpl(const std::map<std::string, std::map<std::string, std::string> > & sectionEntryValues, const std::string& sFilePath, bool bRenew = true) const ;
private:
std::map<std::string, std::map<std::string, std::string> > * oSectionEntryValues;
int _dealState;
int _dealType;
char _end;
char _textData[1024];
char * _text;
std::string _currentSection;
std::string _currentEntry;
std::string _currentValue;
};
class GM_CCXX_CORE_API CxSkverIniFactory : public CxSkverFactoryBase
{
public:
std::string factoryName()
{
return ".ini";
}
CxSkverBase * createObject()
{
return new CxSkverIni();
}
//private:
// static CxSkverIniFactory* s_instance;
// static CxSkverIniFactory * createSingleton()
// {
// CxSkverFactoryManager::createAndRegister<CxSkverIniFactory>();
// static CxSkverIniFactory mSingleton;
// return & mSingleton;
// }
};
#endif // CXPROFILE_H