AutoHotkey Community

It is currently May 26th, 2012, 8:01 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 129 posts ]  Go to page 1, 2, 3, 4, 5 ... 9  Next
Author Message
PostPosted: July 10th, 2009, 8:18 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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:
Code:
FileRead, ini, config.ini

or load default file with:
Code:
ini_load(ini)

or create the content yourself:
Code:
ini =
(

[Tip]
TimeStamp = 20090716194758
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp
)

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

  1. Get value of a key.
    Code:
    value := ini_getValue(ini, "Tip", "TimeStamp")
  2. Get key/value pair.
    Code:
    key := ini_getKey(ini, "Tip", "TimeStamp")
  3. Get full section with all keys.
    Code:
    section := ini_getSection(ini, "Tip")
  4. Update value of a key.
    Code:
    ini_replaceValue(ini, "Tip", "TimeStamp", A_Now)
  5. Delete a key.
    Code:
    ini_replaceKey(ini, "Tip", "Timestamp")
  6. Replace a full section with all its keys.
    Code:
    ini_replaceSection(ini, "Tip", "[Time]HH=12`nMM=34`nSS=10")
  7. Add a key/value pair.
    Code:
    ini_insertKey(ini, "Tip", "Timenow=" . A_Now)
  8. Add a value to existing value.
    Code:
    ini_insertValue(ini, "Section", "Key" ",ListItem")
  9. Add a section.
    Code:
    ini_insertSection(ini, "Meta", "Name=ini`nAuthor=Tuncay")
  10. Get value of first found key.
    Code:
    value := ini_getValue(ini, "", "TimeStamp")

Simple example
Code:
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

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Last edited by Tuncay on September 9th, 2011, 6:36 pm, edited 61 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 11:40 am 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 3:18 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
Well Drugwash from extensive work,
ini_getValue(content, section, key)
ini_getSection(content, section)

for me fine


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 6:05 pm 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 6:07 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
I cant seem to get updatevalue to work, any more information on how to use it?

Heres my block of code

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 6:17 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Code:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 6:53 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
I got it working, thanks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 7:04 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 11:36 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
Majkinetor did.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 3:54 am 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
Tuncay, can you make so updatevalue updates the entire ini variable, not just the section? thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 10:59 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Fry wrote:
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.
Fry wrote:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 3:54 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 5:29 pm 
Offline

Joined: November 1st, 2007, 10:03 pm
Posts: 885
A documentation or atleast explaining how to use it would be helpful.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 10:36 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 12th, 2009, 2:42 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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
Code:
Sleep, 1000
:lol:

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

(First post is updated.)


Last edited by Tuncay on July 15th, 2009, 6:43 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 129 posts ]  Go to page 1, 2, 3, 4, 5 ... 9  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: infogulch, notsoobvious, tomoe_uehara and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group