Recent

Author Topic: [SOLVED] Where to store your options of a program  (Read 11487 times)

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Where to store your options of a program
« on: April 19, 2016, 07:14:03 am »
Hi everyone,


I am trying to add options to my program. The form for that is finished and it all works.
But where can i store these options.


Since i am using a mac and not a windows-based machine.
Is it possible to store them in my recourses and if thats possible how can i access them?
« Last Edit: April 21, 2016, 09:26:46 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

balazsszekely

  • Guest
Re: Where to store your options of a program
« Reply #1 on: April 19, 2016, 07:31:43 am »
Quote
I am trying to add options to my program. The form for that is finished and it all works.
But where can i store these options.
Since i am using a mac and not a windows-based machine.
Is it possible to store them in my recourses and if thats possible how can i access them?
Options that can be changed by the user at run time? If yes then you cannot store it as resource. Save it to the following directory(as ini, txt, custom file,...):
~/Library/Application Support/YourApp/

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1216
    • Burdjia
Re: Where to store your options of a program
« Reply #2 on: April 19, 2016, 11:30:48 am »
Save it to the following directory(as ini, txt, custom file,...):
~/Library/Application Support/YourApp/
Better use RTL functions GetAppConfigDir and/or GetAppConfigFile.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

balazsszekely

  • Guest
Re: Where to store your options of a program
« Reply #3 on: April 19, 2016, 12:03:01 pm »
Yes, I agree API is always a better choise. Unfortunately none of the function will return the correct value under OSX(GetAppConfigDir and/or GetAppConfigFile). 



madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Where to store your options of a program
« Reply #6 on: April 19, 2016, 11:03:07 pm »
after reading all the suggested pages i have come to the conclusion that i need the following function to get the correct directory
Code: Pascal  [Select][+][-]
  1. GetAppConfigDir(False)
and that i probably need to write a .plist - file.
But how do i do that?
Are there standard functions for in Lazarus?
« Last Edit: April 19, 2016, 11:05:54 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

balazsszekely

  • Guest
Re: Where to store your options of a program
« Reply #7 on: April 20, 2016, 05:42:01 am »
Quote
after reading all the suggested pages i have come to the conclusion that i need the following function to get the correct directory
Code: Pascal  [Select][+][-]
  1. GetAppConfigDir(False)
You came to the wrong conclusion, I don't want to repeat myself but GetAppConfigDir might fail under OSX: http://bugs.freepascal.org/view.php?id=20706 . You don't have to believe me, just run a few test yourself.

Quote
and that i probably need to write a .plist - file.
But how do i do that?
Are there standard functions for in Lazarus?
"Under Mac OS X, in most cases config files are preference files, which should be XML files with the ending ".plist" and be stored in /Library/Preferences or ~/Library/Preferences with Names taken from the field "Bundle identifier" in the Info.plist of the application bundle."



mischi

  • Full Member
  • ***
  • Posts: 189
Re: Where to store your options of a program
« Reply #8 on: April 20, 2016, 08:44:47 am »
Maybe my solution in Heatwizard can serve as a starting point:

https://sourceforge.net/p/heatwizard/code-0/HEAD/tree/trunk/UPreferenceData.pas

Michael.

Thaddy

  • Hero Member
  • *****
  • Posts: 18711
  • To Europe: simply sell USA bonds: dollar collapses
Re: Where to store your options of a program
« Reply #9 on: April 20, 2016, 09:40:35 am »
That wiki is in some parts incorrect.
e.g. on Windows semi-permanent user preferences should be stored in the registry, not in appdata.
But anyway: the answer for mac is given by getmem.
And plz: read about your OS. That is really important. There are design specifics that govern where to store which data. Don't re-invent wheels unless you have to...
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Where to store your options of a program
« Reply #10 on: April 20, 2016, 11:24:23 am »
As i am reading on into this matter i found this function which seems do to the trick but what units does it use?

Code: Pascal  [Select][+][-]
  1. function GetPreferencesFolder: String;
  2.   { platform-independend method to search for the location of preferences folder}
  3. const
  4.   kMaxPath = 1024;
  5. var
  6.   {$IFDEF LCLCarbon}
  7.   theError: OSErr;
  8.   theRef: FSRef;
  9.   {$ENDIF}
  10.   pathBuffer: PChar;
  11. begin
  12.   {$IFDEF LCLCarbon}
  13.     try
  14.       pathBuffer := Allocmem(kMaxPath);
  15.     except on exception do exit;
  16.     end;
  17.     try
  18.       Fillchar(pathBuffer^, kMaxPath, #0);
  19.       Fillchar(theRef, Sizeof(theRef), #0);
  20.       theError := FSFindFolder(kOnAppropriateDisk, kPreferencesFolderType, kDontCreateFolder, theRef);
  21.       if (pathBuffer <> nil) and (theError = noErr) then
  22.       begin
  23.         theError := FSRefMakePath(theRef, pathBuffer, kMaxPath);
  24.         if theError = noErr then GetPreferencesFolder := UTF8ToAnsi(StrPas(pathBuffer)) + '/';
  25.       end;
  26.     finally
  27.       Freemem(pathBuffer);
  28.     end
  29.   {$ELSE}
  30.     GetPreferencesFolder := GetAppConfigDir(false);
  31.   {$ENDIF}
  32. end;
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

balazsszekely

  • Guest
Re: Where to store your options of a program
« Reply #11 on: April 20, 2016, 11:50:33 am »
Quote
As i am reading on into this matter i found this function which seems do to the trick but what units does it use?
uses MacOSAll;

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Where to store your options of a program
« Reply #12 on: April 20, 2016, 01:52:12 pm »
Are there functions in lazarus to write .plist or xml files?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

balazsszekely

  • Guest
Re: Where to store your options of a program
« Reply #13 on: April 20, 2016, 02:18:40 pm »
.plist is an XML file, you can read it with "Laz2_XMLCfg" unit for example. Since you're the only one who reads/writes those config files, you don't have to follow any rules. Just use a regular txt file and rename it to .plist, or leave it as .txt, it doesn't really matter. Perhaps an ini file will also do the job? It all depends on the data you want to save/load.
« Last Edit: April 20, 2016, 02:20:12 pm by GetMem »

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Where to store your options of a program
« Reply #14 on: April 20, 2016, 02:57:37 pm »
For now i only need to store 1 value and its either 1, 2 or 3.
So that's gonna be easy ;)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

 

TinyPortal © 2005-2018