Sign in Join

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

Mailing lists / lazarus / 2013-03

Opening a file with spaces in the filename (again)

15 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

John

Hi List,

Using Laz 1.0.6, "included" fpc2.6.0 on win 7 - 32 bit:

I want to open a tiff file using what ever viewer the PC knows about that can do it (it will be irfanview on the test PC). It seemed that "OpenDocument" is the appropriate method. This works fine so long as the path has no spaces, but OpenDocument calls OpenURL, and that converts the spaces to %20 s - which is fine for browsers, but fails at least for irfanview (and other programs (eg dir) from a command line). Quoting the file name does not seem to make any difference - the %20 s are still substituted.

I notice there was considerable discussion about this around bug 21659 about a year ago, but I could not work out what the outcome was.

Am I using the right approach, or is there a better way to do it ? Or does this mean there is still a bug in OpenDocument ?

As far as I can remember, I have occasionally used OpenDocument to open a file (as opposed to a URL), and it should not be limited only to opening files in browsers.

thanks,
John Sunderland

Reply by email

Reinier Olislagers
to John
John wrote
I notice there was considerable discussion about this around bug 21659 about a year ago, but I could not work out what the outcome was.

It was blocked due to the patch not being Win9x compatible. AFAIK, after a long discussion (I think on this ML) it has since been decided that whoever is interested in maintaining Win9x compatibility should do so himself and not require others to adapt patches.

AFAIR, the patch we came up does work nicely.

Am I using the right approach, or is there a better way to do it ? Or does this mean there is still a bug in OpenDocument ?

Yes, there still is a bug in OpenDocument. Or perhaps there is indeed some magical way to do this; if so, I'd appreciate somebody documenting it at
http://wiki.lazarus.freepascal.org/opendocument

As far as I can remember, I have occasionally used OpenDocument to open a file (as opposed to a URL), and it should not be limited only to opening files in browsers.

Totally agreed.

Reply by email

Bart
to John: Using Laz 1.0.6, "included" fpc2.6.0 on win 7 - 32 bit: I want to open a tiff file using…
John wrote 5 lines
I want to open a tiff file using what ever viewer the PC knows about that can do it (it will be irfanview on the test PC). It seemed that "OpenDocument" is the appropriate method. This works fine so long as the path has no spaces, but OpenDocument calls OpenURL, and that converts the spaces to %20 s

In trunk it does not replace spaces with %20.
This was reverted in r36934.

Opne ($Lazarus)/cl/include/sysenvapis_win.inc and simply remove the line

 if Pos(' ', AURL) > 0 then
 	AURL := StringReplace(AURL, ' ', '%20', [rfReplaceAll, rfIgnoreCase]);

from OpenUrl() function and then rebuild your application (LCL should be rebuilt automatically).

r36934. should probably be merged to fixes branch.

@Reinier: this part of the reverting has nothing to do with Win9x compatibility, it would fail on Win7 as well.

The Win9x discussion was about quoting the file:// URI scheme. But on Windows you should probably not use this, because Windows does not have a default handler for file:// URI scheme and simply looks at the extension.

Bart

Reply by email

Bart
to Reinier Olislagers: It was blocked due to the patch not being Win9x compatible. AFAIK, after a long…
Reinier Olislagers wrote
It was blocked due to the patch not being Win9x compatible. AFAIK, after a long discussion (I think on this ML) it has since been decided that whoever is interested in maintaining Win9x compatibility should do so himself and not require others to adapt patches.

