iconvtest.pp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 AnsiChar =
  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:AnsiString);
  25. var
  26. fn : String;
  27. res: Ansistring;
  28. f1:text;
  29. convres: integer;
  30. begin
  31. // note that while the iconvert function is easy, it opens and closes
  32. // a iconv handle each time, and also requires exceptions.
  33. // I do not know how costly this is.
  34. // also iconvert skips unknown chars (EILSEQ).
  35. convres:=Iconvert(inputstring,res,inputencoding,targetencoding);
  36. if convres=0 then
  37. begin
  38. fn:='result-'+targetencoding+'.txt';
  39. Writeln('Succes: writing file ',fn,' with results');
  40. assignfile(f1,fn);
  41. rewrite(f1);
  42. Write(f1,res);
  43. closefile(f1);
  44. end
  45. else
  46. Writeln('Failure for ',TargetEncoding,' error: ',convres);
  47. end;
  48. var s : string;
  49. begin
  50. {$IFDEF LOADDYNAMIC}
  51. if not InitIconv(s) then
  52. begin
  53. Writeln('Iconv initialization failed:',s);
  54. halt;
  55. end ;
  56. {$ENDIF}
  57. DoOneConversion('UTF-8');
  58. DoOneConversion('UTF-16');
  59. end.