Jump to content

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

class_EasyIni: Native syntax - Ini.Section.Key := val - Formatting retained


  • Please log in to reply
39 replies to this topic
Larctic
  • Members
  • 303 posts
  • Last active: May 10 2016 04:56 PM
  • Joined: 21 Jul 2012

Feedback problems.

INI unable to read the following [2]

[2]
a

[3]
a

[1]
w


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

 

Feedback problems.

INI unable to read the following [2]

[2]
a

[3]
a

[1]
w

Thank you. Confirmed bug. Not surprisingly, it is a bug related to sorting. It is triggered when using whole numbers in section names and the numbers are not ordered least to greatest. There is no issue If you start with [1], then [2], and [3], or use, say, a decimal number (2.0, 3.0, and 1.0) or use non-numeric keys, there is no issue. For example,

sIni:="
(LTrim
	[2.0]
	a

	[3.0]
	a

	[1.0]
	w
)"

vIni := class_EasyIni("", sIni)

for sec, aData in vIni
	for k, v in aData
		Msgbox %sec%`n%k%

return

Tracking down this bug will be tricky -- it is in the code I copied from this post and this post. In the meantime, I hope my workaround is sufficient.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

 

Feedback problems.

INI unable to read the following [2]

[2]
a

[3]
a

[1]
w

Thank you. Confirmed bug. Not surprisingly, it is a bug related to sorting. It is triggered when using whole numbers in section names and the numbers are not ordered least to greatest. There is no issue If you start with [1], then [2], and [3], or use, say, a decimal number ([2.0], [3.0], and [1.0]) or use non-numeric keys. For example,

sIni:="
(LTrim
	[2.0]
	a

	[3.0]
	a

	[1.0]
	w
)"

vIni := class_EasyIni("", sIni)

for sec, aData in vIni
	for k, v in aData
		Msgbox %sec%`n%k%

return

Tracking down this bug will be tricky -- it is in the code I copied from this post and this post. In the meantime, I hope my workaround is sufficient.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Hi Larctic, I think this issue is an AutoHotKey limitation having to do with using numeric keys in arrays. I have duplicated the problem in OrderedArray (EasyIni object is basically built from this object) and am waiting on a response to confirm this bug and to see if there is any workaround/fix.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Larctic
  • Members
  • 303 posts
  • Last active: May 10 2016 04:56 PM
  • Joined: 21 Jul 2012

Thanks. This is my temporary solution. Very slowly.

Z_Ini(h,p=""){
    If !InStr(h,"`n")
        FileRead, h, %h%
    If h:=Trim(h," `t`r`n")
    {
        j:=OrderedArray() ;,p ? h:=Replace_Var(h):""
        Loop, Parse, h, `r`n, `r `t
        {
            (!A_LoopField or (p:=SubStr(i:=A_LoopField,1,1)) = ";")
                ? "" : ((p = "[" and p:=InStr(i,"]",0,0))
                    ? n:=j[SubStr(i, 2, p-2)]:=OrderedArray(): ((p:=InStr(i,"="))
                        ? n[Trim(SubStr(i, 1, p-1))] := Trim(SubStr(i, p+1)) : (n[i]:="")))
        }
        return j
    }
}


spirytus
  • Members
  • 1 posts
  • Last active: Apr 12 2014 07:29 AM
  • Joined: 12 Apr 2014

Verdlin,

 

Good work bud. Just a heads up with the .Save() function is a little buggy when it comes to preserving format of the .ini read initially if you throw in comments.

 

EG:

 

Original:

; Some fancy comments
[Section]
key=value
;More fancy comments
[Section 2]
key=value

Will format as this after it's saved:

; Some fancy comments[Section]
key=value
;More fancy comments[Section 2]
key=value

I discovered this minor bug when writing a new function. Your read from string function comes in handy for my purpose and I was disapointed when I couldn't find the oposite, so I made my own. I've revised your .Save() and my SaveToVar() to be a little bit more legible (to me). Still not fully there but it works for me for now... Maybe you can write one better and include in your distro?

 

