forked from mremec/omnixml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OEncoding.pas
904 lines (801 loc) · 26.4 KB
/
OEncoding.pas
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
unit OEncoding;
{
Author:
Ondrej Pokorny, http://www.kluug.net
All Rights Reserved.
License:
MPL 1.1 / GPLv2 / LGPLv2 / FPC modified LGPLv2
Please see the /license.txt file for more information.
}
{
OEncoding.pas
Convert buffers to strings and back with encoding classes.
}
{$I OXml.inc}
{$IFDEF O_DELPHI_XE4_UP}
{$ZEROBASEDSTRINGS OFF}
{$ENDIF}
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$BOOLEVAL OFF}
interface
uses
{$IFDEF O_NAMESPACES}
System.SysUtils,
{$ELSE}
SysUtils,
{$ENDIF}
OWideSupp;
const
//see http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756%28v=vs.85%29.aspx
//single-byte
CP_WIN_1250 = 1250;//ANSI Central European; Central European (Windows)
CP_WIN_1251 = 1251;//ANSI Cyrillic; Cyrillic (Windows)
CP_WIN_1252 = 1252;//ANSI Latin 1; Western European (Windows)
CP_WIN_1253 = 1253;//ANSI Greek; Greek (Windows)
CP_WIN_1254 = 1254;//ANSI Turkish; Turkish (Windows)
CP_WIN_1255 = 1255;//ANSI Hebrew; Hebrew (Windows)
CP_WIN_1256 = 1256;//ANSI Arabic; Arabic (Windows)
CP_WIN_1257 = 1257;//ANSI Baltic; Baltic (Windows)
CP_WIN_1258 = 1258;//ANSI/OEM Vietnamese; Vietnamese (Windows)
CP_ISO_8859_1 = 28591;//ISO 8859-1 Latin 1; Western European (ISO)
CP_ISO_8859_2 = 28592;//ISO 8859-2 Central European; Central European (ISO)
CP_ISO_8859_3 = 28593;//ISO 8859-3 Latin 3
CP_ISO_8859_4 = 28594;//ISO 8859-4 Baltic
CP_ISO_8859_5 = 28595;//ISO 8859-5 Cyrillic
CP_ISO_8859_6 = 28596;//ISO 8859-6 Arabic
CP_ISO_8859_7 = 28597;//ISO 8859-7 Greek
CP_ISO_8859_8 = 28598;//ISO 8859-8 Hebrew; Hebrew (ISO-Visual)
CP_ISO_8859_9 = 28599;//ISO 8859-9 Turkish
CP_ISO_8859_13 = 28603;//ISO 8859-13 Estonian
CP_ISO_8859_15 = 28605;//ISO 8859-15 Latin 9
CP_ISO_2022 = 50220;//ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS)
CP_EUC_JP = 51932;//ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS)
CP_KOI8_R = 20866;//Russian (KOI8-R); Cyrillic (KOI8-R)
CP_KOI8_U = 21866;//Ukrainian (KOI8-U); Cyrillic (KOI8-U)
CP_IBM_437 = 437;//OEM United States
CP_IBM_850 = 850;//OEM Multilingual Latin 1; Western European (DOS)
CP_IBM_852 = 852;//OEM Latin 2; Central European (DOS)
CP_IBM_866 = 866;//OEM Russian; Cyrillic (DOS)
CP_WIN_874 = 874;//ANSI/OEM Thai (ISO 8859-11); Thai (Windows)
CP_IBM_932 = 932;//ANSI/OEM Japanese; Japanese (Shift-JIS)
CP_US_ASCII = 20127;//US-ASCII (7-bit)
//multi-byte
CP_UNICODE = 1200;//Unicode UTF-16, little endian byte order
CP_UNICODE_BE = 1201;//Unicode UTF-16, big endian byte order
CP_UTF7 = 65000;//Unicode (UTF-7)
CP_UTF8 = 65001;//Unicode (UTF-8)
{$IFDEF O_DELPHI_2009_UP}
type
TEncodingBuffer = TBytes;
const
TEncodingBuffer_FirstElement = 0;
{$ELSE}
type
TEncodingBuffer = AnsiString;
const
TEncodingBuffer_FirstElement = 1;
type
TEncoding = class(TObject)
public
//functions that convert strings to buffers and vice versa
function BufferToString(const aBytes: TEncodingBuffer): OWideString; overload;
procedure BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString); overload; virtual; abstract;//faster in D7 and FPC than "function BufferToString"
function StringToBuffer(const S: OWideString): TEncodingBuffer; overload;
procedure StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer); overload; virtual; abstract;//faster in D7 and FPC than "function StringToBuffer"
public
//functions that get information about current encoding object
class function IsSingleByte: Boolean; virtual; abstract;
class function IsStandardEncoding(aEncoding: TEncoding): Boolean;
function EncodingName: OWideString; virtual; abstract;
function EncodingAlias: OWideString;
function EncodingCodePage: Cardinal;
public
//class functions for finding correct encoding from BOM, code page, alias etc.
class function GetEncodingFromBOM(const aBuffer: TEncodingBuffer; var outEncoding: TEncoding): Integer; overload;
class function GetEncodingFromBOM(const aBuffer: TEncodingBuffer; var outEncoding: TEncoding;
aDefaultEncoding: TEncoding): Integer; overload;
function GetBOM: TEncodingBuffer; virtual; abstract;
class function EncodingFromCodePage(aCodePage: Integer): TEncoding;
class function EncodingFromAlias(const aAlias: OWideString; var outEncoding: TEncoding): Boolean;
class function AliasToCodePage(const aAlias: OWideString): Cardinal;
class function CodePageToAlias(const aCodePage: Cardinal): OWideString;
public
//retrieve standard encodings
class function Default: TEncoding;
class function Unicode: TEncoding;
class function UTF8: TEncoding;
class function ANSI: TEncoding;
class function ASCII: TEncoding;
class function OWideStringEncoding: TEncoding;
public
destructor Destroy; override;
end;
TUnicodeEncoding = class(TEncoding)
public
function GetBOM: TEncodingBuffer; override;
procedure BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString); override;
procedure StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer); override;
class function IsSingleByte: Boolean; override;
function EncodingName: OWideString; override;
end;
TUTF8Encoding = class(TEncoding)
public
function GetBOM: TEncodingBuffer; override;
procedure BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString); override;
procedure StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer); override;
class function IsSingleByte: Boolean; override;
function EncodingName: OWideString; override;
end;
TMBCSEncoding = class(TEncoding)
private
fCodePage: Cardinal;
public
constructor Create(aCodePage: Cardinal);
public
function GetBOM: TEncodingBuffer; override;
procedure BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString); override;
procedure StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer); override;
class function IsSingleByte: Boolean; override;
function EncodingName: OWideString; override;
public
property CodePage: Cardinal read fCodePage;
end;
{$ENDIF O_DELPHI_2009_UP}
{$IF (DEFINED(O_DELPHI_2009_UP))}
//Delphi 2009 to 2010
type
TEncodingHelper = class helper for TEncoding
public
function EncodingAlias: String;
function EncodingCodePage: Cardinal;
{$IF NOT DEFINED(O_DELPHI_XE_UP)}
function EncodingName: String;
{$IFEND}
{$IF NOT DEFINED(O_DELPHI_XE2_UP)}
class function ANSI: TEncoding;
{$IFEND}
class function OWideStringEncoding: TEncoding;
class function EncodingFromCodePage(aCodePage: Integer): TEncoding;
class function EncodingFromAlias(const aAlias: OWideString; var outEncoding: TEncoding): Boolean;
class function AliasToCodePage(const aAlias: OWideString): Cardinal;
class function CodePageToAlias(const aCodePage: Cardinal): OWideString;
class function GetEncodingFromBOM(const aBuffer: TEncodingBuffer; var outEncoding: TEncoding): Integer; overload;
class function GetEncodingFromBOM(const aBuffer: TEncodingBuffer; var outEncoding: TEncoding;
aDefaultEncoding: TEncoding): Integer; overload;
function GetBOM: TEncodingBuffer;
end;
{$IF NOT DEFINED(O_DELPHI_XE2_UP)}
TMBCSEncodingHelper = class helper for TMBCSEncoding
public
function GetCodePage: Cardinal;
property CodePage: Cardinal read GetCodePage;
end;
{$IFEND}
{$IFEND}
implementation
{$IFNDEF O_DELPHI_2009_UP}
{$IF DEFINED(MSWINDOWS)}
uses Windows;
{$ELSEIF DEFINED(FPC)}
uses LConvEncoding;
{$IFEND}
{$ENDIF NOT O_DELPHI_2009_UP}
type
TCodePage = record
CodePage: Word;
CPAlias: OWideString;
end;
TCodePages = array[0..34] of TCodePage;
const
CodePages: TCodePages = (
(CodePage: CP_WIN_1250; CPAlias: 'windows-1250'),
(CodePage: CP_WIN_1251; CPAlias: 'windows-1251'),
(CodePage: CP_WIN_1252; CPAlias: 'windows-1252'),
(CodePage: CP_WIN_1253; CPAlias: 'windows-1253'),
(CodePage: CP_WIN_1254; CPAlias: 'windows-1254'),
(CodePage: CP_WIN_1255; CPAlias: 'windows-1255'),
(CodePage: CP_WIN_1256; CPAlias: 'windows-1256'),
(CodePage: CP_WIN_1257; CPAlias: 'windows-1257'),
(CodePage: CP_WIN_1258; CPAlias: 'windows-1258'),
(CodePage: CP_ISO_8859_1; CPAlias: 'iso-8859-1'),
(CodePage: CP_ISO_8859_2; CPAlias: 'iso-8859-2'),
(CodePage: CP_ISO_8859_3; CPAlias: 'iso-8859-3'),
(CodePage: CP_ISO_8859_4; CPAlias: 'iso-8859-4'),
(CodePage: CP_ISO_8859_5; CPAlias: 'iso-8859-5'),
(CodePage: CP_ISO_8859_6; CPAlias: 'iso-8859-6'),
(CodePage: CP_ISO_8859_7; CPAlias: 'iso-8859-7'),
(CodePage: CP_ISO_8859_8; CPAlias: 'iso-8859-8'),
(CodePage: CP_ISO_8859_9; CPAlias: 'iso-8859-9'),
(CodePage: CP_ISO_8859_13; CPAlias: 'iso-8859-13'),
(CodePage: CP_ISO_8859_15; CPAlias: 'iso-8859-15'),
(CodePage: CP_ISO_2022; CPAlias: 'iso-2022-jp'),
(CodePage: CP_EUC_JP; CPAlias: 'euc-jp'),
(CodePage: CP_KOI8_R; CPAlias: 'koi8-r'),
(CodePage: CP_KOI8_U; CPAlias: 'koi8-u'),
(CodePage: CP_IBM_437; CPAlias: 'ibm437'),
(CodePage: CP_IBM_850; CPAlias: 'ibm850'),
(CodePage: CP_IBM_852; CPAlias: 'ibm852'),
(CodePage: CP_IBM_866; CPAlias: 'cp866'),
(CodePage: CP_WIN_874; CPAlias: 'windows-874'),
(CodePage: CP_IBM_932; CPAlias: 'shift-jis'),
(CodePage: CP_US_ASCII; CPAlias: 'us-ascii'),
(CodePage: CP_UNICODE; CPAlias: 'utf-16'),
(CodePage: CP_UNICODE_BE; CPAlias: 'utf-16be'),
(CodePage: CP_UTF7; CPAlias: 'utf-7'),
(CodePage: CP_UTF8; CPAlias: 'utf-8')
);
{$IFNDEF O_DELPHI_2009_UP}
var
fxANSIEncoding: TEncoding = nil;
fxUTF8Encoding: TEncoding = nil;
fxUnicodeEncoding: TEncoding = nil;
fxASCIIEncoding: TEncoding = nil;
{$IF DEFINED(MSWINDOWS) AND NOT DEFINED(O_DELPHI_2009_UP)}
type
_cpinfoExW = record
MaxCharSize: UINT; { max length (bytes) of a char }
DefaultChar: array[0..MAX_DEFAULTCHAR - 1] of Byte; { default character }
LeadByte: array[0..MAX_LEADBYTES - 1] of Byte; { lead byte ranges }
UnicodeDefaultChar: WideChar;
Codepage: UINT;
CodePageName: array[0..MAX_PATH -1] of WideChar;
end;
TCPInfoExW = _cpinfoExW;
function GetCPInfoExW(CodePage: UINT; dwFlags: DWORD; var lpCPInfoEx: TCPInfoExW): BOOL; stdcall; external kernel32 name 'GetCPInfoExW';
{$IFEND}
{$IF DEFINED(FPC) AND NOT DEFINED(MSWINDOWS)}
function UTF8ToCodePage(const S: OWideString; aCodePage: Cardinal): AnsiString;
begin
case aCodePage of
CP_IBM_437, CP_US_ASCII: Result := UTF8ToCP437(S);
CP_IBM_850: Result := UTF8ToCP850(S);
CP_IBM_852: Result := UTF8ToCP852(S);
CP_IBM_866: Result := UTF8ToCP866(S);
CP_WIN_874: Result := UTF8ToCP874(S);
CP_IBM_932: Result := UTF8ToCP932(S);
CP_WIN_1250: Result := UTF8ToCP1250(S);
CP_WIN_1251: Result := UTF8ToCP1251(S);
CP_WIN_1252: Result := UTF8ToCP1252(S);
CP_WIN_1253: Result := UTF8ToCP1253(S);
CP_WIN_1254: Result := UTF8ToCP1254(S);
CP_WIN_1255: Result := UTF8ToCP1255(S);
CP_WIN_1256: Result := UTF8ToCP1256(S);
CP_WIN_1257: Result := UTF8ToCP1257(S);
CP_WIN_1258: Result := UTF8ToCP1258(S);
CP_ISO_8859_1: Result := UTF8ToISO_8859_1(S);
CP_ISO_8859_2: Result := UTF8ToISO_8859_2(S);
CP_KOI8_R, CP_KOI8_U: Result := UTF8ToKOI8(S);
else
Result := S;//Encoding not supported by lazarus
end;
end;
function CodePageToUTF8(const S: AnsiString; aCodePage: Cardinal): OWideString;
begin
case aCodePage of
CP_IBM_437, CP_US_ASCII: Result := CP437ToUTF8(S);
CP_IBM_850: Result := CP850ToUTF8(S);
CP_IBM_852: Result := CP852ToUTF8(S);
CP_IBM_866: Result := CP866ToUTF8(S);
CP_WIN_874: Result := CP874ToUTF8(S);
CP_IBM_932: Result := CP932ToUTF8(S);
CP_WIN_1250: Result := CP1250ToUTF8(S);
CP_WIN_1251: Result := CP1251ToUTF8(S);
CP_WIN_1252: Result := CP1252ToUTF8(S);
CP_WIN_1253: Result := CP1253ToUTF8(S);
CP_WIN_1254: Result := CP1254ToUTF8(S);
CP_WIN_1255: Result := CP1255ToUTF8(S);
CP_WIN_1256: Result := CP1256ToUTF8(S);
CP_WIN_1257: Result := CP1257ToUTF8(S);
CP_WIN_1258: Result := CP1258ToUTF8(S);
CP_ISO_8859_1: Result := ISO_8859_1ToUTF8(S);
CP_ISO_8859_2: Result := ISO_8859_2ToUTF8(S);
CP_KOI8_R, CP_KOI8_U: Result := KOI8ToUTF8(S);
else
Result := S;//Encoding not supported by lazarus
end;
end;
{$IFEND}
{ TEncoding }
class function TEncoding.ANSI: TEncoding;
begin
if not Assigned(fxANSIEncoding) then begin
{$IFDEF MSWINDOWS}
fxANSIEncoding := TMBCSEncoding.Create(GetACP);
{$ELSE}
fxANSIEncoding := TMBCSEncoding.Create(CP_WIN_1252);
{$ENDIF}
end;
Result := fxANSIEncoding;
end;
class function TEncoding.ASCII: TEncoding;
{$IFDEF MSWINDOWS}
var
xCPInfo: TCPInfo;
{$ENDIF}
begin
if not Assigned(fxASCIIEncoding) then begin
{$IFDEF MSWINDOWS}
if GetCPInfo(CP_US_ASCII, {%H-}xCPInfo) then
fxASCIIEncoding := TMBCSEncoding.Create(CP_US_ASCII)
else
fxASCIIEncoding := TMBCSEncoding.Create(CP_IBM_437);
{$ELSE}
fxASCIIEncoding := TMBCSEncoding.Create(CP_IBM_437);
{$ENDIF}
end;
Result := fxASCIIEncoding;
end;
class function TEncoding.Default: TEncoding;
begin
{$IFDEF MSWINDOWS}
Result := ANSI;
{$ELSE}
Result := UTF8;
{$ENDIF}
end;
class function TEncoding.GetEncodingFromBOM(const aBuffer: TEncodingBuffer;
var outEncoding: TEncoding): Integer;
begin
Result := GetEncodingFromBOM(aBuffer, outEncoding, Default);
end;
class function TEncoding.GetEncodingFromBOM(const aBuffer: TEncodingBuffer;
var outEncoding: TEncoding; aDefaultEncoding: TEncoding): Integer;
begin
if (Length(aBuffer) >= 3) and
(aBuffer[TEncodingBuffer_FirstElement+0] = #$EF) and
(aBuffer[TEncodingBuffer_FirstElement+1] = #$BB) and
(aBuffer[TEncodingBuffer_FirstElement+2] = #$BF)
then begin
outEncoding := UTF8;
Result := 3;
end else
if (Length(aBuffer) >= 2) and
(aBuffer[TEncodingBuffer_FirstElement+0] = #$FF) and
(aBuffer[TEncodingBuffer_FirstElement+1] = #$FE)
then begin
outEncoding := Unicode;
Result := 2;
end else begin
outEncoding := aDefaultEncoding;
Result := 0;
end;
end;
function TEncoding.BufferToString(const aBytes: TEncodingBuffer): OWideString;
begin
BufferToString(aBytes, {%H-}Result);
end;
function TEncoding.StringToBuffer(const S: OWideString): TEncodingBuffer;
begin
StringToBuffer(S, {%H-}Result);
end;
class function TEncoding.Unicode: TEncoding;
begin
if not Assigned(fxUnicodeEncoding) then
fxUnicodeEncoding := TUnicodeEncoding.Create;
Result := fxUnicodeEncoding;
end;
class function TEncoding.UTF8: TEncoding;
begin
if not Assigned(fxUTF8Encoding) then
fxUTF8Encoding := TUTF8Encoding.Create;
Result := fxUTF8Encoding;
end;
class function TEncoding.IsStandardEncoding(aEncoding: TEncoding): Boolean;
begin
Result :=
Assigned(aEncoding) and (
(aEncoding = fxANSIEncoding) or
(aEncoding = fxUTF8Encoding) or
(aEncoding = fxUnicodeEncoding) or
(aEncoding = fxASCIIEncoding));
end;
class function TEncoding.OWideStringEncoding: TEncoding;
begin
{$IFDEF FPC}
Result := UTF8;
{$ELSE}
Result := Unicode;
{$ENDIF}
end;
function TEncoding.EncodingAlias: OWideString;
var
xCodePage, I: Integer;
begin
xCodePage := EncodingCodePage;
for I := Low(CodePages) to High(CodePages) do
if CodePages[I].CodePage = xCodePage then begin
Result := CodePages[I].CPAlias;
Exit;
end;
Result := IntToStr(xCodePage);
end;
function TEncoding.EncodingCodePage: Cardinal;
begin
if Self is TMBCSEncoding then
Result := TMBCSEncoding(Self).CodePage
else if Self is TUnicodeEncoding then
Result := CP_UNICODE
else if Self is TUTF8Encoding then
Result := CP_UTF8
else
Result := 0;
end;
destructor TEncoding.Destroy;
begin
if (Self = fxANSIEncoding) then
fxANSIEncoding := nil
else if (Self = fxUTF8Encoding) then
fxUTF8Encoding := nil
else if (Self = fxUnicodeEncoding) then
fxUnicodeEncoding := nil
else if (Self = fxASCIIEncoding) then
fxASCIIEncoding := nil;
inherited;
end;
{ TMBCSEncoding }
constructor TMBCSEncoding.Create(aCodePage: Cardinal);
begin
inherited Create;
fCodePage := aCodePage;
end;
function TMBCSEncoding.EncodingName: OWideString;
{$IFDEF MSWINDOWS}
var
xCPInfo: TCPInfoExW;
begin
if GetCPInfoExW(fCodePage, 0, xCPInfo{%H-}) then
Result := xCPInfo.CodePageName
else
Result := IntToStr(fCodePage);
end;
{$ELSE}
begin
Result := IntToStr(fCodePage);
end;
{$ENDIF}
procedure TMBCSEncoding.StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer);
{$IFDEF MSWINDOWS}
var
xLength: integer;
{$IFDEF FPC}
xUS: UnicodeString;
{$ENDIF}
{$ENDIF}
begin
if S = '' then begin
outBuffer := '';
Exit;
end;
{$IFDEF MSWINDOWS}
{$IFDEF FPC}
xUS := UTF8Decode(S);
xLength := WideCharToMultiByte(fCodePage,
WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
PWideChar(@xUS[1]), -1, nil, 0, nil, nil);
SetLength(outBuffer, xLength-1);
if xLength > 1 then
WideCharToMultiByte(codePage,
WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
PWideChar(@xUS[1]), -1, @outBuffer[1], xLength-1, nil, nil);
{$ELSE}
xLength := WideCharToMultiByte(fCodePage,
WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
PWideChar(@S[1]), -1, nil, 0, nil, nil);
SetLength(outBuffer, xLength-1);
if xLength > 1 then
WideCharToMultiByte(codePage,
WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
PWideChar(@S[1]), -1, @outBuffer[1], xLength-1, nil, nil);
{$ENDIF}
{$ELSE}
outBuffer := UTF8ToCodePage(S, fCodePage);
{$ENDIF}
end;
function TMBCSEncoding.GetBOM: TEncodingBuffer;
begin
Result := '';
end;
procedure TMBCSEncoding.BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString);
{$IFDEF MSWINDOWS}
var
xLength: integer;
{$IFDEF FPC}
xUS: UnicodeString;
{$ENDIF}
{$ENDIF}
begin
if aBytes = '' then begin
outString := '';
Exit;
end;
{$IFDEF MSWINDOWS}
{$IFDEF FPC}
xLength := MultiByteToWideChar(fCodePage, MB_PRECOMPOSED, PAnsiChar(@aBytes[1]), -1, nil, 0);
SetLength(xUS, xLength-1);
if xLength > 1 then
MultiByteToWideChar(CodePage, MB_PRECOMPOSED, PAnsiChar(@aBytes[1]), -1, PWideChar(@xUS[1]), xLength-1);
outString := UTF8Encode(xUS);
{$ELSE}
xLength := MultiByteToWideChar(fCodePage, MB_PRECOMPOSED, PAnsiChar(@aBytes[1]), -1, nil, 0);
SetLength(outString, xLength-1);
if xLength > 1 then
MultiByteToWideChar(CodePage, MB_PRECOMPOSED, PAnsiChar(@aBytes[1]), -1, PWideChar(@outString[1]), xLength-1);
{$ENDIF}
{$ELSE}
outString := CodePageToUTF8(aBytes, fCodePage);
{$ENDIF}
end;
class function TMBCSEncoding.IsSingleByte: Boolean;
begin
Result := True;
end;
{ TUnicodeEncoding }
function TUnicodeEncoding.EncodingName: OWideString;
begin
{$IFDEF MSWINDOWS}
Result := '1200 (Unicode)';
{$ELSE}
Result := 'Unicode (UTF-16LE)';
{$ENDIF}
end;
procedure TUnicodeEncoding.StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer);
var
xCharCount: Integer;
{$IFDEF FPC}
xUS: UnicodeString;
{$ENDIF}
begin
{$IFDEF FPC}
//FPC
xUS := UTF8Decode(S);
xCharCount := Length(xUS);
SetLength(outBuffer, xCharCount*2);
if xCharCount > 0 then begin
Move(xUS[1], outBuffer[1], xCharCount*2);
end;
{$ELSE}
//DELPHI
xCharCount := Length(S);
SetLength(outBuffer, xCharCount*2);
if xCharCount > 0 then begin
Move(S[1], outBuffer[1], xCharCount*2);
end;
{$ENDIF}
end;
function TUnicodeEncoding.GetBOM: TEncodingBuffer;
begin
SetLength(Result, 2);
Result[TEncodingBuffer_FirstElement+0] := #$FF;
Result[TEncodingBuffer_FirstElement+1] := #$FE;
end;
procedure TUnicodeEncoding.BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString);
var
xByteCount: Integer;
{$IFDEF FPC}
xUS: UnicodeString;
{$ENDIF}
begin
xByteCount := Length(aBytes);
if xByteCount = 0 then begin
outString := '';
Exit;
end;
{$IFDEF FPC}
//FPC
SetLength(xUS, xByteCount div 2);
Move(aBytes[1], xUS[1], xByteCount);
outString := UTF8Encode(xUS);
{$ELSE}
//DELPHI
SetLength(outString, xByteCount div 2);
Move(aBytes[1], outString[1], xByteCount);
{$ENDIF}
end;
class function TUnicodeEncoding.IsSingleByte: Boolean;
begin
Result := False;
end;
{ TUTF8Encoding }
function TUTF8Encoding.EncodingName: OWideString;
{$IFDEF MSWINDOWS}
var
xCPInfo: TCPInfoExW;
begin
if GetCPInfoExW(CP_UTF8, 0, xCPInfo{%H-}) then
Result := xCPInfo.CodePageName
else
Result := '65001 (UTF-8)';
end;
{$ELSE}
begin
Result := 'UTF-8';
end;
{$ENDIF}
procedure TUTF8Encoding.StringToBuffer(const S: OWideString; var outBuffer: TEncodingBuffer);
begin
{$IFDEF FPC}
outBuffer := S;
{$ELSE}
//DELPHI
outBuffer := UTF8Encode(S);
{$ENDIF}
end;
function TUTF8Encoding.GetBOM: TEncodingBuffer;
begin
SetLength(Result, 3);
Result[TEncodingBuffer_FirstElement+0] := #$EF;
Result[TEncodingBuffer_FirstElement+1] := #$BB;
Result[TEncodingBuffer_FirstElement+2] := #$BF;
end;
procedure TUTF8Encoding.BufferToString(const aBytes: TEncodingBuffer; var outString: OWideString);
begin
{$IFDEF FPC}
outString := aBytes;
{$ELSE}
//DELPHI
outString := UTF8Decode(aBytes);
{$ENDIF}
end;
class function TUTF8Encoding.IsSingleByte: Boolean;
begin
Result := False;
end;
{$ENDIF O_DELPHI_2009}
{$IF (DEFINED(O_DELPHI_2009_UP))}
class function TEncodingHelper.EncodingFromCodePage(aCodePage: Integer): TEncoding;
{$ELSE}
class function TEncoding.EncodingFromCodePage(aCodePage: Integer): TEncoding;
{$IFEND}
begin
case aCodePage of
CP_UNICODE: Result := Unicode;
{$IFDEF O_DELPHI_2009_UP}
CP_UNICODE_BE: Result := BigEndianUnicode;
CP_UTF7: Result := UTF7;
{$ENDIF}
CP_UTF8: Result := UTF8;
else
Result := TMBCSEncoding.Create(aCodePage);
end;
end;
{$IF (DEFINED(O_DELPHI_2009_UP))}
class function TEncodingHelper.EncodingFromAlias(const aAlias: OWideString; var outEncoding: TEncoding): Boolean;
{$ELSE}
class function TEncoding.EncodingFromAlias(const aAlias: OWideString; var outEncoding: TEncoding): Boolean;
{$IFEND}
var
xCP: Cardinal;
begin
xCP := AliasToCodePage(aAlias);
Result := (xCP <> 0);
if Result then
outEncoding := TEncoding.EncodingFromCodePage(xCP)
else
outEncoding := nil;
end;
{$IF (DEFINED(O_DELPHI_2009_UP))}
class function TEncodingHelper.AliasToCodePage(const aAlias: OWideString): Cardinal;
{$ELSE}
class function TEncoding.AliasToCodePage(const aAlias: OWideString): Cardinal;
{$IFEND}
var
I: Integer;
begin
for I := Low(CodePages) to High(CodePages) do
if SameText(aAlias, CodePages[I].CPAlias) then begin
Result := CodePages[I].CodePage;
Exit;
end;
Result := 0;
end;
{$IF (DEFINED(O_DELPHI_2009_UP))}
class function TEncodingHelper.CodePageToAlias(const aCodePage: Cardinal): OWideString;
{$ELSE}
class function TEncoding.CodePageToAlias(const aCodePage: Cardinal): OWideString;
{$IFEND}
var
I: Integer;
begin
for I := Low(CodePages) to High(CodePages) do
if aCodePage = CodePages[I].CodePage then begin
Result := CodePages[I].CPAlias;
Exit;
end;
Result := '';
end;
{$IF NOT DEFINED(FPC) AND (DEFINED(O_DELPHI_2009_UP))}
{ TEncodingHelper }
{$IF NOT DEFINED(O_DELPHI_XE_UP)}
function TEncodingHelper.EncodingName: String;
begin
if Self is TMBCSEncoding then
Result := IntToStr(TMBCSEncoding(Self).GetCodePage)
else if Self is TUnicodeEncoding then
Result := IntToStr(CP_UNICODE)
else if Self is TBigEndianUnicodeEncoding then
Result := IntToStr(CP_UNICODE_BE)
else
Result := '';
end;
{$IFEND}
{$IF NOT DEFINED(O_DELPHI_XE2_UP)}
class function TEncodingHelper.ANSI: TEncoding;
begin
{$IFDEF MSWINDOWS}
Result := Default;
{$ELSE}
Result := TMBCSEncoding.Create(CP_WIN_1252);
{$ENDIF}
end;
{$IFEND}
class function TEncodingHelper.OWideStringEncoding: TEncoding;
begin
{$IFDEF FPC}
Result := UTF8;
{$ELSE}
Result := Unicode;
{$ENDIF}
end;
function TEncodingHelper.EncodingAlias: String;
var
xCodePage, I: Integer;
begin
xCodePage := EncodingCodePage;
for I := Low(CodePages) to High(CodePages) do
if CodePages[I].CodePage = xCodePage then begin
Result := CodePages[I].CPAlias;
Exit;
end;
Result := IntToStr(xCodePage);
end;
class function TEncodingHelper.GetEncodingFromBOM(const aBuffer: TEncodingBuffer; var outEncoding: TEncoding): Integer;
begin
outEncoding := nil;//must be here: otherwise if outEncoding<>nil, GetBufferEncoding would check only towards outEncoding
Result := Self.GetBufferEncoding(aBuffer, outEncoding);
end;
class function TEncodingHelper.GetEncodingFromBOM(const aBuffer: TEncodingBuffer; var outEncoding: TEncoding;
aDefaultEncoding: TEncoding): Integer;
begin
outEncoding := nil;//must be here: otherwise if outEncoding<>nil, GetBufferEncoding would check only towards outEncoding
{$IFDEF O_DELPHI_XE_UP}
Result := Self.GetBufferEncoding(aBuffer, outEncoding, aDefaultEncoding);
{$ELSE}
Result := Self.GetBufferEncoding(aBuffer, outEncoding);
if Result = 0 then//BOM not found
outEncoding := aDefaultEncoding;
{$ENDIF}
end;
function TEncodingHelper.GetBOM: TEncodingBuffer;
begin
Result := GetPreamble;
end;
function TEncodingHelper.EncodingCodePage: Cardinal;
begin
if Self is TMBCSEncoding then
Result := TMBCSEncoding(Self).CodePage
else if Self is TUnicodeEncoding then
Result := CP_UNICODE
else if Self is TBigEndianUnicodeEncoding then
Result := CP_UNICODE_BE
else
Result := 0;
end;
{$IF NOT DEFINED(O_DELPHI_XE2_UP)}
{ TMBCSEncodingHelper }
function TMBCSEncodingHelper.GetCodePage: Cardinal;
begin
Result := Self.FCodePage;
end;
{$IFEND}
{$IFEND}
{$IFNDEF O_DELPHI_2009_UP}
initialization
finalization
fxANSIEncoding.Free;
fxUTF8Encoding.Free;
fxUnicodeEncoding.Free;
fxASCIIEncoding.Free;
{$ENDIF O_DELPHI_2009_UP}
end.