Recent

Author Topic: [SOLVED] Small Unicode Problem with Filenames  (Read 5166 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] Small Unicode Problem with Filenames
« on: May 22, 2014, 09:22:19 pm »
I have a Unicode problem.

A text file whose name contains Chinese chars is supposed to be "made friendly" on English language systems with :

filename := UTF8ToSys(OpenDialog1.Filename);.

Trouble is : ShowMessage(Filename); 
returns
"C:\temp\???.txt" for the selected file.

Any ideas?
« Last Edit: May 22, 2014, 11:00:15 pm by Gizmo »

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Small Unicode Problem with Filenames
« Reply #1 on: May 22, 2014, 09:25:40 pm »
what if ShowMessage expects UTF-8 string, not "sys" string
I believe all routines in LCL work with UTF-8 strings
Too late to escape fate

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Small Unicode Problem with Filenames
« Reply #2 on: May 22, 2014, 09:42:29 pm »
Pic says a thousand words...

Have tried casting the variables as widestring but same error.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Small Unicode Problem with Filenames
« Reply #3 on: May 22, 2014, 10:01:12 pm »
Try:

Code: [Select]
ShowMessage(AnsiToUTF8(FileName));

or

Code: [Select]
ShowMessage(AnsiToUTF8(OpenDialog.FileName));

Why do you convert it to Sys?
« Last Edit: May 22, 2014, 10:05:46 pm by typo »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Small Unicode Problem with Filenames
« Reply #4 on: May 22, 2014, 10:12:32 pm »
Hi Typo

We're getting there...

No change if I try :
Code: [Select]
filename := UTF8ToSys(OpenDialog1.Filename);
      ShowMessage(AnsiToUTF8(Filename));     

No change if I try
Code: [Select]

filename := UTF8ToSys(OpenDialog1.Filename);
      ShowMessage(UTF8ToAnsi(Filename)); 

I do get a Unicode filename in SHowMessage if I do this:

Code: [Select]
filename := AnsiToUTF8(OpenDialog1.Filename);
      ShowMessage(UTF8ToAnsi(Filename));
if FileExists(UTF8ToAnsi(filename)) then
       begin // this throws an error

but my program says the file does not exists or is invalid.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Small Unicode Problem with Filenames
« Reply #5 on: May 22, 2014, 10:31:39 pm »
See this:

Code: [Select]
var
  fname :string;
begin
  fname := '串.txt';
  if FileExistsUTF8(fname) then  // uses FileUtil
    ShowMessage('File ' + fname + ' exists.');
end;                             

This works fine if the file exists.
« Last Edit: May 22, 2014, 10:45:01 pm by typo »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Small Unicode Problem with Filenames
« Reply #6 on: May 22, 2014, 10:42:48 pm »
It seems to me that OpenDialog1.Filename is already a UTF8 encoded string, or so I assume based on the source code:
Quote
854    class function TWin32WSOpenDialog.GetFileName(ShellItem: IShellItem): String;
855    var
856    FilePath: LPWStr;
857    begin
858    if Succeeded(ShellItem.GetDisplayName(SIGDN(SIGDN_FILESYSPATH), @FilePath)) then
859    begin
860    Result := UTF16ToUTF8(WideString(FilePath));
861    CoTaskMemFree(FilePath);
862    end
863    else
864    Result := '';
865    end;

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Small Unicode Problem with Filenames
« Reply #7 on: May 22, 2014, 11:00:02 pm »
Superb Typo...thank you

For others:

Code: [Select]
filename := (OpenDialog1.Filename);
UTF8ToAnsi(Filename);
if FileExistsUTF8(filename) then
     

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] Small Unicode Problem with Filenames
« Reply #8 on: May 22, 2014, 11:36:18 pm »
For others:

Code: [Select]
filename := (OpenDialog1.Filename);
UTF8ToAnsi(Filename);
if FileExistsUTF8(filename) then
     
You are not using what UTF8ToAnsi returns. So your code is equivalent to:
Code: [Select]
filename := OpenDialog1.Filename;
if FileExistsUTF8(filename) then

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: [SOLVED] Small Unicode Problem with Filenames
« Reply #9 on: May 23, 2014, 12:29:28 am »
As engkin says, no need to convert OpenDialog.FileName.

 

TinyPortal © 2005-2018