mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-09-04 18:30:36 +02:00
* iconvenc packages, first version. Tested on Linux/x86 and FreeBSD/x86
git-svn-id: trunk@11407 -
This commit is contained in:
parent
c2e5f885c6
commit
0ea254295f
8
.gitattributes
vendored
8
.gitattributes
vendored
@ -2692,6 +2692,14 @@ packages/ibase/src/ibase60.pp svneol=native#text/plain
|
|||||||
packages/ibase/src/ibase60dyn.pp svneol=native#text/plain
|
packages/ibase/src/ibase60dyn.pp svneol=native#text/plain
|
||||||
packages/ibase/tests/testib40.pp svneol=native#text/plain
|
packages/ibase/tests/testib40.pp svneol=native#text/plain
|
||||||
packages/ibase/tests/testib60.pp svneol=native#text/plain
|
packages/ibase/tests/testib60.pp svneol=native#text/plain
|
||||||
|
packages/iconvenc/Makefile svneol=native#text/plain
|
||||||
|
packages/iconvenc/Makefile.fpc svneol=native#text/plain
|
||||||
|
packages/iconvenc/examples/Makefile svneol=native#text/plain
|
||||||
|
packages/iconvenc/examples/Makefile.fpc svneol=native#text/plain
|
||||||
|
packages/iconvenc/examples/iconvtest.pp svneol=native#text/plain
|
||||||
|
packages/iconvenc/fpmake.pp svneol=native#text/plain
|
||||||
|
packages/iconvenc/manifest.xml svneol=native#text/plain
|
||||||
|
packages/iconvenc/src/iconvenc.pas svneol=native#text/plain
|
||||||
packages/imagemagick/Makefile svneol=native#text/plain
|
packages/imagemagick/Makefile svneol=native#text/plain
|
||||||
packages/imagemagick/Makefile.fpc svneol=native#text/plain
|
packages/imagemagick/Makefile.fpc svneol=native#text/plain
|
||||||
packages/imagemagick/examples/image.png -text svneol=unset#image/png
|
packages/imagemagick/examples/image.png -text svneol=unset#image/png
|
||||||
|
2783
packages/iconvenc/Makefile
Normal file
2783
packages/iconvenc/Makefile
Normal file
File diff suppressed because it is too large
Load Diff
32
packages/iconvenc/Makefile.fpc
Normal file
32
packages/iconvenc/Makefile.fpc
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#
|
||||||
|
# Makefile.fpc for Free Pascal ImLib 1.x Packages
|
||||||
|
#
|
||||||
|
|
||||||
|
[package]
|
||||||
|
name=iconvenc
|
||||||
|
version=2.2.0
|
||||||
|
|
||||||
|
[target]
|
||||||
|
units=iconvenc
|
||||||
|
exampledirs=examples
|
||||||
|
|
||||||
|
[require]
|
||||||
|
libc=y
|
||||||
|
|
||||||
|
[compiler]
|
||||||
|
includedir=src
|
||||||
|
sourcedir=src
|
||||||
|
|
||||||
|
|
||||||
|
[install]
|
||||||
|
fpcpackage=y
|
||||||
|
fpcsubdir=packages
|
||||||
|
|
||||||
|
[default]
|
||||||
|
fpcdir=../..
|
||||||
|
|
||||||
|
[shared]
|
||||||
|
build=n
|
||||||
|
|
||||||
|
[rules]
|
||||||
|
.NOTPARALLEL:
|
2025
packages/iconvenc/examples/Makefile
Normal file
2025
packages/iconvenc/examples/Makefile
Normal file
File diff suppressed because it is too large
Load Diff
18
packages/iconvenc/examples/Makefile.fpc
Normal file
18
packages/iconvenc/examples/Makefile.fpc
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#
|
||||||
|
# Makefile.fpc for Free Pascal GTK 2.x.y Examples
|
||||||
|
#
|
||||||
|
|
||||||
|
[target]
|
||||||
|
programs=iconvtest
|
||||||
|
|
||||||
|
[require]
|
||||||
|
packages=iconvenc
|
||||||
|
|
||||||
|
[install]
|
||||||
|
fpcpackage=y
|
||||||
|
|
||||||
|
[default]
|
||||||
|
fpcdir=../../..
|
||||||
|
|
||||||
|
[rules]
|
||||||
|
.NOTPARALLEL:
|
69
packages/iconvenc/examples/iconvtest.pp
Executable file
69
packages/iconvenc/examples/iconvtest.pp
Executable file
@ -0,0 +1,69 @@
|
|||||||
|
program doiconv;
|
||||||
|
{
|
||||||
|
Copyright (c) 2008 by Marco van de Voort(marco@freepascal.org)
|
||||||
|
member of the Free Pascal development team
|
||||||
|
|
||||||
|
Test program for the iconvenc package.
|
||||||
|
|
||||||
|
See the file COPYING.FPC, included in this distribution,
|
||||||
|
for details about the copyright. (LGPL)
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
|
||||||
|
Seems not to have memory leaks atm. If you experience them, check procedure
|
||||||
|
newcalc first.
|
||||||
|
}
|
||||||
|
|
||||||
|
{$mode objfpc}{$H+}
|
||||||
|
|
||||||
|
uses
|
||||||
|
SysUtils,
|
||||||
|
iconvenc;
|
||||||
|
|
||||||
|
// some random Hebrew string for testing in CP1255
|
||||||
|
Const InputString : array[0..21] of char =
|
||||||
|
(#$e0,#$e1,#$e2,#$e3,#$e4,#$e5,#$e6,#$e7,
|
||||||
|
#$e8,#$e9,#$eb,#$ec,#$ee,#$f0,#$f1,#$f2,
|
||||||
|
#$f4,#$f6,#$f7,#$f8,#$f9,#$fa);
|
||||||
|
InputEncoding = 'CP1255';
|
||||||
|
|
||||||
|
procedure DoOneConversion(TargetEncoding:string);
|
||||||
|
var
|
||||||
|
fn,res: string;
|
||||||
|
f1:text;
|
||||||
|
convres: integer;
|
||||||
|
begin
|
||||||
|
|
||||||
|
// note that while the iconvert function is easy, it opens and closes
|
||||||
|
// a iconv handle each time, and also requires exceptions.
|
||||||
|
// I do not know how costly this is.
|
||||||
|
// also iconvert skips unknown chars (EILSEQ).
|
||||||
|
convres:=Iconvert(inputstring,res,inputencoding,targetencoding);
|
||||||
|
if convres=0 then
|
||||||
|
begin
|
||||||
|
fn:='result-'+targetencoding+'.txt';
|
||||||
|
Writeln('Succes: writing file ',fn,' with results');
|
||||||
|
assignfile(f1,fn);
|
||||||
|
rewrite(f1);
|
||||||
|
Write(f1,res);
|
||||||
|
closefile(f1);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
Writeln('Failure for ',TargetEncoding,' error: ',convres);
|
||||||
|
end;
|
||||||
|
|
||||||
|
var s : string;
|
||||||
|
begin
|
||||||
|
{$IFDEF LOADDYNAMIC}
|
||||||
|
if not InitIconv(s) then
|
||||||
|
begin
|
||||||
|
Writeln('Iconv initialization failed:',s);
|
||||||
|
halt;
|
||||||
|
end ;
|
||||||
|
{$ENDIF}
|
||||||
|
DoOneConversion('UTF-8');
|
||||||
|
DoOneConversion('UTF-16');
|
||||||
|
end.
|
||||||
|
|
29
packages/iconvenc/fpmake.pp
Normal file
29
packages/iconvenc/fpmake.pp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{$ifndef ALLPACKAGES}
|
||||||
|
{$mode objfpc}{$H+}
|
||||||
|
program fpmake;
|
||||||
|
|
||||||
|
uses fpmkunit;
|
||||||
|
|
||||||
|
Var
|
||||||
|
P : TPackage;
|
||||||
|
T : TTarget;
|
||||||
|
begin
|
||||||
|
With Installer do
|
||||||
|
begin
|
||||||
|
{$endif ALLPACKAGES}
|
||||||
|
|
||||||
|
P:=AddPackage('iconvenc');
|
||||||
|
{$ifdef ALLPACKAGES}
|
||||||
|
P.Directory:='iconvenc';
|
||||||
|
{$endif ALLPACKAGES}
|
||||||
|
P.Version:='2.0.0';
|
||||||
|
P.SourcePath.Add('src');
|
||||||
|
P.IncludePath.Add('src');
|
||||||
|
|
||||||
|
T:=P.Targets.AddUnit('iconvenc.pas');
|
||||||
|
|
||||||
|
{$ifndef ALLPACKAGES}
|
||||||
|
Run;
|
||||||
|
end;
|
||||||
|
end.
|
||||||
|
{$endif ALLPACKAGES}
|
13
packages/iconvenc/manifest.xml
Normal file
13
packages/iconvenc/manifest.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<packages>
|
||||||
|
<package name="iconvenc">
|
||||||
|
<version release="2" major="0" minor="0" suffix=""/>
|
||||||
|
<filename>iconvenc-2.0.0.zip</filename>
|
||||||
|
<author>Marco van de Voort</author>
|
||||||
|
<license>modified LGPL</license>
|
||||||
|
<email>marco@freepascal.org</email>
|
||||||
|
<dependencies>
|
||||||
|
<dependency><package packagename="rtl"/></dependency>
|
||||||
|
</dependencies>
|
||||||
|
</package>
|
||||||
|
</packages>
|
168
packages/iconvenc/src/iconvenc.pas
Executable file
168
packages/iconvenc/src/iconvenc.pas
Executable file
@ -0,0 +1,168 @@
|
|||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
|
See the file COPYING.FPC, included in this distribution,
|
||||||
|
for details about the copyright. (LGPL)
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
|
||||||
|
}
|
||||||
|
unit iconvenc;
|
||||||
|
interface
|
||||||
|
{$mode objfpc}{$H+}
|
||||||
|
|
||||||
|
{DEFINE LOADDYNAMIC}
|
||||||
|
|
||||||
|
uses
|
||||||
|
baseunix,
|
||||||
|
{$ifdef LOADDYNAMIC}
|
||||||
|
dl,
|
||||||
|
{$endif}
|
||||||
|
initc;
|
||||||
|
|
||||||
|
const
|
||||||
|
n=1;
|
||||||
|
type
|
||||||
|
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;
|
||||||
|
|
||||||
|
{$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;
|
||||||
|
|
||||||
|
var
|
||||||
|
IconvLibFound: Boolean = False;
|
||||||
|
|
||||||
|
{$ELSE}
|
||||||
|
var
|
||||||
|
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
|
||||||
|
{$endif}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
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');
|
||||||
|
// if not res then
|
||||||
|
// dlclose(iconv_lib);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error:=error+#13#10+dlerror();
|
||||||
|
end;
|
||||||
|
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
|
function InitIconv(Var error:string): Boolean;
|
||||||
|
var
|
||||||
|
err: PChar;
|
||||||
|
begin
|
||||||
|
result:=true;
|
||||||
|
{$ifdef LOADDYNAMIC}
|
||||||
|
error:='';
|
||||||
|
if not TryLoadLib('libc.so.6',error) then
|
||||||
|
if not TryLoadLib('libiconv.so',error) then
|
||||||
|
result:=false;
|
||||||
|
{$endif}
|
||||||
|
end;
|
||||||
|
|
||||||
|
function Iconvert(S:string;var res:string; FromEncoding, ToEncoding: string): cint;
|
||||||
|
var
|
||||||
|
InLen, OutLen, Offset: longint;
|
||||||
|
Src, Dst: PChar;
|
||||||
|
H: iconv_t;
|
||||||
|
lerr: cint;
|
||||||
|
iconvres : cint;
|
||||||
|
begin
|
||||||
|
H:=iconv_open(PChar(ToEncoding), PChar(FromEncoding));
|
||||||
|
if not assigned(H) then
|
||||||
|
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
|
||||||
|
begin
|
||||||
|
iconvres:= iconv(H, @Src, @InLen, @Dst, @OutLen);
|
||||||
|
if iconvres=Cint(-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
|
||||||
|
begin
|
||||||
|
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;
|
||||||
|
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);
|
||||||
|
finally
|
||||||
|
iconv_close(H);
|
||||||
|
end;
|
||||||
|
result:=0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user