Recent

Author Topic: Read a config file  (Read 621 times)

mikerabat

  • New Member
  • *
  • Posts: 39
Read a config file
« on: March 26, 2023, 01:05:31 pm »
This seems to be a stupid question but I'm from the Windows world and not that firm with Linux....

Here is my task: I'll need to create a small daemon that polls data from a webserver.
I managed to run it as a windows service but the code shall work on a Raspberry Pi so FPC is the obvious choice ;)

Now... I'm familiar in using ini files and TRegistry in Windows but what is that equivalent to read .conf files in Linux? My search wasn't that fruitful so I wanted to ask here...

Thanks in advance.

Thaddy

  • Hero Member
  • *****
  • Posts: 13209
Re: Read a config file
« Reply #1 on: March 26, 2023, 01:23:35 pm »
There is really no difference. If you use an ini file under windows, you can also do that under linux.
A .conf file is just like an ini file plain text, so basically the same. It is how you want to USE the ini or conf file that matters.
And that is up to your programming skills. (you can also use a conf file on Windows if you decide to do so!)
I actually get compliments for being rude... (well, Dutch, but that is the same)

Roland57

  • Sr. Member
  • ****
  • Posts: 397
    • GitLab
Re: Read a config file
« Reply #2 on: March 26, 2023, 02:00:29 pm »
As Thaddy said, you can use an INI file under Linux. But there is a nice example of reading a Linux-style (?) configuration file here.

For the registry, I don't know.

Thaddy

  • Hero Member
  • *****
  • Posts: 13209
Re: Read a config file
« Reply #3 on: March 26, 2023, 02:16:08 pm »
Link is dubious and not working. DO NOT USE.
It works again. But that Rosetta code entry is shamefully badly written.
I will change that when I get home. Or almost everybody else can beat me to it. It is THAT bad....
Look at the delphi entry for much better code.
But it needs a review for both entries.
« Last Edit: March 26, 2023, 02:37:20 pm by Thaddy »
I actually get compliments for being rude... (well, Dutch, but that is the same)

Ally

  • New Member
  • *
  • Posts: 49
Re: Read a config file
« Reply #4 on: March 26, 2023, 03:37:09 pm »
Hello mikerabat,

attached are two examples of how to save the window values so that they are preserved on restart.

The variant in the XReg folder uses TRegistry.
TRegistry is implemented in Lazarus so that it is cross-platform. ( https://wiki.freepascal.org/fcl-registry )
I built a small unit for it, which can write string, integer, boolean, etc. into the registry and read from it.
The whole thing works also under Linux, there registry accesses are stored automatically in the folder .config in a corresponding XML.

Advantage: the program settings are BS-specific in the "right" place and are always accessible.


The variant in the folder XIni uses TIniFile.
With this variant one can write a Ini file into any folder.

Advantage: you can save the program settings where you want.


Greetings Roland

mikerabat

  • New Member
  • *
  • Posts: 39
Re: Read a config file
« Reply #5 on: March 27, 2023, 10:11:10 am »
Dear all!

Thank you for the insights! I will checkout the inifile solution - it seems the most appropriate to
me :)

 

TinyPortal © 2005-2018