2
*****************************************************************************
4
* This file is part of the Lazarus Component Library (LCL) *
6
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
7
* for details about the copyright. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
13
*****************************************************************************
21
{$IFNDEF DisableIconv}
22
{$IFDEF UNIX}{$IF not defined(VER2_2_0) and not defined(VER2_2_2)}{$DEFINE HasIconvEnc}{$ENDIF}{$ENDIF}
26
SysUtils, Classes, dos, LCLProc
27
{$IFDEF HasIconvEnc},iconvenc{$ENDIF};
29
EncodingUTF8 = 'utf8';
30
EncodingAnsi = 'ansi';
31
EncodingUTF8BOM = 'utf8bom'; // UTF-8 with byte order mark
32
EncodingUCS2LE = 'ucs2le'; // UCS 2 byte little endian
33
EncodingUCS2BE = 'ucs2be'; // UCS 2 byte big endian
35
function GuessEncoding(const s: string): string;
37
function ConvertEncoding(const s, FromEncoding, ToEncoding: string): string;
39
function GetDefaultTextEncoding: string;
40
function NormalizeEncoding(const Encoding: string): string;
43
TConvertEncodingFunction = function(const s: string): string;
44
TCharToUTF8Table = array[char] of PChar;
45
TUnicodeToCharID = function(Unicode: cardinal): integer;
47
ConvertAnsiToUTF8: TConvertEncodingFunction = nil;
48
ConvertUTF8ToAnsi: TConvertEncodingFunction = nil;
50
function UTF8BOMToUTF8(const s: string): string; // UTF8 with BOM
51
function ISO_8859_1ToUTF8(const s: string): string; // central europe
52
function ISO_8859_2ToUTF8(const s: string): string; // eastern europe
53
function CP1250ToUTF8(const s: string): string; // central europe
54
function CP1251ToUTF8(const s: string): string; // cyrillic
55
function CP1252ToUTF8(const s: string): string; // latin 1
56
function CP1253ToUTF8(const s: string): string; // greek
57
function CP1254ToUTF8(const s: string): string; // turkish
58
function CP1255ToUTF8(const s: string): string; // hebrew
59
function CP1256ToUTF8(const s: string): string; // arabic
60
function CP1257ToUTF8(const s: string): string; // baltic
61
function CP1258ToUTF8(const s: string): string; // vietnam
62
function CP437ToUTF8(const s: string): string; // DOS central europe
63
function CP850ToUTF8(const s: string): string; // DOS western europe
64
function CP866ToUTF8(const s: string): string; // DOS and Windows console's cyrillic
65
function CP874ToUTF8(const s: string): string; // thai
66
function KOI8ToUTF8(const s: string): string; // russian cyrillic
67
function SingleByteToUTF8(const s: string;
68
const Table: TCharToUTF8Table): string;
69
function UCS2LEToUTF8(const s: string): string; // UCS2-LE 2byte little endian
70
function UCS2BEToUTF8(const s: string): string; // UCS2-BE 2byte big endian
72
function UTF8ToUTF8BOM(const s: string): string; // UTF8 with BOM
73
function UTF8ToISO_8859_1(const s: string): string; // central europe
74
function UTF8ToISO_8859_2(const s: string): string; // eastern europe
75
function UTF8ToCP1250(const s: string): string; // central europe
76
function UTF8ToCP1251(const s: string): string; // cyrillic
77
function UTF8ToCP1252(const s: string): string; // latin 1
78
function UTF8ToCP1253(const s: string): string; // greek
79
function UTF8ToCP1254(const s: string): string; // turkish
80
function UTF8ToCP1255(const s: string): string; // hebrew
81
function UTF8ToCP1256(const s: string): string; // arabic
82
function UTF8ToCP1257(const s: string): string; // baltic
83
function UTF8ToCP1258(const s: string): string; // vietnam
84
function UTF8ToCP437(const s: string): string; // DOS central europe
85
function UTF8ToCP850(const s: string): string; // DOS western europe
86
function UTF8ToCP866(const s: string): string; // DOS and Windows console's cyrillic
87
function UTF8ToCP874(const s: string): string; // thai
88
function UTF8ToKOI8(const s: string): string; // russian cyrillic
89
function UTF8ToSingleByte(const s: string;
90
const UTF8CharConvFunc: TUnicodeToCharID): string;
91
function UTF8ToUCS2LE(const s: string): string; // UCS2-LE 2byte little endian
92
function UTF8ToUCS2BE(const s: string): string; // UCS2-BE 2byte big endian
96
function CP936ToUTF8(const s: string): string; // Chinese
97
function CP950ToUTF8(const s: string): string; // Chinese Complex
98
function CP949ToUTF8(const s: string): string; // korea
99
function CP932ToUTF8(const s: string): string; // japanese
101
function SingleByteToUTF8Ex(const s: string; CodeP: integer): string;
103
function UTF8ToCP936(const s: string): string; // Chinese, essentially the same as GB 2312 and a predecessor to GB 18030
104
function UTF8ToCP950(const s: string): string; // Chinese Complex
105
function UTF8ToCP949(const s: string): string; // korea
106
function UTF8ToCP932(const s: string): string; // japanese
108
function UTF8ToSingleByteEx(const s: string;
109
const UTF8CharConvFunc: TUnicodeToCharID): string;
111
procedure GetSupportedEncodings(List: TStrings);
119
var EncodingValid: boolean = false;
120
DefaultTextEncoding: string = EncodingAnsi;
122
{$include include/asiancodepages.inc}
123
{$include include/asiancodepagefunctions.inc}
126
function GetWindowsEncoding: string;
130
// CP_UTF8 is missing in the windows unit of the Windows CE RTL
137
CP_UTF8: Result := EncodingUTF8;
139
Result:='cp'+IntToStr(GetACP);
144
function GetUnixEncoding:string;
149
Result:=EncodingAnsi;
151
lang := GetEnv('LC_ALL');
152
if Length(lang) = 0 then
154
lang := GetEnv('LC_MESSAGES');
155
if Length(lang) = 0 then
157
lang := GetEnv('LANG');
161
if (i>0) and (i<=length(Lang)) then
162
Result:=copy(Lang,i+1,length(Lang)-i);
167
function GetDefaultTextEncoding: string;
169
if EncodingValid then begin
170
Result:=DefaultTextEncoding;
175
Result:=GetWindowsEncoding;
178
Result:=EncodingUTF8;
180
Result:=GetUnixEncoding;
184
Result:=NormalizeEncoding(Result);
186
DefaultTextEncoding:=Result;
190
function NormalizeEncoding(const Encoding: string): string;
194
Result:=LowerCase(Encoding);
195
for i:=length(Result) downto 1 do
196
if Result[i]='-' then System.Delete(Result,i,1);
200
ArrayISO_8859_1ToUTF8: TCharToUTF8Table = (
459
ArrayISO_8859_2ToUTF8: TCharToUTF8Table = (
719
ArrayCP1250ToUTF8: TCharToUTF8Table = (
848
#226#130#172, // #128
850
#226#128#154, // #130
852
#226#128#158, // #132
853
#226#128#166, // #133
854
#226#128#160, // #134
855
#226#128#161, // #135
857
#226#128#176, // #137
859
#226#128#185, // #139
865
#226#128#152, // #145
866
#226#128#153, // #146
867
#226#128#156, // #147
868
#226#128#157, // #148
869
#226#128#162, // #149
870
#226#128#147, // #150
871
#226#128#148, // #151
873
#226#132#162, // #153
875
#226#128#186, // #155
978
ArrayCP1251ToUTF8: TCharToUTF8Table = (
1109
#226#128#154, // #130
1111
#226#128#158, // #132
1112
#226#128#166, // #133
1113
#226#128#160, // #134
1114
#226#128#161, // #135
1115
#226#130#172, // #136
1116
#226#128#176, // #137
1118
#226#128#185, // #139
1124
#226#128#152, // #145
1125
#226#128#153, // #146
1126
#226#128#156, // #147
1127
#226#128#157, // #148
1128
#226#128#162, // #149
1129
#226#128#147, // #150
1130
#226#128#148, // #151
1132
#226#132#162, // #153
1134
#226#128#186, // #155
1164
#226#132#150, // #185
1237
ArrayCP1252ToUTF8: TCharToUTF8Table = (
1366
#226#130#172, // #128
1368
#226#128#154, // #130
1370
#226#128#158, // #132
1371
#226#128#166, // #133
1372
#226#128#160, // #134
1373
#226#128#161, // #135
1375
#226#128#176, // #137
1377
#226#128#185, // #139
1383
#226#128#152, // #145
1384
#226#128#153, // #146
1385
#226#128#156, // #147
1386
#226#128#157, // #148
1387
#226#128#162, // #149
1388
#226#128#147, // #150
1389
#226#128#148, // #151
1391
#226#132#162, // #153
1393
#226#128#186, // #155
1496
ArrayCP1253ToUTF8: TCharToUTF8Table = (
1625
#226#130#172, // #128
1627
#226#128#154, // #130
1629
#226#128#158, // #132
1630
#226#128#166, // #133
1631
#226#128#160, // #134
1632
#226#128#161, // #135
1634
#226#128#176, // #137
1636
#226#128#185, // #139
1642
#226#128#152, // #145
1643
#226#128#153, // #146
1644
#226#128#156, // #147
1645
#226#128#157, // #148
1646
#226#128#162, // #149
1647
#226#128#147, // #150
1648
#226#128#148, // #151
1650
#226#132#162, // #153
1652
#226#128#186, // #155
1672
#226#128#149, // #175
1755
ArrayCP1254ToUTF8: TCharToUTF8Table = (
1884
#226#130#172, // #128
1886
#226#128#154, // #130
1888
#226#128#158, // #132
1889
#226#128#166, // #133
1890
#226#128#160, // #134
1891
#226#128#161, // #135
1893
#226#128#176, // #137
1895
#226#128#185, // #139
1901
#226#128#152, // #145
1902
#226#128#153, // #146
1903
#226#128#156, // #147
1904
#226#128#157, // #148
1905
#226#128#162, // #149
1906
#226#128#147, // #150
1907
#226#128#148, // #151
1909
#226#132#162, // #153
1911
#226#128#186, // #155
2014
ArrayCP1255ToUTF8: TCharToUTF8Table = (
2143
#226#130#172, // #128
2145
#226#128#154, // #130
2147
#226#128#158, // #132
2148
#226#128#166, // #133
2149
#226#128#160, // #134
2150
#226#128#161, // #135
2152
#226#128#176, // #137
2154
#226#128#185, // #139
2160
#226#128#152, // #145
2161
#226#128#153, // #146
2162
#226#128#156, // #147
2163
#226#128#157, // #148
2164
#226#128#162, // #149
2165
#226#128#147, // #150
2166
#226#128#148, // #151
2168
#226#132#162, // #153
2170
#226#128#186, // #155
2179
#226#130#170, // #164
2268
#226#128#142, // #253
2269
#226#128#143, // #254
2273
ArrayCP1256ToUTF8: TCharToUTF8Table = (
2402
#226#130#172, // #128
2404
#226#128#154, // #130
2406
#226#128#158, // #132
2407
#226#128#166, // #133
2408
#226#128#160, // #134
2409
#226#128#161, // #135
2411
#226#128#176, // #137
2413
#226#128#185, // #139
2419
#226#128#152, // #145
2420
#226#128#153, // #146
2421
#226#128#156, // #147
2422
#226#128#157, // #148
2423
#226#128#162, // #149
2424
#226#128#147, // #150
2425
#226#128#148, // #151
2427
#226#132#162, // #153
2429
#226#128#186, // #155
2431
#226#128#140, // #157
2432
#226#128#141, // #158
2527
#226#128#142, // #253
2528
#226#128#143, // #254
2532
ArrayCP1257ToUTF8: TCharToUTF8Table = (
2661
#226#130#172, // #128
2663
#226#128#154, // #130
2665
#226#128#158, // #132
2666
#226#128#166, // #133
2667
#226#128#160, // #134
2668
#226#128#161, // #135
2670
#226#128#176, // #137
2672
#226#128#185, // #139
2678
#226#128#152, // #145
2679
#226#128#153, // #146
2680
#226#128#156, // #147
2681
#226#128#157, // #148
2682
#226#128#162, // #149
2683
#226#128#147, // #150
2684
#226#128#148, // #151
2686
#226#132#162, // #153
2688
#226#128#186, // #155
2791
ArrayCP1258ToUTF8: TCharToUTF8Table = (
2920
#226#130#172, // #128
2922
#226#128#154, // #130
2924
#226#128#158, // #132
2925
#226#128#166, // #133
2926
#226#128#160, // #134
2927
#226#128#161, // #135
2929
#226#128#176, // #137
2931
#226#128#185, // #139
2937
#226#128#152, // #145
2938
#226#128#153, // #146
2939
#226#128#156, // #147
2940
#226#128#157, // #148
2941
#226#128#162, // #149
2942
#226#128#147, // #150
2943
#226#128#148, // #151
2945
#226#132#162, // #153
2947
#226#128#186, // #155
3046
#226#130#171, // #254
3050
ArrayCP437ToUTF8 : TCharToUTF8Table = (
3209
#226#130#167, // #158
3220
#226#140#144, // #169
3227
#226#150#145, // #176
3228
#226#150#146, // #177
3229
#226#150#147, // #178
3230
#226#148#130, // #179
3231
#226#148#164, // #180
3232
#226#149#161, // #181
3233
#226#149#162, // #182
3234
#226#149#150, // #183
3235
#226#149#149, // #184
3236
#226#149#163, // #185
3237
#226#149#145, // #186
3238
#226#149#151, // #187
3239
#226#149#157, // #188
3240
#226#149#156, // #189
3241
#226#149#155, // #190
3242
#226#148#144, // #191
3243
#226#148#148, // #192
3244
#226#148#180, // #193
3245
#226#148#172, // #194
3246
#226#148#156, // #195
3247
#226#148#128, // #196
3248
#226#148#188, // #197
3249
#226#149#158, // #198
3250
#226#149#159, // #199
3251
#226#149#154, // #200
3252
#226#149#148, // #201
3253
#226#149#169, // #202
3254
#226#149#166, // #203
3255
#226#149#160, // #204
3256
#226#149#144, // #205
3257
#226#149#172, // #206
3258
#226#149#167, // #207
3259
#226#149#168, // #208
3260
#226#149#164, // #209
3261
#226#149#165, // #210
3262
#226#149#153, // #211
3263
#226#149#152, // #212
3264
#226#149#146, // #213
3265
#226#149#147, // #214
3266
#226#149#171, // #215
3267
#226#149#170, // #216
3268
#226#148#152, // #217
3269
#226#148#140, // #218
3270
#226#150#136, // #219
3271
#226#150#132, // #220
3272
#226#150#140, // #221
3273
#226#150#144, // #222
3274
#226#150#128, // #223
3287
#226#136#158, // #236
3290
#226#136#169, // #239
3291
#226#137#161, // #240
3293
#226#137#165, // #242
3294
#226#137#164, // #243
3295
#226#140#160, // #244
3296
#226#140#161, // #245
3298
#226#137#136, // #247
3300
#226#136#153, // #249
3302
#226#136#154, // #251
3303
#226#129#191, // #252
3305
#226#150#160, // #254
3309
ArrayCP850ToUTF8 : TCharToUTF8Table = (
3486
#226#150#145, // #176
3487
#226#150#146, // #177
3488
#226#150#147, // #178
3489
#226#148#130, // #179
3490
#226#148#164, // #180
3495
#226#149#163, // #185
3496
#226#149#145, // #186
3497
#226#149#151, // #187
3498
#226#149#157, // #188
3501
#226#148#144, // #191
3502
#226#148#148, // #192
3503
#226#148#180, // #193
3504
#226#148#172, // #194
3505
#226#148#156, // #195
3506
#226#148#128, // #196
3507
#226#148#188, // #197
3510
#226#149#154, // #200
3511
#226#149#148, // #201
3512
#226#149#169, // #202
3513
#226#149#166, // #203
3514
#226#149#160, // #204
3515
#226#149#144, // #205
3516
#226#149#172, // #206
3527
#226#148#152, // #217
3528
#226#148#140, // #218
3529
#226#150#136, // #219
3530
#226#150#132, // #220
3533
#226#150#128, // #223
3552
#226#128#151, // #242
3564
#226#150#160, // #254
3568
ArrayCP866ToUTF8 : TCharToUTF8Table = (
3745
#226#150#145, //#176
3746
#226#150#146, //#177
3747
#226#150#147, //#178
3748
#226#148#130, //#179
3749
#226#148#164, //#180
3750
#226#149#161, //#181
3751
#226#149#162, //#182
3752
#226#149#150, //#183
3753
#226#149#149, //#184
3754
#226#149#163, //#185
3755
#226#149#145, //#186
3756
#226#149#151, //#187
3757
#226#149#157, //#188
3758
#226#149#156, //#189
3759
#226#149#155, //#190
3760
#226#148#144, //#191
3761
#226#148#148, //#192
3762
#226#148#180, //#193
3763
#226#148#172, //#194
3764
#226#148#156, //#195
3765
#226#148#128, //#196
3766
#226#148#188, //#197
3767
#226#149#158, //#198
3768
#226#149#159, //#199
3769
#226#149#154, //#200
3770
#226#149#148, //#201
3771
#226#149#169, //#202
3772
#226#149#166, //#203
3773
#226#149#160, //#204
3774
#226#149#144, //#205
3775
#226#149#172, //#206
3776
#226#149#167, //#207
3777
#226#149#168, //#208
3778
#226#149#164, //#209
3779
#226#149#165, //#210
3780
#226#149#153, //#211
3781
#226#149#152, //#212
3782
#226#149#146, //#213
3783
#226#149#147, //#214
3784
#226#149#171, //#215
3785
#226#149#170, //#216
3786
#226#148#152, //#217
3787
#226#148#140, //#218
3788
#226#150#136, //#219
3789
#226#150#132, //#220
3790
#226#150#140, //#221
3791
#226#150#144, //#222
3792
#226#150#128, //#223
3818
#226#136#153, //#249
3820
#226#136#154, //#251
3821
#226#132#150, //#252
3823
#226#150#160, //#254
3827
ArrayCP874ToUTF8: TCharToUTF8Table = (
3956
#226#130#172, // #128
3961
#226#128#166, // #133
3973
#226#128#152, // #145
3974
#226#128#153, // #146
3975
#226#128#156, // #147
3976
#226#128#157, // #148
3977
#226#128#162, // #149
3978
#226#128#147, // #150
3979
#226#128#148, // #151
3989
#224#184#129, // #161
3990
#224#184#130, // #162
3991
#224#184#131, // #163
3992
#224#184#132, // #164
3993
#224#184#133, // #165
3994
#224#184#134, // #166
3995
#224#184#135, // #167
3996
#224#184#136, // #168
3997
#224#184#137, // #169
3998
#224#184#138, // #170
3999
#224#184#139, // #171
4000
#224#184#140, // #172
4001
#224#184#141, // #173
4002
#224#184#142, // #174
4003
#224#184#143, // #175
4004
#224#184#144, // #176
4005
#224#184#145, // #177
4006
#224#184#146, // #178
4007
#224#184#147, // #179
4008
#224#184#148, // #180
4009
#224#184#149, // #181
4010
#224#184#150, // #182
4011
#224#184#151, // #183
4012
#224#184#152, // #184
4013
#224#184#153, // #185
4014
#224#184#154, // #186
4015
#224#184#155, // #187
4016
#224#184#156, // #188
4017
#224#184#157, // #189
4018
#224#184#158, // #190
4019
#224#184#159, // #191
4020
#224#184#160, // #192
4021
#224#184#161, // #193
4022
#224#184#162, // #194
4023
#224#184#163, // #195
4024
#224#184#164, // #196
4025
#224#184#165, // #197
4026
#224#184#166, // #198
4027
#224#184#167, // #199
4028
#224#184#168, // #200
4029
#224#184#169, // #201
4030
#224#184#170, // #202
4031
#224#184#171, // #203
4032
#224#184#172, // #204
4033
#224#184#173, // #205
4034
#224#184#174, // #206
4035
#224#184#175, // #207
4036
#224#184#176, // #208
4037
#224#184#177, // #209
4038
#224#184#178, // #210
4039
#224#184#179, // #211
4040
#224#184#180, // #212
4041
#224#184#181, // #213
4042
#224#184#182, // #214
4043
#224#184#183, // #215
4044
#224#184#184, // #216
4045
#224#184#185, // #217
4046
#224#184#186, // #218
4051
#224#184#191, // #223
4052
#224#185#128, // #224
4053
#224#185#129, // #225
4054
#224#185#130, // #226
4055
#224#185#131, // #227
4056
#224#185#132, // #228
4057
#224#185#133, // #229
4058
#224#185#134, // #230
4059
#224#185#135, // #231
4060
#224#185#136, // #232
4061
#224#185#137, // #233
4062
#224#185#138, // #234
4063
#224#185#139, // #235
4064
#224#185#140, // #236
4065
#224#185#141, // #237
4066
#224#185#142, // #238
4067
#224#185#143, // #239
4068
#224#185#144, // #240
4069
#224#185#145, // #241
4070
#224#185#146, // #242
4071
#224#185#147, // #243
4072
#224#185#148, // #244
4073
#224#185#149, // #245
4074
#224#185#150, // #246
4075
#224#185#151, // #247
4076
#224#185#152, // #248
4077
#224#185#153, // #249
4078
#224#185#154, // #250
4079
#224#185#155, // #251
4086
ArrayKOI8ToUTF8: TCharToUTF8Table = (
4345
function UTF8BOMToUTF8(const s: string): string;
4347
Result:=copy(s,4,length(s));
4350
function ISO_8859_1ToUTF8(const s: string): string;
4352
Result:=SingleByteToUTF8(s,ArrayISO_8859_1ToUTF8);
4355
function ISO_8859_2ToUTF8(const s: string): string;
4357
Result:=SingleByteToUTF8(s,ArrayISO_8859_2ToUTF8);
4360
function CP1250ToUTF8(const s: string): string;
4362
Result:=SingleByteToUTF8(s,ArrayCP1250ToUTF8);
4365
function CP1251ToUTF8(const s: string): string;
4367
Result:=SingleByteToUTF8(s,ArrayCP1251ToUTF8);
4370
function CP1252ToUTF8(const s: string): string;
4372
Result:=SingleByteToUTF8(s,ArrayCP1252ToUTF8);
4375
function CP1253ToUTF8(const s: string): string;
4377
Result:=SingleByteToUTF8(s,ArrayCP1253ToUTF8);
4380
function CP1254ToUTF8(const s: string): string;
4382
Result:=SingleByteToUTF8(s,ArrayCP1254ToUTF8);
4385
function CP1255ToUTF8(const s: string): string;
4387
Result:=SingleByteToUTF8(s,ArrayCP1255ToUTF8);
4390
function CP1256ToUTF8(const s: string): string;
4392
Result:=SingleByteToUTF8(s,ArrayCP1256ToUTF8);
4395
function CP1257ToUTF8(const s: string): string;
4397
Result:=SingleByteToUTF8(s,ArrayCP1257ToUTF8);
4400
function CP1258ToUTF8(const s: string): string;
4402
Result:=SingleByteToUTF8(s,ArrayCP1258ToUTF8);
4405
function CP437ToUTF8(const s: string): string;
4407
Result:=SingleByteToUTF8(s,ArrayCP437ToUTF8);
4410
function CP850ToUTF8(const s: string): string;
4412
Result:=SingleByteToUTF8(s,ArrayCP850ToUTF8);
4415
function CP866ToUTF8(const s: string): string;
4417
Result:=SingleByteToUTF8(s,ArrayCP866ToUTF8);
4420
function CP874ToUTF8(const s: string): string;
4422
Result:=SingleByteToUTF8(s,ArrayCP874ToUTF8);
4425
function KOI8ToUTF8(const s: string): string;
4427
Result:=SingleByteToUTF8(s,ArrayKOI8ToUTF8);
4430
function SingleByteToUTF8(const s: string; const Table: TCharToUTF8Table
4445
SetLength(Result,len*4);// UTF-8 is at most 4 bytes
4447
Dest:=PChar(Result);
4448
for i:=1 to len do begin
4451
if ord(c)<128 then begin
4456
if p<>nil then begin
4457
while p^<>#0 do begin
4465
SetLength(Result,PtrUInt(Dest)-PtrUInt(Result));
4468
function UCS2LEToUTF8(const s: string): string;
4480
len:=length(s) div 2;
4481
SetLength(Result,len*3);// UTF-8 is at most three times the size
4482
Src:=PWord(Pointer(s));
4483
Dest:=PChar(Result);
4484
for i:=1 to len do begin
4487
if ord(c)<128 then begin
4491
inc(Dest,UnicodeToUTF8SkipErrors(c,Dest));
4494
len:=PtrUInt(Dest)-PtrUInt(Result);
4495
if len>length(Result) then
4496
RaiseGDBException('');
4497
SetLength(Result,len);
4500
function UCS2BEToUTF8(const s: string): string;
4512
len:=length(s) div 2;
4513
SetLength(Result,len*3);// UTF-8 is at most three times the size
4514
Src:=PWord(Pointer(s));
4515
Dest:=PChar(Result);
4516
for i:=1 to len do begin
4519
if ord(c)<128 then begin
4523
inc(Dest,UnicodeToUTF8SkipErrors(c,Dest));
4526
len:=PtrUInt(Dest)-PtrUInt(Result);
4527
if len>length(Result) then
4528
RaiseGDBException('');
4529
SetLength(Result,len);
4532
function UnicodeToCP1250(Unicode: cardinal): integer;
4535
0..127: Result:=Unicode;
4538
166..169: Result:=Unicode;
4539
171..174: Result:=Unicode;
4540
176..177: Result:=Unicode;
4541
180..184: Result:=Unicode;
4543
193..194: Result:=Unicode;
4548
205..206: Result:=Unicode;
4549
211..212: Result:=Unicode;
4550
214..215: Result:=Unicode;
4552
220..221: Result:=Unicode;
4554
225..226: Result:=Unicode;
4559
237..238: Result:=Unicode;
4560
243..244: Result:=Unicode;
4561
246..247: Result:=Unicode;
4563
252..253: Result:=Unicode;
4621
8211..8212: Result:=Unicode-8061;
4622
8216..8217: Result:=Unicode-8071;
4624
8220..8221: Result:=Unicode-8073;
4626
8224..8225: Result:=Unicode-8090;
4638
function UnicodeToCP1251(Unicode: cardinal): integer;
4641
0..127: Result:=Unicode;
4644
166..167: Result:=Unicode;
4646
171..174: Result:=Unicode;
4647
176..177: Result:=Unicode;
4648
181..183: Result:=Unicode;
4651
1026..1027: Result:=Unicode-898;
4663
1040..1103: Result:=Unicode-848;
4680
8211..8212: Result:=Unicode-8061;
4681
8216..8217: Result:=Unicode-8071;
4683
8220..8221: Result:=Unicode-8073;
4685
8224..8225: Result:=Unicode-8090;
4698
function UnicodeToCP1252(Unicode: cardinal): integer;
4701
0..127: Result:=Unicode;
4702
160..255: Result:=Unicode;
4713
8211..8212: Result:=Unicode-8061;
4714
8216..8217: Result:=Unicode-8071;
4716
8220..8221: Result:=Unicode-8073;
4718
8224..8225: Result:=Unicode-8090;
4730
function UnicodeToCP1253(Unicode: cardinal): integer;
4733
0..127: Result:=Unicode;
4735
163..169: Result:=Unicode;
4736
171..174: Result:=Unicode;
4737
176..179: Result:=Unicode;
4738
181..183: Result:=Unicode;
4743
901..902: Result:=Unicode-740;
4744
904..906: Result:=Unicode-720;
4746
910..929: Result:=Unicode-720;
4747
931..974: Result:=Unicode-720;
4748
8211..8212: Result:=Unicode-8061;
4750
8216..8217: Result:=Unicode-8071;
4752
8220..8221: Result:=Unicode-8073;
4754
8224..8225: Result:=Unicode-8090;
4766
function UnicodeToCP1254(Unicode: cardinal): integer;
4769
0..127: Result:=Unicode;
4770
160..207: Result:=Unicode;
4771
209..220: Result:=Unicode;
4772
223..239: Result:=Unicode;
4773
241..252: Result:=Unicode;
4789
8211..8212: Result:=Unicode-8061;
4790
8216..8217: Result:=Unicode-8071;
4792
8220..8221: Result:=Unicode-8073;
4794
8224..8225: Result:=Unicode-8090;
4806
function UnicodeToCP1255(Unicode: cardinal): integer;
4809
0..127: Result:=Unicode;
4810
160..163: Result:=Unicode;
4811
165..169: Result:=Unicode;
4812
171..185: Result:=Unicode;
4813
187..191: Result:=Unicode;
4819
1456..1465: Result:=Unicode-1264;
4820
1467..1475: Result:=Unicode-1264;
4821
1488..1514: Result:=Unicode-1264;
4822
1520..1524: Result:=Unicode-1308;
4823
8206..8207: Result:=Unicode-7953;
4824
8211..8212: Result:=Unicode-8061;
4825
8216..8217: Result:=Unicode-8071;
4827
8220..8221: Result:=Unicode-8073;
4829
8224..8225: Result:=Unicode-8090;
4842
function UnicodeToCP1256(Unicode: cardinal): integer;
4845
0..127: Result:=Unicode;
4847
162..169: Result:=Unicode;
4848
171..185: Result:=Unicode;
4849
187..190: Result:=Unicode;
4853
231..235: Result:=Unicode;
4854
238..239: Result:=Unicode;
4858
251..252: Result:=Unicode;
4866
1569..1590: Result:=Unicode-1376;
4867
1591..1594: Result:=Unicode-1375;
4868
1600..1603: Result:=Unicode-1380;
4870
1605..1608: Result:=Unicode-1378;
4871
1609..1610: Result:=Unicode-1373;
4872
1611..1614: Result:=Unicode-1371;
4873
1615..1616: Result:=Unicode-1370;
4888
8204..8205: Result:=Unicode-8047;
4889
8206..8207: Result:=Unicode-7953;
4890
8211..8212: Result:=Unicode-8061;
4891
8216..8217: Result:=Unicode-8071;
4893
8220..8221: Result:=Unicode-8073;
4895
8224..8225: Result:=Unicode-8090;
4907
function UnicodeToCP1257(Unicode: cardinal): integer;
4910
0..127: Result:=Unicode;
4912
162..164: Result:=Unicode;
4913
166..167: Result:=Unicode;
4916
171..174: Result:=Unicode;
4918
176..183: Result:=Unicode;
4921
187..190: Result:=Unicode;
4922
196..197: Result:=Unicode;
4926
213..215: Result:=Unicode;
4930
228..229: Result:=Unicode;
4934
245..247: Result:=Unicode;
4988
8211..8212: Result:=Unicode-8061;
4989
8216..8217: Result:=Unicode-8071;
4991
8220..8221: Result:=Unicode-8073;
4993
8224..8225: Result:=Unicode-8090;
5005
function UnicodeToCP1258(Unicode: cardinal): integer;
5008
0..127: Result:=Unicode;
5009
160..194: Result:=Unicode;
5010
196..203: Result:=Unicode;
5011
205..207: Result:=Unicode;
5013
211..212: Result:=Unicode;
5014
214..220: Result:=Unicode;
5015
223..226: Result:=Unicode;
5016
228..235: Result:=Unicode;
5017
237..239: Result:=Unicode;
5019
243..244: Result:=Unicode;
5020
246..252: Result:=Unicode;
5041
8211..8212: Result:=Unicode-8061;
5042
8216..8217: Result:=Unicode-8071;
5044
8220..8221: Result:=Unicode-8073;
5046
8224..8225: Result:=Unicode-8090;
5059
function UnicodeToCP437(Unicode: cardinal): integer;
5062
0..127: Result:=Unicode;
5065
162..163: Result:=Unicode-7;
5080
196..197: Result:=Unicode-54;
5097
234..235: Result:=Unicode-98;
5137
8992..8993: Result:=Unicode-8748;
5151
9554..9555: Result:=Unicode-9341;
5162
9566..9567: Result:=Unicode-9368;
5164
9569..9570: Result:=Unicode-9388;
5166
9572..9573: Result:=Unicode-9363;
5168
9575..9576: Result:=Unicode-9368;
5178
9617..9619: Result:=Unicode-9441;
5184
function UnicodeToCP850(Unicode: cardinal): integer;
5187
0..127: Result:=Unicode;
5221
193..194: Result:=Unicode-12;
5223
196..197: Result:=Unicode-54;
5228
202..203: Result:=Unicode--8;
5230
205..207: Result:=Unicode--9;
5241
218..219: Result:=Unicode--15;
5256
234..235: Result:=Unicode-98;
5305
9617..9619: Result:=Unicode-9441;
5311
function UnicodeToCP866(Unicode: cardinal): integer;
5314
0..127: Result:=Unicode;
5315
1040..1087 : Result := Unicode-912;
5316
9617..9619 : Result := Unicode-9441;
5317
9474 : Result := 179;
5318
9508 : Result := 180;
5319
9569 : Result := 181;
5320
9570 : Result := 182;
5321
9558 : Result := 183;
5322
9557 : Result := 184;
5323
9571 : Result := 185;
5324
9553 : Result := 186;
5325
9559 : Result := 187;
5326
9565 : Result := 188;
5327
9564 : Result := 189;
5328
9563 : Result := 190;
5329
9488 : Result := 191;
5330
9492 : Result := 192;
5331
9524 : Result := 193;
5332
9516 : Result := 194;
5333
9500 : Result := 195;
5334
9472 : Result := 196;
5335
9532 : Result := 197;
5336
9566 : Result := 198;
5337
9567 : Result := 199;
5338
9562 : Result := 200;
5339
9556 : Result := 201;
5340
9577 : Result := 202;
5341
9574 : Result := 203;
5342
9568 : Result := 204;
5343
9552 : Result := 205;
5344
9580 : Result := 206;
5345
9575 : Result := 207;
5346
9576 : Result := 208;
5347
9572 : Result := 209;
5348
9573 : Result := 210;
5349
9561 : Result := 211;
5350
9560 : Result := 212;
5351
9554 : Result := 213;
5352
9555 : Result := 214;
5353
9579 : Result := 215;
5354
9578 : Result := 216;
5355
9496 : Result := 217;
5356
9484 : Result := 218;
5357
9608 : Result := 219;
5358
9604 : Result := 220;
5359
9612 : Result := 221;
5360
9616 : Result := 222;
5361
9600 : Result := 223;
5362
1088..1103 : Result := Unicode-864;
5363
1025 : Result := 240;
5364
1105 : Result := 241;
5365
1028 : Result := 242;
5366
1108 : Result := 243;
5367
1031 : Result := 244;
5368
1111 : Result := 245;
5369
1038 : Result := 246;
5370
1118 : Result := 247;
5371
176 : Result := 248;
5372
8729 : Result := 249;
5373
183 : Result := 250;
5374
8730 : Result := 251;
5375
8470 : Result := 252;
5376
164 : Result := 253;
5377
9632 : Result := 254;
5378
160 : Result := 255;
5383
function UnicodeToCP874(Unicode: cardinal): integer;
5386
0..127: Result:=Unicode;
5388
3585..3642: Result:=Unicode-3424;
5389
3647..3675: Result:=Unicode-3424;
5390
8211..8212: Result:=Unicode-8061;
5391
8216..8217: Result:=Unicode-8071;
5392
8220..8221: Result:=Unicode-8073;
5400
function UnicodeToKOI8(Unicode: cardinal): integer;
5403
0..127: Result:=Unicode;
5404
1040..1041: Result:=Unicode-815;
5407
1044..1045: Result:=Unicode-816;
5410
1048..1055: Result:=Unicode-815;
5411
1056..1059: Result:=Unicode-814;
5423
1072..1073: Result:=Unicode-879;
5426
1076..1077: Result:=Unicode-880;
5429
1080..1087: Result:=Unicode-879;
5430
1088..1091: Result:=Unicode-878;
5447
function UnicodeToISO_8859_1(Unicode: cardinal): integer;
5450
0..255: Result:=Unicode;
5455
function UnicodeToISO_8859_2(Unicode: cardinal): integer;
5458
0..127: Result:=Unicode;
5459
128..160: Result:=Unicode;
5461
167..168: Result:=Unicode;
5466
193..194: Result:=Unicode;
5471
205..206: Result:=Unicode;
5472
211..212: Result:=Unicode;
5473
214..215: Result:=Unicode;
5475
220..221: Result:=Unicode;
5477
225..226: Result:=Unicode;
5482
237..238: Result:=Unicode;
5483
243..244: Result:=Unicode;
5484
246..247: Result:=Unicode;
5486
252..253: Result:=Unicode;
5548
function UTF8ToUTF8BOM(const s: string): string;
5550
Result:=#$EF#$BB#$BF+s;
5553
function UTF8ToISO_8859_1(const s: string): string;
5555
Result:=UTF8ToSingleByte(s,@UnicodeToISO_8859_1);
5558
function UTF8ToISO_8859_2(const s: string): string;
5560
Result:=UTF8ToSingleByte(s,@UnicodeToISO_8859_2);
5563
function UTF8ToCP1250(const s: string): string;
5565
Result:=UTF8ToSingleByte(s,@UnicodeToCP1250);
5568
function UTF8ToCP1251(const s: string): string;
5570
Result:=UTF8ToSingleByte(s,@UnicodeToCP1251);
5573
function UTF8ToCP1252(const s: string): string;
5575
Result:=UTF8ToSingleByte(s,@UnicodeToCP1252);
5578
function UTF8ToCP1253(const s: string): string;
5580
Result:=UTF8ToSingleByte(s,@UnicodeToCP1253);
5583
function UTF8ToCP1254(const s: string): string;
5585
Result:=UTF8ToSingleByte(s,@UnicodeToCP1254);
5588
function UTF8ToCP1255(const s: string): string;
5590
Result:=UTF8ToSingleByte(s,@UnicodeToCP1255);
5593
function UTF8ToCP1256(const s: string): string;
5595
Result:=UTF8ToSingleByte(s,@UnicodeToCP1256);
5598
function UTF8ToCP1257(const s: string): string;
5600
Result:=UTF8ToSingleByte(s,@UnicodeToCP1257);
5603
function UTF8ToCP1258(const s: string): string;
5605
Result:=UTF8ToSingleByte(s,@UnicodeToCP1258);
5608
function UTF8ToCP437(const s: string): string;
5610
Result:=UTF8ToSingleByte(s,@UnicodeToCP437);
5613
function UTF8ToCP850(const s: string): string;
5615
Result:=UTF8ToSingleByte(s,@UnicodeToCP850);
5618
function UTF8ToCP866(const s: string): string;
5620
Result:=UTF8ToSingleByte(s,@UnicodeToCP866);
5623
function UTF8ToCP874(const s: string): string;
5625
Result:=UTF8ToSingleByte(s,@UnicodeToCP874);
5628
function UTF8ToKOI8(const s: string): string;
5630
Result:=UTF8ToSingleByte(s,@UnicodeToKOI8);
5633
function UTF8ToSingleByte(const s: string;
5634
const UTF8CharConvFunc: TUnicodeToCharID): string;
5649
SetLength(Result,len);
5651
Dest:=PChar(Result);
5652
while len>0 do begin
5654
if c<#128 then begin
5660
Unicode:=UTF8CharacterToUnicode(Src,CharLen);
5663
i:=UTF8CharConvFunc(Unicode);
5670
SetLength(Result,Dest-PChar(Result));
5673
function UTF8ToUCS2LE(const s: string): string;
5687
SetLength(Result,len*2);
5689
Dest:=PWord(Pointer(Result));
5690
while len>0 do begin
5692
if c<#128 then begin
5693
Dest^:=NtoLE(Word(ord(c)));
5698
Unicode:=UTF8CharacterToUnicode(Src,CharLen);
5701
if Unicode<=$ffff then begin
5702
Dest^:=NtoLE(Word(Unicode));
5707
len:=PtrUInt(Dest)-PtrUInt(Result);
5708
if len>length(Result) then
5709
RaiseGDBException('');
5710
SetLength(Result,len);
5713
function UTF8ToUCS2BE(const s: string): string;
5727
SetLength(Result,len*2);
5729
Dest:=PWord(Pointer(Result));
5730
while len>0 do begin
5732
if c<#128 then begin
5733
Dest^:=NtoBE(Word(ord(c)));
5738
Unicode:=UTF8CharacterToUnicode(Src,CharLen);
5741
if Unicode<=$ffff then begin
5742
Dest^:=NtoBE(Word(Unicode));
5747
len:=PtrUInt(Dest)-PtrUInt(Result);
5748
if len>length(Result) then
5749
RaiseGDBException('');
5750
SetLength(Result,len);
5753
procedure GetSupportedEncodings(List: TStrings);
5756
List.Add('UTF-8BOM');
5775
List.Add('ISO-8859-1');
5776
List.Add('ISO-8859-2');
5778
List.Add('UCS-2LE');
5779
List.Add('UCS-2BE');
5782
function GuessEncoding(const s: string): string;
5784
function CompareI(p1, p2: PChar; Count: integer): boolean;
5790
for i:=1 to Count do begin
5793
if Chr1<>Chr2 then begin
5794
if Chr1 in [97..122] then
5796
if Chr2 in [97..122] then
5798
if Chr1<>Chr2 then exit(false);
5806
{$IFDEF VerboseIDEEncoding}
5807
function PosToStr(p: integer): string;
5816
while (i<=length(s)) and (i<p) do begin
5817
if s[i] in [#10,#13] then begin
5821
if (i<=length(s)) and (s[i] in [#10,#13]) and (s[i]<>s[i-1]) then
5828
Result:='x='+IntToStr(x)+',y='+IntToStr(y);
5844
// try UTF-8 BOM (Byte Order Mark)
5845
if CompareI(@s[1],#$EF#$BB#$BF,3) then begin
5846
Result:=EncodingUTF8BOM;
5850
// try ucs-2le BOM FF FE
5851
if (length(s)>=2) and (s[1]=#$FF) and (s[2]=#$FE) then begin
5852
Result:=EncodingUCS2LE;
5856
// try ucs-2be BOM FE FF
5857
if (length(s)>=2) and (s[1]=#$FE) and (s[2]=#$FF) then begin
5858
Result:=EncodingUCS2BE;
5862
// try {%encoding eee}
5863
if CompareI(@s[1],'{%encoding ',11) then begin
5865
while (p<=l) and (s[p] in [' ',#9]) do inc(p);
5867
while (EndPos<=l) and (not (s[EndPos] in ['}',' ',#9])) do inc(EndPos);
5868
Result:=NormalizeEncoding(copy(s,p,EndPos-p));
5872
// try UTF-8 (this includes ASCII)
5874
while (p<=l) do begin
5875
if ord(s[p])<128 then begin
5879
i:=UTF8CharacterStrictLength(@s[p]);
5880
//DebugLn(['GuessEncoding ',i,' ',DbgStr(s[p])]);
5882
{$IFDEF VerboseIDEEncoding}
5883
DebugLn(['GuessEncoding non UTF-8 found at ',PosToStr(p),' ',dbgstr(copy(s,p-10,20))]);
5891
Result:=EncodingUTF8;
5895
// use system encoding
5896
Result:=GetDefaultTextEncoding;
5898
if NormalizeEncoding(Result)=EncodingUTF8 then begin
5899
// the system encoding is UTF-8, but it is not UTF-8
5900
// use ISO-8859-1 instead. This encoding has a full 1:1 mapping to unicode,
5901
// so no character is lost during conversions.
5902
Result:='ISO-8859-1';
5906
function ConvertEncoding(const s, FromEncoding, ToEncoding: string): string;
5908
AFrom, ATo, SysEnc : String;
5910
{$ifdef HasIconvEnc}
5914
AFrom:=NormalizeEncoding(FromEncoding);
5915
ATo:=NormalizeEncoding(ToEncoding);
5916
SysEnc:=NormalizeEncoding(GetDefaultTextEncoding);
5917
if AFrom=EncodingAnsi then AFrom:=SysEnc;
5918
if ATo=EncodingAnsi then ATo:=SysEnc;
5919
if AFrom=ATo then begin
5923
//DebugLn(['ConvertEncoding ',AFrom,' ',ATo]);
5925
if (AFrom=EncodingUTF8) then begin
5926
if ATo=EncodingUTF8BOM then begin Result:=UTF8ToUTF8BOM(s); exit; end;
5927
if ATo='iso88591' then begin Result:=UTF8ToISO_8859_1(s); exit; end;
5928
if ATo='iso88592' then begin Result:=UTF8ToISO_8859_2(s); exit; end;
5929
if ATo='cp1250' then begin Result:=UTF8ToCP1250(s); exit; end;
5930
if ATo='cp1251' then begin Result:=UTF8ToCP1251(s); exit; end;
5931
if ATo='cp1252' then begin Result:=UTF8ToCP1252(s); exit; end;
5932
if ATo='cp1253' then begin Result:=UTF8ToCP1253(s); exit; end;
5933
if ATo='cp1254' then begin Result:=UTF8ToCP1254(s); exit; end;
5934
if ATo='cp1255' then begin Result:=UTF8ToCP1255(s); exit; end;
5935
if ATo='cp1256' then begin Result:=UTF8ToCP1256(s); exit; end;
5936
if ATo='cp1257' then begin Result:=UTF8ToCP1257(s); exit; end;
5937
if ATo='cp1258' then begin Result:=UTF8ToCP1258(s); exit; end;
5938
if ATo='cp437' then begin Result:=UTF8ToCP437(s); exit; end;
5939
if ATo='cp850' then begin Result:=UTF8ToCP850(s); exit; end;
5940
if ATo='cp866' then begin Result:=UTF8ToCP866(s); exit; end;
5941
if ATo='cp874' then begin Result:=UTF8ToCP874(s); exit; end;
5942
if ATo = 'cp936' then
5944
Result := UTF8ToCP936(s);
5947
if ATo = 'cp950' then
5949
Result := UTF8ToCP950(s);
5952
if ATo = 'cp949' then
5954
Result := UTF8ToCP949(s);
5957
if ATo = 'cp932' then
5959
Result := UTF8ToCP932(s);
5962
if ATo='koi8' then begin Result:=UTF8ToKOI8(s); exit; end;
5963
if ATo=EncodingUCS2LE then begin Result:=UTF8ToUCS2LE(s); exit; end;
5964
if ATo=EncodingUCS2BE then begin Result:=UTF8ToUCS2BE(s); exit; end;
5966
if (ATo=SysEnc) and Assigned(ConvertUTF8ToAnsi) then begin
5967
Result:=ConvertUTF8ToAnsi(s);
5970
end else if ATo=EncodingUTF8 then begin
5971
if AFrom=EncodingUTF8BOM then begin Result:=UTF8BOMToUTF8(s); exit; end;
5972
if AFrom='iso88591' then begin Result:=ISO_8859_1ToUTF8(s); exit; end;
5973
if AFrom='iso88592' then begin Result:=ISO_8859_2ToUTF8(s); exit; end;
5974
if AFrom='cp1250' then begin Result:=CP1250ToUTF8(s); exit; end;
5975
if AFrom='cp1251' then begin Result:=CP1251ToUTF8(s); exit; end;
5976
if AFrom='cp1252' then begin Result:=CP1252ToUTF8(s); exit; end;
5977
if AFrom='cp1253' then begin Result:=CP1253ToUTF8(s); exit; end;
5978
if AFrom='cp1254' then begin Result:=CP1254ToUTF8(s); exit; end;
5979
if AFrom='cp1255' then begin Result:=CP1255ToUTF8(s); exit; end;
5980
if AFrom='cp1256' then begin Result:=CP1256ToUTF8(s); exit; end;
5981
if AFrom='cp1257' then begin Result:=CP1257ToUTF8(s); exit; end;
5982
if AFrom='cp1258' then begin Result:=CP1258ToUTF8(s); exit; end;
5983
if AFrom='cp437' then begin Result:=CP437ToUTF8(s); exit; end;
5984
if AFrom='cp850' then begin Result:=CP850ToUTF8(s); exit; end;
5985
if AFrom='cp866' then begin Result:=CP866ToUTF8(s); exit; end;
5986
if AFrom='cp874' then begin Result:=CP874ToUTF8(s); exit; end;
5987
if AFrom = 'cp936' then
5989
Result := CP936ToUTF8(s);
5992
if AFrom = 'cp950' then
5994
Result := CP950ToUTF8(s);
5997
if AFrom = 'cp949' then
5999
Result := CP949ToUTF8(s);
6002
if AFrom = 'cp932' then
6004
Result := CP932ToUTF8(s);
6007
if AFrom='koi8' then begin Result:=KOI8ToUTF8(s); exit; end;
6008
if AFrom=EncodingUCS2LE then begin Result:=UCS2LEToUTF8(s); exit; end;
6009
if AFrom=EncodingUCS2BE then begin Result:=UCS2BEToUTF8(s); exit; end;
6011
if (AFrom=SysEnc) and Assigned(ConvertAnsiToUTF8) then begin
6012
Result:=ConvertAnsiToUTF8(s);
6017
//ATo and AFrom <> EncodingUTF8. Need to do ANSI->UTF8->ANSI.
6022
if AFrom='iso88591' then begin
6023
Result:=ISO_8859_1ToUTF8(s);
6026
else if AFrom='iso88592' then begin
6027
Result:=ISO_8859_2ToUTF8(s);
6030
else if AFrom='cp1250' then begin
6031
Result:=CP1250ToUTF8(s);
6034
else if AFrom='cp1251' then begin
6035
Result:=CP1251ToUTF8(s);
6038
else if AFrom='cp1252' then begin
6039
Result:=CP1252ToUTF8(s);
6042
else if AFrom='cp1253' then begin
6043
Result:=CP1253ToUTF8(s);
6046
else if AFrom='cp1254' then begin
6047
Result:=CP1254ToUTF8(s);
6050
else if AFrom='cp1255' then begin
6051
Result:=CP1255ToUTF8(s);
6054
else if AFrom='cp1256' then begin
6055
Result:=CP1256ToUTF8(s);
6058
else if AFrom='cp1257' then begin
6059
Result:=CP1257ToUTF8(s);
6062
else if AFrom='cp1258' then begin
6063
Result:=CP1258ToUTF8(s);
6066
else if AFrom='cp850' then begin
6067
Result:=CP850ToUTF8(s);
6070
else if AFrom='cp866' then begin
6071
Result:=CP866ToUTF8(s);
6074
else if AFrom='cp874' then begin
6075
Result:=CP874ToUTF8(s);
6078
else if AFrom = 'cp936' then
6080
Result := CP936ToUTF8(s);
6083
else if AFrom = 'cp950' then
6085
Result := CP950ToUTF8(s);
6088
else if AFrom = 'cp949' then
6090
Result := CP949ToUTF8(s);
6093
else if AFrom = 'cp932' then
6095
Result := CP932ToUTF8(s);
6098
else if AFrom='koi8' then begin
6099
Result:=KOI8ToUTF8(s);
6102
else if (AFrom=SysEnc) and Assigned(ConvertAnsiToUTF8) then begin
6103
Result:=ConvertAnsiToUTF8(s);
6107
if Encoded = true then begin
6110
if ATo='iso88591' then begin
6111
Result:=UTF8ToISO_8859_1(Result);
6114
else if ATo='iso88592' then begin
6115
Result:=UTF8ToISO_8859_2(Result);
6118
else if ATo='cp1250' then begin
6119
Result:=UTF8ToCP1250(Result);
6122
else if ATo='cp1251' then begin
6123
Result:=UTF8ToCP1251(Result);
6126
else if ATo='cp1252' then begin
6127
Result:=UTF8ToCP1252(Result);
6130
else if ATo='cp1253' then begin
6131
Result:=UTF8ToCP1253(Result);
6134
else if ATo='cp1254' then begin
6135
Result:=UTF8ToCP1254(Result);
6138
else if ATo='cp1255' then begin
6139
Result:=UTF8ToCP1255(Result);
6142
else if ATo='cp1256' then begin
6143
Result:=UTF8ToCP1256(Result);
6146
else if ATo='cp1257' then begin
6147
Result:=UTF8ToCP1257(Result);
6150
else if ATo='cp1258' then begin
6151
Result:=UTF8ToCP1258(Result);
6154
else if ATo='cp850' then begin
6155
Result:=UTF8ToCP850(Result);
6158
else if ATo='cp866' then begin
6159
Result:=UTF8ToCP866(Result);
6162
else if ATo='cp874' then begin
6163
Result:=UTF8ToCP874(Result);
6166
else if ATo = 'cp936' then
6168
Result := UTF8ToCP936(Result);
6171
else if ATo = 'cp950' then
6173
Result := UTF8ToCP950(Result);
6176
else if ATo = 'cp949' then
6178
Result := UTF8ToCP949(Result);
6181
else if ATo = 'cp932' then
6183
Result := UTF8ToCP932(Result);
6186
else if ATo='koi8' then begin
6187
Result:=UTF8ToKOI8(Result);
6190
else if (ATo=SysEnc) and Assigned(ConvertUTF8ToAnsi) then begin
6191
Result:=ConvertUTF8ToAnsi(Result);
6196
//Exit if encoded succesfully.
6197
if Encoded = true then begin
6204
{$ifdef HasIconvEnc}
6206
if not IconvLibFound and not InitIconv(Dummy) then
6208
{$IFNDEF DisableChecks}
6209
DebugLn(['Can not init iconv: ',Dummy]);
6213
if Iconvert(s, Result, AFrom, ATo)<>0 then