-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIniEx.cpp
More file actions
1040 lines (889 loc) · 24 KB
/
IniEx.cpp
File metadata and controls
1040 lines (889 loc) · 24 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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// IniEx.cpp: implementation of the CIniEx class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "IniEx.h"
#include "malloc.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// UNICODE File Reader
////////////////////////////////
CStdioUnicodeFile::CStdioUnicodeFile(CStdioUnicodeFile::FILEENCODING encoding):
CStdioFile(),
encoding(encoding),
newLine("\r\n")
{
}
CStdioUnicodeFile::CStdioUnicodeFile(LPCTSTR lpszFileName, UINT nOpenFlags, CStdioUnicodeFile::FILEENCODING encoding):
CStdioFile(lpszFileName, nOpenFlags),
encoding(encoding)
{
}
void CStdioUnicodeFile::WriteBOM()
{
switch(encoding)
{
case CStdioUnicodeFile::FILEENCODING_UTF16LE:
{
BYTE bom[2] = { 0xFF, 0xFE };
Write(bom, 2);
}
break;
case CStdioUnicodeFile::FILEENCODING_UTF8:
case CStdioUnicodeFile::FILEENCODING_UTF8_WITHOUT_BOM:
{
BYTE bom[3] = { 0xEF, 0xBB, 0xBF };
Write(bom, 3);
}
break;
}
}
CStdioUnicodeFile::FILEENCODING CStdioUnicodeFile::ReadBOM()
{
BYTE dummy[3] = {0, 0, 0};
UINT len = Read(dummy, 2);
// check first two bytes (BOT)
if (len == 2)
{
if (dummy[0] == 0xFF && dummy[1] == 0xFE)
{
return CStdioUnicodeFile::FILEENCODING_UTF16LE;
}
}
// check first three bytes (BOM)
len += Read(dummy + 2, 1);
if (len == 3)
{
if (dummy[0] == 0xEF && dummy[1] == 0xBB && dummy[2] == 0xBF)
{
return CStdioUnicodeFile::FILEENCODING_UTF8;
}
}
Seek(0, CFile::begin);
return CStdioUnicodeFile::FILEENCODING_ANSI;
}
// static method to check if file is unicode
CStdioUnicodeFile::FILEENCODING CStdioUnicodeFile::GetFileEncoding(LPCTSTR lpszFileName)
{
CStdioUnicodeFile File(CStdioUnicodeFile::FILEENCODING_UNKNOWN);
if( !File.Open( lpszFileName, CFile::modeRead | CFile::typeBinary ) )
return CStdioUnicodeFile::FILEENCODING_UNKNOWN;
CStdioUnicodeFile::FILEENCODING result = File.ReadBOM();
if( result == CStdioUnicodeFile::FILEENCODING_ANSI )
{
// UTF-8 without BOM ?
unsigned char buf[8];
while( !feof(File.m_pStream) )
{
if( fread(buf, 1, 1, File.m_pStream) == 0 ) break;
size_t bytes = 0;
if( buf[0] < 0x80 )
{
// 0xxxxxxx ASCII < 0x80 (128)
continue;
}
else
{
if( (buf[0] & 0xE0) == 0xC0 )
{
// 110xxxxx 10xxxxxx 2-byte
bytes = 1;
}
else if( (buf[0] & 0xF0) == 0xE0 )
{
// 1110xxxx 10xxxxxx 10xxxxxx 3-byte
bytes = 2;
}
else if( (buf[0] & 0xF8) == 0xF0 )
{
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 4-byte
bytes = 3;
}
if( fread(buf, 1, bytes, File.m_pStream) != bytes ) goto ansi;
for( size_t i = 0; i < bytes; ++i )
if( (buf[i] & 0xC0) != 0x80 ) goto ansi;
}
}
result = CStdioUnicodeFile::FILEENCODING_UTF8_WITHOUT_BOM;
}
ansi:
File.Close();
return result;
}
BOOL CStdioUnicodeFile::ReadString(CString& rString)
{
ASSERT_VALID(this);
rString = _T(""); // empty string without deallocating
const int nMaxSize = 128;
BOOL result = FALSE;
switch(encoding)
{
#ifdef UNICODE
case CStdioUnicodeFile::FILEENCODING_UTF8:
case CStdioUnicodeFile::FILEENCODING_UTF8_WITHOUT_BOM:
{
char * utf8 = nullptr;
size_t size = 1;
size_t len = 0;
for(;;)
{
char * p = static_cast<char*>(realloc(utf8, size += nMaxSize));
if(p == nullptr)
{
free(utf8);
AfxThrowMemoryException();
}
utf8 = p;
p = fgets(utf8 + len, nMaxSize + 1, m_pStream);
// handle error/eof case
if(p == nullptr && !feof(m_pStream))
{
free(utf8);
clearerr(m_pStream);
AfxThrowFileException(CFileException::genericException, _doserrno,
m_strFileName);
}
// if string is read completely or EOF
if(p == nullptr)
{
return FALSE;
}
if(strlen(utf8 + len) < nMaxSize ||
utf8[len + nMaxSize - 1] == '\n')
{
break;
}
len += nMaxSize;
}
len = strlen(utf8);
int rc = ::MultiByteToWideChar(
CP_UTF8, 0,
utf8, len,
nullptr, 0);
if(rc > 0)
{
wchar_t * lpsz = rString.GetBuffer(rc);
rc = ::MultiByteToWideChar(
CP_UTF8, 0,
utf8, len,
lpsz, rc + 1);
lpsz[rc] = 0;
rString.ReleaseBuffer();
result = TRUE;
}
free(utf8);
}
break;
#endif
default:
{
LPTSTR lpsz = rString.GetBuffer(nMaxSize);
LPTSTR lpszResult;
int nLen = 0;
for(;;)
{
lpszResult = _fgetts(lpsz, nMaxSize + 1, m_pStream);
rString.ReleaseBuffer();
// handle error/eof case
if(lpszResult == NULL && !feof(m_pStream))
{
clearerr(m_pStream);
AfxThrowFileException(CFileException::genericException, _doserrno,
m_strFileName);
}
// if string is read completely or EOF
if(lpszResult == NULL ||
(nLen = lstrlen(lpsz)) < nMaxSize ||
lpsz[nLen - 1] == '\n')
break;
nLen = rString.GetLength();
lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
}
result = lpszResult != NULL;
}
}
// remove '\n' from end of string if present
LPTSTR lpsz = rString.GetBuffer(0);
int nLen = rString.GetLength();
if (nLen != 0 && lpsz[nLen-1] == '\n') {
rString.GetBufferSetLength(nLen-1);
nLen = rString.GetLength();
if( nLen != 0 && lpsz[nLen - 1] == '\r' )
{
newLine = "\r\n";
rString.GetBufferSetLength(nLen - 1);
}
else
{
newLine = "\n";
}
}
return result;
}
void CStdioUnicodeFile::WriteString(LPCTSTR lpsz)
{
switch(encoding)
{
#ifdef UNICODE
case CStdioUnicodeFile::FILEENCODING_UTF8:
case CStdioUnicodeFile::FILEENCODING_UTF8_WITHOUT_BOM:
{
int len = static_cast<int>(wcslen(lpsz));
int rc = ::WideCharToMultiByte(
CP_UTF8, 0,
lpsz, len,
nullptr, 0,
nullptr, nullptr);
if(rc > 0)
{
char * utf8 = static_cast<char*>(malloc(rc + 1));
if(utf8)
{
rc = ::WideCharToMultiByte(
CP_UTF8, 0,
lpsz, len,
utf8, rc,
nullptr, nullptr);
Write(utf8, rc);
}
}
}
break;
#endif
default:
CStdioFile::WriteString(lpsz);
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//GrowSize for Dynmiz Section Allocation
CIniEx::CIniEx(int GrowSize/*=4*/)
{
m_GrowSize=GrowSize;
m_SectionNo=0;
m_writeWhenChange=FALSE;
m_makeBackup=FALSE;
m_NoCaseSensitive=TRUE;
m_Changed=FALSE;
m_Keys=NULL;
m_Values=NULL;
m_allocatedObjectCount=0;
m_NewLine = "\r\n";
}
CIniEx::~CIniEx()
{
if (m_writeWhenChange)
WriteFile(m_makeBackup);
ResetContent();
}
void CIniEx::ParseLine (const CString& line, CString& key, CString& val)
{
int iStart = 0, iEnd;
bool quoted = false;
//Clear params
key.Empty();
val.Empty();
//First eat whitespaces
while ( line[iStart] && _istspace(line[iStart]) )
iStart++;
// return if empty input string
if ( line[iStart] == _T('\0') )
return;
//Save starting position and continue
iEnd = iStart;
// First character is " -> everything is quoted (always)
if ( line[iStart] == _T('\"') )
{
quoted = true;
iEnd++;
}
//Walk until first quote skipping pairs
while ( line[iEnd] )
{
if ( !quoted && (line[iEnd] == _T('=')) )
{
break;
}
else if ( quoted && line[iEnd] == _T('\"') )
{
iEnd++;
if ( line[iEnd] != _T('\"') )
{
break;
}
}
iEnd++;
}
//We have our key
key = line.Mid(iStart, iEnd - iStart);
//Remove spaces only from right
key.TrimRight();
//Find the equal.. we should NOT have any remaining chars here...
//but we could if we have something like:
//"key" extra = value
//In that case we discard the extra data
while ( line[iEnd] && line[iEnd] != _T('=') )
iEnd++;
if ( !line[iEnd] )
return;
//Onto the value
iStart = iEnd + 1;
//Eat whitespaces again
while ( line[iStart] && _istspace(line[iStart]) )
iStart++;
val = line.Mid(iStart);
val.TrimRight();
}
BOOL CIniEx::OpenAtExeDirectory(LPCTSTR pFileName,
BOOL writeWhenChange,/*=TRUE*/
BOOL createIfNotExist/*=TRUE*/,
BOOL noCaseSensitive /*=TRUE*/,
BOOL makeBackup /*=FALSE*/)
{
CString filePath;
//if it's a dll argv will be NULL and it may cause memory leak
#ifndef _USRDLL
CString tmpFilePath;
int nPlace=0;
tmpFilePath=__argv[0];
nPlace=tmpFilePath.ReverseFind('\\');
if (nPlace!=-1)
{
filePath=tmpFilePath.Left(nPlace);
}
else
{
TCHAR curDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,curDir);
filePath=curDir;
}
#else
//it must be safe for dll's
TCHAR curDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,curDir);
filePath=curDir;
#endif
filePath+="\\";
filePath+=pFileName;
return Open(filePath,writeWhenChange,createIfNotExist,noCaseSensitive,makeBackup);
}
BOOL CIniEx::Open(LPCTSTR pFileName,
BOOL writeWhenChange,/*=FALSE*/
BOOL createIfNotExist/*=TRUE*/,
BOOL noCaseSensitive /*=FALSE*/,
BOOL makeBackup /*=FALSE*/)
{
CFileException e;
BOOL bRet;
CString Line;
CString sectionStr;
//int nPlace;
UINT mode=CFile::modeReadWrite;
//if it's second ini file for this instance
//we have to save it and delete member variables contents
if (!m_FileName.IsEmpty())
{
if( m_writeWhenChange )
WriteFile();
ResetContent();
}
m_NoCaseSensitive=noCaseSensitive;
m_writeWhenChange=writeWhenChange;
m_makeBackup=makeBackup;
CString tmpKey;
CString tmpValue;
if (createIfNotExist)
mode= mode | CFile::modeCreate | CFile::modeNoTruncate;
CStdioUnicodeFile::FILEENCODING encoding = CStdioUnicodeFile::GetFileEncoding( pFileName );
try
{
m_FileName=pFileName;
//Grow the arrays as GrowSize(which given constructor)
m_allocatedObjectCount = m_GrowSize;
m_Keys=(CStringArray **)malloc( m_allocatedObjectCount * sizeof(CStringArray *) );
m_Values=(CStringArray **)malloc( m_allocatedObjectCount * sizeof(CStringArray *) );
for (int i=0;i<m_GrowSize;i++)
{
m_Keys[m_SectionNo+i]=NULL;
m_Values[m_SectionNo+i]=NULL;
}
CStdioUnicodeFile file(encoding);
#ifdef UNICODE
switch(encoding)
{
case CStdioUnicodeFile::FILEENCODING_UTF16LE:
case CStdioUnicodeFile::FILEENCODING_UTF8:
case CStdioUnicodeFile::FILEENCODING_UTF8_WITHOUT_BOM:
if(!file.Open(pFileName, mode | CFILEFLAG_UNICODEHELPER, &e))
{
return false;
}
file.ReadBOM();
break;
default:
if(!file.Open(pFileName, mode, &e))
return false;
}
#else
switch( encoding )
{
case CStdioUnicodeFile::FILEENCODING_UTF16LE:
case CStdioUnicodeFile::FILEENCODING_UTF8:
CString strErr;
strErr.Format( IDS_UNICODEFILE, pFileName );
AfxMessageBox( strErr );
return false; // cancel
default:
if( !file.Open( pFileName, mode, &e ) )
return false;
}
#endif
for(;;)
{
//Read one line from given ini file
bRet=file.ReadString(Line);
if (!bRet)
break;
Line.TrimRight();
if (Line=="")
continue;
//if line's first character = '['
//and last character = ']' it's section
if (Line.Left(1)=="[" && Line.Right(1)=="]")
{
m_Keys[m_SectionNo]=new CStringArray;
m_Values[m_SectionNo]=new CStringArray;
m_SectionNo++;
GrowIfNecessary();
sectionStr=Line.Mid(1,Line.GetLength()-2);
m_Sections.SetAtGrow(m_SectionNo-1,sectionStr);
continue;
}
ParseLine( Line, tmpKey, tmpValue );
/*nPlace=Line.Find(_T("="));
if (nPlace==-1)
{
tmpKey=Line;
tmpValue="";
}
else
{
tmpKey=Line.Left(nPlace);
tmpValue=Line.Mid(nPlace+1);
}*/
// create Section of no one exists in file
if( m_SectionNo == 0 )
{
m_Keys[m_SectionNo]=new CStringArray;
m_Values[m_SectionNo]=new CStringArray;
m_SectionNo++;
}
m_Keys[m_SectionNo-1]->Add(tmpKey);
m_Values[m_SectionNo-1]->Add(tmpValue);
m_Sections.SetAtGrow(m_SectionNo-1,sectionStr);
}
m_NewLine = file.GetNewLine();
file.Close();
}
catch (CFileException *e)
{
m_ErrStr.Format( _T("%d"), e->m_cause );
}
return TRUE;
}
CString CIniEx::GetValue(CString Key)
{
return GetValue(_T(""),Key);
}
//if Section Name="" -> looking up key for witout section
CString CIniEx::GetValue(CString Section,CString Key,CString DefaultValue/*=""*/)
{
int nIndex=LookupSection(&Section);
if (nIndex==-1) return DefaultValue;
int nRet;
CString retStr;
for (INT_PTR i=m_Keys[nIndex]->GetUpperBound();i>=0;i--)
{
nRet=CompareStrings(&(m_Keys[nIndex]->GetAt(i)),&Key);
if (nRet==0)
{
retStr=m_Values[nIndex]->GetAt(i);
/*int nPlace=retStr.ReverseFind(';');
if (nPlace!=-1)
retStr.Delete(nPlace,retStr.GetLength()-nPlace);*/
return retStr;
}
}
return DefaultValue;
}
//returns index of key for given section
//if no result returns -1
int CIniEx::LookupKey(int nSectionIndex,CString *Key)
{
ASSERT(nSectionIndex<=m_SectionNo);
int nRet;
for (INT_PTR i=m_Keys[nSectionIndex]->GetUpperBound();i>=0;i--)
{
nRet=CompareStrings(&m_Keys[nSectionIndex]->GetAt(i),Key);
if (nRet==0)
return (int)i;
}
return -1;
}
//return given sections index in array
int CIniEx::LookupSection(CString *Section)
{
int nRet;
for (int i=0;i<m_Sections.GetSize();i++)
{
nRet=CompareStrings(&m_Sections.GetAt(i),Section);
if (nRet==0) return i;
}
return -1;
}
//Sets for Key=Value for without section
void CIniEx::SetValue(CString Key,CString Value)
{
SetValue(_T(""),Key,Value);
}
//writes Key=value given section
void CIniEx::SetValue(CString Section,CString Key,CString Value)
{
//file opened?
ASSERT(!m_FileName.IsEmpty());
//if given key already existing, overwrite it
int nIndex=LookupSection(&Section);
int nKeyIndex;
if (nIndex==-1)
{
//if key not exist grow arrays (if necessary)
m_Changed=TRUE;
m_SectionNo++;
GrowIfNecessary();
m_Keys[m_SectionNo-1]=new CStringArray;
m_Values[m_SectionNo-1]=new CStringArray;
nIndex=m_SectionNo-1;
m_Sections.SetAtGrow(m_SectionNo-1,Section);
}
//looking up keys for section
nKeyIndex=LookupKey(nIndex,&Key);
//if key exist -> overwrite it
//if not add to end of array
if (nKeyIndex!=-1)
{
if (CompareStrings(&m_Values[nIndex]->GetAt(nKeyIndex),&Value)!=0)
m_Changed=TRUE;
m_Values[nIndex]->SetAt(nKeyIndex,Value);
}
else //if not exist
{
m_Changed=TRUE;
m_Keys[nIndex]->Add(Key);
m_Values[nIndex]->Add(Value);
}
}
//returns backup file name
//if you didn't want backup (when openning file) it returns ""
CString CIniEx::WriteFile(BOOL makeBackup/*=FALSE*/)
{
if (!m_Changed)
return _T("");
CString tmpFileName=m_FileName;
if (makeBackup)
{
if (m_BackupFileName.IsEmpty())
{
FindBackupFile();
}
CopyFile(m_FileName,m_BackupFileName,FALSE);
}
CStdioUnicodeFile file(CStdioUnicodeFile::FILEENCODING_UTF8);
if (!file.Open(m_FileName, CFILEFLAG_UNICODEHELPER | CFile::modeCreate | CFile::modeWrite))
{
#ifdef _DEBUG
afxDump << "ERROR!!!!: The file could not open for writing\n";
#endif
return _T("");
}
file.WriteBOM();
CString tmpLine;
for (int i=0;i<m_Sections.GetSize();i++)
{
if (!m_Sections.GetAt(i).IsEmpty())
{
tmpLine.Format(_T("[%s]%s"),m_Sections.GetAt(i), m_NewLine );
file.WriteString(tmpLine);
}
if (!m_Keys[i]) continue;
for (int j=0;j<=m_Keys[i]->GetUpperBound();j++)
{
//if key is empts we don't write "="
tmpLine.Format(_T("%s%s%s%s"),m_Keys[i]->GetAt(j),
m_Keys[i]->GetAt(j).IsEmpty()?"":"=",
m_Values[i]->GetAt(j), m_NewLine );
file.WriteString(tmpLine);
}
}
file.Close();
return m_BackupFileName;
}
void CIniEx::WriteFileXliff()
{
CStdioUnicodeFile file(CStdioUnicodeFile::FILEENCODING_UTF8);
if( !file.Open(m_FileName.Left(m_FileName.GetLength() - 4) + CString(".xlf"), CFILEFLAG_UNICODEHELPER | CFile::modeCreate | CFile::modeWrite) )
{
#ifdef _DEBUG
afxDump << "ERROR!!!!: The file could not open for writing\n";
#endif
return;
}
file.WriteBOM();
file.WriteString(CString("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"));
file.WriteString(CString("<xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd\">\n"));
file.WriteString(CString("\t<file source-language=\"en\" target-language=\"en\" datatype=\"winres\" original=\"") + m_FileName + CString("\">\n"));
file.WriteString(CString("\t\t<body>\n"));
CString tmpLine;
for (int i=0;i<m_Sections.GetSize();i++)
{
if( m_Sections.GetAt(i).IsEmpty() )
{
file.WriteString(CString("\t\t\t<group>\n"));
}
else
{
tmpLine.Format(_T("\t\t\t<group id=\"%s\">\n"),m_Sections.GetAt(i));
file.WriteString(tmpLine);
}
if( m_Keys[i] )
{
for( int j = 0; j <= m_Keys[i]->GetUpperBound(); j++ )
{
const CString& ref1 = m_Keys[i]->GetAt(j);
CString xmlencodedSource = ref1.Mid(1, ref1.GetLength() - 2);
xmlencodedSource.Replace(_T("&"), _T("&"));
xmlencodedSource.Replace(_T("'"), _T("'"));
xmlencodedSource.Replace(_T("\""), _T("""));
xmlencodedSource.Replace(_T("<"), _T("<"));
xmlencodedSource.Replace(_T(">"), _T(">"));
const CString& ref2 = m_Values[i]->GetAt(j);
CString xmlencodedTarget = ref2.Mid(1, ref2.GetLength() - 2);
xmlencodedTarget.Replace(_T("&"), _T("&"));
xmlencodedTarget.Replace(_T("'"), _T("'"));
xmlencodedTarget.Replace(_T("\""), _T("""));
xmlencodedTarget.Replace(_T("<"), _T("<"));
xmlencodedTarget.Replace(_T(">"), _T(">"));
tmpLine.Format(_T("\t\t\t\t<trans-unit id=\"%d\" resname=\"%d\" datatype=\"plaintext\">\n\t\t\t\t\t<source>%s</source>\n\t\t\t\t\t<target>%s</target>\n\t\t\t\t</trans-unit>\n"),
i * 10000 + j,
i * 10000 + j,
xmlencodedSource,
xmlencodedTarget);
file.WriteString(tmpLine);
}
}
file.WriteString(CString("\t\t\t</group>\n"));
}
file.WriteString(CString("\t\t</body>\n"));
file.WriteString(CString("\t</file>\n"));
file.WriteString(CString("</xliff>"));
file.Close();
}
BOOL CIniEx::GetWriteWhenChange(void)
{
return m_writeWhenChange;
}
void CIniEx::SetWriteWhenChange(BOOL WriteWhenChange)
{
m_writeWhenChange=WriteWhenChange;
}
void CIniEx::SetBackupFileName(CString &backupFile)
{
m_BackupFileName=backupFile;
}
void CIniEx::FindBackupFile(void)
{
WIN32_FIND_DATA ffData;
BOOL bContinue=TRUE;
CString filePath(m_FileName);
CString ext;
int nPlace=filePath.ReverseFind('.');
filePath.Delete(nPlace,filePath.GetLength()-nPlace);
filePath+="*.*";
int extNo=0;
LPTSTR p;
HANDLE handle=FindFirstFile(filePath,&ffData);
while (bContinue)
{
bContinue=FindNextFile(handle,&ffData);
p=ffData.cFileName;
p+=_tcslen(ffData.cFileName)-3;
if (_ttoi(p)>extNo) extNo=_ttoi(p);
}
m_BackupFileName.Format(_T("%s.%.3d"),m_FileName,extNo+1);
}
void CIniEx::ResetContent()
{
if (!m_Keys) return;
if ( m_Keys)
{
for (int i=0;i<m_allocatedObjectCount;i++)
{
if (m_Keys[i])
delete m_Keys[i];
if (m_Values[i])
delete m_Values[i];
}
free(m_Keys);
free(m_Values);
}
m_Keys=NULL;
m_Values=NULL;
m_Sections.RemoveAll();
m_SectionNo=0;
m_FileName="";
m_Changed=FALSE;
}
//Removes key and it's value from given section
BOOL CIniEx::RemoveKey(CString Section,CString Key)
{
int nIndex=LookupSection(&Section);
if (nIndex==-1) return FALSE;
int nKeyIndex=LookupKey(nIndex,&Key);
if (nKeyIndex==-1) return FALSE;
m_Keys[nIndex]->RemoveAt(nKeyIndex);
m_Values[nIndex]->RemoveAt(nKeyIndex);
m_Changed=TRUE;
return TRUE;
}
//Removes key and it's value
BOOL CIniEx::RemoveKey(CString Key)
{
return RemoveKey(_T(""),Key);
}
//Removes given section(including all keys and values)
//return FALSE when given section not found
//It won't couse memory leak because when deleting object
//the msize (malloc size) checking
BOOL CIniEx::RemoveSection(CString Section)
{
int nIndex=LookupSection(&Section);
if (nIndex==-1) return FALSE;
m_Keys[nIndex]->RemoveAll();
m_Values[nIndex]->RemoveAll();
delete m_Keys[nIndex];
delete m_Values[nIndex];
m_Keys[nIndex]=NULL;
m_Values[nIndex]=NULL;
m_Sections.RemoveAt(nIndex);
m_SectionNo--;
m_Changed=TRUE;
return TRUE;
}
int CIniEx::CompareStrings(const CString *str1, CString *str2)
{
if (m_NoCaseSensitive)
return str1->CompareNoCase(*str2);
else
return str1->Compare(*str2);
}
void CIniEx::GrowIfNecessary(void)
{
//for first gives GrowSize
if (m_SectionNo>=m_allocatedObjectCount)
{
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//realloc for GrowSize
m_allocatedObjectCount += m_GrowSize;
m_Keys=(CStringArray **)realloc(m_Keys,sizeof(CStringArray*) * (m_allocatedObjectCount) );
m_Values=(CStringArray **)realloc(m_Values,sizeof(CStringArray*) * (m_allocatedObjectCount) );
//allocated + GrowSize
//zero memory for new allocation
for (int i=0;i<m_GrowSize;i++)
{
m_Keys[m_SectionNo+i]=NULL;
m_Values[m_SectionNo+i]=NULL;
}
}
}
//copy each string (section name) because
//if sections parametter be a pointer it may clear content of member
void CIniEx::GetSections(CStringArray §ions)
{
for (int i=0;i<m_Sections.GetSize();i++)
sections.Add(m_Sections.GetAt(i));
}
void CIniEx::GetKeysInSection(CString section,CStringArray &keys)
{
int nIndex=LookupSection(§ion);
if (nIndex==-1) return;
for (int i=0;i<m_Keys[nIndex]->GetSize();i++)
{
keys.Add(m_Keys[nIndex]->GetAt(i));
}
}
void CIniEx::SortIniValues()
{
for (int i=0;i<m_Sections.GetSize();i++)
{
if (!m_Keys[i])
continue;
// Quicksort
QuickSortRecursive( i, 0, m_Keys[i]->GetUpperBound(), true );
}
}
int CIniEx::CompareItems( CString str1, CString str2 )
{
return str1.CompareNoCase(str2);
}
bool CIniEx::Swap( int nSection, int nLeftIndex, int nRightIndex )
{
// Ignore unnecessary swaps
if( nLeftIndex == nRightIndex ) return false;
if( nRightIndex == (nLeftIndex + 1) && CompareItems(m_Keys[nSection]->GetAt(nLeftIndex), m_Keys[nSection]->GetAt(nRightIndex)) == 0 ) return false;
CString strHelp = m_Keys[nSection]->GetAt(nLeftIndex);
m_Keys[nSection]->SetAt(nLeftIndex, m_Keys[nSection]->GetAt(nRightIndex) );
m_Keys[nSection]->SetAt(nRightIndex, strHelp );
strHelp = m_Values[nSection]->GetAt(nLeftIndex);
m_Values[nSection]->SetAt(nLeftIndex, m_Values[nSection]->GetAt(nRightIndex));
m_Values[nSection]->SetAt(nRightIndex, strHelp);
return true;
}
void CIniEx::QuickSortRecursive(int nSection, int iLow, int iHigh, bool bAscending)