Recent

Author Topic: [SOLVED]program not reading the ini file.  (Read 4513 times)

MadMike

  • New Member
  • *
  • Posts: 32
[SOLVED]program not reading the ini file.
« on: September 07, 2021, 02:39:09 pm »
hi,

Curious behavior.

Wrote and tested an application on a Raspberry Pi, and it accesses its ini file no problem.

Ran the app. from the command prompt, no problem.

Added a desktop entry and ran it from there, doesn't read the ini file.

The ini is in the same directory as the app.

Am I missing something?

(Nearly posted this in the beginners section, because I am one!)

thanks for any help

mike
« Last Edit: September 07, 2021, 04:31:35 pm by MadMike »

balazsszekely

  • Guest
Re: program not reading the ini file.
« Reply #1 on: September 07, 2021, 02:47:31 pm »
hi,

Curious behavior.

Wrote and tested an application on a Raspberry Pi, and it accesses its ini file no problem.

Ran the app. from the command prompt, no problem.

Added a desktop entry and ran it from there, doesn't read the ini file.

The ini is in the same directory as the app.

Am I missing something?

(Nearly posted this in the beginners section, because I am one!)

thanks for any help

mike

Are you using relative of absolute path? Since you are running the application from a desktop entry, I would use absolute path, something like this:
Code: Pascal  [Select][+][-]
  1. uses IniFiles, LazFileUtils;
  2.  
  3. //...
  4.  
  5. var
  6.   MyIniFile: TIniFile;
  7.   Path: string;
  8. begin
  9.   Path := AppendPathDelim(ExtractFilePath(Application.ExeName)) + 'MyIni.ini';
  10.   MyIniFile := TIniFile.Create(Path);
  11.   try
  12.     //do something with ini
  13.   finally
  14.     MyIniFile.Free
  15.   end;
  16. end;
  17.  

MadMike

  • New Member
  • *
  • Posts: 32
Re: program not reading the ini file.
« Reply #2 on: September 07, 2021, 04:31:02 pm »
GetMem,

Works a treat. Thanks very much.

learning every day...

Mike

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [SOLVED]program not reading the ini file.
« Reply #3 on: September 07, 2021, 04:49:49 pm »
Hi!

Modern times:
Code: Pascal  [Select][+][-]
  1.  
  2. ExtractFilePath(Application.ExeName)

can now be replaced by
Code: Pascal  [Select][+][-]
  1. Application.Location

Winni

MadMike

  • New Member
  • *
  • Posts: 32
Re: [SOLVED]program not reading the ini file.
« Reply #4 on: September 13, 2021, 01:53:01 pm »
Hi!

Modern times:
Code: Pascal  [Select][+][-]
  1.  
  2. ExtractFilePath(Application.ExeName)

can now be replaced by
Code: Pascal  [Select][+][-]
  1. Application.Location


Winni

There always seems to be more than one way.

Why is that do you think?
Mike
« Last Edit: September 13, 2021, 02:48:22 pm by MadMike »

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: [SOLVED]program not reading the ini file.
« Reply #5 on: September 13, 2021, 03:12:54 pm »
There always seems to be more than one way.

Why is that do you think?
Mike
Because having/knowing only one way might get you end up in a Dead-end.
It's always good to know, that you can take a left turn instead of going forward blind and get stuck.

In this special case: FPC/Lazarus is cross-Platform
There is no EXE on Linux.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED]program not reading the ini file.
« Reply #6 on: September 13, 2021, 04:17:09 pm »
> The ini is in the same directory as the app.

And, perhaps thats the underlying problem ? This is Linux, we don't do that here. Ultimately, when the rest of the world discovers how great your application is and everyone wants a copy, you will find that the binary (ie, what you might call the exe file) lives somewhere like /usr/bin and the config files live in $HOME/.config/$APPNAME/.

