[fpc-devel] Unicode support - for the 20th time... ;-)

dmitry boyarintsev skalogryz.lists at gmail.com
Thu Nov 20 13:29:43 CET 2008


Graeme

if the matter is only loading text from file, why don't you write your
own utility function to load files content. After text is loaded from
file, you could process them as WideStrings.

That's the way i'm doing. Since LCL handles UTF8 strings I always
encode widestrings for LCL components, but for internal processing i
use WideStrings.

For easy of use, i've written utility function, so loaded files (or
streams) are first checked if they're unicode. Unicode text are then
converted to required format (UTF8 or UTF16). See the attached unit.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Memo1.Text := ReadTextFile(OpenDialog1.FileName, true);
  // if file is UTF16 and utf16 mark is present, then ReadTextFile
will convert read WideString to the UTF8string
  // if i need WideString first, i'd use ReadTextFileWide, that reads
WideString (so no additional converting is used if UTF16 file is read)
  // but if file is UTF8 then ReadTextFileWide convert to UTF16.
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  prc : TProcess;
begin
  prc := TProcess.Create(nil);
  try
    prc.Options := [poUsePipes, poStderrToOutPut];
    prc.CommandLine := Edit1.Text;
    prc.Execute;
    Memo1.Text := ReadTextStreamWide(prc.Output);
  finally
    prc.Free;
  end;
end;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unicodefileutils.pas.zip
Type: application/zip
Size: 2558 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20081120/336512c8/attachment.zip>


More information about the fpc-devel mailing list