Browse Source

* trim output buffer in try..finally, catch result iconv in variable.

git-svn-id: trunk@26008 -
marco 11 years ago
parent
commit
b4496c4aba
1 changed files with 7 additions and 4 deletions
  1. 7 4
      packages/iconvenc/src/iconvert.inc

+ 7 - 4
packages/iconvenc/src/iconvert.inc

@@ -8,7 +8,9 @@ var
   InLen, OutLen, Offset: size_t;
   Src, Dst: pchar;
   H: iconv_t;
+  {$ifndef noerrnoiconv}
   lerr: cint;
+  {$endif}
   iconvres: size_t;
 begin
   H := iconv_open(PChar(ToEncoding), PChar(FromEncoding));
@@ -81,7 +83,7 @@ begin
     end;
    
     // iconv has a buffer that needs flushing, specially if the last char is not #0
-    iconv(H, nil, nil, @Dst, @Outlen);
+    iconvres:=iconv(H, nil, nil, @Dst, @Outlen);
     lerr:=cerrno;
     if (iconvres=size_t(-1)) and (lerr=ESysE2BIG) then
       begin
@@ -94,12 +96,13 @@ begin
    {$endif}
     // trim output buffer
     SetLength(Res, Length(Res) - Outlen);
-  finally Offset:=Dst-PChar(Res);
+  finally 
+        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;
-        iconv(H, nil, nil, @Dst, @Outlen);
-
+        iconvres:=iconv(H, nil, nil, @Dst, @Outlen);
+        setlength(Res,Length(Res) - Outlen);
     iconv_close(H);
   end;