Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-09

SynEdit.Lines.LoadFromFile and non-ascii filenames

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi,

After staring myself blind for several hours on why my program malfunctioned, I finally noticed a difference in behaviour between SynEdit.Lines.LoadFromFile and e.g. TMemo.Lines.LoadFromFile with regard to non-ascii characters in filenames.

As I understood it the proper way to open files, when getting the filename form a control like a TOpenDialog is to first convert the Utf8 string to Ansi/Systme-enccoding and then use this converted string in LoadFromFile():

begin
  Utf8Fn := Opendialog1.Filename;
  //convert to system encoding (I am on Windows), using FileUtils unit
  AnsiFn := Utf8ToSys(Utf8Fn);
  //Use converted string in LoadFromFile()
  SynEdit1.LoadFromFile(AnsiFn);
end;

This however throws an exception when using a filename like ?bc.txt. The exceptionmessage is:
Unable to open file "F:\LazarusProjecten\bugs\FileNames\?bc.txt" (Notice the ? in the name, this message was coverted back to Utf8 to show it in a TMemo)

If I do NOT convert the string first, the SynEdit loads the file just fine.

This behaviour however, is exactly the opposite of e.g TMemo, where you MUST convert the filename to Ansi/System encoding, or the LoadFromFile() will fail.

I find this a bit confusing, to say the least.

Does anyone know if this behaviour (and thus the discrepancy with e.g. TMemo) is by design or a bug?

B.t.w. I tested this behaviour on WinMe (Win9x)/vFat filesystem, and at first I thougt it might be just an OS issue, but the behaviour is the same on Win7/NTFS (Laz. r32445/Fpc 2.4.4).

Bart

Reply by email

Mattias Gaertner
to Bart
Show quoted text 31 lines
Hi, After staring myself blind for several hours on why my program malfunctioned, I finally noticed a difference in behaviour between SynEdit.Lines.LoadFromFile and e.g. TMemo.Lines.LoadFromFile with regard to non-ascii characters in filenames. As I understood it the proper way to open files, when getting the filename form a control like a TOpenDialog is to first convert the Utf8 string to Ansi/Systme-enccoding and then use this converted string in LoadFromFile(): begin Utf8Fn := Opendialog1.Filename; //convert to system encoding (I am on Windows), using FileUtils unit AnsiFn := Utf8ToSys(Utf8Fn); //Use converted string in LoadFromFile() SynEdit1.LoadFromFile(AnsiFn); end; This however throws an exception when using a filename like ?bc.txt. The exceptionmessage is: Unable to open file "F:\LazarusProjecten\bugs\FileNames\?bc.txt" (Notice the ? in the name, this message was coverted back to Utf8 to show it in a TMemo) If I do NOT convert the string first, the SynEdit loads the file just fine. This behaviour however, is exactly the opposite of e.g TMemo, where you MUST convert the filename to Ansi/System encoding, or the LoadFromFile() will fail.

TMemo.LoadFromFile is part of the FCL, which expects system encoding, which might not support unicode. TSynEdit is part of SynEdit which expects UTF8 encoding. All packages in the Lazarus sources expect UTF8 encoding.
For Delphi compatibility Lazarus uses the FCL classes.

I find this a bit confusing, to say the least.

Yes.
An Unicode FCL is planned. There is slow but steady progress.

Does anyone know if this behaviour (and thus the discrepancy with e.g. TMemo) is by design or a bug?

By design.

B.t.w. I tested this behaviour on WinMe (Win9x)/vFat filesystem, and at first I thougt it might be just an OS issue, but the behaviour is the same on Win7/NTFS (Laz. r32445/Fpc 2.4.4).

Mattias

Reply by email

Bart
to Mattias Gaertner

When I control-clicked on TSynEdit.Lines.LoadFromFile() I ended up in TSrings.LoadFromFile, which does
TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite). I also ended up here when control-clicking on TMemo.Lines.LoadFromFile.

So I did not really understand why it worked different.

Coming form Delphi(3) on Win9x, I still cannot get to grips with all this Unicode string stuff.
It would be nice if RTL and LCL would speak the same "string-encoding-language", or at least it would be transparent to the programmer.

That being said, it is not meant as critisizing Fpc/Lazarus. Current Lazarus/Fpc outperforms my old Delphi 3 by far. (Only the D3 compiler is much faster)
I now only use Delphi for comaparing Laz vs. Delphi behaviour if I suspect a bug in Laz.

Bart

Reply by email

Mattias Gaertner
to Bart
When I control-clicked on TSynEdit.Lines.LoadFromFile() I ended up in TSrings.LoadFromFile, which does TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite). I also ended up here when control-clicking on TMemo.Lines.LoadFromFile.

TSynEdit.Lines is a TSynEditLines, which uses TSynEditFileReader which does UTF8ToSys.

Show quoted text 7 lines
So I did not really understand why it worked different. Coming form Delphi(3) on Win9x, I still cannot get to grips with all this Unicode string stuff. It would be nice if RTL and LCL would speak the same "string-encoding-language", or at least it would be transparent to the programmer.

Yes.
And that's the goal. As I wrote: there is progress.

That being said, it is not meant as critisizing Fpc/Lazarus.

You are welcome. The Unicode problem is known.

Current Lazarus/Fpc outperforms my old Delphi 3 by far. (Only the D3 compiler is much faster) I now only use Delphi for comaparing Laz vs. Delphi behaviour if I suspect a bug in Laz.

Mattias

Reply by email