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