-
Notifications
You must be signed in to change notification settings - Fork 475
/
IniFileClass.h
130 lines (108 loc) · 4.12 KB
/
IniFileClass.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
#pragma once
#ifndef _WIN32
#include <strings.h>
#endif
#include "FileClass.h"
#include "CriticalSection.h"
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <memory>
class CIniFileBase
{
public:
typedef std::map<std::string, std::string> KeyValueData;
typedef std::set<std::string> SectionList;
typedef std::list<std::string> strlist;
typedef std::pair<const std::string *, const std::string *> KeyValueItem;
typedef std::vector<KeyValueItem> KeyValueVector;
typedef void(*SortData)(KeyValueVector &);
CIniFileBase(CFileBase & FileObject, const char * FileName);
virtual ~CIniFileBase(void);
bool IsEmpty();
bool IsFileOpen(void);
bool DeleteSection(const char * lpSectionName);
bool GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, std::string & Value);
std::string GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault);
uint32_t GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize);
uint32_t GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault);
bool GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value);
virtual void SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString);
virtual void SaveNumber(const char * lpSectionName, const char * lpKeyName, int32_t Value);
void SetAutoFlush(bool AutoFlush);
void FlushChanges(void);
bool EntryExists(const char * lpSectionName, const char * lpKeyName);
void GetKeyList(const char * lpSectionName, strlist &List);
void GetKeyValueData(const char * lpSectionName, KeyValueData & List);
void SetCustomSort(SortData SortFunction);
void GetVectorOfSections(SectionList & sections);
const std::string &GetFileName() { return m_FileName; }
protected:
void OpenIniFileReadOnly();
void OpenIniFile(bool bCreate = true);
void SaveCurrentSection(void);
std::string FormatStr(const char * strFormat, ...);
CFileBase & m_File;
std::string m_FileName;
private:
struct insensitive_compare
{
bool operator() (const std::string & a, const std::string & b) const
{
return _stricmp(a.c_str(), b.c_str()) < 0;
}
};
typedef std::map<std::string, long> FILELOC;
typedef FILELOC::iterator FILELOC_ITR;
typedef std::map<std::string, std::string, insensitive_compare> KeyValueList;
std::string m_CurrentSection;
bool m_CurrentSectionDirty;
int m_CurrentSectionFilePos; // Where in the file is the current section
KeyValueList m_CurrentSectionData;
long m_lastSectionSearch; // When scanning for a section, what was the last scanned POS
bool m_ReadOnly;
bool m_InstantFlush;
const char * m_LineFeed;
CriticalSection m_CS;
FILELOC m_SectionsPos;
SortData m_SortFunction;
void fInsertSpaces(int Pos, int NoOfSpaces);
int GetStringFromFile(char * & String, std::unique_ptr<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos);
bool MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection);
const char * CleanLine(char * Line);
void ClearSectionPosList(long FilePos);
};
template <class CFileStorage>
class CIniFileT :
public CIniFileBase
{
public:
CIniFileT(const char * FileName) :
CIniFileBase(m_FileObject, FileName)
{
// Try to open file for reading
OpenIniFile();
}
CIniFileT(const char * FileName, bool bCreate, bool bReadOnly) :
CIniFileBase(m_FileObject, FileName)
{
if (bReadOnly)
{
OpenIniFileReadOnly();
}
else
{
// Try to open file for reading
OpenIniFile(bCreate);
}
}
virtual ~CIniFileT(void)
{
SaveCurrentSection();
}
protected:
CFileStorage m_FileObject;
};
typedef CIniFileT<CFile> CIniFile;