-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIniFile.cpp
More file actions
721 lines (626 loc) · 17 KB
/
IniFile.cpp
File metadata and controls
721 lines (626 loc) · 17 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) Li Haolai 2012 - All Rights Reserved
//
// Author: Li HaoLai (lihaolai0000@163.com, QQ:3883140)
// File: CIniFile
// Version: 1.0
// Date: 2012-01-09
//
// Purpose: Ini文件的标准输入输出类,用来读入Ini文件和写入Ini文件。
//
/////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IniFile.h"
#include "Utility.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
static const LPTSTR CommentFlag = "#";
static const int MAX_PATH_SIZE = 256;
// ---------------------------------------------------------------------------
//
// Function: CIniFile()
//
// Description:
// 构造函数,根据指定的Ini文件来生成本对象。
// (如果Ini文件中不带\\,那么自动添加当前目录。)
//
//
// Input:
// LPCTSTR szFileName -- Ini文件名
//
// Returns:
// Nothing
//
// ---------------------------------------------------------------------------
CIniFile::CIniFile(LPCTSTR szFileName)
{
CString szFilePath (szFileName);
if( szFilePath.ReverseFind(TEXT('\\')) < 0)
{
CHAR szPath[MAX_PATH] = {0};
//::GetCurrentDirectory(MAX_PATH,szPath);
// the current directory would be changed frequently, so destory this call.
::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, MAX_PATH);
CString szModuleFileName(szPath);
int iPos = szModuleFileName.ReverseFind('\\');
m_szFileName = szModuleFileName.Left(iPos);
m_szFileName += "\\";
m_szFileName += szFileName;
}else
{
m_szFileName = szFileName;
}
m_pTxtFile = new CFile();
m_bOpened = FALSE;
m_bIsReadOnly = FALSE;
}
// ---------------------------------------------------------------------------
//
// Function: CIniFile()
//
// Description:
// 析构函数,删除成员变量,释放成员变量的指针。
//
//
// Input:
// Nothing
//
// Returns:
// Nothing
//
// ---------------------------------------------------------------------------
CIniFile::~CIniFile()
{
Section *pSection = NULL;
for( int iLoop =0 ; iLoop < m_AryLine.GetSize(); iLoop++)
{
pSection = m_AryLine.GetAt(iLoop);
delete pSection;
m_AryLine.SetAt(iLoop, NULL);
}
m_AryLine.RemoveAll();
if(m_pTxtFile != NULL)
{
if( m_bOpened)
{
m_pTxtFile->Close();
}
delete m_pTxtFile;
m_pTxtFile = NULL;
}
}
// ---------------------------------------------------------------------------
//
// Function: Open()
//
// Description:
// 打开Ini文件,同时读入到成员变量中。
//
//
// Input:
// BOOL isReadOnly -- 是否以只读方式打开,缺省为非只读。
//
// Returns:
// BOOL TRUE/FALSE --文件打开成功或者失败。
//
// ---------------------------------------------------------------------------
BOOL CIniFile::Open(BOOL isReadOnly /* = FALSE */)
{
BOOL bReturn = FALSE;
m_bIsReadOnly = isReadOnly;
if( isReadOnly)
{
bReturn =m_pTxtFile->Open(m_szFileName,CFile::modeRead);
}
else
{
bReturn =m_pTxtFile->Open(m_szFileName,CFile::modeReadWrite);
}
if( !bReturn)
{
// bReturn = m_pTxtFile->Open(m_szFileName,CFile::modeCreate | CFile::modeReadWrite );
if( !bReturn)
{
return bReturn;
}
}
int iLength= (int)m_pTxtFile->GetLength();
CHAR *szContent = (CHAR *) malloc((iLength +1)* sizeof (CHAR));
memset(szContent, 0, (iLength +1) * sizeof(CHAR));
m_pTxtFile->Read(szContent,iLength);
m_szContent = szContent;
free(szContent);
szContent = NULL;
CStringArray StringLine ;
CUtility::SplitString(m_szContent,"\r\n",&StringLine);
CString szTemp = "";
for(int iLoop =0 ; iLoop < StringLine.GetSize(); iLoop++)
{
szTemp = StringLine.GetAt(iLoop);
szTemp.TrimLeft(" ");
szTemp.TrimRight(" ");
// re-write the trimed string to string array;
StringLine.SetAt(iLoop,szTemp);
// if the line is started with # -- CommentFlag.
if( szTemp.Left((int)strlen(CommentFlag)) == CommentFlag)
{
Section *pSec = new Section();
// Get the left string started with 1, except #.
pSec->Comment = szTemp.Right(szTemp.GetLength() -1 );
pSec->Type = IsComment;
m_AryLine.Add(pSec);
// if the line is started with [ and the have the ].
}else if(szTemp.Left(1) == "[" && szTemp.Find("]") > 0)
{
int iPos = szTemp.Find("]");
CString szSectionName = szTemp.Mid(1,iPos-1);
szSectionName.TrimLeft(" ");
szSectionName.TrimRight(" ");
Section *pSec = new Section();
pSec->SectionName = szSectionName;
// split the comment from #.
iPos = szTemp.Find(CommentFlag, iPos);
if(iPos > 0)
{
CString szComment = szTemp.Mid(iPos+1);
szComment.TrimLeft(" ");
pSec->Comment = szComment;
}
pSec->Type = IsSection;
m_AryLine.Add(pSec);
// if include =, that means the type is Item.
}else if( szTemp.Find("=",0) > 0 )
{
Section *pSec = new Section();
int iPos = szTemp.Find("=",0);
CString szItem = szTemp.Left(iPos);
szItem.TrimLeft(" ");
szItem.TrimRight(" ");
pSec->Item = szItem;
pSec->Type = IsItem;
iPos ++; // escape the = charactor
int iPos2 = szTemp.Find(CommentFlag, iPos );
if(iPos2 > 0 ) // if found the comment flag
{
// get right value of the item.
CString szValue = szTemp.Mid(iPos , iPos2- iPos);
szValue.TrimLeft(" ");
szValue.TrimRight(" ");
pSec->Value = szValue;
// get the comment content.
CString szComment = szTemp.Right(szTemp.GetLength() - iPos2);
szComment.TrimLeft(" ");
pSec->Comment = szComment;
m_AryLine.Add(pSec);
}else
{
// read all content as the right value.
CString szValue = szTemp.Right(szTemp.GetLength() - iPos);
szValue.TrimLeft(" ");
pSec->Value = szValue;
m_AryLine.Add(pSec);
}
}
}
m_bOpened = bReturn;
return bReturn;
}
// ---------------------------------------------------------------------------
//
// Function: Close()
//
// Description:
// 关闭Ini文件,同时把成员变量写到文件中。
//
//
// Input:
// Nothing
//
// Returns:
// BOOL TRUE/FALSE --文件关闭成功或者失败。
//
// ---------------------------------------------------------------------------
BOOL CIniFile::Close()
{
if(m_bOpened)
{
if( !m_bIsReadOnly)
{
this->Flush();
}
m_pTxtFile->Close();
delete m_pTxtFile;
m_pTxtFile = NULL;
m_bOpened = FALSE;
}
return TRUE;
}
// ---------------------------------------------------------------------------
//
// Function: Find()
//
// Description:
// 根据SectionName和Item名称来查找对应的值。
//
//
// Input:
// LPCTSTR szSection -- Section Name,不能为空。
// LPCTSTR szItem -- Item Name,不能为空。
//
// Returns:
// CString -- 返回的Value,如果没有找到,那么返回空串
//
// ---------------------------------------------------------------------------
CString CIniFile::Find(LPCTSTR szSection, LPCTSTR szItem)
{
CString strSection(szSection);
CString strItem(szItem);
strSection.TrimLeft();
strSection.TrimRight();
strItem.TrimLeft();
strItem.TrimRight();
CString szValue = "";
BOOL bFoundSection = FALSE;
CString szTemp = "";
for(int iLoop =0 ; iLoop < m_AryLine.GetSize(); iLoop++)
{
Section *pSec = m_AryLine.GetAt(iLoop);
szTemp = pSec->SectionName;
szTemp.MakeLower();
strSection.MakeLower();
if(szTemp== strSection)
{
bFoundSection = TRUE;
}
szTemp = pSec->Item ;
szTemp.MakeLower();
strItem.MakeLower();
if( bFoundSection && szTemp == strItem)
{
szValue = pSec->Value ;
break;
}
}
return szValue;
}
// ---------------------------------------------------------------------------
//
// Function: WriteValue()
//
// Description:
// 把参数中的内容写到类的成员变量中。
//
//
// Input:
// LPCTSTR szSection -- Section Name,不能为空。
// LPCTSTR szItem -- Item Name,可以为空。
// LPCTSTR szValue -- 要写入的值。
// LPCTSTR szComment -- 要写入的Comment,可以为空。
//
// Returns:
// BOOL TRUE/FALSE -- 写操作成功或者失败。
//
// ---------------------------------------------------------------------------
BOOL CIniFile::WriteValue(LPCTSTR szSection, LPCTSTR szItem, LPCTSTR szValue, LPCTSTR szComment /* = NULL*/)
{
CString strSection(szSection);
strSection.MakeLower();
CString strItem(szItem);
if(szItem == NULL)
{
strItem.Empty();
}
strItem.MakeLower();
BOOL bFoundSection = FALSE;
BOOL bFoundItem = FALSE;
int iPosSection = 0;
CString szTemp;
for(int iLoop =0 ; iLoop < m_AryLine.GetSize(); iLoop++)
{
Section *pSec = m_AryLine.GetAt(iLoop);
// re-locate the position of section.
if( pSec->Type == IsSection)
{
szTemp = pSec->SectionName ;
szTemp.MakeLower();
szTemp.TrimLeft(" ");
if(szTemp == strSection )
{
iPosSection = iLoop;
bFoundSection = TRUE;
}
}
// relocated the position of item.
if( pSec->Type == IsItem)
{
szTemp = pSec->Item;
szTemp.MakeLower();
if( bFoundSection && szTemp == strItem) // Found Item & Found Section
{
// change the value of located item.
bFoundItem = TRUE;
pSec->Value = szValue;
if(szComment != NULL)
{
pSec->Comment = szComment;
}
break;
}
}
}
CArray< Section *, Section *> newArray;
if( !bFoundSection )
{
// even if the section is not found, write a new section to ini file.
Section *pSec = new Section();
pSec->SectionName = szSection;
if( szComment != NULL )
{
pSec->Comment = szComment;
}
pSec->Type = IsSection;
m_AryLine.Add(pSec);
// if szItem is aslo defined, then write the item to ini file too.
if( szItem != NULL)
{
Section *pItem = new Section();
pItem->Item = szItem;
pItem->Value = szValue;
if(szComment != NULL)
{
pItem->Comment = szComment;
}
pItem->Type = IsItem;
m_AryLine.Add(pItem);
}
}else // if found section.
{
if (!bFoundItem) // if not found the item, then add a new item to the located section.
{
Section *pSec = new Section();
pSec->Item = szItem;
pSec->Value = szValue;
if(szComment != NULL)
{
pSec->Comment = szComment;
}
pSec->Type = IsItem;
// insert the pSec to current postion of a new array.
for( int jLoop =0 ; jLoop < iPosSection ; jLoop ++)
{
newArray.Add(m_AryLine.GetAt(jLoop));
}
newArray.Add(pSec);
for(int jLoop = iPosSection; jLoop < m_AryLine.GetSize(); jLoop++)
{
newArray.Add(m_AryLine.GetAt(jLoop));
}
// copy the value to m_AryLine;
for( int lLoop = 0; lLoop < m_AryLine.GetSize(); lLoop ++)
{
delete m_AryLine.GetAt(lLoop);
}
m_AryLine.RemoveAll();
for( int kLoop = 0; kLoop < newArray.GetSize(); kLoop++)
{
m_AryLine.Add(newArray.GetAt(kLoop));
}
}// else do nothing because change the item value has already be processed during for loop.
}
return TRUE;
}
// ---------------------------------------------------------------------------
//
// Function: Flush()
//
// Description:
// write the m_AryLine value to file.
//
// Input:
// Nothing.
//
// Returns:
// BOOL : TRUE - success.
//
// ---------------------------------------------------------------------------
BOOL CIniFile::Flush()
{
if(m_bOpened)
{
m_szContent.Empty();
for(int iLoop =0; iLoop <m_AryLine.GetSize(); iLoop++)
{
Section *pSec = m_AryLine.GetAt(iLoop);
switch (pSec->Type )
{
case IsSection:
m_szContent += "\r\n"; // add new blank line.
m_szContent += "[";
m_szContent += pSec->SectionName ;
m_szContent += "]";
if( !pSec->Comment.IsEmpty())
{
m_szContent += " "; // five spaces
m_szContent += CommentFlag;
m_szContent += pSec->Comment ;
}
break;
case IsComment:
m_szContent += CommentFlag;
m_szContent += pSec->Comment ;
break;
case IsItem:
m_szContent += pSec->Item;
m_szContent += " = ";
m_szContent += pSec->Value ;
if( !pSec->Comment.IsEmpty())
{
m_szContent += " "; // five spaces
m_szContent += CommentFlag;
m_szContent += pSec->Comment ;
}
break;
default:
break;
}
m_szContent += "\r\n";
}
int iLength = m_szContent.GetLength();
m_pTxtFile->SeekToBegin(); // rewrite the ini file from the first postion.
m_pTxtFile->SetLength(0);
m_pTxtFile->Write((LPCTSTR)m_szContent, iLength);
m_pTxtFile->Flush();
} //if(m_bOpened)
return TRUE;
}
// ---------------------------------------------------------------------------
//
// Function: IsFileExist()
//
// Description:
// 文件操作类函数,这个函数用来判断文件是否存在。
//
//
// Input:
// CString szFileName -- 文件名(全路径),不能为空。
//
// Returns:
// BOOL TRUE/FALSE -- 存在或则不存在。
//
// ---------------------------------------------------------------------------
bool CIniFile::IsFileExist(CString szFileName)
{
WIN32_FIND_DATA data;
HANDLE hd= FindFirstFile(szFileName,&data);
if(hd != INVALID_HANDLE_VALUE)
return true;
return false;
}
// ---------------------------------------------------------------------------
//
// Function: CopyFileTo()
//
// Description:
// 文件操作类函数,拷贝一个文件到另外一个文件名。
//
//
// Input:
// CString szDestFileName -- 目的文件名(全路径),不能为空。
// CString szSrcFileName -- 源文件名(全路径),不能为空。
//
// Returns:
// Nothing
//
// ---------------------------------------------------------------------------
void CIniFile::CopyFileTo(CString szDestFileName, CString szSrcFileName)
{
bool bb=IsFileExist(szSrcFileName);
if(!bb)return;
CopyFile( szSrcFileName, szDestFileName, FALSE);
}
// ---------------------------------------------------------------------------
//
// Function: GetWinSysPath()
//
// Description:
// 文件操作类函数,读取系统文件夹的路径。
//
//
// Input:
// Nothing
//
// Returns:
// CString -- 读出来的路径。
//
// ---------------------------------------------------------------------------
CString CIniFile::GetWinSysPath(void)
{
char lpBuffer[MAX_PATH_SIZE];
GetSystemDirectory( lpBuffer, MAX_PATH_SIZE);
CString ss=lpBuffer;
ss += "\\";
return ss;
}
// ---------------------------------------------------------------------------
//
// Function: GetAppPath()
//
// Description:
// 文件操作类函数,读取应用程序文件夹的路径。
//
//
// Input:
// Nothing
//
// Returns:
// CString -- 读出来的路径。
//
// ---------------------------------------------------------------------------
CString CIniFile::GetAppPath(void)
{
char lpBuffer[MAX_PATH_SIZE];
GetCurrentDirectory(MAX_PATH_SIZE,lpBuffer);
CString ss=lpBuffer;
ss += "\\";
return ss;
}
// ---------------------------------------------------------------------------
//
// Function: GetKeyVal()
//
// Description:
// 文件操作类函数,根据Section和Key来读取一个整形值。
//
//
// Input:
// CString szSectionName -- 目的文件名(全路径),不能为空。
// CString szKeyName -- 源文件名(全路径),不能为空。
// int ipDefault -- 缺省值
//
// Returns:
// int -- 读出来的值,或者是缺省值。
//
// ---------------------------------------------------------------------------
int CIniFile::GetKeyVal(CString szSectionName, CString szKeyName, int ipDefault)
{
int iValue = ipDefault;
CString szValue = Find(szSectionName, szKeyName);
if (szValue != "")
{
iValue = atoi(szValue);
}
return iValue;
}
// ---------------------------------------------------------------------------
//
// Function: GetKeyVal()
//
// Description:
// 文件操作类函数,根据Section和Key来读取一个字符串值。
//
//
// Input:
// CString szSectionName -- 目的文件名(全路径),不能为空。
// CString szKeyName -- 源文件名(全路径),不能为空。
// CString szDefault -- 缺省值
//
// Returns:
// CString -- 读出来的值,或者是缺省值。
//
// ---------------------------------------------------------------------------
CString CIniFile::GetKeyVal(CString szSectionName, CString szKeyName, LPCTSTR szDefault)
{
CString szValue = Find(szSectionName, szKeyName);
if (szValue == "")
{
szValue = szDefault;
}
return szValue;
}