SNFstream.cpp
8.9 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#ifdef __cplusplus
#define DLL_API extern "C" _declspec(dllexport)
#else
#define DLL_API _declspec(dllexport)
#endif
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include "SNFstream.h"
using namespace std;
string& trim(string &s)
{
if (s.empty())
{
return s;
}
int pos = s.find_first_not_of(" ");
if (pos != -1)
{
s.erase(0, pos);
}
pos = s.find_last_not_of(" ") + 1;
if (pos != -1)
{
s.erase(pos);
}
return s;
}
RW_SetupFile_Status ReadIniItemData(const char* pFilePath, IniData_struct iniData)
{
if(!pFilePath)
return INVALID_PARAMETER;
ifstream readStream;
readStream.open(pFilePath);//以读方式打
if(!readStream.is_open())//判断是否打开
{
return OPEN_FILE_FAIL;
}
char tmpBuf[1024] = {0};
char fileLineBuf[1024] = {0};
string fileLine;
string subStr;
string fieldKeyword;
int subIndex = 0;
fieldKeyword = iniData.pItemKeyWord;
trim(fieldKeyword);
while(!readStream.eof())
{
readStream.getline(fileLineBuf,1024); //读入打开文件的每一行
if (fileLineBuf[0] == '\0' || fileLineBuf[0] == '\n')
{
continue;
}
fileLine = fileLineBuf; //将读取文件的文件内容存到字符串中
subIndex = fileLine.find("=");
subStr = fileLine.substr(0,subIndex);
trim(subStr);
if(stricmp(subStr.c_str(), fieldKeyword.c_str()) == 0)
{
int length = strlen(fileLineBuf);
subStr = fileLine.substr(subIndex+1,length);
trim(subStr);
strncpy(tmpBuf, subStr.c_str(), strlen(subStr.c_str())+1);
switch (iniData.dataType)
{
case INI_BOOLEAN:
if (stricmp(tmpBuf, "true") == 0)
{
*((bool*)iniData.pItemData) = true;
}
else
{
*((bool*)iniData.pItemData) = false;
}
cout << *((bool*)iniData.pItemData) << endl;
break;
case INI_INTEGER:
*((int*)iniData.pItemData) = atoi(tmpBuf);
cout << *((int*)iniData.pItemData) << endl;
break;
case INI_STRING:
strncpy(((char*)iniData.pItemData), tmpBuf, strlen(tmpBuf)+1);
cout << ((char*)iniData.pItemData) << endl;
break;
}
break;
}
}
return INI_RW_SUCCESS;
}
RW_SetupFile_Status WriteIniItemData(const char* pFilePath, IniData_struct iniData)
{
if(!pFilePath)
return INVALID_PARAMETER;
LPCTSTR lpSectionName = "System Setup";
LPCTSTR lpKeyName = iniData.pItemKeyWord;
BOOL bWriteSuccess = FALSE;
char tmpBuf[1024] = {0};
char lpValue[1024] = {0};
switch (iniData.dataType)
{
case INI_BOOLEAN:
if (*((bool*)iniData.pItemData) == true)
{
sprintf(lpValue, " %s", "True");
}
else
{
sprintf(lpValue, " %s", "False");
}
break;
case INI_INTEGER:
itoa((*((int*)iniData.pItemData)), tmpBuf, 10);
sprintf(lpValue, " %s", tmpBuf);
break;
case INI_STRING:
sprintf(lpValue, " %s", ((char*)iniData.pItemData));
break;
}
bWriteSuccess = WritePrivateProfileString(lpSectionName, lpKeyName, lpValue, pFilePath);
if (bWriteSuccess == TRUE)
{
return INI_RW_SUCCESS;
}
else
{
return INI_RW_ERROR;
}
}
RW_SetupFile_Status ReadIniData(const char* pFilePath, IniData_struct IniData[], int IniData_size)
{
if(!pFilePath || !IniData || !IniData_size)
return INVALID_PARAMETER;
char tmpBuf[1024] = {0};
char fileLineBuf[1024] = {0};
ifstream readStream;
readStream.open(pFilePath);//以读方式打
if(!readStream.is_open())//判断是否打开
{
return OPEN_FILE_FAIL;
}
string fileLine;
string subStr;
string fieldKeyword;
int subIndex = 0;
bool bFindKeyWord = false;
bool bResetToBegFlag = false;
int retryTime = 0; //每一行的都有一次从头开始找的retry机会
int i = 0;
for (i = 0; i < IniData_size; i++)
{
if (bResetToBegFlag)
{
readStream.seekg(0,ios::beg); //定位到文件开头
retryTime = 1;
bResetToBegFlag = false;
}
bFindKeyWord = false;
cout << i << ": " << IniData[i].pItemKeyWord << " = ";
while(!readStream.eof())
{
readStream.getline(fileLineBuf,1024); //读入打开文件的每一行
if (fileLineBuf[0] == '\0' || fileLineBuf[0] == '\n')
{
continue;
}
fieldKeyword = IniData[i].pItemKeyWord;
fileLine = fileLineBuf; //将读取文件的文件内容存到字符串中
subIndex = fileLine.find("=");
subStr = fileLine.substr(0,subIndex);
trim(subStr);
trim(fieldKeyword);
if(stricmp(subStr.c_str(), fieldKeyword.c_str()) == 0)
{
int length = strlen(fileLineBuf);
subStr = fileLine.substr(subIndex+1,length);
trim(subStr);
strncpy(tmpBuf, subStr.c_str(), strlen(subStr.c_str())+1);
switch (IniData[i].dataType)
{
case INI_BOOLEAN:
if (stricmp(tmpBuf, "true") == 0)
{
*((bool*)IniData[i].pItemData) = true;
}
else
{
*((bool*)IniData[i].pItemData) = false;
}
cout << *((bool*)IniData[i].pItemData) << endl;
break;
case INI_INTEGER:
*((int*)IniData[i].pItemData) = atoi(tmpBuf);
cout << *((int*)IniData[i].pItemData) << endl;
break;
case INI_STRING:
strncpy(((char*)IniData[i].pItemData), tmpBuf, strlen(tmpBuf)+1);
cout << ((char*)IniData[i].pItemData) << endl;
break;
}
bFindKeyWord = true;
retryTime = 0;
break;
}
}
if (bFindKeyWord == false)
{
bResetToBegFlag = true;
if (retryTime == 0)
{
i -= 1; //关建行不变,重新再找一次
}
cout << "Can't find this keyline" << endl;
readStream.clear(); //重新定位到文件开头前必须先clear
}
}
return INI_RW_SUCCESS;
}
RW_SetupFile_Status WriteIniData(const char* pFilePath, IniData_struct IniData[], int IniData_size)
{
if(!pFilePath || !IniData || !IniData_size)
return INVALID_PARAMETER;
ofstream writeStream;
writeStream.open(pFilePath);//以读方式打
if(!writeStream.is_open())//判断是否打开
{
return OPEN_FILE_FAIL;
}
string strFile;
char tmpBuf[1024] = {0};
char tmpLineBuf[1024] = {0};
sprintf(tmpLineBuf, "%s", "[System Setup]");
strFile.append(tmpLineBuf); //新增
strFile.append("\n"); //追加换行符
for (int i = 0; i < IniData_size; i++)
{
switch (IniData[i].dataType)
{
case INI_BOOLEAN:
if (*((bool*)IniData[i].pItemData) == true)
{
cout << *((bool*)IniData[i].pItemData) << endl;
sprintf(tmpLineBuf, "%s = %s", IniData[i].pItemKeyWord, "True");
}
else
{
cout << *((bool*)IniData[i].pItemData) << endl;
sprintf(tmpLineBuf, "%s = %s", IniData[i].pItemKeyWord, "False");
}
break;
case INI_INTEGER:
cout << *((int*)IniData[i].pItemData) << endl;
itoa((*((int*)IniData[i].pItemData)), tmpBuf, 10);
sprintf(tmpLineBuf, "%s = %s", IniData[i].pItemKeyWord, tmpBuf);
break;
case INI_STRING:
cout << ((char*)IniData[i].pItemData) << endl;
sprintf(tmpLineBuf, "%s = %s", IniData[i].pItemKeyWord, ((char*)IniData[i].pItemData));
break;
}
strFile.append(tmpLineBuf); //新增加一行
strFile.append("\n"); //追加换行符
}
writeStream<<strFile;//重新ini文件
writeStream.close();
return INI_RW_SUCCESS;
}