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 Previous  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: 897
Location: Berlin, DE

PostPosted: Thu Jul 16, 2009 7:04 pm    Post subject: Reply with quote

First post updated. Also first example corrected.
Back to top
View user's profile Send private message
Tuncay



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

PostPosted: Sat Jul 18, 2009 2:48 am    Post subject: Update Reply with quote

Updated to version 0.6.

Last changes
  • Now at most functions, the name of section can be specified to "". In that case, it behaves like if the first section was specified or at key commands the first matching key is found (not just in first section). All, but ini_insertSection(), does have this functionality.

  1. Get value of first found key.
    Code:
    value := ini_getValue(ini, "", "TimeStamp")


Last edited by Tuncay on Sat Jul 18, 2009 11:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
Tuncay



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

PostPosted: Sat Jul 18, 2009 11:06 am    Post subject: Reply with quote

Updated to version 0.7.

Last changes
  • New functions "ini_getAllKeyNames()" and "ini_getAllSectionNames()" added. It returns a comma separated list of all keys and section names.

  1. Get a comma separated list of all key names.
    Code:
    keys := ini_getAllKeyNames(ini, "Tip")

  2. Get a comma separated list of all section names.
    Code:
    sections := ini_getAllSectionNames(ini)
Back to top
View user's profile Send private message
Drugwash



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

PostPosted: Sat Jul 18, 2009 9:39 pm    Post subject: Reply with quote

Simple question: why not a single function with optional second parameter that makes the difference between keys and sections? Rolling Eyes
Code:
sections := ini_getNames(ini)
keys := ini_getNames(ini, "K")
Back to top
View user's profile Send private message Yahoo Messenger
Tuncay



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

PostPosted: Sat Jul 18, 2009 10:16 pm    Post subject: Reply with quote

I was not sure if I should have a function for every command or make one all purpose command. I think, this is just a philosophical question. First answer, why I should make different functions is, they are not identical in code and have more difference than expected.

Second answer is, users have not to remember what is the parameter for and what other parameters it have and in what syntax to write. The name of the function says everything about the usage, its more readable.

Third and for me most important answer is, the second parameter on ini_getAllKeyNames() is optional, where ini_getAllSectionNames() does not allow that parameter.

Its same question why I have separate functions for ini_insertSection() and ini_insertKey().

One philosophy of this library was, to integrate less as possible optional parameters, to operate faster in execution and work simpler for user. And why should I do the way you showed me? Is it better? faster? readabler? matching any standard??

post updated
Back to top
View user's profile Send private message
Mystiq



Joined: 08 Jan 2007
Posts: 42

PostPosted: Sun Aug 16, 2009 9:19 pm    Post subject: Reply with quote

Just wanted to say thanks!

It's good to have simple alternatives which even us "newbies" can understand. Smile Theres already other way more complicated and powerful options (like IniFile by bmcclure etc.) but they seem to be more than i need and if it ever comes down to it, my skills aren't enough to fix or modify if it breaks in the future (who knows...).

Again, big thanks for sharing this.
Back to top
View user's profile Send private message
Tuncay



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

PostPosted: Sun Aug 16, 2009 9:35 pm    Post subject: Reply with quote

Thanks for you comment Mystiq. Good to know that these functions are not made in vain. I call the state still beta, because tests are done only synthetical with a test module.
Back to top
View user's profile Send private message
Mystiq



Joined: 08 Jan 2007
Posts: 42

PostPosted: Sun Aug 16, 2009 10:57 pm    Post subject: Reply with quote

Don't worry, well aware of the state. Smile I'm doing tests and provide feedback/suggestions/code back here.

Update:
Found possible typo in the code(?)

In "ini_getAllKeyNames" and "ini_getAllSectionNames":

Code:

