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