 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Thu Jul 16, 2009 7:04 pm Post subject: |
|
|
| First post updated. Also first example corrected. |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Sat Jul 18, 2009 2:48 am Post subject: Update |
|
|
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.
- 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 |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Sat Jul 18, 2009 11:06 am Post subject: |
|
|
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.
- Get a comma separated list of all key names.
| Code: | | keys := ini_getAllKeyNames(ini, "Tip") |
Get a comma separated list of all section names.
| Code: | | sections := ini_getAllSectionNames(ini) |
|
|
| Back to top |
|
 |
Drugwash
Joined: 08 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Sat Jul 18, 2009 9:39 pm Post subject: |
|
|
Simple question: why not a single function with optional second parameter that makes the difference between keys and sections?
| Code: | sections := ini_getNames(ini)
keys := ini_getNames(ini, "K") |
|
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Sat Jul 18, 2009 10:16 pm Post subject: |
|
|
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 |
|
 |
Mystiq
Joined: 08 Jan 2007 Posts: 42
|
Posted: Sun Aug 16, 2009 9:19 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Sun Aug 16, 2009 9:35 pm Post subject: |
|
|
| 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 |
|
 |
Mystiq
Joined: 08 Jan 2007 Posts: 42
|
Posted: Sun Aug 16, 2009 10:57 pm Post subject: |
|
|
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.  |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Mon Aug 17, 2009 2:56 pm Post subject: |
|
|
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. 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 |
|
 |
Mystiq
Joined: 08 Jan 2007 Posts: 42
|
Posted: Mon Aug 17, 2009 3:28 pm Post subject: |
|
|
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? |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 897 Location: Berlin, DE
|
Posted: Mon Aug 17, 2009 7:16 pm Post subject: Update |
|
|
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 |
|
 |
Mystiq
Joined: 08 Jan 2007 Posts: 42
|
Posted: Tue Aug 18, 2009 3:58 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
erinau
Joined: 25 Sep 2009 Posts: 2
|
Posted: Fri Sep 25, 2009 2:27 pm Post subject: |
|
|
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.  |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4116 Location: Belgrade
|
Posted: Fri Sep 25, 2009 3:43 pm Post subject: |
|
|
| 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 |
|
 |
erinau
Joined: 25 Sep 2009 Posts: 2
|
Posted: Fri Sep 25, 2009 5:33 pm Post subject: Problems |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|