AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Lib] ini v0.15.1 - Basic ini string functions
Goto page 1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Fri Jul 10, 2009 7:18 am    Post subject: [Lib] ini v0.15.1 - Basic ini string functions Reply with quote

Keywords: ini file parser function lib library module alternative read write string variable memory section key value simple easy basic regex download free gnu gpl

ini: Basic ini string functions - Library Version: 0.15.1 (2010-02-05)

Download (~52 kb)
    The archive contains the ini library, a test module and full documentation generated with custom installer of Natural Docs from majkinetor. I am calling it stable now.

Online Documentation

LICENSE: Please note, the library is GNU LGPL licensed. Your script using this module do not need to be open source. See http://en.wikipedia.org/wiki/GNU_Lesser_General_Public_License

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
v0.15.1

  • CHANGE: Status changed from Beta to Stable.
  • BUG: Function ini_getAllValues() updated, because it returned emtpy lines between all entries.


Its a small update.[/list]


Last edited by Tuncay on Fri Feb 05, 2010 10:43 pm; edited 56 times in total
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Fri Jul 10, 2009 10:40 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message Yahoo Messenger
Fry



Joined: 01 Nov 2007
Posts: 880

PostPosted: Fri Jul 10, 2009 2:18 pm    Post subject: Reply with quote

Well Drugwash from extensive work,
ini_getValue(content, section, key)
ini_getSection(content, section)

for me fine
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Fri Jul 10, 2009 5:05 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Yahoo Messenger
Fry



Joined: 01 Nov 2007
Posts: 880

PostPosted: Fri Jul 10, 2009 5:07 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Fri Jul 10, 2009 5:17 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Fry



Joined: 01 Nov 2007
Posts: 880

PostPosted: Fri Jul 10, 2009 5:53 pm    Post subject: Reply with quote

I got it working, thanks!
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Fri Jul 10, 2009 6:04 pm    Post subject: Reply with quote

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. Cool
Back to top
View user's profile Send private message
Fry



Joined: 01 Nov 2007
Posts: 880

PostPosted: Fri Jul 10, 2009 10:36 pm    Post subject: Reply with quote

Majkinetor did.
Back to top
View user's profile Send private message
Fry



Joined: 01 Nov 2007
Posts: 880

PostPosted: Sat Jul 11, 2009 2:54 am    Post subject: Reply with quote

Tuncay, can you make so updatevalue updates the entire ini variable, not just the section? thanks
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Sat Jul 11, 2009 9:59 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Sat Jul 11, 2009 2:54 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Fry



Joined: 01 Nov 2007
Posts: 880

PostPosted: Sat Jul 11, 2009 4:29 pm    Post subject: Reply with quote

A documentation or atleast explaining how to use it would be helpful.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Sat Jul 11, 2009 9:36 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 753
Location: Berlin, DE

PostPosted: Sun Jul 12, 2009 1:42 pm    Post subject: Reply with quote

So it seems now to be working correctly. But I would not trust them. Very Happy There is a testing module with 17 tests of the functions. All passed. Cool If any change breaks a function, then the chance with the test module to find that bug is good. I need
Code:
Sleep, 1000
Laughing

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

(First post is updated.)


Last edited by Tuncay on Wed Jul 15, 2009 5:43 am; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3, 4, 5  Next
Page 1 of 5

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group