Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[module] Ini 1.0b2 - ini-like handling of strings


  • Please log in to reply
32 replies to this topic
majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
I n i
Set of functions for ini-like string handling


Download/View               Documentation



History
1.0 b2
- Added MakeSection function

1.0 b1
- Interface upgraded with many new functions. Existing API changed.
- Optimisation

0.1
Initial release
Posted Image

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
cool. I know a few ask for help questions that can be solved by this.

Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
I posted something similar to this a while back

http://www.autohotke...ghlight=loadini

Ive added a few extra functions for my own use too.
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Ini files are so simple you should be able to parse them without regex. That would make scripts with large datasets launch a lot faster with less memory.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Superfraggle
  • Members
  • 1019 posts
  • Last active: Sep 25 2011 01:06 AM
  • Joined: 02 Nov 2004
Titan

mine was originally written for an addition to a template designed to help newcomers.

The idea being just by adding three function calls they can load and save any variables at will.

I agree they are simple enough to use, and for large data sets this isnt the way to go, but for medium size and small size it will cut the need for writting all the reads and writes.

I guess it was also brewed out of lazyness lol

Or did i completely mis interprete what you mean and was you suggesting stringsplit would be a better way to go ??
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle

freakkk
  • Members
  • 182 posts
  • Last active: Dec 16 2014 06:23 PM
  • Joined: 29 Jul 2005
This will come in phenomenally useful in a number of applications; thus far I have created specialized ini handlers for various projects. This is an open ended solution that can replace most of them!

Thanks for sharing!

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

Ini files are so simple you should be able to parse them without regex

Its fast enough for 99% of usagees and the code is elegant. You don't need to be concerned about ultimate speed everywhere. The functionality is more important here.

I intenionately avoided String functions cuz I don't see much benefit over RegEx in normal Ini files. I care about code sophistication more and leave optimisation for time critical things. IMO, its wrong to optimise everything.
Also if you cheked out the carefuly you would see that the most frequent task is done without single RegEx.

Its also faster then multiple IniReads. Low number of RegExes are used.

Again, to quote Laszlo, how far you want to go in optimisation ?
Ultimately I can provide machine code, but that is hardly the point.

This is basicly configuration automation. So you can load your entire configuration in global vars in one step instead of series of IniReads.

Its probably not suitable for special cases but again, nothing is.
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
String functions are more elegant, regex is cryptic and takes time to learn. By using the standard set of parsing functions not only will scripts perform better but your code will be easier to follow and modify - another checkpoint for stdlib candidates.

Anyway the only reason I posted is because I'll be using these functions. I will modify them to suit my needs since like Skan, you prefer compact code over anything else. Thanks.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
LOL TITAN.

I had quolity laugh now :)

And if we talk about cryptic lets talk about Anchor :)
Anyway, you can argue that RegEx is cryptic, but the one there is fairly trivial, can be decifered by ppl knowing RegEx in several minutes, and basicly that is the only thing to care about in this code.

Since like Skan, you prefer compact code over anything else.
My coding is nothing like Skans. I don't practice obfuscation and I know very well that "code size" has nothing to do with speed. Skan got it all wrong when short code philosophy is in question. Nevertheless, I prefer recursion instead itereation because of its ultimate elegance. I don't know if you ever programmed in Prolog, but there even the very complex tasks can be coded in 3 lines. Not obfuscation if you know rules of the language, as much as RegEx crytpic patterns are not obfuscation once you did your lessons.

RegEx... I just love that thing. It makes me happy. :D
I am not sure how AHK lived up so long without it, I find it ridiculous that lanugage having primarly strings as types doesn't have the most sophisticated way to handle them.

String functions are more elegant

Not if I have to use 5 instead of 1 RE. 1 against 1, you are right :)

Anyway the only reason I posted is because I'll be using these functions

