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
|
{
*********************************************************************
Copyright (C) 2012 Paul Ishenin,
member of the Free Pascal Development Team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*********************************************************************
}
{$ifndef VER2_4}
type
EEncodingError = class(Exception);
{ TEncoding }
TEncoding = class
strict private
type
TStandardEncoding = (
seAnsi,
seAscii,
seUnicode,
seBigEndianUnicode,
seUTF7,
seUTF8);
var
FStandardEncodings: array[TStandardEncoding] of TEncoding; static;
FSystemEncodings: array of TEncoding; static;
Class Var
FLock : TRTLCriticalSection;
class function GetANSI: TEncoding; static;
class function GetASCII: TEncoding; static;
class function GetBigEndianUnicode: TEncoding; static;
class function GetDefault: TEncoding; static;
class function GetSystemEncoding: TEncoding; static;
class function GetUnicode: TEncoding; static;
class function GetUTF7: TEncoding; static;
class function GetUTF8: TEncoding; static;
class constructor Create;
class destructor Destroy;
strict protected
FIsSingleByte: Boolean;
FMaxCharSize: Integer;
class procedure FreeEncodings;
function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract;
function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract;
function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; virtual; abstract;
function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; virtual; abstract;
function GetCodePage: Cardinal; virtual; abstract;
function GetEncodingName: UnicodeString; virtual; abstract;
public
function Clone: TEncoding; virtual;
class function Convert(Source, Destination: TEncoding; const Bytes: TBytes): TBytes; overload;
class function Convert(Source, Destination: TEncoding; const Bytes: TBytes; StartIndex, Count: Integer): TBytes; overload;
class function IsStandardEncoding(AEncoding: TEncoding): Boolean; static;
class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer; overload; static;
class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding;
ADefaultEncoding: TEncoding): Integer; overload; static;
function GetByteCount(const Chars: TUnicodeCharArray): Integer; overload;
function GetByteCount(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer): Integer; overload;
function GetByteCount(const S: UnicodeString): Integer; overload;
function GetByteCount(const S: UnicodeString; CharIndex, CharCount: Integer): Integer; overload;
function GetBytes(const Chars: TUnicodeCharArray): TBytes; overload;
function GetBytes(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer): TBytes; overload;
function GetBytes(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer;
const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetBytes(const S: UnicodeString): TBytes; overload;
function GetBytes(const S: UnicodeString; CharIndex, CharCount: Integer;
const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
function GetCharCount(const Bytes: TBytes): Integer; overload;
function GetCharCount(const Bytes: TBytes; ByteIndex, ByteCount: Integer): Integer; overload;
function GetChars(const Bytes: TBytes): TUnicodeCharArray; overload;
function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer): TUnicodeCharArray; overload;
function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer;
const Chars: TUnicodeCharArray; CharIndex: Integer): Integer; overload;
class function GetEncoding(CodePage: Integer): TEncoding; overload; static;
class function GetEncoding(const EncodingName: UnicodeString): TEncoding; overload; static;
function GetMaxByteCount(CharCount: Integer): Integer; virtual; abstract;
function GetMaxCharCount(ByteCount: Integer): Integer; virtual; abstract;
function GetPreamble: TBytes; virtual; abstract;
function GetString(const Bytes: TBytes): UnicodeString; overload;
function GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): UnicodeString; overload;
function GetAnsiBytes(const S: string): TBytes; overload;
function GetAnsiBytes(const S: string; CharIndex, CharCount: Integer): TBytes; overload;
function GetAnsiString(const Bytes: TBytes): string; overload;
function GetAnsiString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): string; overload;
property CodePage: Cardinal read GetCodePage;
property EncodingName: UnicodeString read GetEncodingName;
property IsSingleByte: Boolean read FIsSingleByte;
class property ANSI: TEncoding read GetANSI;
class property ASCII: TEncoding read GetASCII;
class property BigEndianUnicode: TEncoding read GetBigEndianUnicode;
class property Default: TEncoding read GetDefault;
class property SystemEncoding: TEncoding read GetSystemEncoding;
class property Unicode: TEncoding read GetUnicode;
class property UTF7: TEncoding read GetUTF7;
class property UTF8: TEncoding read GetUTF8;
end;
{ TMBCSEncoding }
TMBCSEncoding = class(TEncoding)
strict private
FCodePage: Integer;
FMBToWCharFlags: Integer;
FWCharToMBFlags: Integer;
strict protected
function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override;
function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override;
function GetCodePage: Cardinal; override;
function GetEncodingName: UnicodeString; override;
public
constructor Create; overload; virtual;
constructor Create(ACodePage: Integer); overload; virtual;
constructor Create(ACodePage, MBToWCharFlags, WCharToMBFlags: Integer); overload; virtual;
function Clone: TEncoding; override;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
function GetPreamble: TBytes; override;
end;
{ TUTF7Encoding }
TUTF7Encoding = class(TMBCSEncoding)
public
constructor Create; override;
function Clone: TEncoding; override;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
end;
{ TUTF8Encoding }
TUTF8Encoding = class(TUTF7Encoding)
public
constructor Create; override;
function Clone: TEncoding; override;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
function GetPreamble: TBytes; override;
end;
{ TUnicodeEncoding }
TUnicodeEncoding = class(TEncoding)
strict protected
function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override;
function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override;
function GetCodePage: Cardinal; override;
function GetEncodingName: UnicodeString; override;
public
constructor Create; virtual;
function Clone: TEncoding; override;
function GetMaxByteCount(CharCount: Integer): Integer; override;
function GetMaxCharCount(ByteCount: Integer): Integer; override;
function GetPreamble: TBytes; override;
end;
{ TBigEndianUnicodeEncoding }
TBigEndianUnicodeEncoding = class(TUnicodeEncoding)
strict protected
procedure Swap(var B: TBytes);
function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override;
function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override;
function GetCodePage: Cardinal; override;
function GetEncodingName: UnicodeString; override;
public
function Clone: TEncoding; override;
function GetPreamble: TBytes; override;
end;
{$endif VER2_4}
|