summaryrefslogtreecommitdiff
path: root/packages/iconvenc
diff options
context:
space:
mode:
authorflorian <florian@3ad0048d-3df7-0310-abae-a5850022a9f2>2008-09-10 20:14:31 +0000
committerflorian <florian@3ad0048d-3df7-0310-abae-a5850022a9f2>2008-09-10 20:14:31 +0000
commit9d56e8e9dc1741d26eed6ce1aabd57ab4e567c51 (patch)
treef0eb9ebd75b648436bb4335911b8e5fc5deddcfe /packages/iconvenc
parent725cb10ae6727ed5f0789685d1df5eb872cb0192 (diff)
downloadfpc-9d56e8e9dc1741d26eed6ce1aabd57ab4e567c51.tar.gz
Merged revisions 11665-11738 via svnmerge from
http://svn.freepascal.org/svn/fpc/branches/unicodestring ........ r11665 | florian | 2008-08-30 13:30:17 +0200 (Sat, 30 Aug 2008) | 1 line * continued to work on unicodestring type support ........ r11666 | florian | 2008-08-30 19:02:26 +0200 (Sat, 30 Aug 2008) | 2 lines * expectloc for wide/ansi/unicode strings is LOC_CONSTANT or LOC_REGISTER now ........ r11667 | florian | 2008-08-30 20:42:37 +0200 (Sat, 30 Aug 2008) | 1 line * more unicodestring stuff fixed, test results on win32 are already good ........ r11670 | florian | 2008-08-30 23:21:48 +0200 (Sat, 30 Aug 2008) | 2 lines * first fixes for unix bootstrapping ........ r11683 | ivost | 2008-09-01 12:46:39 +0200 (Mon, 01 Sep 2008) | 2 lines * fixed 64bit bug in iconvenc.pas ........ r11689 | florian | 2008-09-01 23:12:34 +0200 (Mon, 01 Sep 2008) | 1 line * fixed several errors when building on unix ........ r11694 | florian | 2008-09-03 20:32:43 +0200 (Wed, 03 Sep 2008) | 1 line * fixed unix compilation ........ r11695 | florian | 2008-09-03 21:01:04 +0200 (Wed, 03 Sep 2008) | 1 line * bootstrapping fix ........ r11696 | florian | 2008-09-03 21:07:18 +0200 (Wed, 03 Sep 2008) | 1 line * more bootstrapping fixed ........ r11698 | florian | 2008-09-03 22:47:54 +0200 (Wed, 03 Sep 2008) | 1 line + two missing compiler procs exported ........ r11701 | florian | 2008-09-04 16:42:34 +0200 (Thu, 04 Sep 2008) | 2 lines + lazarus project for the linux rtl ........ r11702 | florian | 2008-09-04 16:43:27 +0200 (Thu, 04 Sep 2008) | 2 lines + set unicode string procedures ........ r11707 | florian | 2008-09-04 23:23:02 +0200 (Thu, 04 Sep 2008) | 2 lines * fixed several type casting stuff ........ r11712 | florian | 2008-09-05 22:46:03 +0200 (Fri, 05 Sep 2008) | 1 line * fixed unicodestring compilation on windows after recent unix changes ........ r11713 | florian | 2008-09-05 23:35:12 +0200 (Fri, 05 Sep 2008) | 1 line + UnicodeString support for Variants ........ r11715 | florian | 2008-09-06 20:59:54 +0200 (Sat, 06 Sep 2008) | 1 line * patch by Martin Schreiber for UnicodeString streaming ........ r11716 | florian | 2008-09-06 22:22:55 +0200 (Sat, 06 Sep 2008) | 2 lines * fixed test ........ r11717 | florian | 2008-09-07 10:25:51 +0200 (Sun, 07 Sep 2008) | 1 line * fixed typo when converting tunicodestring to punicodechar ........ r11718 | florian | 2008-09-07 11:29:52 +0200 (Sun, 07 Sep 2008) | 3 lines * fixed writing of UnicodeString properties * moved some helper routines to unicode headers ........ r11734 | florian | 2008-09-09 22:38:55 +0200 (Tue, 09 Sep 2008) | 1 line * fixed bootstrapping ........ r11735 | florian | 2008-09-10 11:25:28 +0200 (Wed, 10 Sep 2008) | 2 lines * first fixes for persisten unicodestrings ........ r11736 | florian | 2008-09-10 14:31:00 +0200 (Wed, 10 Sep 2008) | 3 lines Initialized merge tracking via "svnmerge" with revisions "1-11663" from http://svn.freepascal.org/svn/fpc/trunk ........ r11737 | florian | 2008-09-10 21:06:57 +0200 (Wed, 10 Sep 2008) | 3 lines * fixed unicodestring <-> variant handling * fixed unicodestring property reading ........ git-svn-id: http://svn.freepascal.org/svn/fpc/trunk@11739 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'packages/iconvenc')
-rwxr-xr-xpackages/iconvenc/src/iconvenc.pas165
1 files changed, 86 insertions, 79 deletions
diff --git a/packages/iconvenc/src/iconvenc.pas b/packages/iconvenc/src/iconvenc.pas
index c955398de5..11c144c85e 100755
--- a/packages/iconvenc/src/iconvenc.pas
+++ b/packages/iconvenc/src/iconvenc.pas
@@ -2,7 +2,7 @@
This file is part of the Free Pascal run time library.
Copyright (c) 2000 by Marco van de Voort(marco@freepascal.org)
member of the Free Pascal development team
-
+
libiconv header translation + a helper routine
http://wiki.freepascal.org/iconvenc
@@ -15,7 +15,8 @@
}
unit iconvenc;
-interface
+
+interface
{$mode objfpc}{$H+}
{DEFINE LOADDYNAMIC}
@@ -23,144 +24,150 @@ interface
uses
baseunix,
{$ifdef LOADDYNAMIC}
- dl,
+ dl,
{$endif}
initc;
const
- n=1;
+ n = 1;
+
type
- Piconv_t = ^iconv_t;
+ piconv_t = ^iconv_t;
iconv_t = pointer;
-
- Ticonv_open = function (__tocode:Pchar; __fromcode:Pchar):iconv_t;cdecl;
- Ticonv = function (__cd:iconv_t; __inbuf:PPchar; __inbytesleft:Psize_t; __outbuf:PPchar; __outbytesleft:Psize_t):size_t;cdecl;
- Ticonv_close = function (__cd:iconv_t):longint;cdecl;
+
+ Ticonv_open = function(__tocode: pchar; __fromcode: pchar): iconv_t; cdecl;
+ Ticonv = function(__cd: iconv_t; __inbuf: ppchar; __inbytesleft: psize_t; __outbuf: ppchar; __outbytesleft: psize_t): size_t; cdecl;
+ Ticonv_close = function(__cd: iconv_t): cint; cdecl;
{$IFNDEF LOADDYNAMIC}
{$ifndef Linux} // and other OSes with iconv in libc.
{$linklib iconv}
{$endif}
-function iconv_open (__tocode:Pchar; __fromcode:Pchar):iconv_t;cdecl; external;
-function iconv (__cd:iconv_t; __inbuf:PPchar; __inbytesleft:Psize_t; __outbuf:PPchar; __outbytesleft:Psize_t):size_t;cdecl; external;
-function iconv_close (__cd:iconv_t):longint;cdecl; external;
+function iconv_open(__tocode: pchar; __fromcode: pchar): iconv_t; cdecl; external;
+function iconv (__cd: iconv_t; __inbuf: ppchar; __inbytesleft: psize_t; __outbuf: ppchar; __outbytesleft: psize_t): size_t; cdecl; external;
+function iconv_close (__cd: iconv_t): cint; cdecl; external;
-var
- IconvLibFound: Boolean = False;
+var
+ IconvLibFound: boolean = False;
{$ELSE}
var
- iconv_lib: Pointer;
+ iconv_lib: pointer;
iconv_open: Ticonv_open;
iconv: Ticonv;
iconv_close: Ticonv_close;
- IconvLibFound: Boolean = True;
-function TryLoadLib(LibName:String;var error:string):Boolean; // can be used to load non standard libname
+ IconvLibFound: boolean = true;
+
+function TryLoadLib(LibName: string; var error: string): boolean; // can be used to load non standard libname
{$endif}
-function Iconvert(S:string;var res:string; FromEncoding, ToEncoding: string): cint;
-function InitIconv (Var error:string): Boolean;
+function Iconvert(s: string; var res: string; FromEncoding, ToEncoding: string): cint;
+function InitIconv(var error: string): boolean;
implementation
{$IFDEF LOADDYNAMIC}
-function TryLoadLib(LibName:String;var error:string):Boolean;
-function resolvesymbol (var funcptr; symbol:string):boolean;
+function TryLoadLib(LibName: string; var error: string): boolean;
-begin
- pointer(funcptr):=pointer(dlsym(iconv_lib, pchar(symbol)));
- result:=assigned(pointer(funcptr));
- if not result then
- error:=error+#13#10+dlerror();
-end;
-
-var res:boolean;
+ function resolvesymbol (var funcptr; symbol: string): Boolean;
+ begin
+ pointer(funcptr) := pointer(dlsym(iconv_lib, pchar(symbol)));
+ result := assigned(pointer(funcptr));
+ if not result then
+ error := error+#13#10+dlerror();
+ end;
+var
+ res: boolean;
begin
- result:=false;
- Error:=Error+#13#10'Trying '+LibName;
- iconv_lib:=dlopen(pchar(libname), RTLD_NOW);
+ result := false;
+ Error := Error+#13#10'Trying '+LibName;
+ iconv_lib := dlopen(pchar(libname), RTLD_NOW);
if Assigned(iconv_lib) then
begin
- result:=true;
- result := result and resolvesymbol(pointer(iconv),'iconv');
- result := result and resolvesymbol(pointer(iconv_open),'iconv_open');
- result := result and resolvesymbol(pointer(iconv_close),'iconv_close');
+ result := true;
+ result := result and resolvesymbol(pointer(iconv),'iconv');
+ result := result and resolvesymbol(pointer(iconv_open),'iconv_open');
+ result := result and resolvesymbol(pointer(iconv_close),'iconv_close');
// if not res then
// dlclose(iconv_lib);
- end
- else
- error:=error+#13#10+dlerror();
+ end else
+ error:=error+#13#10+dlerror();
end;
{$ENDIF}
-function InitIconv(Var error:string): Boolean;
+function InitIconv(var error: string): boolean;
begin
- result:=true;
+ result := true;
{$ifdef LOADDYNAMIC}
- error:='';
- if not TryLoadLib('libc.so.6',error) then
- if not TryLoadLib('libiconv.so',error) then
- result:=false;
+ error := '';
+ if not TryLoadLib('libc.so.6', error) then
+ if not TryLoadLib('libiconv.so', error) then
+ result := false;
{$endif}
- iconvlibfound:=iconvlibfound or result;
+ iconvlibfound := iconvlibfound or result;
end;
-function Iconvert(S:string;var res:string; FromEncoding, ToEncoding: string): cint;
+function Iconvert(S: string; var Res: string; FromEncoding, ToEncoding: string): cint;
var
InLen, OutLen, Offset: size_t;
- Src, Dst: PChar;
+ Src, Dst: pchar;
H: iconv_t;
lerr: cint;
- iconvres : cint;
+ iconvres: size_t;
begin
- H:=iconv_open(PChar(ToEncoding), PChar(FromEncoding));
+ H := iconv_open(PChar(ToEncoding), PChar(FromEncoding));
if not assigned(H) then
- begin
- Res:=S;
- Exit(-1);
+ begin
+ Res := S;
+ exit(-1);
end;
+
try
SetLength(Res, Length(S));
- InLen:=Length(S);
- OutLen:=Length(Res);
- Src:=PChar(S);
- Dst:=PChar(Res);
- while InLen>0 do
+ InLen := Length(S);
+ OutLen := Length(Res);
+ Src := PChar(S);
+ Dst := PChar(Res);
+
+ while InLen > 0 do
begin
- iconvres:= iconv(H, @Src, @InLen, @Dst, @OutLen);
- if iconvres=Cint(-1) then
+ iconvres := iconv(H, @Src, @InLen, @Dst, @OutLen);
+ if iconvres = size_t(-1) then
begin
- lerr:=cerrno;
- if lerr=ESysEILSEQ then // unknown char, skip
- begin
- Dst^:=Src^;
- Inc(Src);
- Inc(Dst);
- Dec(InLen);
- Dec(OutLen);
- end
- else
- if lerr=ESysE2BIG then
+ lerr := cerrno;
+ if lerr = ESysEILSEQ then // unknown char, skip
+ begin
+ Dst^ := Src^;
+ Inc(Src);
+ Inc(Dst);
+ Dec(InLen);
+ Dec(OutLen);
+ end
+ else
+ if lerr = ESysE2BIG then
begin
- Offset:=Dst-PChar(Res);
+ Offset := Dst - PChar(Res);
SetLength(Res, Length(Res)+InLen*2+5); // 5 is minimally one utf-8 char
- Dst:=PChar(Res)+Offset;
- OutLen:=Length(Res)-Offset;
+ Dst := PChar(Res) + Offset;
+ OutLen := Length(Res) - Offset;
end
else
exit(-1)
end;
end;
+
// iconv has a buffer that needs flushing, specially if the last char is not #0
- iconvres:=iconv(H, nil, nil, @Dst, @Outlen);
-
- SetLength(Res, Length(Res)-outlen);
+ iconv(H, nil, nil, @Dst, @Outlen);
+
+ // trim output buffer
+ SetLength(Res, Length(Res) - Outlen);
finally
iconv_close(H);
- end;
- result:=0;
+ end;
+
+ Result := 0;
end;
end.