Nice to hear that. I can't wait to see your update. After all, if somebody else is willing to update anything I did, especialy you, I am happy. Due to your speed concern I checked the speed of the functions, and even with very large ini files (I tested on wincmd.ini, hardly any AHK script will have that much options) to get values from 0 - 10 ms. OFC, I can imediately spot problematic places, like StringTrimLeft in 2Var when all sections are iterated.

Then we come to idea that I force: Do I really want to spead up something that takes 10ms in worst case ? Do I really want to do that without debuger, without appropriate IDE and stuff like that. Do I really want to lose enormous amount of time to get small benefits, as currently, in AHK, i miss like 1000 libraries and I am slowely writting them as I need them. Imagine I optimise like crazy.... that would be horror.

On the other hand if somebody entusiastic enogh, like you, with your fanatic optimisation skills (not sarcastic, I learned alot from your and Laszlos optimisation skills), I can be only happy. I am more concerned currently about API that is extremely practical and easy to use in most cases.

Then if you do update them, please add the folowuing I missed - list of sections. If this is to be used with 3th party INIs keep in mind that some of the section names may not be valid variables (contain spaces, dots and sht like that).

Also, I am not sure if reverse opeerations shold be done (vars to sections) for ultimate config handling. Superfraggle did it as saveini but that way is not possible here, because first, you don't know what globals should be saved. I imagine you give a prefix of globals, and section, and it does the job, but that is not possible as you can't find out the full name of the variable. If listvars exists in some more advanced forms, that could be used... for instance listvars cfg_* but Chris doesn't wont to cooperate here so its just dreaming. Arrays will probably do good here, but again, when ?

Thx
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

if we talk about cryptic lets talk about Anchor

Commenting that is somewhere on my informal todo list.

RegEx crytpic patterns are not obfuscation

