AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 129 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6 ... 9  Next
Author Message
 Post subject:
PostPosted: July 16th, 2009, 7:04 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
First post updated. Also first example corrected.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Update
PostPosted: July 18th, 2009, 2:48 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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 July 18th, 2009, 11:53 pm, edited 1 time in total.

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

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 18th, 2009, 9:39 pm 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
Simple question: why not a single function with optional second parameter that makes the difference between keys and sections? :roll:
Code:
sections := ini_getNames(ini)
keys := ini_getNames(ini, "K")


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

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 9:19 pm 
Offline

Joined: January 8th, 2007, 1:14 pm
Posts: 83
Just wanted to say thanks!

It's good to have simple alternatives which even us "newbies" can understand. :) 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 9:35 pm 
Offline

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 10:57 pm 
Offline

Joined: January 8th, 2007, 1:14 pm
Posts: 83
Don't worry, well aware of the state. :) 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. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2009, 2:56 pm 
Offline

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2009, 3:28 pm 
Offline

Joined: January 8th, 2007, 1:14 pm
Posts: 83
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. :)

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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Update
PostPosted: August 17th, 2009, 7:16 pm 
Offline

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


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

Joined: January 8th, 2007, 1:14 pm
Posts: 83
Good to be of some help. :)

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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 25th, 2009, 2:27 pm 
Offline

Joined: September 25th, 2009, 2:25 pm
Posts: 3
Hello Tuncay,

thank you for your script.:)

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. :?:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 25th, 2009, 3:43 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
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).

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Problems
PostPosted: September 25th, 2009, 5:33 pm 
Offline

Joined: September 25th, 2009, 2:25 pm
Posts: 3
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]


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 Previous  1, 2, 3, 4, 5, 6 ... 9  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Google [Bot], Google Feedfetcher, notsoobvious, tomoe_uehara and 16 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