xmliconv_windows.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. {
  2. This file is part of the Free Component Library
  3. libiconv-based XML decoder (Windows version).
  4. Binds to the native (not Cygwin or Mingw) build of libiconv.
  5. Copyright (c) 2009 by Sergei Gorelkin, [email protected]
  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. {$IFNDEF FPC_DOTTEDUNITS}
  13. unit xmliconv_windows;
  14. {$ENDIF FPC_DOTTEDUNITS}
  15. {$mode objfpc}
  16. {$h+}
  17. interface
  18. implementation
  19. {$IFDEF FPC_DOTTEDUNITS}
  20. uses
  21. Xml.Read;
  22. {$ELSE FPC_DOTTEDUNITS}
  23. uses
  24. xmlread;
  25. {$ENDIF FPC_DOTTEDUNITS}
  26. type
  27. iconv_t = Pointer;
  28. const
  29. iconvlib = 'iconv.dll';
  30. function iconv_open(ToCode, FromCode: PAnsiChar): iconv_t; cdecl; external iconvlib name 'libiconv_open';
  31. function iconv(__cd: iconv_t; __inbuf: PPAnsiChar; var __inbytesleft: size_t; __outbuf:PPAnsiChar; var __outbytesleft: size_t): size_t; cdecl; external iconvlib name 'libiconv';
  32. function iconv_close(cd: iconv_t): Integer; cdecl; external iconvlib name 'libiconv_close';
  33. function errno_location: PInteger; cdecl; external 'msvcrt.dll' name '_errno';
  34. function Iconv_Decode(Context: Pointer; InBuf: PAnsiChar; var InCnt: Cardinal; OutBuf: PWideChar; var OutCnt: Cardinal): Integer; stdcall;
  35. var
  36. OutChars: size_t;
  37. InChars : size_t;
  38. begin
  39. OutChars := OutCnt * sizeof(WideChar);
  40. InChars:=InCnt;
  41. Result := iconv(Context, @InBuf, InChars, @OutBuf, OutChars);
  42. InCnt:=InChars;
  43. OutCnt := OutChars div sizeof(WideChar);
  44. if Result = -1 then
  45. begin
  46. case errno_location^ of
  47. // when iconv reports insufficient input or output space, still return
  48. // a positive number of converted chars
  49. 7, 22: Result := OutCnt - (OutChars div sizeof(WideChar));
  50. else
  51. Result := -errno_location^;
  52. end;
  53. end;
  54. end;
  55. procedure Iconv_Cleanup(Context: Pointer); stdcall;
  56. begin
  57. iconv_close(Context);
  58. end;
  59. function GetIconvDecoder(const AEncoding: string; out Decoder: TDecoder): Boolean; stdcall;
  60. var
  61. f: iconv_t;
  62. begin
  63. f := iconv_open('UCS-2-INTERNAL', PAnsiChar(AEncoding));
  64. if f <> Pointer(-1) then
  65. begin
  66. Decoder.Context := f;
  67. Decoder.Decode := @Iconv_Decode;
  68. Decoder.Cleanup := @Iconv_Cleanup;
  69. Result := True;
  70. end
  71. else
  72. Result := False;
  73. end;
  74. initialization
  75. RegisterDecoder(@GetIconvDecoder);
  76. end.