Steps to enable and use iconv on Windows with Indy 10.5.9

廉价感情. 提交于 2020-01-23 03:35:32

问题


For cross-platform development with Internet Direct (Indy), it can be useful to enable iconv support instead of the OS-specific Windows character conversions.

This would allow to continue developing and testing code on Windows in the Delphi IDE, but still be able to find conversion related issues before compiling the code on a different platform.

Indy 10 already includes code to use the iconv API on Linux and Windows.

On Windows, some preparation is required:

  • copy the iconv API DLL to the app folder or the search path
  • define the use_iconv conditional symbol

The intention of this question is to help getting started with iconv on the Windows platform.


回答1:


If I have a quick look at the iconv.h I found with the gettext for windows sourceforge project, and if I understood the C code correctly, a wrapper-unit for iconv.dll could look like this, and could be used on the input and output of Indy components.

unit iconv;

interface

//based on version 1.9.1 https://sourceforge.net/projects/gettext/

type
  TIconv = pointer;

function iconv_open(const PAnsiChar:tocode; const PAnsiChar:fromcode): TIconv; cdecl;
function iconv(cd:TIconv; var inbuf:PAnsiChar; var inbytesleft:integer; var output:PAnsiChar; var outbytesleft:integer): integer; cdecl;
function iconv_close(cd:TIconv): integer; cdecl;

const
  ICONV_TRIVIALP            =0;  // int *argument
  ICONV_GET_TRANSLITERATE   =1;  // int *argument
  ICONV_SET_TRANSLITERATE   =2;  // const int *argument
  ICONV_GET_DISCARD_ILSEQ   =3;  // int *argument
  ICONV_SET_DISCARD_ILSEQ   =4;  // const int *argument

type
  TiconvlistDoOne=function(namescount:cardinal; const names:PAnsiChar; data:pointer): integer; cdecl;

function iconvctl(cd:TIconv; request:integer; argument:pointer): integer; cdecl;
procedure iconvlist(do_one:TiconvlistDoOne; data:pointer); cdecl;
procedure libiconv_set_relocation_prefix(const orig_prefix:PAnsiChar; const curr_prefix:PAnsiChar); cdecl;

implementation

function iconv_open; external 'iconv.dll';
function iconv; external 'iconv.dll';
function iconv_close; external 'iconv.dll';
function iconvctl; external 'iconv.dll';
procedure iconvlist; external 'iconv.dll';
procedure libiconv_set_relocation_prefix; external 'iconv.dll';

end.



回答2:


Free Pascal has an iconv header (package iconvenc), and that either is delphi compatible or should be easily updatable. Get it from FPC websvn interface module iconvenc or the latest RC (2.6.2rc1), since it might have updated since the year old 2.6.0.

But it is more complicated than just providing the header, since iconv on windows doesn't support errno, and thus can't handle EILSEQ and EI2BIG, so you need to properly allocate memory up front (like 4*charsize).

I got code how to handle non errno iconv targets PHP, but it hasn't been tested very well. (see src/iconvert.inc)



来源:https://stackoverflow.com/questions/14140315/steps-to-enable-and-use-iconv-on-windows-with-indy-10-5-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!