Recent

Author Topic: [Solved] Opening files  (Read 10919 times)

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
[Solved] Opening files
« on: January 31, 2014, 12:04:07 am »
I am converting a Windows application to run in Linux. After some practice with cmod and chown, I think can control access to files and directories. My problem  is that although when I create a directory with a name like "mydir" I can read and write files there, if I create a directory with a name like ".mydir", I cannot access it.

This works:
Code: [Select]
   
      if (Key = VK_F1) or ((ssCtrl in Shift) and (Key = VK_H)) then
      if FileExistsUTF8('./help/help.html') then
      begin
        ShowMessage('Help file found.');
        OpenDocument('./help/help.html')
      end
      else ShowMessage('Help file not found.');

This does not:
Code: [Select]
    if (Key = VK_F1) or ((ssCtrl in Shift) and (Key = VK_H)) then
      if FileExistsUTF8('~/.mydir/help/help.html') then
      begin
        ShowMessage('Help file found.');
        OpenDocument('~/.mydir/help/help.html')
      end
      else ShowMessage('Help file not found.');

Is there some kind of delimiter required before the "." that is part of the directory name?
« Last Edit: February 07, 2014, 09:28:40 am by Windsurfer »

userx-bw

  • Full Member
  • ***
  • Posts: 178
Re: Opening files
« Reply #1 on: January 31, 2014, 01:08:17 am »


the Function opendocument
http://wiki.freepascal.org/opendocument

 Description

opendocument opens a document/file with the default viewer/editor registered with the operating system for that file/file extension.  E.g. on Windows, the code will use the registry to look up the file association for the extension.




« Last Edit: January 31, 2014, 01:58:51 am by userx-bw »
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
Re: Opening files
« Reply #2 on: January 31, 2014, 11:59:15 am »
Thanks userx-bw.

Opendocument works on both Windows and Linux, so long as the linux folder/directory does not have a "." in front of the name.

I have made sure the owner is the same for all files, and that all permissions are open. (Not good practice once debugging is finished.)

There seems to be something stopping the folder/directory from being seen.

I am using Laz1.2RC2 on a 64bit i7 Intel platform. Could this be a bug, because a dot (".") is a permitted file name character?


« Last Edit: January 31, 2014, 12:10:46 pm by Windsurfer »

userx-bw

  • Full Member
  • ***
  • Posts: 178
Re: Opening files
« Reply #3 on: January 31, 2014, 02:56:14 pm »
Thanks userx-bw.

Opendocument works on both Windows and Linux, so long as the linux folder/directory does not have a "." in front of the name.

I have made sure the owner is the same for all files, and that all permissions are open. (Not good practice once debugging is finished.)

There seems to be something stopping the folder/directory from being seen.

I am using Laz1.2RC2 on a 64bit i7 Intel platform. Could this be a bug, because a dot (".") is a permitted file name character?

I don't know that much about it yet myself, I just started programming.

I'm running Linux 32bit on a Acer D250 netbook for all my programming and fun. what unit are you using in uses to make the opendocument work because I tried calling that function and Lazarus said it had no idea what that was.

then I googled it and that is what I came up with. have you tried the openDialog yet?

Code: [Select]
procedure TForm1.Button1Click( Sender: TObject) ;
begin
    if OpenDialog1.Execute then
    OpenDocument(OpenDialog1.FileName);

  end; 

properties were set to ofForceShowHidden, that gets me an open file window that you can look for the dot directories and files. unit LCLIntf

found that code here

https://freepascalanswers.wordpress.com/2012/06/
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

Bart

  • Hero Member
  • *****
  • Posts: 3541
    • Bart en Mariska's Webstek
Re: Opening files
« Reply #4 on: January 31, 2014, 11:23:17 pm »
Define: does not work.
Does FileExistsUtf8 return False, or does it return True and OpenDocument fail?

Bart

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
Re: Opening files
« Reply #5 on: February 01, 2014, 07:47:07 pm »
userx-bw,

I have lclintf in my uses clause to call OpenDocument.

Bart,

