iconvenc_dyn.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2000 by Marco van de Voort([email protected])
  4. member of the Free Pascal development team
  5. libiconv header translation + a helper routine
  6. http://wiki.freepascal.org/iconvenc Dynamic version
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright. (LGPL)
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. }
  13. unit iconvenc_dyn;
  14. interface
  15. {$mode objfpc}{$H+}
  16. uses
  17. ctypes,unixtype,baseunix,
  18. dl,
  19. initc;
  20. const
  21. n = 1;
  22. {$ifdef beos}
  23. ESysEILSEQ = EILSEQ;
  24. {$endif}
  25. type
  26. piconv_t = ^iconv_t;
  27. iconv_t = pointer;
  28. Ticonv_open = function(__tocode: pchar; __fromcode: pchar): iconv_t; cdecl;
  29. Ticonv = function(__cd: iconv_t; __inbuf: ppchar; __inbytesleft: psize_t; __outbuf: ppchar; __outbytesleft: psize_t): size_t; cdecl;
  30. Ticonv_close = function(__cd: iconv_t): cint; cdecl;
  31. var
  32. iconv_lib: pointer;
  33. iconv_open: Ticonv_open;
  34. iconv: Ticonv;
  35. iconv_close: Ticonv_close;
  36. IconvLibFound: boolean = False;
  37. function TryLoadLib(LibName: string; var error: string): boolean; // can be used to load non standard libname
  38. function Iconvert(s: string; var res: string; FromEncoding, ToEncoding: string): cint;
  39. function InitIconv(var error: string): boolean;
  40. implementation
  41. function TryLoadLib(LibName: string; var error: string): boolean;
  42. function resolvesymbol (var funcptr; symbol: string): Boolean;
  43. begin
  44. pointer(funcptr) := pointer(dlsym(iconv_lib, pchar(symbol)));
  45. result := assigned(pointer(funcptr));
  46. if not result then
  47. error := error+#13#10+dlerror();
  48. end;
  49. var
  50. res: boolean;
  51. begin
  52. result := false;
  53. Error := Error+#13#10'Trying '+LibName;
  54. iconv_lib := dlopen(pchar(libname), RTLD_NOW);
  55. if Assigned(iconv_lib) then
  56. begin
  57. result := true;
  58. result := result and resolvesymbol(pointer(iconv),'iconv');
  59. result := result and resolvesymbol(pointer(iconv_open),'iconv_open');
  60. result := result and resolvesymbol(pointer(iconv_close),'iconv_close');
  61. if not result then
  62. begin
  63. result:=true;
  64. result := result and resolvesymbol(pointer(iconv),'libiconv');
  65. result := result and resolvesymbol(pointer(iconv_open),'libiconv_open');
  66. result := result and resolvesymbol(pointer(iconv_close),'libiconv_close');
  67. end;
  68. // if not res then
  69. // dlclose(iconv_lib);
  70. end else
  71. error:=error+#13#10+dlerror();
  72. end;
  73. function InitIconv(var error: string): boolean;
  74. begin
  75. result := true;
  76. error := '';
  77. if not TryLoadLib('libc.so.6', error) then
  78. if not TryLoadLib('libiconv.so', error) then
  79. {$if defined(haiku)}
  80. if not TryLoadLib('libtextencoding.so', error) then
  81. {$ifend}
  82. result := false;
  83. iconvlibfound := iconvlibfound or result;
  84. end;
  85. {$i iconvert.inc}
  86. end.