-
Notifications
You must be signed in to change notification settings - Fork 4
/
Helpers.cpp
242 lines (200 loc) · 5.09 KB
/
Helpers.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "stdafx.h"
#include "Helpers.h"
//////////////////////////////////////////////////////////////////////
//
// ReadINIValue:
//! Read the value of a key in a specified section of an INI file
//
//////////////////////////////////////////////////////////////////////
DWORD ReadINIValue (CString sINIFile, CString sSection, CString sKey, CString* sValue)
{
// Check parameters
if (! sValue)
{
return ERROR_INVALID_PARAMETER;
}
// Empty the output string
sValue->Empty ();
//
// Check the INI file for existence
//
if (! IsFileExistent (sINIFile))
{
return ERROR_FILE_NOT_FOUND;
}
//
// Read the value from the config file
//
DWORD nBuffer = 4096;
TCHAR* pBuffer = NULL;
DWORD nCopied = 0;
do
{
// Allocate a buffer
pBuffer = (TCHAR*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof (TCHAR) * nBuffer);
if (pBuffer == NULL)
{
return ERROR_OUTOFMEMORY;
}
// Try to read the value in the section specified by sName
nCopied = GetPrivateProfileString (sSection, sKey, NULL, pBuffer, nBuffer, sINIFile);
if (nCopied == 0)
{
HeapFree(GetProcessHeap(), 0, pBuffer);
pBuffer = NULL;
return ERROR_NOT_FOUND;
}
else if (nCopied == nBuffer - 2)
{
// The buffer is too small
nBuffer += 4096;
HeapFree(GetProcessHeap(), 0, pBuffer);
pBuffer = NULL;
continue;
}
else if (nCopied > nBuffer - 2)
{
// This should not happen
HeapFree(GetProcessHeap(), 0, pBuffer);
pBuffer = NULL;
return ERROR_NOT_FOUND;
}
else
{
break;
}
} while (1);
// Copy the returned string into the out parameter
sValue->Format (TEXT ("%s"), pBuffer);
HeapFree(GetProcessHeap(), 0, pBuffer);
pBuffer = NULL;
return ERROR_SUCCESS;
}
//////////////////////////////////////////////////////////////////////
//
// IsFileExistent:
//! Checks whether file exists (and is accessible)
//
//////////////////////////////////////////////////////////////////////
BOOL IsFileExistent (CString sPath)
{
BOOL bExists = FALSE;
DWORD nAttrs = 0;
DWORD nError = GetFileAttributesAPIWrapper (sPath, &nAttrs);
if (nError == ERROR_SUCCESS)
{
if (nAttrs & FILE_ATTRIBUTE_DIRECTORY)
{
bExists = FALSE;
}
else
{
bExists = TRUE;
}
}
return bExists;
}
//////////////////////////////////////////////////////////////////////
//
// GetFileAttributesAPIWrapper:
//! Wrapper for the API function GetFileAttributes
//
//////////////////////////////////////////////////////////////////////
DWORD GetFileAttributesAPIWrapper (CString sPath, DWORD* nAttributes)
{
DWORD nError = ERROR_INVALID_FUNCTION;
// Check parameters
if (! nAttributes)
{
return ERROR_INVALID_PARAMETER;
}
// Call GetFileAttributes
*nAttributes = GetFileAttributes (sPath);
if (*nAttributes == INVALID_FILE_ATTRIBUTES)
{
nError = GetLastError ();
}
else
{
nError = ERROR_SUCCESS;
}
return nError;
}
//////////////////////////////////////////////////////////////////////
//
// WriteINIValue:
//! Writes the value of a key in a specified section of an INI file
//
//////////////////////////////////////////////////////////////////////
DWORD WriteINIValue (const CString &sINIFile, const CString &sSection, const CString &sKey, const CString &sValue)
{
DWORD nError;
//
// Write the value to the config file
//
nError = WritePrivateProfileString (sSection, sKey, sValue, sINIFile);
if (nError == 0)
{
nError = GetLastError();
return nError;
}
return ERROR_SUCCESS;
}
DWORD WriteINIValue (const CString &sINIFile, const CString &sSection, const CString &sKey, const int &nValue)
{
CString sValue;
sValue.Format (TEXT ("%d"), nValue);
return WriteINIValue (sINIFile, sSection, sKey, sValue);
}
DWORD WriteINIValue (const CString &sINIFile, const CString &sSection, const CString &sKey, const double &nValue)
{
CString sValue;
sValue.Format (TEXT ("%f"), nValue);
return WriteINIValue (sINIFile, sSection, sKey, sValue);
}
//////////////////////////////////////////////////////////////////////
//
// Split: Called by various CSetACL functions. Emulation of Perl's split function.
//
//////////////////////////////////////////////////////////////////////
BOOL Split (CString sDelimiter, CString sInput, CStringArray* asOutput)
{
try
{
// Delete contents of the output array
asOutput->RemoveAll ();
// Remove leading and trailing whitespace
sInput.TrimLeft ();
sInput.TrimRight ();
// Find each substring and add it to the output array
int i = 0, j;
while ((j = sInput.Find (sDelimiter, i)) != -1)
{
asOutput->Add (sInput.Mid (i, j - i));
i = j + 1;
}
// Is there an element left at the end?
if (sInput.GetLength () > i)
{
asOutput->Add (sInput.Mid (i, sInput.GetLength () - i));
}
return TRUE;
}
catch (CMemoryException* exc)
{
exc->Delete ();
return FALSE;
}
}
//////////////////////////////////////////////////////////////////////
//
// Select the item in a combo box list that contains a given string
//
//////////////////////////////////////////////////////////////////////
/*void SelectComboBoxString (CComboBox& oBox, CString& sItem)
{
for (int nIndex = 0; nIndex < oBox.GetCount (); nIndex++)
{
oBox.GetItemData (nIndex);
}
}*/