FileExists, FileExistsUTF8, and OpenDocument all return a Bool, which shows as '0' with BoolToStr in all cases.

I have checked the permissions and ownership and they appear OK.



hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Opening files
« Reply #6 on: February 01, 2014, 08:54:08 pm »
I just checked this on Ubuntu. Had a guess from the beginning. Turned out: just like I thought:

Works:
Code: [Select]
OpenDocument('/home/hinst/.config/user-dirs.locale');
Does not work:
Code: [Select]
OpenDocument('~/.config/user-dirs.locale');
Seems like OpenDocument function does not substitute ~ symbol with home dir
Too late to escape fate

userx-bw

  • Full Member
  • ***
  • Posts: 178
Re: Opening files
« Reply #7 on: February 01, 2014, 09:08:28 pm »
I just checked this on Ubuntu. Had a guess from the beginning. Turned out: just like I thought:


Does not work:
Code: [Select]
OpenDocument('~/.config/user-dirs.locale');
Seems like OpenDocument function does not substitute ~ symbol with home dir

the

Quote
~/

is a relative path you have to have access to your PATH statements in Linux to get to it in order to use in in OpenDocument if it could even be done.

you'd have to somehow get the users name even if you are are to put the actual path to the file(s) if you don't have the user pick the directory.

ie.
Code: [Select]

OpenDirectory('/home/$USER/.config/')

My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Opening files
« Reply #8 on: February 01, 2014, 09:25:13 pm »
one can use GetUserDir function from sysutils unit
Code: [Select]
  OpenDocument(GetUserDir + '/.config/user-dirs.locale');
Too late to escape fate

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
Re: Opening files
« Reply #9 on: February 01, 2014, 11:39:51 pm »
Thanks userx-bw and hinst,

It works perfectly. I will now be able to use GetAppConfigDir properly.

I would not have worked this out on my own.

userx-bw

  • Full Member
  • ***
  • Posts: 178
Re: Opening files
« Reply #10 on: February 01, 2014, 11:46:39 pm »
Thanks userx-bw and hinst,

It works perfectly. I will now be able to use GetAppConfigDir properly.

I would not have worked this out on my own.

Your Welcome, glad it works now.  ;D
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

Bart

  • Hero Member
  • *****
  • Posts: 3541
    • Bart en Mariska's Webstek
Re: Opening files
« Reply #11 on: February 02, 2014, 11:09:48 am »
Expanding of the tilde (~) is noramlly done by the shell (bash, sh etc.), before the filename is passed to the proper API.

If you need expanding, then you can use ExpandFileNameUtf8() function.
This function will take care of the tilde on Unix like systems.
So, if the file in your example exists, then this should work:

Code: [Select]
    if (Key = VK_F1) or ((ssCtrl in Shift) and (Key = VK_H)) then
      if FileExistsUTF8(ExpandFileNameUtf8('~/.mydir/help/help.html')) then
      begin
        ShowMessage('Help file found.');
        OpenDocument(ExpandFileNameUtf8('~/.mydir/help/help.html'))
      end
      else ShowMessage('Help file not found.');

Bart

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
Re: Opening files
« Reply #12 on: February 02, 2014, 01:34:00 pm »
Thanks Bart,

It works perfectly. I'll update some of the Wiki entries to mention it.
[Edit] ExpandFileNameUTF8 is already mentioned in the Wiki, but I'll draw attention to it.
« Last Edit: February 02, 2014, 01:54:33 pm by Windsurfer »

Bart

  • Hero Member
  • *****
  • Posts: 3541
    • Bart en Mariska's Webstek
Re: Opening files
« Reply #13 on: February 02, 2014, 03:06:04 pm »
You may (or may not) need to know that ExpandFilename(UTF8) is not threadsafe on Windows.

Bart

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
Re: Opening files
« Reply #14 on: February 02, 2014, 04:42:15 pm »
Does that apply to all of the functions in FileUtil? I see from the MultiThreaded Application Tutorial that file operations are not CPU intensive.

It does not affect me yet. Is there a general way to know what is thread safe?