Actually in most cases they are, e.g. (?<=(["']))[^\1]*(?=\1\B) is an optimized version of "(.*?)".

I can't wait to see your update.

They're too specific for my usage now but I might post them later.

Do I really want to spead up something that takes 10ms in worst case ?

Well I think if you can, why not? It could just be my perfectionist nature but I don't feel comfortable releasing scripts whilst knowing they can be much improved. I'm sure Chris and other developers feel the same way.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

Commenting that is somewhere on my informal todo list.

And updating Ini module is on my Todo list.

Actually in most cases they are, e.g. (?<=(["']))[^\1]*(?=\1\B) is an optimized version of "(.*?)".

True. Optimisaion of REs is anoying even to me, the biggest fan.

Well I think if you can, why not?

Because, it always can and you have to stop somewhere. I am sure Anchor can be lot faster and without some suspicious/problematic functions like InvalidateRect. But hey, it does the job (in 99% of cases), nobody complains.
If I wanted to use uber fast code I would programm in ASM, not in AHK.

It could just be my perfectionist nature but I don't feel comfortable releasing scripts whilst knowing they can be much improved.

The life, the universe and everything else requires delicate measure of balanse. I am constantly updating my own. I find fairly biased for you to comment like this, knowing that your main prog environment is C# & dotNet the king of low speed. If you really want to talk to me about how things should be done, switch to ASM instead of dotNet. After all, you can't go lower then ASM... or ....

The truth is bitter - higher level of abstraction makes code slower, yet not enough abstraction makes code fast and hard to use. Balance again.

I'm sure Chris and other developers feel the same way.

Well, then I don't agree with you, Chris & other developers. But who cares? We definitely don't have same priorities in life. Something like dolphins and humans.

They're too specific for my usage now but I might post them later.

I see.
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

it always can and you have to stop somewhere

It's up to the author to exercise reasonable discretion. Mine goes as far to suggest using simple string functions over cryptic overzealous regex for something as basic as INI. I never ordered you to do anything or stated anywhere that optimization was an absolute necessity. If I had that mindset I most probably would program in ASM.

I find fairly biased for you to comment like this, knowing that your main prog environment is C# & dotNet the king of low speed

That's another common misconception. I don't use reflection and much polymorphism and inheritance so my C# apps are quite fast once compiled to CLR.

I don't agree with you, Chris & other developers. But who cares?

For an extra five or so minutes of work I would push for quality. If I was ignorant and had low substandards I could make-do with anything, so its a question of personality I suppose.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

That's another common misconception.

Its not common misconecption. Its a fact. I love dotNet but I know my beast. That tech will shine in few years.
Ye, its proven that C# can be as fast (and even faster) as C++ app, but only if you are programming guru and have no life other then optimising things (something like WoW episode of South Park). Even Rusinovich stated that he is scared about dotNet. So if even one of the biggest Windows gurus out there is "commonly missconcepted" what makes you think you are the one that is not ? Other then that, you don't have to measure programs running in dotNet VM. I can noticed their slow speed and large memory footprint as soon as I start them.

I don't use reflection and much polymorphism and inheritance so my C# apps are quite fast once compiled to CLR.

So you don't use framework at all ?

For an extra five or so minutes of work I would push for quality.

Perhaps its 5 minutes for you. I don't beleive anything in this world is possible in 5 minutes.

Mine goes as far to suggest using simple string functions over cryptic overzealous regex for something as basic as INI

I think AHK REs are very fast based on personal experience and some measures.
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

its proven that C# can be as fast (and even faster) as C++ app

You've just contradicted yourself.

...only if you are programming guru and have no life other then optimising things

I'm no .NET optimization expert (those who are often get paid a lot for it) but there are truths in both of our statements. AutoHotkey is a different story, here optimization techniques are pretty much common sense to anybody with prior programming experience, and there is nothing much that needs to be studied and learned; i.e. use commands over functions, avoid dynamic variables and expressions for simple assignments, parsing loop instead of StringSplit etc.

I don't beleive anything in this world is possible in 5 minutes.

I believe anything is possible. Perhaps this reflects our contrasting philosophies on many of the issues that's been discussed.

I think AHK REs are very fast based on personal experience and some measures.

Orly? From my experience and benchmarks I found that string commands outperform regex in almost any case.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Made it all purpose now (would you believe if I said I wrote it in 3ish mins?):

/*
	Function: Ini_GetVars
		Loads variables from an INI file.
	
	Parameters:
		src - INI file path
		sec - filter variables by sections with this name
		pre - common variable name prefix
		d - separator for section name and prefix
	
	Returns:
		Number of variables loaded.
	
	About: Examples
		- Ini_GetVars("settings.ini") ; load all variables indiscriminately
		- Ini_GetVars("application.ini", "window") ; load variables under 'window' section
		- Ini_GetVars("test.ini", "", "") ; use no prefix
	
	About: License
		- GNU General Public License 3.0 or higher <http://www.gnu.org/licenses/gpl-3.0.txt>
	
*/

Ini_GetVars(src, sec = "", pre = "", d = "_") {
	local s, p, v1, v2, t, at
	static x = " ,	,``,¬,¦,!,"",£,%,^,&,*,(,),-,=,+,{,},;,:,',~,,,<,.,>,/,\,|,,,"
	at = %A_AutoTrim%
	AutoTrim, On
	If pre !=
		pre .= d
	Loop, Read, %src%
	{
		If InStr(A_LoopReadLine, "[") = 1
		{
			StringMid, s, A_LoopReadLine, 2, InStr(A_LoopReadLine, "]") - 2
			If s contains %x%
				s =
			Else If s !=
				s .= d
		}
		Else If !(sec != "" and sec != s) and p := InStr(A_LoopReadLine, "=") 
		{
			StringLeft, v1, A_LoopReadLine, p - 1
			v1 = %v1%
			If v1 contains %x%
				Continue
			StringTrimLeft, v2, A_LoopReadLine, p
			%pre%%s%%v1% = %v2%
			t++
		}
	}
	AutoTrim, %at%
	Return, t
}

I'll wait on you for the reverse functions lol.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit