Recent

Author Topic: [Solved] AssignFile does not work with russian chars on windows.  (Read 5524 times)

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Hi,

why doesn't assignfile work with a file like 'Lumen - Дух времени.mp3' in windows. It does work in Linux and MacOS.

Code: [Select]
var bestand: string;
      Sourcefile: file;
...
bestand := opendialog1.filename;
...
assignfile(sourcefile,bestand);
reset(sourcefile,1);
closefile(sourcefile);


I have tried utf8tosys() utf8toansi(), but no luck
I have read help about systoutf8() and utf8tosys(), but it does not help with these chars.
If I am not mistaking, Windows works with utf16.

Accented and french chars do work.

Using Lazarus 1.2.6
« Last Edit: January 25, 2015, 06:51:42 pm by Zittergie »
Be the difference that makes a difference

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: AssignFile does not work with russian chars on windows.
« Reply #1 on: January 25, 2015, 05:12:45 pm »
Is it true, that the system you're trying to open the file is not Cyrillic based?

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Re: AssignFile does not work with russian chars on windows.
« Reply #2 on: January 25, 2015, 05:17:51 pm »
Indeed.
I am using a dutch Windows 8.1 to compile and test

Something about the codepages ?
Be the difference that makes a difference

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: AssignFile does not work with russian chars on windows.
« Reply #3 on: January 25, 2015, 05:25:03 pm »
Indeed.
I am using a dutch Windows 8.1 to compile and test

Something about the codepages ?
Yes. RTL ( assignfile() function ) is ANSI based (it would remain so until the next FPC release) - that means that Window's *A functions are used..

Cyrillic characters are not part of Dutch ANSI code page, thus it's impossible to open a file with russian characters in the name using *A functions on Dutch windows. It's possible, if you open the file using short 8.3 name. Short name contains only native (dutch) characters. Short name could be found in FindData.cAlternateFileName of TSearchRecreturned by FindFirst/FindNext functions. But it's windows specific.
Instead *W functions must be used, and they're not yet used by FPC RTL.

If possible, you can rewrite the code to use streams. LazUTF8 provides enough wrappers to open the file. Here's an example
Code: [Select]
var
  h : THandleStream;
begin
  h := THandleStream.Create( FileOpenUTF8(Edit1.Text, fmOpenRead or fmShareDenyNone) );
  try
    Memo1.Lines.LoadFromStream(h);
  finally
    h.Free;
  end;
This code works with any file name and with any windows codepage used.
« Last Edit: January 25, 2015, 05:28:49 pm by skalogryz »

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Re: AssignFile does not work with russian chars on windows.
« Reply #4 on: January 25, 2015, 05:32:21 pm »
Thanks,
did know that I had to switch to filestreams sooner or later, but you know, old habits die hard.
Be the difference that makes a difference

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: AssignFile does not work with russian chars on windows.
« Reply #5 on: January 25, 2015, 05:42:11 pm »
there's nothing wrong with old habits. You just need to adapt them to this changing world ;)

Try AssignFileUTF8. It would work on Dutch window for Russian file name BUT make sure these "todos" are implemented!
Code: [Select]
procedure AssignFileUTF8(out f:text; const nameutf8: string);
var
  r: TSearchRec;
  nm : string;
begin
  {$ifdef mswindows}
  //todo: add error handling here and thown an error as RTL AssignFile would do
  FindFirstUTF8(nameutf8, faAnyFile, r);
  nm:=r.FindData.cAlternateFileName;
  FindCloseUTF8(r);
  //todo: the full path must be resolved, with directories must be also short-named.
  AssignFile(f, ExtractFilePath(nameutf8)+nm);
  {$else}
  AssignFile(f, nameutf8);
  {$endif}
end;

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
[Solved] Re: AssignFile does not work with russian chars on windows.
« Reply #6 on: January 25, 2015, 06:51:14 pm »
The AssignFileUTF8 procedure works fine.
Tested on folders without Cyrillic chars ;)
Be the difference that makes a difference

 

TinyPortal © 2005-2018