You see, the binary is in a read only area and each individual user has their own separate config area. You cannot put the ini file up in /usr/bin because the app, running with your permissions, would not be able to change it. And other users might need different settings. So, following the binary back to where it lives hoping to find the ini file is a bad approach. And then there might be symlink issues ....

This strict rule about where things go might seem over the top right now but you are better doing things correctly right from the start than having, at some stage, to backtrack.

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MadMike

  • New Member
  • *
  • Posts: 32
Re: [SOLVED]program not reading the ini file.
« Reply #7 on: September 13, 2021, 04:53:52 pm »
> The ini is in the same directory as the app.

And, perhaps thats the underlying problem ? This is Linux, we don't do that here. Ultimately, when the rest of the world discovers how great your application is and everyone wants a copy, you will find that the binary (ie, what you might call the exe file) lives somewhere like /usr/bin and the config files live in $HOME/.config/$APPNAME/.

You see, the binary is in a read only area and each individual user has their own separate config area. You cannot put the ini file up in /usr/bin because the app, running with your permissions, would not be able to change it. And other users might need different settings. So, following the binary back to where it lives hoping to find the ini file is a bad approach. And then there might be symlink issues ....

This strict rule about where things go might seem over the top right now but you are better doing things correctly right from the start than having, at some stage, to backtrack.

Davo

Seems to work now, but I'll go back and check the program can actually write to the ini!

I have placed the program and its ini in /usr/local/bin. Is this a read only section?

You are quite right in stating I should be doing things correctly: Following ''best practice".

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED]program not reading the ini file.
« Reply #8 on: September 14, 2021, 01:57:14 am »
> I have placed the program and its ini in /usr/local/bin. Is this a read only section?

Yes Mike, anything starting with /usr should be read only.  If you can write to that as an ordinary user, you have security problems !

Even though you probably will not use your Pi as a multiuser system, you should always think of it that way. And putting your ini file in /usr/local/bin would make it visible to other users.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MadMike

  • New Member
  • *
  • Posts: 32
Re: [SOLVED]program not reading the ini file.
« Reply #9 on: September 14, 2021, 12:02:11 pm »
Well thanks for everyone's help.

All seems to be working now.

I couldn't use the 'universal':

Code: Pascal  [Select][+][-]
  1. Path:= AppendPathDelim('/$HOME/.config')+'MyIni.ini';

or even

Code: Pascal  [Select][+][-]
  1. Path:= AppendPathDelim(ExtractFilePath('/$HOME/.config'))+'MyIni.ini';

I had to construct a more specific:

Code: Pascal  [Select][+][-]
  1. Path:= AppendPathDelim('/home/pi/.config')+'MyIni.ini';

The project is a Raspberry Pi - based piece of stand alone test equip (piece of pi?). Installation involves burning an SD card with an image so I can't foresee any problems.

Hopefully.

I'll have to read up more on extracting file paths for the next project.

Mike

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED]program not reading the ini file.
« Reply #10 on: September 15, 2021, 08:18:23 am »
Mike, I am sorry, I did not realise no one mentioned -

GetAppConfigDirUTF8(false);

Works across all platforms quite reliably.  Its in LazUtils.

Davo

EDIT :

You were on "a" right path anyway, its just you needed different syntax,  instead of -
 
Code: Pascal  [Select][+][-]
  1.  Path:= AppendPathDelim('/$HOME/.config')+'MyIni.ini';

you needed -
Code: Pascal  [Select][+][-]
  1. Path:= AppendPathDelim(GetEnvironmentVariable('HOME')) + '.config' + PathDelim + 'MyIni.ini';

Probably better to have a directory with the apps name down there.

Davo
« Last Edit: September 15, 2021, 08:34:51 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MadMike

  • New Member
  • *
  • Posts: 32
Re: [SOLVED]program not reading the ini file.
« Reply #11 on: September 15, 2021, 11:16:19 am »
Thanks,

More to read up on.  Boy, sooo much to learn

Mike

 

TinyPortal © 2005-2018