1{ 2 ********************************************************************* 3 Copyright (C) 2012 Paul Ishenin, 4 member of the Free Pascal Development Team 5 6 See the file COPYING.FPC, included in this distribution, 7 for details about the copyright. 8 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. 12 ********************************************************************* 13} 14 15{$ifndef VER2_4} 16type 17 EEncodingError = class(Exception); 18 19 { TEncoding } 20 21 TEncoding = class 22 strict private 23 type 24 TStandardEncoding = ( 25 seAnsi, 26 seAscii, 27 seUnicode, 28 seBigEndianUnicode, 29 seUTF7, 30 seUTF8); 31 var 32 FStandardEncodings: array[TStandardEncoding] of TEncoding; static; 33 FSystemEncodings: array of TEncoding; static; 34 Class Var 35 FLock : TRTLCriticalSection; 36 class function GetANSI: TEncoding; static; 37 class function GetASCII: TEncoding; static; 38 class function GetBigEndianUnicode: TEncoding; static; 39 class function GetDefault: TEncoding; static; 40 class function GetSystemEncoding: TEncoding; static; 41 class function GetUnicode: TEncoding; static; 42 class function GetUTF7: TEncoding; static; 43 class function GetUTF8: TEncoding; static; 44 45 class constructor Create; 46 class destructor Destroy; 47 strict protected 48 FIsSingleByte: Boolean; 49 FMaxCharSize: Integer; 50 class procedure FreeEncodings; 51 function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract; 52 function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract; 53 function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract; 54 function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract; 55 function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; virtual; abstract; 56 function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; virtual; abstract; 57 function GetCodePage: Cardinal; virtual; abstract; 58 function GetEncodingName: UnicodeString; virtual; abstract; 59 public 60 function Clone: TEncoding; virtual; 61 class function Convert(Source, Destination: TEncoding; const Bytes: TBytes): TBytes; overload; 62 class function Convert(Source, Destination: TEncoding; const Bytes: TBytes; StartIndex, Count: Integer): TBytes; overload; 63 class function IsStandardEncoding(AEncoding: TEncoding): Boolean; static; 64 class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer; overload; static; 65 class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding; 66 ADefaultEncoding: TEncoding): Integer; overload; static; 67 function GetByteCount(const Chars: TUnicodeCharArray): Integer; overload; 68 function GetByteCount(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer): Integer; overload; 69 function GetByteCount(const S: UnicodeString): Integer; overload; 70 function GetByteCount(const S: UnicodeString; CharIndex, CharCount: Integer): Integer; overload; 71 function GetBytes(const Chars: TUnicodeCharArray): TBytes; overload; 72 function GetBytes(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer): TBytes; overload; 73 function GetBytes(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer; 74 const Bytes: TBytes; ByteIndex: Integer): Integer; overload; 75 function GetBytes(const S: UnicodeString): TBytes; overload; 76 function GetBytes(const S: UnicodeString; CharIndex, CharCount: Integer; 77 const Bytes: TBytes; ByteIndex: Integer): Integer; overload; 78 function GetCharCount(const Bytes: TBytes): Integer; overload; 79 function GetCharCount(const Bytes: TBytes; ByteIndex, ByteCount: Integer): Integer; overload; 80 function GetChars(const Bytes: TBytes): TUnicodeCharArray; overload; 81 function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer): TUnicodeCharArray; overload; 82 function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer; 83 const Chars: TUnicodeCharArray; CharIndex: Integer): Integer; overload; 84 class function GetEncoding(CodePage: Integer): TEncoding; overload; static; 85 class function GetEncoding(const EncodingName: UnicodeString): TEncoding; overload; static; 86 function GetMaxByteCount(CharCount: Integer): Integer; virtual; abstract; 87 function GetMaxCharCount(ByteCount: Integer): Integer; virtual; abstract; 88 function GetPreamble: TBytes; virtual; abstract; 89 function GetString(const Bytes: TBytes): UnicodeString; overload; 90 function GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): UnicodeString; overload; 91 function GetAnsiBytes(const S: string): TBytes; overload; 92 function GetAnsiBytes(const S: string; CharIndex, CharCount: Integer): TBytes; overload; 93 function GetAnsiString(const Bytes: TBytes): string; overload; 94 function GetAnsiString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): string; overload; 95 96 property CodePage: Cardinal read GetCodePage; 97 property EncodingName: UnicodeString read GetEncodingName; 98 property IsSingleByte: Boolean read FIsSingleByte; 99 100 class property ANSI: TEncoding read GetANSI; 101 class property ASCII: TEncoding read GetASCII; 102 class property BigEndianUnicode: TEncoding read GetBigEndianUnicode; 103 class property Default: TEncoding read GetDefault; 104 class property SystemEncoding: TEncoding read GetSystemEncoding; 105 class property Unicode: TEncoding read GetUnicode; 106 class property UTF7: TEncoding read GetUTF7; 107 class property UTF8: TEncoding read GetUTF8; 108 end; 109 110 { TMBCSEncoding } 111 112 TMBCSEncoding = class(TEncoding) 113 strict private 114 FCodePage: Integer; 115 FMBToWCharFlags: Integer; 116 FWCharToMBFlags: Integer; 117 strict protected 118 function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override; 119 function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override; 120 function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override; 121 function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override; 122 function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override; 123 function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override; 124 function GetCodePage: Cardinal; override; 125 function GetEncodingName: UnicodeString; override; 126 public 127 constructor Create; overload; virtual; 128 constructor Create(ACodePage: Integer); overload; virtual; 129 constructor Create(ACodePage, MBToWCharFlags, WCharToMBFlags: Integer); overload; virtual; 130 function Clone: TEncoding; override; 131 function GetMaxByteCount(CharCount: Integer): Integer; override; 132 function GetMaxCharCount(ByteCount: Integer): Integer; override; 133 function GetPreamble: TBytes; override; 134 end; 135 136 { TUTF7Encoding } 137 138 TUTF7Encoding = class(TMBCSEncoding) 139 public 140 constructor Create; override; 141 function Clone: TEncoding; override; 142 function GetMaxByteCount(CharCount: Integer): Integer; override; 143 function GetMaxCharCount(ByteCount: Integer): Integer; override; 144 end; 145 146 { TUTF8Encoding } 147 148 TUTF8Encoding = class(TUTF7Encoding) 149 public 150 constructor Create; override; 151 function Clone: TEncoding; override; 152 function GetMaxByteCount(CharCount: Integer): Integer; override; 153 function GetMaxCharCount(ByteCount: Integer): Integer; override; 154 function GetPreamble: TBytes; override; 155 end; 156 157 { TUnicodeEncoding } 158 159 TUnicodeEncoding = class(TEncoding) 160 strict protected 161 function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override; 162 function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override; 163 function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override; 164 function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override; 165 function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override; 166 function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override; 167 function GetCodePage: Cardinal; override; 168 function GetEncodingName: UnicodeString; override; 169 public 170 constructor Create; virtual; 171 function Clone: TEncoding; override; 172 function GetMaxByteCount(CharCount: Integer): Integer; override; 173 function GetMaxCharCount(ByteCount: Integer): Integer; override; 174 function GetPreamble: TBytes; override; 175 end; 176 177 { TBigEndianUnicodeEncoding } 178 179 TBigEndianUnicodeEncoding = class(TUnicodeEncoding) 180 strict protected 181 procedure Swap(var B: TBytes); 182 function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override; 183 function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override; 184 function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override; 185 function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override; 186 function GetCodePage: Cardinal; override; 187 function GetEncodingName: UnicodeString; override; 188 public 189 function Clone: TEncoding; override; 190 function GetPreamble: TBytes; override; 191 end; 192{$endif VER2_4} 193