sysencodingh.inc 9.2 KB

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