Lazarus

Free Pascal => Other OS => Topic started by: BeanzMaster on August 13, 2020, 10:54:24 pm

Title: [SOLVED][MACOS] Loading file with relative path problem
Post by: BeanzMaster on August 13, 2020, 10:54:24 pm
Hi to all i have a little problem, and can say how to solve it

I have several applications that I compile under different OS (Windows, Linux and Mac)
In order not to duplicate my resource files, I have a folder structure like below

-Root
   - Demos
      - Resources
        - images
        - sounds
      - App1
         - Bin
            - Windows
               - compileMode
                  - cpu/App1.exe
            - Linux
               - compileMode
                  - cpu/App1
            - MacOS
               - compileMode
                  - cpu/App1.app
      - App2
         - Bin
            - Windows
               - compileMode
                  - cpu/App2.exe
            - Linux
               - compileMode
                  - cpu/App2
            - MacOS
               - compileMode
                  - cpu/App2.app
       - etc...

In my code i have

Code: Pascal  [Select][+][-]
  1. FTextureBuffer.LoadFromFile('../../../../../media/images/texturemaphq.tga');

It work under Windows and Linux but not on MacOS
Note i say the folder of MacOS app is in "Content/MacOS"

My problem is how to accessing to the Resources folder from the current folder of the executable ?

I tried by adding "../" twice but it's not work.

The problem is that the folder is in the back. If it will be a subfolder I wouldn't have any problems

Thanks in advance
Title: Re: [MACOS] Loading file with relative path problem
Post by: jwdietrich on August 13, 2020, 11:06:01 pm
You might want to check for the working directory with GetCurrentDir.
Title: Re: [MACOS] Loading file with relative path problem
Post by: BeanzMaster on August 13, 2020, 11:30:44 pm
You might want to check for the working directory with GetCurrentDir.

Get the current dir don't solve the problem. Like i said resources folder is not a subfolder. Under Linux and Windows we get directly the right folder but not under MacOS. I think i'll must do a special function to get the absolute path.
Title: Re: [MACOS] Loading file with relative path problem
Post by: winni on August 14, 2020, 12:00:18 am
Hi!

start with

fname := application.exename;

Then start counting from the end the number of slashes plus one.
Then you got the offset were to delete the right part of fname.

Now you got the absolute path.
That should solve it.

On a normal system.
I don't know nothing about the Mac.

Winni
Title: Re: [MACOS] Loading file with relative path problem
Post by: trev on August 14, 2020, 03:09:41 am
If the issue is locating the macOS application bundle's resources directory, then see the Wiki Locating the macOS application resources directory (https://wiki.freepascal.org/Locating_the_macOS_application_resources_directory).

(It might have been better to post in the Forum » Programming » Operating Systems » macOS / Mac OS X (https://forum.lazarus.freepascal.org/index.php/board,33.0.html) forum as I nearly missed this post.)
Title: Re: [MACOS] Loading file with relative path problem
Post by: BeanzMaster on August 14, 2020, 10:18:07 am
Hi
If the issue is locating the macOS application bundle's resources directory, then see the Wiki Locating the macOS application resources directory (https://wiki.freepascal.org/Locating_the_macOS_application_resources_directory).

I say that and
 is  exactly what i won't do because i have more than 20 applications demos and also because i don't provide any executable only source code

(It might have been better to post in the Forum » Programming » Operating Systems » macOS / Mac OS X (https://forum.lazarus.freepascal.org/index.php/board,33.0.html) forum as I nearly missed this post.)

No problem i don't take care. It will be for the next problem ;)

start with

fname := application.exename;

Then start counting from the end the number of slashes plus one.
Then you got the offset were to delete the right part of fname.

Now you got the absolute path.
That should solve it.


That's exactly what i was thinking  :)

Cheers
Title: Re: [MACOS] Loading file with relative path problem
Post by: BeanzMaster on August 14, 2020, 12:18:51 pm
If someone need something similar i'm doing like this

Code: Pascal  [Select][+][-]
  1. function GetApplicationPath : string;
  2. begin
  3.   {$IFDEF DARWIN}
  4.   result := Copy(Application.ExeName, 1, Pos(ApplicationName + '.app', Application.ExeName) - 1);
  5.   {$ELSE}
  6.   Result :=  Application.Location;
  7.   {$ENDIF}
  8.   Result :=  IncludeTrailingPathDelimiter(Result);
  9. end;
  10.  
  11. function GetAbsolutePathFromApp(SearchPath : String) : String;
  12. Var
  13.   RelTag : String;
  14.   AppPath : String;
  15.   TmpRelPath : String;
  16.   cnt,i : Integer;
  17. begin
  18.   RelTag := '..' + pathdelim;
  19.   AppPath := GetApplicationPath;
  20.   TmpRelPath := FixPathDelimiter(SearchPath);
  21.   if (Pos(RelTag, TmpRelPath) > 0) then
  22.   begin
  23.     cnt := 0;
  24.     while (Pos(RelTag, TmpRelPath) > 0) do
  25.     begin
  26.       TmpRelPath := Copy(TmpRelPath,4,Length(TmpRelPath));
  27.       inc(cnt);
  28.     end;
  29.  
  30.     if cnt > 0 then
  31.     begin
  32.       for i:= 1 to cnt do
  33.       begin
  34.         AppPath := ExpandFileName(IncludeTrailingPathDelimiter(AppPath) + '..');
  35.       end;
  36.     end;
  37.  
  38.     Result := IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(AppPath) + TmpRelPath);
  39.   end
  40.   else
  41.   begin
  42.     Result := IncludeTrailingPathDelimiter(IncludeTrailingPathDelimiter(AppPath) + SearchPath);
  43.   end;
  44. end;  
TinyPortal © 2005-2018