-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextfile.cpp
1278 lines (1035 loc) · 23.6 KB
/
textfile.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
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
/* NOTE: If you running this in a none-Windows platform,
then you should remove the include file below.
*/
//#include <afx.h>
#include "stdafx.h"
#include "textfile.h"
#if PEK_TX_TECHLEVEL == 0
//Include iostream if running in ANSI mode.
#include <iostream>
#endif
//Base constructor
CTextFileBase::CTextFileBase()
{
#if PEK_TX_TECHLEVEL > 0
m_codepage = CP_ACP;
m_unknownChar = 0;
#else
m_unknownChar = '?';
#endif
#if PEK_TX_TECHLEVEL == 1
m_hFile = INVALID_HANDLE_VALUE;
#elif PEK_TX_TECHLEVEL == 2
m_file = NULL;
m_closeAndDeleteFile = false;
#endif
m_datalost = false;
m_buffpos = -1;
}
//Base destructor
CTextFileBase::~CTextFileBase()
{
Close();
}
//Set which character to use when conversion can't be done
void CTextFileBase::SetUnknownChar(const char unknown)
{
m_unknownChar = unknown;
}
//True if data was lost during conversion
bool CTextFileBase::IsDataLost() const
{
return m_datalost;
}
//Reset the data lost flag
void CTextFileBase::ResetDataLostFlag()
{
m_datalost = false;
}
#if PEK_TX_TECHLEVEL > 0
//Make sure we have a legal value for code page to use when
//converting string. Used for debugging only.
#define IsLegalCodePage(cp) (cp == CP_ACP || cp == CP_MACCP || cp == CP_OEMCP || cp == CP_SYMBOL || cp == CP_THREAD_ACP || cp == CP_UTF7 || cp == CP_UTF8 || IsValidCodePage(cp))
#ifndef ASSERT
#define ASSERT 0
#endif
//(Windows-specific) Set codepage to use when working with non-unicode strings.
void CTextFileBase::SetCodePage(const UINT codepage)
{
ASSERT(IsLegalCodePage(codepage));
m_codepage = codepage;
}
//(Windows-specific) Get codepage to use when working with non-unicode strings.
UINT CTextFileBase::GetCodePage() const
{
return m_codepage;
}
//(Windows-specific) Convert string to wstring with current codepage
inline void CTextFileBase::CharToWstring(const char* from, wstring &to) const
{
ConvertCharToWstring(from, to, m_codepage);
}
//(Windows-specific) Convert wstring to string with current codepage
inline void CTextFileBase::WcharToString(const wchar_t* from, string &to)
{
ConvertWcharToString(from, to, m_codepage, &m_datalost, m_unknownChar);
}
//(Windows-specific) Convert char* to wstring
void CTextFileBase::ConvertCharToWstring(const char* from, wstring &to, UINT codepage)
{
to = L"";
ASSERT(IsLegalCodePage(codepage));
//Use api convert routine
int wlen = MultiByteToWideChar( codepage,
0,
from,
-1,
NULL,
0);
//if wlen == 0, some unknown codepage was probably used.
ASSERT(wlen);
if(wlen == 0)
return;
wchar_t* wbuffer = new wchar_t[wlen+2];
MultiByteToWideChar( codepage,
0,
from,
-1,
wbuffer,
wlen);
to = wbuffer;
delete [] wbuffer;
}
//(Windows-specific) Convert wchar_* to string
void CTextFileBase::ConvertWcharToString(const wchar_t* from, string &to,
UINT codepage, bool* datalost, char unknownchar)
{
to = "";
ASSERT(IsLegalCodePage(codepage));
int alen = WideCharToMultiByte( codepage,
0,
from,
-1,
NULL,
0,
NULL,
NULL);
//if alen == 0, some unknown codepage was probably used.
ASSERT(alen);
if(alen == 0)
return;
//Use mfc convert routine
char* abuffer = new char[alen+2];
BOOL UsedDefault=FALSE;
WideCharToMultiByte( codepage,
0,
from,
-1,
abuffer,
alen,
(unknownchar != 0 ? &unknownchar : NULL),
(datalost != NULL ? &UsedDefault : NULL)
);
if( datalost != NULL && UsedDefault != FALSE)
*datalost = true;
to = abuffer;
delete [] abuffer;
}
#else
//(None-windows-specific) Convert string to wstring
inline void CTextFileBase::CharToWstring(const char* from, wstring &to) const
{
ConvertCharToWstring(from, to);
}
//(None-windows-specific) Convert wstring to string
inline void CTextFileBase::WcharToString(const wchar_t* from, string &to)
{
ConvertWcharToString(from, to, &m_datalost, m_unknownChar);
}
//(None-windows-specific) Convert char* to wstring
void CTextFileBase::ConvertCharToWstring(const char* from, wstring &to)
{
to = L"";
size_t pos=0;
wchar_t temp[1];
while(true)
{
size_t len = mbtowc(temp, from+pos, MB_CUR_MAX);
//Found end
if(len == 0)
return;
else if(len == (size_t)-1)
{
//Unknown character, should never happen
pos++;
}
else
{
to += temp[0];
pos += len;
}
}
}
//(None-windows-specific) Convert wchar_* to string
void CTextFileBase::ConvertWcharToString(const wchar_t* from, string &to, bool* datalost, char unknownchar)
{
to = "";
char* temp = new char[MB_CUR_MAX];
while(*from != L'\0')
{
size_t len = wctomb(temp, *from);
//Found end
if(len == 0)
break;
else if(len == (size_t)-1)
{
//Replace with unknown character
to += unknownchar;
if(datalost != NULL)
*datalost=true;
}
else
{
//Copy all characters
for(size_t i=0; i<len; i++)
to += temp[i];
}
from++;
}
delete [] temp;
}
#endif
//Return file encoding
CTextFileBase::TEXTENCODING CTextFileBase::GetEncoding() const
{
return m_encoding;
};
//Is file open?
int CTextFileBase::IsOpen()
{
#if PEK_TX_TECHLEVEL == 0
return m_file.is_open();
#elif PEK_TX_TECHLEVEL == 1
return m_hFile != INVALID_HANDLE_VALUE;
#else
return (m_file != NULL && m_file->m_hFile != CFile::hFileNull);
#endif
}
//Close file
void CTextFileBase::Close()
{
if(IsOpen())
{
#if PEK_TX_TECHLEVEL == 0
m_file.close();
#elif PEK_TX_TECHLEVEL == 1
::CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
#else
if(m_closeAndDeleteFile)
{
m_file->Close();
}
#endif
}
#if PEK_TX_TECHLEVEL == 2
if(m_closeAndDeleteFile)
{
delete m_file;
m_file = NULL;
}
#endif
}
//Create textfile
CTextFileWrite::CTextFileWrite(const FILENAMECHAR* filename, TEXTENCODING encoding)
{
#if PEK_TX_TECHLEVEL == 0
m_file.open(filename, ios::binary | ios::out );
#elif PEK_TX_TECHLEVEL == 1
m_hFile = ::CreateFile( filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
#else
m_file = new CFile;
m_file->Open(filename, CFile::modeCreate | CFile::modeWrite);
m_closeAndDeleteFile = true;
#endif
m_buffpos = -1;
m_buffsize = 0;
m_encoding = encoding;
WriteBOM();
}
#if PEK_TX_TECHLEVEL==2
//Create textfile from CFile object
CTextFileWrite::CTextFileWrite(CFile* file, TEXTENCODING encoding)
{
ASSERT(file);
m_file = file;
m_closeAndDeleteFile = false;
m_encoding = encoding;
m_buffpos = -1;
m_buffsize = 0;
WriteBOM();
}
#endif
CTextFileWrite::~CTextFileWrite()
{
Close();
}
void CTextFileWrite::WriteBOM()
{
//Write BOM
if(IsOpen())
{
if(m_encoding == UNI16_BE || m_encoding == UNI16_LE)
{
//Write BOM
WriteWchar( 0xFEFF );
}
else if(m_encoding == UTF_8)
{
//Write UTF-8 BOM. 0xEF 0xBB 0xBF
WriteByte(0xEF);
WriteByte(0xBB);
WriteByte(0xBF);
}
}
}
//Write one byte
void CTextFileWrite::WriteByte(const unsigned char byte)
{
//Instead of writing, save data in buffer and write when buffer is full
if(m_buffpos+1 >= BUFFSIZE)
Flush();
m_buffpos++;
m_buf[m_buffpos] = byte;
}
//Write and empty buffer
void CTextFileWrite::Flush()
{
#if PEK_TX_TECHLEVEL==0
m_file.write(m_buf, m_buffpos+1);
#elif PEK_TX_TECHLEVEL==1
DWORD nWritten;
if (!::WriteFile(m_hFile, m_buf, m_buffpos+1, &nWritten, NULL))
{
//Something bad has happend! Close file
CTextFileBase::Close();
//Throw exception
throw CTextFileException(GetLastError());
}
#else
m_file->Write(m_buf, m_buffpos+1);
#endif
m_buffpos = -1;
}
void CTextFileWrite::WriteWchar(const wchar_t ch)
{
//Write HO byte first?
if(m_encoding == UNI16_BE)
{
//Write HO byte
WriteByte((unsigned char) (ch >> 8) );
//Write LO byte
WriteByte((unsigned char) ch);
}
else if(m_encoding == UNI16_LE)
{
//Write LO byte
WriteByte((unsigned char) ch);
//Write HO byte
WriteByte((unsigned char) (ch >> 8) );
}
else
{
//http://www.cl.cam.ac.uk/~mgk25/unicode.html#examples
//http://www.ietf.org/rfc/rfc3629.txt
//Just a single byte?
if(ch <= 0x7F)
{
//U-00000000 - U-0000007F: 0xxxxxxx
WriteByte( (unsigned char) ch );
}
//Two bytes?
else if(ch <= 0x7FF)
{
//U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
WriteByte( (unsigned char) (0xC0 | (ch>>6)) );
WriteByte( (unsigned char) (0x80 | (ch&0x3F)) );
}
//Three bytes?
else if(ch <= 0xFFFF)
{
//U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
WriteByte( (unsigned char) (0xE0 | ( ch>>12) ));
WriteByte( (unsigned char) (0x80 | ( (ch>>6)&0x3F ) ));
WriteByte( (unsigned char) (0x80 | ( ch&0x3F ) ));
}
/* //UPS! I did some coding for UTF-32, may be useful in the future :-)
//Four bytes?
else if(ch <= 0x1FFFFF)
{
//U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
WriteByte( (unsigned char) (0xF0 | ( ch>>18) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>12)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>6)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( ch&0xA0 ) ));
}
//Five bytes bytes?
else if(ch <= 0x3FFFFFF)
{
//U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
WriteByte( (unsigned char) (0xF8 | ( ch>>24) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>18)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>12)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>6)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( ch&0xA0 ) ));
}
//Five bytes bytes?
else if(ch <= 0x7FFFFFFF)
{
//U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
WriteByte( (unsigned char) (0xFC | ( ch>>30) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>24)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>18)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>12)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( (ch>>6)&0xA0 ) ));
WriteByte( (unsigned char) (0xA0 | ( ch&0xA0 ) ));
}*/
}
}
//Write new line
void CTextFileWrite::WriteEndl()
{
if(m_encoding == ASCII)
{
WriteByte(0x0D);
WriteByte(0x0A);
}
else
{
WriteWchar(0x0D);
WriteWchar(0x0A);
}
}
//Write a c-string in ASCII.
//In versions before 1.02 this function wrote directly to file,
//no buffer was used. But sense WriteEndl() used buffer the file was
//written incorretly. Now buffer is used here too, this is the
//fastest solution.
void CTextFileWrite::WriteAsciiString(const char* s)
{
while(*s != '\0')
{
WriteByte(*s);
s++;
}
}
CTextFileWrite& CTextFileWrite::operator<< (const char* text)
{
Write(text);
return *this;
};
CTextFileWrite& CTextFileWrite::operator << (const string& text)
{
Write(text.c_str());
return *this;
}
CTextFileWrite& CTextFileWrite::operator<< (const wchar_t* text)
{
Write(text);
return *this;
};
CTextFileWrite& CTextFileWrite::operator << (const wstring& text)
{
Write(text.c_str());
return *this;
}
CTextFileWrite& CTextFileWrite::operator<< (const wchar_t wc)
{
//Not the perfect code, but it's easy!
wstring text;
text=wc;
Write(text.c_str());
return *this;
};
CTextFileWrite& CTextFileWrite::operator<< (const char c)
{
//Not the perfect code, but it's easy!
string text;
text=c;
Write(text.c_str());
return *this;
};
void CTextFileWrite::Write(const string& text)
{
Write(text.c_str());
}
void CTextFileWrite::Write(const wstring& text)
{
Write(text.c_str());
}
//Write char*
void CTextFileWrite::Write(const char* text)
{
//ASCIItext file format?
if(m_encoding == ASCII)
WriteAsciiString(text);
else
{
//Convert text to unicode
wstring utext;
CharToWstring(text, utext);
//OK, lets write unicode
for(wstring::const_iterator i=utext.begin();
i < utext.end();
i++)
{
WriteWchar(*i);
}
}
}
//Write wcar_t*
void CTextFileWrite::Write(const wchar_t* utext)
{
//ASCII text file format?
if(m_encoding == ASCII)
{
//Convert to string and write
string text;
WcharToString(utext, text);
WriteAsciiString(text.c_str());
}
else
{
while(*utext != 0)
{
WriteWchar(*utext);
utext++;
}
}
}
//Close the file
void CTextFileWrite::Close()
{
if(IsOpen())
Flush();
CTextFileBase::Close();
}
CTextFileRead::CTextFileRead(const FILENAMECHAR* filename)
{
#if PEK_TX_TECHLEVEL==0
//If not Windows, do this
m_file.open(filename, ios::binary | ios::in);
#elif PEK_TX_TECHLEVEL == 1
m_hFile = ::CreateFile( filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
#else
m_file = new CFile;
//In windows, do this
m_file->Open(filename, CFile::modeRead | CFile::shareDenyWrite );
m_closeAndDeleteFile = true;
#endif
m_firstLine = true;
m_endoffile = (IsOpen()==0);
//Force reading to buffer next time
m_buffpos=-1;
m_useExtraBuffer=false;
ReadBOM();
}
#if PEK_TX_TECHLEVEL==2
CTextFileRead::CTextFileRead(CFile* file)
{
ASSERT(file);
m_file = file;
m_closeAndDeleteFile = false;
m_firstLine = true;
m_endoffile = (IsOpen()==0);
//Force reading to buffer next time
m_buffpos=-1;
m_useExtraBuffer=false;
ReadBOM();
}
#endif
void CTextFileRead::ReadBOM()
{
if( IsOpen() )
{
unsigned char bytes[2];
//Read the first two bytes
ReadByte(bytes[0]);
ReadByte(bytes[1]);
//Figure out what format the file is in
if( bytes[0] == 0xFF && bytes[1] == 0xFE)
m_encoding = UNI16_LE;
else if( bytes[0] == 0xFE && bytes[1] == 0xFF)
m_encoding = UNI16_BE;
else if( bytes[0] == 0xEF && bytes[1] == 0xBB)
{
//This is probably UTF-8, check the third byte
unsigned char temp;
ReadByte(temp);
if( temp == 0xBF)
m_encoding = UTF_8;
else
{
//Set text format.
m_encoding = ASCII;
ResetFilePointer();
}
}
else
{
m_encoding = ASCII;
//Set start pos
ResetFilePointer();
}
}
}
//End of file?
bool CTextFileRead::Eof() const
{
return m_endoffile;
}
//Read one byte
void CTextFileRead::ReadByte(unsigned char& ch)
{
//Use extrabuffer if needed
if(m_useExtraBuffer)
{
m_useExtraBuffer=false;
ch = m_extraBuffer_char;
return;
}
//In Windows, do this...
//If buffer used or not read
if(m_buffpos==-1 || m_buffpos == BUFFSIZE-1)
{
#if PEK_TX_TECHLEVEL==0
m_buffsize = m_file.read(m_buf, BUFFSIZE).gcount();
#elif PEK_TX_TECHLEVEL==1
DWORD dwRead;
if (!::ReadFile(m_hFile, m_buf, BUFFSIZE, &dwRead, NULL))
{
//Couldn't read!
Close();
m_buffsize = 0;
//Throw exception
throw CTextFileException(GetLastError());
}
else
m_buffsize = (int) dwRead;
#else
m_buffsize=m_file->Read(m_buf, BUFFSIZE);
#endif
if(m_buffsize == 0)
{
m_endoffile=true;
ch = 0;
return;
}
m_buffpos=0;
}
else
{
m_buffpos++;
if(m_buffpos >= m_buffsize)
{
m_endoffile=true;
ch = 0;
return;
}
}
ch = m_buf[m_buffpos];
}
void CTextFileRead::ReadWchar(wchar_t& ch)
{
if(m_useExtraBuffer)
{
m_useExtraBuffer=false;
ch = m_extraBuffer_wchar;
return;
}
if(m_encoding == UTF_8)
{
//This is quite tricky :-/
//http://www.cl.cam.ac.uk/~mgk25/unicode.html#examples
unsigned char byte;
ReadByte(byte);
int onesBeforeZero = 0;
{ //Calc how many ones before the first zero...
unsigned char temp = byte;
while( (temp & 0x80)!=0 )
{
temp = (unsigned char) (temp << 1);
onesBeforeZero++;
}
}
if(onesBeforeZero==0)
{
ch = byte;
return;
}
else if(onesBeforeZero == 2)
{
//U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
unsigned char byteb;
ReadByte(byteb);
ch = (wchar_t) ( ((0x1F & byte) << 6)|
(0x3F & byteb)
) ;
return;
}
else if(onesBeforeZero == 3)
{
//U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
unsigned char byteb, bytec;
ReadByte(byteb);
ReadByte(bytec);
ch = (wchar_t) ( ((0x0F & byte) << 12) |
((0x3F & byteb) << 6) |
(0x3F & bytec) );
return;
}
//This should never happend! It it do, something is wrong with the file.
ch = 0xFFFD;
}
else
{
unsigned char bytes[2];
ReadByte(bytes[0]);
ReadByte(bytes[1]);
if(m_encoding == UNI16_BE)
ch = (wchar_t) ( ((wchar_t) bytes[0] << 8) |
(wchar_t) bytes[1]
) ;
else
ch = (wchar_t) ( ((wchar_t) bytes[1] << 8) |
(wchar_t) bytes[0]
);
}
}
void CTextFileRead::ResetFilePointer()
{
m_useExtraBuffer=false;
#if PEK_TX_TECHLEVEL==0
m_file.clear();
m_file.seekg(0, ios::beg);
#elif PEK_TX_TECHLEVEL==1
::SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN);
#else
m_file->SeekToBegin();
#endif
//Force reread buffer
m_buffpos=-1;
m_firstLine = true;
m_endoffile = false;
}
bool CTextFileRead::ReadLine(string& line)
{
//EOF?
if(Eof())
return false;
if(m_encoding == ASCII)
{
return ReadCharLine(line);
}
wstring wline;
if(!ReadWcharLine(wline))
return false;
//Convert
WcharToString(wline.c_str(), line);
return true;
}
bool CTextFileRead::ReadLine(wstring& line)
{
//EOF?
if(Eof())
return false;
if(m_encoding == ASCII)
{
string cline;
if(!ReadCharLine(cline))
return false;
//Convert to wstring
CharToWstring(cline.c_str(), line);
return true;
}
return ReadWcharLine(line);
}
bool CTextFileRead::Read(string& all, const string newline)
{
if(!IsOpen())
return false;
int buffsize = GuessCharacterCount()+2;
int buffpos = 0;
//Create buffer
char* buffer = new char[buffsize];
//If not possible, don't use any buffer
if(buffer == NULL)
buffsize = 0;
string temp;
all = temp;
all.reserve(buffsize);
bool firstLine=true;
while(!Eof())
{
if(ReadLine(temp))
{
//Add new line, if not first line
if(!firstLine)
temp.insert(0, newline.c_str());
else
firstLine=false;
//Add to buffer if possible
if(buffpos + (int) temp.size() < buffsize)
{
strcpy(buffer+buffpos, temp.c_str());
buffpos += (int) temp.size();
}
else
{
//Copy to all string
if(buffpos != 0)
{
all.append(buffer, buffpos);
buffpos = 0;
}
all += temp;
}
}