home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / Chip_2004-03_cd1.bin / program / delphi / download / kompon / d234567 / DIIconv.exe / Source / DIIconv.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-01-02  |  6.8 KB  |  321 lines

  1. {-------------------------------------------------------------------------------
  2.  
  3.  DIIconv - libiconv character set conversion library for Borland Delphi.
  4.  
  5.  Copyright (c) 2003-2004 The Delphi Inspiration - Ralf Junker
  6.  Internet: http://www.zeitungsjunge.de/delphi/
  7.  E-Mail:   delphi@zeitungsjunge.de
  8.  
  9.  This library is free software; you can redistribute it and/or
  10.  modify it under the terms of the GNU Library General Public
  11.  License as published by the Free Software Foundation; either
  12.  version 2 of the License, or (at your option) any later version.
  13.  
  14.  This library is distributed in the hope that it will be useful,
  15.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17.  Library General Public License for more details.
  18.  
  19.  You should have received a copy of the GNU Library General Public
  20.  License along with this library; if not, write to the Free
  21.  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  22.  MA 02111-1307, USA.
  23.  
  24. -------------------------------------------------------------------------------}
  25.  
  26. unit DIIconv;
  27.  
  28. {$I DI.inc}
  29.  
  30. interface
  31.  
  32. const
  33.  
  34.   EINVAL = 19;
  35.  
  36.   E2BIG = 20;
  37.  
  38.   EILSEQ = 50;
  39.  
  40.   ICONV_TRIVIALP = 0;
  41.  
  42.   ICONV_GET_TRANSLITERATE = 1;
  43.  
  44.   ICONV_SET_TRANSLITERATE = 2;
  45.  
  46.   ICONV_GET_DISCARD_ILSEQ = 3;
  47.  
  48.   ICONV_SET_DISCARD_ILSEQ = 4;
  49.  
  50. type
  51.  
  52.   iconv_t = Pointer;
  53.  
  54. function iconv_open(
  55.   const tocode, fromcode: PAnsiChar): iconv_t;
  56.  
  57. function iconv(
  58.   const CD: iconv_t;
  59.   var inbuf: Pointer;
  60.   var inbytesleft: Cardinal;
  61.   var outbuf: Pointer;
  62.   var outbytesleft: Cardinal): Integer;
  63.  
  64. function iconv_close(
  65.   const CD: iconv_t): Integer;
  66.  
  67. type
  68.   TAnsiCharArray = array[0..MaxInt div SizeOf(PAnsiChar) - 1] of PAnsiChar;
  69.   PAnsiCharArray = ^TAnsiCharArray;
  70.  
  71.   TIconvListFunc = function(const NamesCount: Cardinal; const Names: PAnsiCharArray; const Data: Pointer): Integer;
  72.  
  73. procedure iconvlist(do_one: TIconvListFunc; const Data: Pointer);
  74.  
  75. function errno: Integer;
  76.  
  77. implementation
  78.  
  79. uses
  80.   Windows, SysUtils
  81.  
  82.   ;
  83.  
  84. function _malloc(const Size: Cardinal): Pointer; cdecl;
  85. begin
  86.   GetMem(Result, Size);
  87. end;
  88.  
  89. procedure _free(const p: Pointer); cdecl;
  90. begin
  91.   FreeMem(p);
  92. end;
  93.  
  94. function _memcmp(const s1: Pointer; const s2: Pointer; const n: Cardinal): Integer; cdecl;
  95. label
  96.   Success;
  97. type
  98.   TByte4 = packed record
  99.     b0, b1, b2, b3: Byte;
  100.   end;
  101.   PByte4 = ^TByte4;
  102. var
  103.   p1, p2: PByte4;
  104.   b1, b2: Byte;
  105.   l: Cardinal;
  106. begin
  107.   p1 := s1;
  108.   p2 := s2;
  109.   if p1 = p2 then goto Success;
  110.   l := n;
  111.  
  112.   repeat
  113.     if l = 0 then goto Success;
  114.     b1 := p1^.b0; b2 := p2^.b0;
  115.     if b1 <> b2 then Break;
  116.     Dec(l);
  117.  
  118.     if l = 0 then goto Success;
  119.     b1 := p1^.b1; b2 := p2^.b1;
  120.     if b1 <> b2 then Break;
  121.     Dec(l);
  122.  
  123.     if l = 0 then goto Success;
  124.     b1 := p1^.b2; b2 := p2^.b2;
  125.     if b1 <> b2 then Break;
  126.     Dec(l);
  127.  
  128.     if l = 0 then goto Success;
  129.     b1 := p1^.b3; b2 := p2^.b3;
  130.     if b1 <> b2 then Break;
  131.  
  132.     Inc(p1);
  133.     Inc(p2);
  134.     Dec(l);
  135.   until False;
  136.  
  137.   Result := b1 - b2;
  138.   Exit;
  139.  
  140.   Success:
  141.   Result := 0;
  142. end;
  143.  
  144. function _memset(const s: Pointer; const c: Integer; const n: Cardinal): Pointer; cdecl;
  145. begin
  146.   Result := s;
  147.   FillChar(Result^, n, c);
  148. end;
  149.  
  150. type
  151.   TQSortCompareFunc = function(const p1, p2: Pointer): Integer; cdecl;
  152.  
  153. procedure _qsort(
  154.   const baseP: Pointer;
  155.   const NElem: Cardinal;
  156.   const Width: Integer;
  157.   const comparF: TQSortCompareFunc); cdecl;
  158.  
  159.   procedure Exchange(leftP, rightP: PAnsiChar);
  160.   var
  161.     i: Integer;
  162.     c: AnsiChar;
  163.   begin
  164.     i := Width;
  165.     while i > 0 do
  166.       begin
  167.         c := rightP^;
  168.         rightP^ := leftP^;
  169.         Inc(rightP);
  170.         leftP^ := c;
  171.         Inc(leftP);
  172.         Dec(i);
  173.       end;
  174.   end;
  175.  
  176.   procedure qSortHelp(pivotP: PAnsiChar; NElem: Integer);
  177.   label
  178.     TailRecursion, qBreak;
  179.   var
  180.     leftP, rightP, pivotEnd, pivotTemp, leftTemp: PAnsiChar;
  181.     lNum: Integer;
  182.     retval: Integer;
  183.   begin
  184.  
  185.     TailRecursion:
  186.     if NElem <= 2 then
  187.       begin
  188.         if NElem = 2 then
  189.           begin
  190.             rightP := pivotP + Width;
  191.             if comparF(pivotP, rightP) > 0 then
  192.               Exchange(pivotP, rightP);
  193.           end;
  194.         Exit;
  195.       end;
  196.  
  197.     rightP := (NElem - 1) * Width + pivotP;
  198.     leftP := (NElem shr 1) * Width + pivotP;
  199.  
  200.     if comparF(leftP, rightP) > 0 then
  201.       Exchange(leftP, rightP)
  202.     else
  203.       if comparF(leftP, pivotP) > 0 then
  204.         Exchange(leftP, pivotP)
  205.       else
  206.         if comparF(pivotP, rightP) > 0 then
  207.           Exchange(pivotP, rightP);
  208.  
  209.     if (NElem = 3) then
  210.       begin
  211.         Exchange(pivotP, leftP);
  212.         Exit;
  213.       end;
  214.  
  215.     leftP := pivotP + Width;
  216.     pivotEnd := leftP;
  217.  
  218.     repeat
  219.       retval := comparF(leftP, pivotP);
  220.       while retval <= 0 do
  221.         begin
  222.           if retval = 0 then
  223.             begin
  224.               Exchange(leftP, pivotEnd);
  225.               Inc(pivotEnd, Width);
  226.             end;
  227.           if (leftP < rightP) then
  228.             Inc(leftP, Width)
  229.           else
  230.             goto qBreak;
  231.           retval := comparF(leftP, pivotP);
  232.         end;
  233.  
  234.       while leftP < rightP do
  235.         begin
  236.           retval := comparF(pivotP, rightP);
  237.           if retval < 0 then
  238.             Dec(rightP, Width)
  239.           else
  240.             begin
  241.               Exchange(leftP, rightP);
  242.               if retval <> 0 then
  243.                 begin
  244.                   Inc(leftP, Width);
  245.                   Dec(rightP, Width);
  246.                 end;
  247.               Break;
  248.             end;
  249.         end;
  250.  
  251.     until not (leftP < rightP);
  252.  
  253.     qBreak:
  254.  
  255.     if comparF(leftP, pivotP) <= 0 then
  256.       Inc(leftP, Width);
  257.  
  258.     leftTemp := leftP - Width;
  259.  
  260.     pivotTemp := pivotP;
  261.  
  262.     while (pivotTemp < pivotEnd) and (leftTemp >= pivotEnd) do
  263.       begin
  264.         Exchange(pivotTemp, leftTemp);
  265.         Inc(pivotTemp, Width);
  266.         Dec(leftTemp, Width);
  267.       end;
  268.  
  269.     lNum := (leftP - pivotEnd) div Width;
  270.     NElem := ((NElem * Width + pivotP) - leftP) div Width;
  271.  
  272.     if NElem < lNum then
  273.       begin
  274.         qSortHelp(leftP, NElem);
  275.         NElem := lNum;
  276.       end
  277.     else
  278.       begin
  279.         qSortHelp(pivotP, lNum);
  280.         pivotP := leftP;
  281.       end;
  282.  
  283.     goto TailRecursion;
  284.   end;
  285.  
  286. begin
  287.   if Width = 0 then Exit;
  288.   qSortHelp(baseP, NElem);
  289. end;
  290.  
  291. var
  292.   LocaleCharset: array[0..12] of AnsiChar = 'CP';
  293.  
  294. function locale_charset: PAnsiChar; cdecl;
  295. var
  296.   s: ShortString;
  297. begin
  298.   Str(GetACP, s);
  299.   StrLCopy(@LocaleCharset[2], @s[1], SizeOf(LocaleCharset) - 2);
  300.   LocaleCharset[Length(s) + 2] := #0;
  301.   Result := LocaleCharset;
  302. end;
  303.  
  304. var
  305.   _errno: Integer;
  306.  
  307. function errno: Integer;
  308. begin
  309.   Result := _errno;
  310. end;
  311.  
  312. {$L iconv.obj}
  313.  
  314. function iconv_open; external;
  315. function iconv; external;
  316. function iconv_close; external;
  317. procedure iconvlist; external;
  318.  
  319. end.
  320.  
  321.