sysencodingh.inc 7.7 KB

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