RegEx = `aisUm)^.*(?:\[\s*?.*\s*?]|\s*?(.*)\s*?=.*)s*?.*$


Should the end be "\s*?.*$" instead of "s*?.*$"? Is that even necessary? Doesn't the ".*" pretty much cover spaces as well? Just wondering. Smile
Back to top
View user's profile Send private message
Tuncay



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

PostPosted: Mon Aug 17, 2009 2:56 pm    Post subject: Reply with quote

Yes, you right. It is a typo and if I correct it to "\s", then the test with that function fails. The "\s*?" is a old part of the regex, where I was trying to not to include any space after the catched name.

However, I deleted that part now and leave it like you suggested (or asked) to ".*" only. The test results are saying its all right. Then it is all right. Razz You can try by yourself running the test module on any change. Its really cool and I find it useful. Every step where an error is detected will be showed detailed.

The download link is updated.
Back to top
View user's profile Send private message
Mystiq



Joined: 08 Jan 2007
Posts: 42

PostPosted: Mon Aug 17, 2009 3:28 pm    Post subject: Reply with quote

I rewrote the regex for getValue and getKey, they didn't seem to "respect" the sections e.g.

Code:

ini =
(% ` RTrim0 Join`n
[Section1]
key1=value1
[Section2]
key2=value2
)

ini_getValue(ini, "Section1", "key2") ; returns -> value2


I made the regular expressions more strict/specific and removed the "dotall" option etc. Now it does not match keys outside specified section. Ran it through the "ini_test.ahk" and it checks out. Also i've done my own tests, everything seems to be ok.

I left in the more readable code for the regex as the one line regex are pain to look at/understand. Smile

Code:

ini_getValue(ByRef _Content, _Section, _Key, _PreserveSpace = False)
{
    If (_Section = "")
        _Section = (?:\[.*])?
    Else
         _Section = \[\s*?\Q%_Section%\E\s*?]

    RegEx = `aiU)(?:\R|^)\s*%_Section%\s*(?:\R\s*|\R\s*[\w\s]+\s*=\s*.*?\s*(?=\R)|\R\s*[;#].*?(?=\R))*\R\s*\Q%_Key%\E\s*=(.*)(?=\R|$)
/*
    RegEx := "`aiU)"
      . "(?:\R|^)\s*" . _Section . "\s*"         ;-- section
      . "(?:"
      . "\R\s*"                           ;-- empty lines
      . "|\R\s*[\w\s]+\s*=\s*.*?\s*(?=\R)"      ;-- OR other key=value pairs
      . "|\R\s*[;#].*?(?=\R)"                  ;-- OR commented lines
      . ")*"
      . "\R\s*\Q" . _Key . "\E\s*=(.*)(?=\R|$)"   ;-- match
*/
   
    RegExMatch(_Content, RegEx, Value)
    If Not _PreserveSpace
        Value1 = %Value1% ; Trim spaces.
    Return Value1
}

ini_getKey(ByRef _Content, _Section, _Key)
{
    If (_Section = "")
        _Section = (?:\[.*])?
    Else
         _Section = \[\s*?\Q%_Section%\E\s*?]
   
   RegEx = `aiU)(?:\R|^)\s*%_Section%\s*(?:\R\s*|\R\s*[\w\s]+\s*=\s*.*?\s*(?=\R)|\R\s*[;#].*?(?=\R))*\R(\s*\Q%_Key%\E\s*=.*)(?=\R|$)
/*
    RegEx := "`aiU)"
      . "(?:\R|^)\s*" . _Section . "\s*"         ;-- section
      . "(?:"
      . "\R\s*"                           ;-- empty line
      . "|\R\s*[\w\s]+\s*=\s*.*?\s*(?=\R)"      ;-- OR other key=value pairs
      . "|\R\s*[;#].*?(?=\R)"                  ;-- OR comments
      . ")*"
      . "\R(\s*\Q" . _Key . "\E\s*=.*)(?=\R|$)"   ;-- match
*/
    RegExMatch(_Content, RegEx, Value)
    Return Value1
}


Thoughts?
Back to top
View user's profile Send private message
Tuncay



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

PostPosted: Mon Aug 17, 2009 7:16 pm    Post subject: Update Reply with quote

Updated to version 0.8.

Last changes
  • Replaced getValue() and getKey() with the reworked version made by Mystiq.
  • Also test code updated.


Thanks to Mystiq. I have adopted your code and it works very good.

If you have more test cases, so please let me know. I am interested in your own tests you wrote about. Can you show me? And, I never saved and reloaded a file content manipulated with these set of functions. What about the new lines? May be there are some side effects.

If enough tested, then we can say it is stable and usable by others.

Mhmm, can anybody make a speed test and comparison to other functions related to this? I don`t know how and where to begin.
Back to top
View user's profile Send private message
Mystiq



Joined: 08 Jan 2007
Posts: 42

PostPosted: Tue Aug 18, 2009 3:58 pm    Post subject: Reply with quote

Good to be of some help. Smile

The tests i talked about are simply some random ini strings, just things i come up with like that section issue i mentioned, nothing fancy. I have not yet tested the read/write of these ini strings either. I'm not sure what you mean by the new lines(?).

I'm with you on the speed/performance thing, i did try Titan's Advanced Benchmark but could not make it work on my own function(s), but the test cases worked fine. Personally i've not really bothered with doing any major benchmarking.
Back to top
View user's profile Send private message
erinau



Joined: 25 Sep 2009
Posts: 2

PostPosted: Fri Sep 25, 2009 2:27 pm    Post subject: Reply with quote

Hello Tuncay,

thank you for your script.Smile

While using your ini functions I noticed a problem if the same keyname occurs in 2 sections.
Code:

#Include %A_ScriptDir%\..\..\..\Funktionen\ini.ahk
ini=
(
[DATA]
AAAAA=20090708112129|20090708112129
BBBBB=20090923101825|20090923101825
CCCCC=20090922110042|20090922110042
[Test]
AAAAA=20090708112129|20090708112129
BBBBB=20090923101825|20090923101825
CCCCC=20090922110042|20090922110042
)
msgbox, %ini%
ini_replaceKey(ini, "DATA", "AAAAA","hkjh")
msgbox, %ini%
ini_replaceKey(ini, "DATA", "AAAAA")
msgbox, %ini%
ini_replaceKey(ini, "DATA", "BBBBB")
msgbox, %ini%
ini_replaceKey(ini, "DATA", "CCCCC")
msgbox, %ini%


You see, that instead of deleting the keys in section [Data], the keys in section [Test]are deleted. Question
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4116
Location: Belgrade

PostPosted: Fri Sep 25, 2009 3:43 pm    Post subject: Reply with quote

Tunkay wrote:
Disadvantage of his functions are the overhead and speed, I think.

You should provide some proof for this claim, which is not that easy to do properly as functions you compare should have exactly the same outcome (Titan already made mistake before, trying to do that).
_________________
Back to top
View user's profile Send private message
erinau



Joined: 25 Sep 2009
Posts: 2

PostPosted: Fri Sep 25, 2009 5:33 pm    Post subject: Problems Reply with quote

Hello majkinetor

I hope I understand you right.

In the script I posted, the wrong keys where deleted.

The function:
Code:
ini_replaceKey(ini, "DATA", "CCCCC")


deletes the key "CCCCC" in the [Test] section and not in the [Data] section.
You can follow this in the message boxes.


Or do you think about something else?

Erich

[/quote]
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 Previous  1, 2, 3, 4, 5  Next
Page 3 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