Oh, and did I say great work? you guys rock.

	SaveToVar()
	{
		ReturnString :="" ; initialize the return variable (old habit)
		; Formatting is preserved in ini object
		for k, v in this.EasyIni_ReservedFor_TopComments
		{
			if NOT (A_Index == 1)
				ReturnString .= "`n"
			if NOT (v==Chr(14))
				ReturnString .= v
		}
		for section, aKeysAndVals in this
		{
			ReturnString .= "`n[" . section . "]" ; always a new line at the start of section (just like keys below)
			bEmptySection := true
			for key, val in aKeysAndVals
			{
				bEmptySection := false
				ReturnString .= "`n" . key . "=" . val
				; Add the comment(s) for this key
				stComments := this[section].EasyIni_ReservedFor_Comments[key]
				Loop, Parse, stComments, `n
				{
					ReturnString .= "`n"
					if NOT (A_LoopField == Chr(14))
						ReturnString .= A_LoopField
				}
			}
			if (bEmptySection)
			{
				; An empy section may contain comments...
				stComments := this[section].EasyIni_ReservedFor_Comments["SectionComment"]
				Loop, Parse, stComments, `n
					if NOT (A_LoopField == Chr(14))
						ReturnString .= A_LoopField
			}
		}
		return ReturnString
	}


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012
...Save() function is a little buggy when it comes to preserving format of the .ini read initially if you throw in comments.

 

Thanks for catching that! This has been fixed.

 

Your read from string function comes in handy for my purpose and I was disapointed when I couldn't find the oposite...

...Maybe you can write one better and include in your distro?

 

Oh, and did I say great work? you guys rock.

 

Wow. That is so obvious I don't even know how I missed it. That's a great idea. I went ahead and added my own version of it, but there are two things I should note:

  1. The function is named ToVar() -- I try to keep function names simple; I think "ToVar" is still obvious to users that this function will output the ini to a var.
  2. ToVar() actually calls save, saves to a temp file, reads that file in a var, deletes that file, and then returns the file as a var. I chose this method to reduce code cloning. It obviously is inefficient, but this class is very fast, and I think speed won't be an issue except for extremely large inis. I'm open to criticism, and I will certainly change this is it becomes apparent that this function is too slow, but for now it looks like a good approach.

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Vilkra
  • Members
  • 1 posts
  • Last active: Sep 20 2014 04:03 PM
  • Joined: 15 Sep 2014

Hello, I'm an ultra newbie, so please bear with me.

 

How do I actually read the keys exactly as they are into variables? I mean, getting a result of Var1=11 and etc. I read the OP, but obviously I'm doing it wrong.

;This is my ini Test.ini

[Test]
Var1=11
Var2=33

I'm trying to switch all my files to .ini and this looks like the thing to use, Thanks for the help!



Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Hello, I'm an ultra newbie, so please bear with me.

 

How do I actually read the keys exactly as they are into variables? I mean, getting a result of Var1=11 and etc. I read the OP, but obviously I'm doing it wrong.

;This is my ini Test.ini

[Test]
Var1=11
Var2=33

I'm trying to switch all my files to .ini and this looks like the thing to use, Thanks for the help!

 

Hi Vilkra! Sorry for the late reply -- I have been rather inactive lately. If you have a file named Test.ini, then this is how you would load it...

vTestIni := class_EasyIni("Test.ini") ; this assumes Test.ini is in the same location as AutoHotkey.exe

for sec, aData in vTestIni
	for k, v in aData
		Msgbox [%sec%]`n%k%=%v%

; To answer you question about getting the result of Var1, the shorthand way of doing that is:
Msgbox % vTestIni.Var1 ; Show output "11"

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Support discontinued here and continued in http://ahkscript.org....php?f=6&t=5522


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.