I can easily patch OpenURL to enclose the URL in double quotes for NT only. Problem is: are we sure it wil not break OpenUrl('file://some file.html') (Note the file:// URI scheme there)
It seems to work for my Win7 setup, but it should be tested on more systems.

And should this be done for WinCE also? I cannot test that at all.

I took the liberty to slightly alter the text.

Bart

Reply by email

Reinier Olislagers
to Bart: In trunk it does not replace spaces with %20. This was reverted in r36934. Opne…
Bart wrote 7 lines
On 3/5/13, John wrote: > I want to open a tiff file using what ever viewer the PC knows about > that can do it (it will be irfanview on the test PC). It seemed that > "OpenDocument" is the appropriate method. This works fine so long as > the path has no spaces, but OpenDocument calls OpenURL, and that > converts the spaces to %20 s

<snip>

@Reinier: this part of the reverting has nothing to do with Win9x compatibility, it would fail on Win7 as well.

Haven't followed whatever was reverted when...

The Win9x discussion was about quoting the file:// URI scheme. But on Windows you should probably not use this, because Windows does not have a default handler for file:// URI scheme and simply looks at the extension.

Having a working OpenDocument or documented behaviour would be nice. Perhaps you have a good idea for a patch?

Reply by email

Reinier Olislagers
to Bart: I can easily patch OpenURL to enclose the URL in double quotes for NT only. Problem is:…
Bart wrote 11 lines
On 3/5/13, Reinier Olislagers wrote: > It was blocked due to the patch not being Win9x compatible. AFAIK, after > a long discussion (I think on this ML) it has since been decided that > whoever is interested in maintaining Win9x compatibility should do so > himself and not require others to adapt patches. I can easily patch OpenURL to enclose the URL in double quotes for NT only. Problem is: are we sure it wil not break OpenUrl('file://some file.html') (Note the file:// URI scheme there) It seems to work for my Win7 setup, but it should be tested on more systems.

Ah sorry, replied to your other post before reading this. IIRC, I reported in the bug report on which machine my patch worked (Vista x64 likely, perhaps tested it on Win2K)

Why not add it to the Lazarus test suite (and ideally put it on a build server that executes it after running, and reports results but I digress)... people experiencing problems can run the suite to check.

Show quoted text 5 lines
And should this be done for WinCE also? I cannot test idea. I'd guess yes... but we'd need somebody to test, yes. > http://wiki.lazarus.freepascal.org/opendocument I took the liberty to slightly alter the text.

No problem, glad somebody cares!

Reply by email

Reinier Olislagers
to Bart: I can easily patch OpenURL to enclose the URL in double quotes for NT only. Problem is:…
Bart wrote 5 lines
On 3/5/13, Reinier Olislagers wrote: > http://wiki.lazarus.freepascal.org/opendocument I took the liberty to slightly alter the text.

Mmm, I don't really understand this part:

Also on Windows, opening a file using the file:// URI scheme may fail if there are spaces in the filename. This is unfortunately system and browser dependant. On "modern" Windows (XP and later) it may help to enclose the entire string in double quotes: "file://some file.htm", whereas this will fail on Win9x systems.

The article discusses opendocument, not openurl!?!? Why would you pass file://whatever in an opendocument call?

Reply by email

Bart
to Reinier Olislagers
Reinier Olislagers wrote 5 lines
Mmm, I don't really understand this part: The article discusses opendocument, not openurl!?!? Why would you pass file://whatever in an opendocument call?

The original bugreport was about OpenUrl.
And thing went sour (for 9x) when the file:// URI scheme was used and it was quoted, and it seemed it needed quoting on some NT based systems.

OpenDocument broke when escaping spaces was introduced in OpenUrl, and it was abandonded swiftly.

But you are right. It should not be on that page. I removed it. I'll make a note on the OpenUrl page instead.

Bart

Reply by email

Bart
to Bart
Bart wrote
The original bugreport was about OpenUrl. And thing went sour (for 9x) when the file:// URI scheme was used and it was quoted, and it seemed it needed quoting on some NT based systems.

I patched OpenURL on Windows to quote the URL if it is in file:// URI schem and contains spaces on NT platform.
This should fix the original issue of
http://bugs.freepascal.org/view.php?id=21659

Question remains if we should quote APath in OpenDocument?

AFAIK ShellExecute(0, nil,PChar(APath), nil, nil, SW_SHOWNORMAL) will always eventually call something like "\path\to\exe" "%1", where the %1 is quoted and will become "APath", otherwise no app would never be able to open files with spaces in the name via this route. (Meaning any sane app should register itself in such a way that quotes are forced around the parameter)

Bart

Reply by email

John
to Bart: In trunk it does not replace spaces with %20. This was reverted in r36934. Opne…
Bart wrote 9 lines
On 3/5/13, John wrote: > I want to open a tiff file using what ever viewer the PC knows about > that can do it (it will be irfanview on the test PC). It seemed that > "OpenDocument" is the appropriate method. This works fine so long as > the path has no spaces, but OpenDocument calls OpenURL, and that > converts the spaces to %20 s In trunk it does not replace spaces with %20. This was reverted in r36934.

I see this in trunk, but not in release 1.0.6, which purports to be r40xxx ? I upgraded when I saw the note about the reversion in the bug tracker. (I usually work from trunk on my development PC, but I am doing this on a separate PC, and wanted to keep the install as simple as possible).

Anyway, I removed the clause that inserts the %20s, it all works fine, so I can leave it at that for now.
(This, of course, is the joy of open source software!)

However, I can't help thinking that the REAL problem is that probably there were originally an OpenURL and an OpenDocument for a reason. OpenURL could be optimised for URLs, OpenDocuments for file system documents. OpenDocument could possibly look for a '://' and pass it off to OpenURL if it found one.

(Sorry, it is hard to keep up a "conversation" from GMT+11)

cheers,
John Sunderland

Reply by email

Alejandro Gonzalo
to John

If you only will be using Windows, you can use:
?
????ShellExecute(HWND(nil),nil,PChar('myFile.pdf'),PChar(''),nil ,1); ?
You will need ShellAPI in the uses clause.
?
A. G.

Show quoted text 36 lines
________________________________ From: John To: Lazarus mailing list Sent: Tuesday, March 5, 2013 3:23 PM Subject: Re: [Lazarus] Opening a file with spaces in the filename (again) On 03/06/2013 04:49 AM, Bart wrote: > On 3/5/13, John wrote: > >> I want to open a tiff file using what ever viewer the PC knows about >> that can do it (it will be irfanview on the test PC).? It seemed that >> "OpenDocument"? is the appropriate method.? This works fine so long as >> the path has no spaces, but OpenDocument calls OpenURL, and that >> converts the spaces to %20 s > In trunk it does not replace spaces with %20. > This was reverted in r36934. I see this in trunk, but not in release 1.0.6, which purports to be r40xxx ?? I upgraded when I saw the note about the reversion in the bug tracker.? (I usually work from trunk on my development PC, but I am doing this on a separate PC, and wanted to keep the install as simple as possible). Anyway, I removed the clause that inserts the %20s, it all works fine, so I can leave it at that for now. (This, of course, is the joy of open source software!) However, I can't help thinking that the REAL problem is that probably there were originally an OpenURL and an OpenDocument for a reason.? OpenURL could be optimised for URLs, OpenDocuments for file system documents.? OpenDocument could possibly look for a '://' and pass it off to OpenURL if it found one. (Sorry, it is hard to keep up a "conversation" from GMT+11) cheers, John Sunderland -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130305/9e48c2e1/attachment.html>

Reply by email

Reinier Olislagers
to Bart: I patched OpenURL on Windows to quote the URL if it is in file:// URI schem and contains…
Bart wrote 5 lines
On 3/5/13, Bart wrote: I patched OpenURL on Windows to quote the URL if it is in file:// URI schem and contains spaces on NT platform. This should fix the original issue of http://bugs.freepascal.org/view.php?id=21659

Nice - I updated
http://wiki.lazarus.freepascal.org/index.php?title=openurl Please correct if wrong.

Show quoted text 8 lines
Question remains if we should quote APath in OpenDocument? AFAIK ShellExecute(0, nil,PChar(APath), nil, nil, SW_SHOWNORMAL) will always eventually call something like "\path\to\exe" "%1", where the %1 is quoted and will become "APath", otherwise no app would never be able to open files with spaces in the name via this route. (Meaning any sane app should register itself in such a way that quotes are forced around the parameter)

My test program in
http://bugs.freepascal.org/file_download.php?file_id=15801&type=bug suggests doing nothing is fine ;)
Opening pdfs with sumatrapdf and text files with Notepad++ having spaces in their names works fine here.

Reply by email

Reinier Olislagers
to Alejandro Gonzalo: If you only will be using Windows, you can use: ?…
Alejandro Gonzalo wrote 6 lines
If you only will be using Windows, you can use: ShellExecute(HWND(nil),nil,PChar('myFile.pdf'),PChar(''),nil ,1); You will need ShellAPI in the uses clause.

OpenDocument calls OpenURL which calls ShellExecute (Laz trunk version below):

// Open a given URL with the default browser
function OpenURL(AURL: String): Boolean;
var
{$IFDEF WinCE}
  Info: SHELLEXECUTEINFO;
{$ELSE}
  ws: WideString;
  ans: AnsiString;
  IsFileUriWithSpaces, IsQuoted: Boolean;
const
  FileURIScheme = 'file://';
{$ENDIF}
begin
  Result := False;
  if AURL = '' then Exit;
  {$IFDEF WinCE}
  FillChar(Info, SizeOf(Info), 0);
  Info.cbSize := SizeOf(Info);
  Info.fMask := SEE_MASK_FLAG_NO_UI;
  Info.lpVerb := 'open';
  Info.lpFile := PWideChar(UTF8Decode(AURL));
  Result := ShellExecuteEx(@Info);
  {$ELSE}
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    //Urls that start with file:// are allowed to contain spaces and
should be quoted on NT platform,
    //but on Win9x quoting it fails
    //Since on Windows filenames cannot contain the " character, we need
not care about it and simply enclose the AURL
    IsFileUriWithSpaces := (Pos(#32,AURL) > 0) and
(CompareText(Copy(AURL,1,Length(FileURIScheme)), FileURIScheme) = 0);
    IsQuoted := (Length(AURL) > 1) and (AURL[1] = '"') and
(AURL[Length(AURL)] = '"');
    if IsFileUriWithSpaces and not IsQuoted then AURL := '"' + AURL + '"';
    ws := UTF8Decode(AURL);
    Result := ShellExecuteW(0, nil, PWideChar(ws), nil, nil,
SW_SHOWNORMAL) > 32;
  end
  else
  begin
    ans := Utf8ToAnsi(AURL); // utf8 must be converted to Windows
Ansi-codepage
    Result := ShellExecute(0, nil, PAnsiChar(ans), nil, nil,
SW_SHOWNORMAL) > 32;
  end;
  {$ENDIF}
end;

Reply by email