2066 |
kakl |
1 |
#ifndef __INI_H__ |
|
|
2 |
#define __INI_H__ |
|
|
3 |
|
|
|
4 |
#include <windows.h> |
|
|
5 |
#include <tchar.h> |
|
|
6 |
|
|
|
7 |
// If MFC is linked, we will use CStringArray for great convenience |
|
|
8 |
#ifdef __AFXWIN_H__ |
|
|
9 |
#include <afxtempl.h> |
|
|
10 |
#endif |
|
|
11 |
|
|
|
12 |
// Number bases |
|
|
13 |
#define BASE_BINARY 2 |
|
|
14 |
#define BASE_OCTAL 8 |
|
|
15 |
#define BASE_DECIMAL 10 |
|
|
16 |
#define BASE_HEXADECIMAL 16 |
|
|
17 |
|
|
|
18 |
//--------------------------------------------------------------- |
|
|
19 |
// Callback Function Type Definition |
|
|
20 |
//--------------------------------------------------------------- |
|
|
21 |
// The callback function used for parsing a "double-null terminated string". |
|
|
22 |
// When called, the 1st parameter passed in will store the newly extracted sub |
|
|
23 |
// string, the 2nd parameter is a 32-bit user defined data, this parameter can |
|
|
24 |
// be NULL. The parsing will terminate if this function returns zero. To use |
|
|
25 |
// the callback, function pointer needs to be passed to "CIni::ParseDNTString". |
|
|
26 |
typedef BOOL (CALLBACK *SUBSTRPROC)(LPCTSTR, LPVOID); |
|
|
27 |
|
|
|
28 |
class CIni |
|
|
29 |
{ |
|
|
30 |
public: |
|
|
31 |
|
|
|
32 |
//----------------------------------------------------------- |
|
|
33 |
// Constructors & Destructor |
|
|
34 |
//----------------------------------------------------------- |
|
|
35 |
CIni(); // Default constructor |
|
|
36 |
CIni(LPCTSTR lpPathName); // Construct with a given file name |
|
|
37 |
virtual ~CIni(); |
|
|
38 |
|
|
|
39 |
//----------------------------------------------------------- |
|
|
40 |
// Ini File Path Name Access |
|
|
41 |
//----------------------------------------------------------- |
|
|
42 |
void SetPathName(LPCTSTR lpPathName); // Specify a new file name |
|
|
43 |
DWORD GetPathName(LPTSTR lpBuffer, DWORD dwBufSize) const; // Retrieve current file name |
|
|
44 |
#ifdef __AFXWIN_H__ |
|
|
45 |
CString GetPathName() const; |
|
|
46 |
#endif |
|
|
47 |
|
|
|
48 |
//------------------------------------------------------------ |
|
|
49 |
// String Access |
|
|
50 |
//------------------------------------------------------------ |
|
|
51 |
DWORD GetString(LPCTSTR lpSection, LPCTSTR lpKey, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDefault = NULL) const; |
|
|
52 |
#ifdef __AFXWIN_H__ |
|
|
53 |
CString GetString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpDefault = NULL) const; |
|
|
54 |
#endif |
|
|
55 |
BOOL WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue) const; |
|
|
56 |
|
|
|
57 |
// Read a string from the ini file, append it with another string then write it |
|
|
58 |
// back to the ini file. |
|
|
59 |
BOOL AppendString(LPCTSTR Section, LPCTSTR lpKey, LPCTSTR lpString) const; |
|
|
60 |
|
|
|
61 |
//------------------------------------------------------------ |
|
|
62 |
// Ini File String Array Access |
|
|
63 |
//------------------------------------------------------------ |
|
|
64 |
// Parse the string retrieved from the ini file and split it into a set of sub strings. |
|
|
65 |
DWORD GetArray(LPCTSTR lpSection, LPCTSTR lpKey, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDelimiter = NULL, BOOL bTrimString = TRUE) const; |
|
|
66 |
#ifdef __AFXWIN_H__ |
|
|
67 |
void GetArray(LPCTSTR lpSection, LPCTSTR lpKey, CStringArray* pArray, LPCTSTR lpDelimiter = NULL, BOOL bTrimString = TRUE) const; |
|
|
68 |
BOOL WriteArray(LPCTSTR lpSection, LPCTSTR lpKey, const CStringArray* pArray, int nWriteCount = -1, LPCTSTR lpDelimiter = NULL) const; |
|
|
69 |
#endif |
|
|
70 |
|
|
|
71 |
//------------------------------------------------------------ |
|
|
72 |
// Primitive Data Type Access |
|
|
73 |
//------------------------------------------------------------ |
|
|
74 |
int GetInt(LPCTSTR lpSection, LPCTSTR lpKey, int nDefault, int nBase = BASE_DECIMAL) const; |
|
|
75 |
BOOL WriteInt(LPCTSTR lpSection, LPCTSTR lpKey, int nValue, int nBase = BASE_DECIMAL) const; |
|
|
76 |
BOOL IncreaseInt(LPCTSTR lpSection, LPCTSTR lpKey, int nIncrease = 1, int nBase = BASE_DECIMAL) const; |
|
|
77 |
|
|
|
78 |
UINT GetUInt(LPCTSTR lpSection, LPCTSTR lpKey, UINT nDefault, int nBase = BASE_DECIMAL) const; |
|
|
79 |
BOOL WriteUInt(LPCTSTR lpSection, LPCTSTR lpKey, UINT nValue, int nBase = BASE_DECIMAL) const; |
|
|
80 |
BOOL IncreaseUInt(LPCTSTR lpSection, LPCTSTR lpKey, UINT nIncrease = 1, int nBase = BASE_DECIMAL) const; |
|
|
81 |
|
|
|
82 |
BOOL GetBool(LPCTSTR lpSection, LPCTSTR lpKey, BOOL bDefault) const; |
|
|
83 |
BOOL WriteBool(LPCTSTR lpSection, LPCTSTR lpKey, BOOL bValue) const; |
|
|
84 |
BOOL InvertBool(LPCTSTR lpSection, LPCTSTR lpKey) const; |
|
|
85 |
|
|
|
86 |
double GetDouble(LPCTSTR lpSection, LPCTSTR lpKey, double fDefault) const; |
|
|
87 |
BOOL WriteDouble(LPCTSTR lpSection, LPCTSTR lpKey, double fValue, int nPrecision = -1) const; |
|
|
88 |
BOOL IncreaseDouble(LPCTSTR lpSection, LPCTSTR lpKey, double fIncrease, int nPrecision = -1) const; |
|
|
89 |
|
|
|
90 |
TCHAR GetChar(LPCTSTR lpSection, LPCTSTR lpKey, TCHAR cDefault) const; |
|
|
91 |
BOOL WriteChar(LPCTSTR lpSection, LPCTSTR lpKey, TCHAR c) const; |
|
|
92 |
|
|
|
93 |
//------------------------------------------------------------ |
|
|
94 |
// User-Defined Data Type & Data Block Access |
|
|
95 |
//------------------------------------------------------------ |
|
|
96 |
POINT GetPoint(LPCTSTR lpSection, LPCTSTR lpKey, POINT ptDefault) const; |
|
|
97 |
BOOL WritePoint(LPCTSTR lpSection, LPCTSTR lpKey, POINT pt) const; |
|
|
98 |
|
|
|
99 |
RECT GetRect(LPCTSTR lpSection, LPCTSTR lpKey, RECT rcDefault) const; |
|
|
100 |
BOOL WriteRect(LPCTSTR lpSection, LPCTSTR lpKey, RECT rc) const; |
|
|
101 |
|
|
|
102 |
DWORD GetDataBlock(LPCTSTR lpSection, LPCTSTR lpKey, LPVOID lpBuffer, DWORD dwBufSize, DWORD dwOffset = 0) const; |
|
|
103 |
BOOL WriteDataBlock(LPCTSTR lpSection, LPCTSTR lpKey, LPCVOID lpData, DWORD dwDataSize) const; |
|
|
104 |
BOOL AppendDataBlock(LPCTSTR lpSection, LPCTSTR lpKey, LPCVOID lpData, DWORD dwDataSize) const; |
|
|
105 |
|
|
|
106 |
//------------------------------------------------------------ |
|
|
107 |
// Section Operations |
|
|
108 |
//------------------------------------------------------------ |
|
|
109 |
BOOL IsSectionExist(LPCTSTR lpSection) const; |
|
|
110 |
DWORD GetSectionNames(LPTSTR lpBuffer, DWORD dwBufSize) const; |
|
|
111 |
#ifdef __AFXWIN_H__ |
|
|
112 |
void GetSectionNames(CStringArray* pArray) const; |
|
|
113 |
#endif |
|
|
114 |
BOOL CopySection(LPCTSTR lpSrcSection, LPCTSTR lpDestSection, BOOL bFailIfExist) const; |
|
|
115 |
BOOL MoveSection(LPCTSTR lpSrcSection, LPCTSTR lpDestSection, BOOL bFailIfExist = TRUE) const; |
|
|
116 |
BOOL DeleteSection(LPCTSTR lpSection) const; |
|
|
117 |
|
|
|
118 |
//------------------------------------------------------------ |
|
|
119 |
// Key Operations |
|
|
120 |
//------------------------------------------------------------ |
|
|
121 |
BOOL IsKeyExist(LPCTSTR lpSection, LPCTSTR lpKey) const; |
|
|
122 |
DWORD GetKeyLines(LPCTSTR lpSection, LPTSTR lpBuffer, DWORD dwBufSize) const; |
|
|
123 |
#ifdef __AFXWIN_H__ |
|
|
124 |
void GetKeyLines(LPCTSTR lpSection, CStringArray* pArray) const; |
|
|
125 |
#endif |
|
|
126 |
DWORD GetKeyNames(LPCTSTR lpSection, LPTSTR lpBuffer, DWORD dwBufSize) const; |
|
|
127 |
#ifdef __AFXWIN_H__ |
|
|
128 |
void GetKeyNames(LPCTSTR lpSection, CStringArray* pArray) const; |
|
|
129 |
#endif |
|
|
130 |
BOOL CopyKey(LPCTSTR lpSrcSection, LPCTSTR lpSrcKey, LPCTSTR lpDestSection, LPCTSTR lpDestKey, BOOL bFailIfExist) const; |
|
|
131 |
BOOL MoveKey(LPCTSTR lpSrcSection, LPCTSTR lpSrcKey, LPCTSTR lpDestSection, LPCTSTR lpDestKey, BOOL bFailIfExist = TRUE) const; |
|
|
132 |
BOOL DeleteKey(LPCTSTR lpSection, LPCTSTR lpKey) const; |
|
|
133 |
|
|
|
134 |
//------------------------------------------------------------ |
|
|
135 |
// Parse a "Double-Null Terminated String" |
|
|
136 |
//------------------------------------------------------------ |
|
|
137 |
static BOOL ParseDNTString(LPCTSTR lpString, SUBSTRPROC lpFnStrProc, LPVOID lpParam = NULL); |
|
|
138 |
|
|
|
139 |
//------------------------------------------------------------ |
|
|
140 |
// Check for Whether a String Representing TRUE or FALSE |
|
|
141 |
//------------------------------------------------------------ |
|
|
142 |
static BOOL StringToBool(LPCTSTR lpString, BOOL bDefault = FALSE); |
|
|
143 |
|
|
|
144 |
protected: |
|
|
145 |
|
|
|
146 |
//------------------------------------------------------------ |
|
|
147 |
// Helper Functions |
|
|
148 |
//------------------------------------------------------------ |
|
|
149 |
static LPTSTR __StrDupEx(LPCTSTR lpStart, LPCTSTR lpEnd); |
|
|
150 |
static BOOL __TrimString(LPTSTR lpBuffer); |
|
|
151 |
LPTSTR __GetStringDynamic(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpDefault = NULL) const; |
|
|
152 |
static DWORD __StringSplit(LPCTSTR lpString, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDelimiter = NULL, BOOL bTrimString = TRUE); |
|
|
153 |
static void __ToBinaryString(UINT nNumber, LPTSTR lpBuffer, DWORD dwBufSize); |
|
|
154 |
static int __ValidateBase(int nBase); |
|
|
155 |
static void __IntToString(int nNumber, LPTSTR lpBuffer, int nBase); |
|
|
156 |
static void __UIntToString(UINT nNumber, LPTSTR lpBuffer, int nBase); |
|
|
157 |
static BOOL CALLBACK __SubStrCompare(LPCTSTR lpString1, LPVOID lpParam); |
|
|
158 |
static BOOL CALLBACK __KeyPairProc(LPCTSTR lpString, LPVOID lpParam); |
|
|
159 |
#ifdef __AFXWIN_H__ |
|
|
160 |
static BOOL CALLBACK __SubStrAdd(LPCTSTR lpString, LPVOID lpParam); |
|
|
161 |
#endif |
|
|
162 |
|
|
|
163 |
//------------------------------------------------------------ |
|
|
164 |
// Member Data |
|
|
165 |
//------------------------------------------------------------ |
|
|
166 |
LPTSTR m_pszPathName; // Stores path of the associated ini file |
|
|
167 |
}; |
|
|
168 |
|
|
|
169 |
#endif // #ifndef __INI_H__ |