2
0

sysencodingh.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. type
  13. EEncodingError = class(Exception);
  14. { TEncoding }
  15. TEncoding = class
  16. strict private
  17. type
  18. TStandardEncoding = (
  19. seAnsi,
  20. seAscii,
  21. seUnicode,
  22. seBigEndianUnicode,
  23. seUTF7,
  24. seUTF8);
  25. var
  26. FStandardEncodings: array[TStandardEncoding] of TEncoding; static;
  27. FSystemEncodings: array 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 GetSystemEncoding: TEncoding; static;
  35. class function GetUnicode: TEncoding; static;
  36. class function GetUTF7: TEncoding; static;
  37. class function GetUTF8: TEncoding; static;
  38. class constructor Create;
  39. class destructor Destroy;
  40. strict protected
  41. FIsSingleByte: Boolean;
  42. FMaxCharSize: Integer;
  43. class procedure FreeEncodings;
  44. function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract;
  45. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
  46. function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
  47. function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract;
  48. function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; virtual; abstract;
  49. function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; virtual; abstract;
  50. function GetCodePage: Cardinal; virtual; abstract;
  51. function GetEncodingName: UnicodeString; virtual; abstract;
  52. public
  53. function Clone: TEncoding; virtual;
  54. class function Convert(Source, Destination: TEncoding; const Bytes: TBytes): TBytes; overload;
  55. class function Convert(Source, Destination: TEncoding; const Bytes: TBytes; StartIndex, Count: Integer): TBytes; overload;
  56. class function IsStandardEncoding(AEncoding: TEncoding): Boolean; static;
  57. class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer; overload; static;
  58. class function GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding;
  59. ADefaultEncoding: TEncoding): Integer; overload; static;
  60. function GetByteCount(const Chars: TUnicodeCharArray): Integer; overload;
  61. function GetByteCount(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer): Integer; overload;
  62. function GetByteCount(const S: UnicodeString): Integer; overload;
  63. function GetByteCount(const S: UnicodeString; CharIndex, CharCount: Integer): Integer; overload;
  64. function GetBytes(const Chars: TUnicodeCharArray): TBytes; overload;
  65. function GetBytes(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer): TBytes; overload;
  66. function GetBytes(const Chars: TUnicodeCharArray; CharIndex, CharCount: Integer;
  67. const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
  68. function GetBytes(const S: UnicodeString): TBytes; overload;
  69. function GetBytes(const S: UnicodeString; CharIndex, CharCount: Integer;
  70. const Bytes: TBytes; ByteIndex: Integer): Integer; overload;
  71. function GetCharCount(const Bytes: TBytes): Integer; overload;
  72. function GetCharCount(const Bytes: TBytes; ByteIndex, ByteCount: Integer): Integer; overload;
  73. function GetChars(const Bytes: TBytes): TUnicodeCharArray; overload;
  74. function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer): TUnicodeCharArray; overload;
  75. function GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer;
  76. const Chars: TUnicodeCharArray; CharIndex: Integer): Integer; overload;
  77. class function GetEncoding(CodePage: Integer): TEncoding; overload; static;
  78. class function GetEncoding(const EncodingName: UnicodeString): TEncoding; overload; static;
  79. function GetMaxByteCount(CharCount: Integer): Integer; virtual; abstract;
  80. function GetMaxCharCount(ByteCount: Integer): Integer; virtual; abstract;
  81. function GetPreamble: TBytes; virtual; abstract;
  82. function GetString(const Bytes: TBytes): UnicodeString; overload;
  83. function GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): UnicodeString; overload;
  84. function GetAnsiBytes(const S: string): TBytes; overload;
  85. function GetAnsiBytes(const S: string; CharIndex, CharCount: Integer): TBytes; overload;
  86. function GetAnsiString(const Bytes: TBytes): string; overload;
  87. function GetAnsiString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): string; overload;
  88. property CodePage: Cardinal read GetCodePage;
  89. property EncodingName: UnicodeString read GetEncodingName;
  90. property IsSingleByte: Boolean read FIsSingleByte;
  91. class property ANSI: TEncoding read GetANSI;
  92. class property ASCII: TEncoding read GetASCII;
  93. class property BigEndianUnicode: TEncoding read GetBigEndianUnicode;
  94. class property Default: TEncoding read GetDefault;
  95. class property SystemEncoding: TEncoding read GetSystemEncoding;
  96. class property Unicode: TEncoding read GetUnicode;
  97. class property UTF7: TEncoding read GetUTF7;
  98. class property UTF8: TEncoding read GetUTF8;
  99. end;
  100. { TMBCSEncoding }
  101. TMBCSEncoding = class(TEncoding)
  102. strict private
  103. FCodePage: Integer;
  104. FMBToWCharFlags: Integer;
  105. FWCharToMBFlags: Integer;
  106. strict protected
  107. function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  108. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  109. function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  110. function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  111. function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override;
  112. function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override;
  113. function GetCodePage: Cardinal; override;
  114. function GetEncodingName: UnicodeString; override;
  115. public
  116. constructor Create; overload; virtual;
  117. constructor Create(ACodePage: Integer); overload; virtual;
  118. constructor Create(ACodePage, MBToWCharFlags, WCharToMBFlags: Integer); overload; virtual;
  119. function Clone: TEncoding; override;
  120. function GetMaxByteCount(CharCount: Integer): Integer; override;
  121. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  122. function GetPreamble: TBytes; override;
  123. end;
  124. { TUTF7Encoding }
  125. TUTF7Encoding = class(TMBCSEncoding)
  126. public
  127. constructor Create; override;
  128. function Clone: TEncoding; override;
  129. function GetMaxByteCount(CharCount: Integer): Integer; override;
  130. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  131. end;
  132. { TUTF8Encoding }
  133. TUTF8Encoding = class(TUTF7Encoding)
  134. public
  135. constructor Create; override;
  136. function Clone: TEncoding; override;
  137. function GetMaxByteCount(CharCount: Integer): Integer; override;
  138. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  139. function GetPreamble: TBytes; override;
  140. end;
  141. { TUnicodeEncoding }
  142. TUnicodeEncoding = class(TEncoding)
  143. strict protected
  144. function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  145. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  146. function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; override;
  147. function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar; CharCount: Integer): Integer; overload; override;
  148. function GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes; override;
  149. function GetAnsiString(Bytes: PByte; ByteCount: Integer): string; override;
  150. function GetCodePage: Cardinal; override;
  151. function GetEncodingName: UnicodeString; override;
  152. public
  153. constructor Create; virtual;
  154. function Clone: TEncoding; override;
  155. function GetMaxByteCount(CharCount: Integer): Integer; override;
  156. function GetMaxCharCount(ByteCount: Integer): Integer; override;
  157. function GetPreamble: TBytes; override;
  158. end;
  159. { TBigEndianUnicodeEncoding }
  160. TBigEndianUnicodeEncoding = class(TUnicodeEncoding)
  161. strict protected
  162. procedure Swap(var B: TBytes);
  163. function GetBytes(Chars: PUnicodeChar; CharCount: Integer; 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. function Clone: TEncoding; override;
  171. function GetPreamble: TBytes; override;
  172. end;