INI file read... [SOLVED]

Everything else that doesn't fall into one of the other PB categories.
mocitymm
User
User
Posts: 37
Joined: Wed Feb 08, 2017 6:56 pm

INI file read... [SOLVED]

Post by mocitymm »

Looks like another search I can't seem to find an answer for. Tried numerous permutations on INI (only one produced anything and that was preferences) so, here it goes.

I have multiple *.conf files (INI files) on linux that my finished project (written in Pascal/Lazarus) is opening and reading, there is a built-in function for doing this.

I would like to convert the project to PureBasic and this seems to be the last hurdle on capabilities comparison to determine a 'yes' it's convertible (without a complete reworking of the design).

From what I am reading it looks like a process of string delimiters, parsing and reg expression process in PureBasic, if I am wrong and not seeing a more obvious process... please let me know.

I do not know where to start with this one so, any pointers are greatly appreciated.

Regards,

-M
Last edited by mocitymm on Thu Apr 02, 2020 12:20 pm, edited 1 time in total.
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: INI file read...

Post by blueb »

If you're looking for an INI replacement that goes a little deeper than PureBasic's Preferences, then I suggest you take a look at user kenmo's INI program:
https://github.com/kenmo-pb/includes/bl ... er/ini.pbi


But to actually read & write Windows INI files (code below taken from Droopy's Lib)...

Code: Select all

; --------------------------------------------
;Writes a key to an Ini File 
;Returns 1 if successful or 0 if it fails 
Procedure IniWrite(INIFile.s,Section.s,Key.s,string.s) 
  ret=WritePrivateProfileString_(@Section,@Key,@string,@INIFile)
  If ret<>0 : ret=1:EndIf
  ProcedureReturn ret
EndProcedure
; --------------------------------------------

;Returns the contents of a key from an Ini File 
Procedure.s IniRead(INIFile.s,Section.s,Key.s) 
  ret.s=Space(512)
  GetPrivateProfileString_(@Section,@Key,@"",@ret,512,@INIFile)
  ProcedureReturn ret
EndProcedure
; -------------------------------------------

;Example (run it with debug) 
;
IniWrite("c:\test.ini","section1","Key1Section1","data_Key1Section1")
IniWrite("c:\test.ini","section1","Key2Section1","data_Key2Section1")
IniWrite("c:\test.ini","section2","Key1Section2","data_Key1Section2")
Debug IniRead("c:\test.ini","section1","Key1Section1")
Debug IniRead("c:\test.ini","section1","Key2Section1")
Debug IniRead("c:\test.ini","section2","Key1Section2")
; -------------------------------------------
HTH :)
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
mocitymm
User
User
Posts: 37
Joined: Wed Feb 08, 2017 6:56 pm

Re: INI file read... [SOLVED]

Post by mocitymm »

blueb,

Thank you. Exactly what I needed.

Still getting through the learning curve with PB so, I appreciate the help.

-M
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: INI file read... [SOLVED]

Post by kenmo »

Hi mocitymm,

The built-in Preferences library is the easiest way and (I hope I don't get in trouble) it handles most INI files. **

** Really, technically "INI" is Windows' format and can be slightly different from what PureBasic calls "Preference" files.

Do you know what's specifically different between your CONF file format vs. PB's preferences format vs. true INI?



PS. I wrote the "INI" functions that blueb linked, it's a few years old, but it should work fine too. Now I understand I shouldn't have called it "INI" :)
mocitymm
User
User
Posts: 37
Joined: Wed Feb 08, 2017 6:56 pm

Re: INI file read... [SOLVED]

Post by mocitymm »

And after experimenting with it.... I can also use 'Preference' file function to do the same thing.

-M
mocitymm
User
User
Posts: 37
Joined: Wed Feb 08, 2017 6:56 pm

Re: INI file read... [SOLVED]

Post by mocitymm »

kenmo wrote:Hi mocitymm,

The built-in Preferences library is the easiest way and (I hope I don't get in trouble) it handles most INI files. **

** Really, technically "INI" is Windows' format and can be slightly different from what PureBasic calls "Preference" files.

Do you know what's specifically different between your CONF file format vs. PB's preferences format vs. true INI?



PS. I wrote the "INI" functions that blueb linked, it's a few years old, but it should work fine too. Now I understand I shouldn't have called it "INI" :)
Yep... just found that out. I pulled an ID10T with that one.

Format is the same as an Windows ini, section names, name and values. Really simple file to read, just didn't know how it was done in PB.

Just tested it with the built in preferences function and it works. If it was a snake as the expression goes. :?

Thanks for the reach out on it though.

-M
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: INI file read... [SOLVED]

Post by Marc56us »

Do you know what's specifically different between your CONF file format vs. PB's preferences format vs. true INI?
One of the differences (and maybe the only one) is that when PB creates a new "INI" file (~= prefs), it creates it in UTF-8 with a BOM and some old third party programs will block reading from the beginning (on the BOM).

:!: PB can read an ANSI or UTF "ini" file without BOM, but as soon as you change even one key (add or modify), it rewrites all the file to UTF-8 with BOM.

If you want to modify your INI file with PB and the other application can no longer handle UTF-8, you still have the possibility, after "ClosePreferences()", to rewrite (CreateFile(...#PB_Ascii) everything to another file (with normal text file commands), save. Finally rename the temporary file in the name of original file.
(Put the whole thing in a macro or procedure.)

@Fred :idea: It would be nice to have a parameter that keeps the original format.
(it's up to the user to deal with the page codes then)

:wink:
mocitymm
User
User
Posts: 37
Joined: Wed Feb 08, 2017 6:56 pm

Re: INI file read... [SOLVED]

Post by mocitymm »

Marc56us wrote:One of the differences (and maybe the only one) is that when PB creates a new "INI" file (~= prefs), it creates it in UTF-8 with a BOM and some old third party programs will block reading from the beginning (on the BOM).

:!: PB can read an ANSI or UTF "ini" file without BOM, but as soon as you change even one key (add or modify), it rewrites all the file to UTF-8 with BOM.

If you want to modify your INI file with PB and the other application can no longer handle UTF-8, you still have the possibility, after "ClosePreferences()", to rewrite (CreateFile(...#PB_Ascii) everything to another file (with normal text file commands), save. Finally rename the temporary file in the name of original file.
(Put the whole thing in a macro or procedure.)

:wink:
Good to know Marc56us. Thank you for the heads up on that. All processes are read only, a rewrite mistake would mess up the service that is running in the background (writes the *.conf files).

-M
Post Reply