iconvtest.pp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. program doiconv;
  2. {
  3. Copyright (c) 2008 by Marco van de Voort([email protected])
  4. member of the Free Pascal development team
  5. Test program for the iconvenc package.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright. (LGPL)
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. Seems not to have memory leaks atm. If you experience them, check procedure
  12. newcalc first.
  13. }
  14. {$mode objfpc}{$H+}
  15. uses
  16. SysUtils,
  17. iconvenc;
  18. // some random Hebrew string for testing in CP1255
  19. Const InputString : array[0..21] of char =
  20. (#$e0,#$e1,#$e2,#$e3,#$e4,#$e5,#$e6,#$e7,
  21. #$e8,#$e9,#$eb,#$ec,#$ee,#$f0,#$f1,#$f2,
  22. #$f4,#$f6,#$f7,#$f8,#$f9,#$fa);
  23. InputEncoding = 'CP1255';
  24. procedure DoOneConversion(TargetEncoding:string);
  25. var
  26. fn,res: string;
  27. f1:text;
  28. convres: integer;
  29. begin
  30. // note that while the iconvert function is easy, it opens and closes
  31. // a iconv handle each time, and also requires exceptions.
  32. // I do not know how costly this is.
  33. // also iconvert skips unknown chars (EILSEQ).
  34. convres:=Iconvert(inputstring,res,inputencoding,targetencoding);
  35. if convres=0 then
  36. begin
  37. fn:='result-'+targetencoding+'.txt';
  38. Writeln('Succes: writing file ',fn,' with results');
  39. assignfile(f1,fn);
  40. rewrite(f1);
  41. Write(f1,res);
  42. closefile(f1);
  43. end
  44. else
  45. Writeln('Failure for ',TargetEncoding,' error: ',convres);
  46. end;
  47. var s : string;
  48. begin
  49. {$IFDEF LOADDYNAMIC}
  50. if not InitIconv(s) then
  51. begin
  52. Writeln('Iconv initialization failed:',s);
  53. halt;
  54. end ;
  55. {$ENDIF}
  56. DoOneConversion('UTF-8');
  57. DoOneConversion('UTF-16');
  58. end.