Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Lib] ini v1.0 - Basic ini string functions


  • Please log in to reply
23 replies to this topic
Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Keywords: ini file parser function lib library module alternative read write string variable memory section key value simple easy basic regex download free
License: New BSD License

Download (~31 kb)
The archive contains the library, a demo script and full documentation.Online Documentation

Description

Ini files are used mostly as configuration files. In general, they have the ".ini"-extension. It is a simple standardized organization of text data. Many other simple programs use them for storing text.

AutoHotkey provides three commands IniDelete, IniRead and IniWrite. These commands are stable, but they have some disadvantages. First disadvantage is, that they access the file directly. The file on the disk is opened, load into memory and then read or manipulated and then saved with every single command.

With the custom functions I wrote here, the user accessess on variables instead of files. This is super fast, in comparison to disk access. Ini files can be created by Ahk just like any other variable. But Ahk itself does not provide any function to operate on ini strings (variables). If you work often with ini files, then this might for you.

No other framework or library is required, no special object files are created; just work on ordinary ini file contents or variables. The load and save functions are added for comfort reason and are not really needed.

First do this:
FileRead, [color=olive]ini[/color], config.ini
or load default file with:
ini_load([color=olive]ini[/color])
or create the content yourself:
[color=olive]ini =
([/color]
[color=orange][Tip][/color]
[color=blue]TimeStamp[/color] = [color=green]20090716194758[/color]
[color=orange][Recent File List][/color]
[color=blue]File1[/color]=[color=green]F:\testfile.ahk[/color]
[color=blue]File2[/color]=[color=green]Z:\tempfile.tmp[/color]
[color=olive])[/color]
The orange part is a section name. The section have some variables, called keys. Every key is part of a section. In this example, the section "[Tip]" have one key "TimeStamp". And every key have a content, called value. The "TimeStamp" key have the value "20090716194758".

After that, you can access and modify the content of the ini variable with the following functions. But the modifications are only temporary and must be saved to disk. This should be done by overwriting the source (not appending).

Some usage examples

[*:13ckdnjd]Get value of a key.
value := ini_getValue(ini, "Tip", "TimeStamp")
[*:13ckdnjd]Get key/value pair.
key := ini_getKey(ini, "Tip", "TimeStamp")
[*:13ckdnjd]Get full section with all keys.
section := ini_getSection(ini, "Tip")
[*:13ckdnjd]Update value of a key.
ini_replaceValue(ini, "Tip", "TimeStamp", A_Now)
[*:13ckdnjd]Delete a key.
ini_replaceKey(ini, "Tip", "Timestamp")
[*:13ckdnjd]Replace a full section with all its keys.
ini_replaceSection(ini, "Tip", "[Time]HH=12`nMM=34`nSS=10")
[*:13ckdnjd]Add a key/value pair.
ini_insertKey(ini, "Tip", "Timenow=" . A_Now)
[*:13ckdnjd]Add a value to existing value.
ini_insertValue(ini, "Section", "Key" ",ListItem")
[*:13ckdnjd]Add a section.
ini_insertSection(ini, "Meta", "Name=ini`nAuthor=Tuncay")
[*:13ckdnjd]Get value of first found key.
value := ini_getValue(ini, "", "TimeStamp")
Simple example
path := ini_load(ini)
last := ini_getValue(ini, "Tip", "TimeStamp")
...
ini_replaceValue(ini, "Tip", "TimeStamp", A_Now)
ini_save(ini, path)

Need Testers

Special thanks goes to Fry, for testing and who was the person why I made this.
Special thanks goes to Mystiq for testing and reworking on an important regex.

In current state, the functions are tested. It seems so as if there would be no problem... but if you know regex, you know what I mean.
I need some testers and helpers. Please look at the regex and give hints if you have some. Test the funtions on real ini files. Report to me.

Last changes
v1.0

No signature.


Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
I have one script in mind that would fit this perfectly but I'll wait for some more testing before implementing it, since a small bug could break everything. Thanks for creating and sharing it!

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
Well Drugwash from extensive work,
ini_getValue(content, section, key)
ini_getSection(content, section)

for me fine

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
All four would have to work flawlessly but thank you for the confirmation on those two. Hopefully there's no problem with any of the four.

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
I cant seem to get updatevalue to work, any more information on how to use it?

Heres my block of code

Fileread,ReadData,%PinDir%
     Dec := RC4hex2txt(ReadData,TDec)
 
     ini_updateSection(Dec, Passlist, "")
     Filedelete, %PinDir%
     Enc := RC4hex2txt(Dec,TDec)
     msgbox,%Enc%
     Fileappend,%Enc%,%PinDir%
     Msgbox,64,Deleted
     gosub Auto
     return


Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
f =
(
[Home]
Path=C:\
UserInfo=John Doe
[Settings]
AutoStart=1
)
msgbox before`n`n %f%
ini_updateValue(f, "Home", "Path", "test")
msgbox % ini_getValue(f, "Home", "Path")
msgbox after `n`n%f%
updateValue() works fine to me. Look at this example. The update functions updates the variable f directly in function. Return is 1 if updated and 0 of not updated.

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
I got it working, thanks!

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
If someone developed such functions also, let me know please. I know, that I have seen such functions somewhere. But I dont know where it was ... And if someone want to test for bugs or speed tests... (s)he is welcome. 8)

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
Majkinetor did.

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
Tuncay, can you make so updatevalue updates the entire ini variable, not just the section? thanks

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006

Majkinetor did.

Thx for the info. This is it: [module] Ini 1.0b2 - ini-like handling of strings with an incredible documentation and more features. He is an expert in Ahk, so you can trust him. Disadvantage of his functions are the overhead and speed, I think.

Tuncay, can you make so updatevalue updates the entire ini variable, not just the section? thanks

I dont understand what you mean by this? UpdateValue replaces the keys content and UpdateSection a complete section. And both can operate on ini file strings read (or create) into variable.
May be if you know how, show an example for better understanding.

May be I add some more functions and write a small docu. Then I test them a bit.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Made new version. Still in beta. Please dont use it productively. Its in Regex and I am not an expert and its not really tested. Please test and report. First post contains the updates.

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
A documentation or atleast explaining how to use it would be helpful.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
I am writing a test case module, for automated testing of some functionalites on source changes. If this part is completed, then I make a simple documentation and explanation how to use.

BUT currently I experience heavy problems. Please never use expecielly the replace/update functions.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
So it seems now to be working correctly. But I would not trust them. :D There is a testing module with 17 tests of the functions. All passed. 8) If any change breaks a function, then the chance with the test module to find that bug is good. I need
Sleep, 1000
:lol:

RegEx is hard to beat, ... if it is possible. Hope it works now.

(First post is updated.)