2
0

sysencodingh.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. {%MainUnit sysutils.pp}
  2. {
  3. *********************************************************************
  4. Copyright (C) 2012 Paul Ishenin,
  5. member of the Free Pascal Development Team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *********************************************************************
  12. }
  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. TCreateEncodingProc = function: TEncoding; // "case Se of" or the like is simpler but would link all encodings into the executable.
  27. Class Var
  28. FStandardEncodings: array[TStandardEncoding] of TEncoding;
  29. FSystemEncodingsList: TEncoding;
  30. var
  31. FNext: TEncoding;
  32. class function GetStandard(Se: TStandardEncoding; Ctr: TCreateEncodingProc): TEncoding; static;
  33. class function CreateANSI: TEncoding; static;
  34. class function GetANSI: TEncoding; static; inline;
  35. class function CreateASCII: TEncoding; static;
  36. class function GetASCII: TEncoding; static; inline;
  37. class function CreateBigEndianUnicode: TEncoding; static;
  38. class function GetBigEndianUnicode: TEncoding; static; inline;
  39. class function GetDefault: TEncoding; static;
  40. class function GetSystemEncoding: TEncoding; static;
  41. class function CreateUnicode: TEncoding; static;
  42. class function GetUnicode: TEncoding; static; inline;
  43. class function CreateUTF7: TEncoding; static;
  44. class function GetUTF7: TEncoding; static; inline;
  45. class function CreateUTF8: TEncoding; static;
  46. class function GetUTF8: TEncoding; static; inline;
  47. class destructor Destroy;
  48. strict protected
  49. FIsSingleByte: Boolean;
  50. FMaxCharSize: Integer;
  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: PAnsiChar; CharCount: Integer): TBytes; virtual; abstract;
  56. function GetAnsiString(Bytes: PByte; ByteCount: Integer): ansistring; 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: Ansistring): TBytes; overload;
  92. function GetAnsiBytes(const S: Ansistring; CharIndex, CharCount: Integer): TBytes; overload;
  93. function GetAnsiString(const Bytes: TBytes): ansistring; overload;
  94. function GetAnsiString(const Bytes: TBytes; ByteIndex, ByteCount: Integer):ansistring; overload;
  95. property CodePage: Cardinal read GetCodePage;
  96. property EncodingName: UnicodeString read GetEncodingName;
  97. property IsSingleByte: Boolean read FIsSingleByte;
  98. class property ANSI: TEncoding read GetANSI;
  99. class property ASCII: TEncoding read GetASCII;
  100. class property BigEndianUnicode: TEncoding read GetBigEndianUnicode;
  101. class property Default: TEncoding read GetDefault;
  102. class property SystemEncoding: TEncoding read GetSystemEncoding;
  103. class property Unicode: TEncoding read GetUnicode;
  104. class property UTF7: TEncoding read GetUTF7;
  105. class property UTF8: TEncoding read GetUTF8;
  106. end;
  107. { TMBCSEncoding }
  108. TMBCSEncoding = class(TEncoding)
  109. strict private
  110. FCodePage: Integer;
  111. FMBToWCharFlags: Integer;
  112. FWCharToMBFlags: Integer;
  113. strict protected
  114. function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  115. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  116. function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  117. function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  118. function GetAnsiBytes(Chars: PAnsiChar; CharCount: Integer): TBytes; override;
  119. function GetAnsiString(Bytes: PByte; ByteCount: Integer): ansistring; override;
  120. function GetCodePage: Cardinal; override;
  121. function GetEncodingName: UnicodeString; override;
  122. public
  123. constructor Create; overload; virtual;
  124. constructor Create(ACodePage: Integer); overload; virtual;
  125. constructor Create(ACodePage, MBToWCharFlags, WCharToMBFlags: Integer); overload; virtual;
  126. function Clone: TEncoding; override;
  127. function GetMaxByteCount(CharCount: Integer): Integer; override;
  128. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  129. function GetPreamble: TBytes; override;
  130. end;
  131. { TUTF7Encoding }
  132. TUTF7Encoding = class(TMBCSEncoding)
  133. public
  134. constructor Create; override;
  135. function Clone: TEncoding; override;
  136. function GetMaxByteCount(CharCount: Integer): Integer; override;
  137. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  138. end;
  139. { TUTF8Encoding }
  140. TUTF8Encoding = class(TUTF7Encoding)
  141. public
  142. constructor Create; override;
  143. function Clone: TEncoding; override;
  144. function GetMaxByteCount(CharCount: Integer): Integer; override;
  145. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  146. function GetPreamble: TBytes; override;
  147. end;
  148. { TUnicodeEncoding }
  149. TUnicodeEncoding = class(TEncoding)
  150. strict protected
  151. function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  152. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  153. function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  154. function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  155. function GetAnsiBytes(Chars: PAnsiChar; CharCount: Integer): TBytes; override;
  156. function GetAnsiString(Bytes: PByte; ByteCount: Integer): ansistring; override;
  157. function GetCodePage: Cardinal; override;
  158. function GetEncodingName: UnicodeString; override;
  159. public
  160. constructor Create; virtual;
  161. function Clone: TEncoding; override;
  162. function GetMaxByteCount(CharCount: Integer): Integer; override;
  163. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  164. function GetPreamble: TBytes; override;
  165. end;
  166. { TBigEndianUnicodeEncoding }
  167. TBigEndianUnicodeEncoding = class(TUnicodeEncoding)
  168. strict protected
  169. procedure Swap(var B: TBytes);
  170. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  171. function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  172. function GetAnsiBytes(Chars: PAnsiChar; CharCount: Integer): TBytes; override;
  173. function GetAnsiString(Bytes: PByte; ByteCount: Integer): ansistring; override;
  174. function GetCodePage: Cardinal; override;
  175. function GetEncodingName: UnicodeString; override;
  176. public
  177. function Clone: TEncoding; override;
  178. function GetPreamble: TBytes; override;
  179. end;