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