Jump to content

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

INI Class


  • Please log in to reply
32 replies to this topic
zzzooo10
  • Members
  • 237 posts
  • Last active: Apr 14 2015 01:57 AM
  • Joined: 19 Dec 2010
INI Class
Quick and simple

Download

Version 0.3:
NEW THIS RELEASE
[*:1vxbik74] Removed adding comments for keys.
[*:1vxbik74] Added comments for sections.To get a keys value or a sections comment:
Msgbox % Ini.SectionName.KeyName ; Gets keys value

Msgbox % Ini.SectionName.__Comment ; Gets sections comment

To create a new section with a key do this
Ini.SectionName.KeyName:= "Foo" ; Sets the keys variab;e

Ini.SectionName.__Comment := "Bar" ; adds a comment to the key.



Use as a class: You'll have to include this file in your script.

#Include, cIni.ahk
Ini := new Ini("IniFile.ini")


Use as a lib: If this is in your library, load the ini like so.
Ini := cIni("IniFile.ini")


Idea:
I got the idea to make this class from here.

Example:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, Force
#Persistent
#Include, cIni.ahk

Info =
(
[Hotkeys]
Pause=^v
Exit=Esc
Blank=
Reload=#2
[Settings]
Autostart=True
Update=False
[I have spaces]
So do I=Sapces!
)
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ Load the Ini into memory
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ini := new Ini(Info) ; Load the ini, accepts an ini file or a var.
 
 ;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ GET
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Sections := Ini.Sections() 
 
MsgBox,  , Sections, % Sections
MsgBox, , Keys, % Ini.Keys() ; Gets every key in the ini.

Loop, Parse, Sections, `n
    MsgBox, , % A_LoopField, % Ini.Keys(A_LoopField) ; Get all the keys within the section.

;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ CREATE
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ini.NewSection.NewKey := 1 ; Create a new section with a key called "NewValue" with a value of 1.

SectionName := "Foo"
KeyName := "Bar"

Ini[SectionName][KeyName] := 9001 ; Create new section with a key.

MsgBox % Ini[SectionName][KeyName] ; To use a var use "[]" with no "."

; If a section or key has spaces in their name access them like this. Any spaces at the beginning and end of a Section, Key, or Value are removed.
MsgBox % Ini["I have spaces"]["So do I"]

 ;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ DELETE
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ini.Delete("Settings") ; Deletes whole section.
Ini.Delete("Hotkeys", "Exit") ; Delete a single key.

 ;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ RENAME
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ini.Rename("Hotkeys", "Magical Keys") ; Rename the section "Hotkeys".
Ini.Rename("Magical Keys", "Resume", "Pause") ; Rename the key "Resume".

 ;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ COMMENT
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ini.Foo.__Comment := "This is a simple`nMultiline comment." ; Add a comment to the section foo.

 ;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;~ SAVE 
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ini.Save("NewIni.ini") ; Save the modified ini to a new file.


sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010
1) Ini is popular :p Not the first ini class, I think there's another one called cIni too.

2) How do I set a value? Using rename?

hoppfrosch
  • Members
  • 399 posts
  • Last active: Feb 26 2016 05:31 AM
  • Joined: 25 Jan 2006

1) Ini is popular :p Not the first ini class, I think there's another one called cIni too.


You're right. It's quite popular. Just at the moment I'm working on my own cIni class (to learn classes in AHK ...). My approach is also object oriented, but suports comments within INI-Files ...

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

1) Ini is popular :p Not the first ini class, I think there's another one called cIni too.


You're right. It's quite popular. Just at the moment I'm working on my own cIni class (to learn classes in AHK ...). My approach is also object oriented, but suports comments within INI-Files ...


Something I would like to see is a json class, since json supports more structured "tree" structures of any depth. That way you can more or less have all data in one file, with a good structure from a programming point of view. Json is also common for web-APIs.

hoppfrosch
  • Members
  • 399 posts
  • Last active: Feb 26 2016 05:31 AM
  • Joined: 25 Jan 2006

Something I would like to see is a json class, since json supports more structured "tree" structures of any depth. That way you can more or less have all data in one file, with a good structure from a programming point of view. Json is also common for web-APIs.

Isn't lib_json [AHK_L] Json<--->Object already what you're looking for?

_________________________

;     (.)~(.)   
;    (-------)                                    
;---ooO-----Ooo---------------------------------------------------
;    Hoppfrosch  - AHK 1.1.00 Unicode 32bit on Win7 Ultimate
;-----------------------------------------------------------------                        
;    ( )   ( )                            
;    /|\   /|\


sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

Something I would like to see is a json class, since json supports more structured "tree" structures of any depth. That way you can more or less have all data in one file, with a good structure from a programming point of view. Json is also common for web-APIs.

Isn't lib_json [AHK_L] Json<--->Object already what you're looking for?


Oh wait, it seems to be. I had just heard of it but not been looking much at it, so realized it wasn't a class. The only annoyance there was the non-default comment flag, but I bet that could somehow be remedied rather easely.

  • Guests
  • Last active:
  • Joined: --
@sumon: couldn't you at least edit your quoted posts if you feel you need to quote at all? @reply is just as easy :D

zzzooo10
  • Members
  • 237 posts
  • Last active: Apr 14 2015 01:57 AM
  • Joined: 19 Dec 2010

2) How do I set a value? Using rename?

To set a value all you have to do is
Ini.NewSection.NewValue := 1 ;Create a new section with a key called "NewValue" with a value of 1. 
Msgbox % Ini.NewSection.NewValue
To use a variable for the name do this
SectionName := "Foo"
KeyName := "Bar"

Ini[SectionName][KeyName] := 9001 ; Create new section with a key.

MsgBox % Ini[SectionName][KeyName]


sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

@sumon: couldn't you at least edit your quoted posts if you feel you need to quote at all? @reply is just as easy :D


I do not understand, edit quoted posts? That's chronologically messy, no? Anyway, I like quoting like most habe noticed, I feel it makes it easier to both reply and read that way. I will however try to not make huge quotes or quote the OP!

  • Guests
  • Last active:
  • Joined: --

I do not understand, edit quoted posts? That's chronologically messy, no?

No that is not messy, that is sensible :!: You can just keep the important part, look at the above quote you made which included a fake signature block as well. (which is also "huge" for no good reason). So yes, edit quotes or just hit post reply. Thank you.

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010

I do not understand, edit quoted posts? That's chronologically messy, no?

No that is not messy, that is sensible :!: You can just keep the important part, look at the above quote you made which included a fake signature block as well. (which is also "huge" for no good reason). So yes, edit quotes or just hit post reply. Thank you.


Ah, I understand. Good point, will edit :)

zzzooo10
  • Members
  • 237 posts
  • Last active: Apr 14 2015 01:57 AM
  • Joined: 19 Dec 2010
Version 0.2 available.
[*:h4t379ex]Supports comments[*:h4t379ex]Minor bugfixes

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Oh I had not seen your note about the source where you got the idea and did a little bitching.^^ :oops: But I find you could do a little credit in the source...
How fast and reliable is it compared to other libraries? Did anyone made any tests?

No signature.


zzzooo10
  • Members
  • 237 posts
  • Last active: Apr 14 2015 01:57 AM
  • Joined: 19 Dec 2010
I will add credit in the source when I get a chance, and have not had a chance to test it against other libraries but it is faster then the regular ini commands.

MilesAhead
  • Members
  • 578 posts
  • Last active: Feb 29 2016 05:15 PM
  • Joined: 21 Jan 2009

INI Class
Quick and simple


This looks very interesting. If you are accepting feature requests I'd also like to be able to insert multi-line comments just before the start of a section.

As it is, to get the results below I'm deleting my .ini file, writing out comment lines, then closing the file. Then I use the standard Ini functions to write the section values. Kind of cumbersome doing it that way. But with multi-line comments an .ini could be self-documenting.

This is an .ini file I use that illustrates what I mean:

; valid modifier keys include
; ^ for Control
; ! for Alt
; # for WinKey
;
; LButton left mouse button
; MButton middle mouse button
; RButton right mouse button
;
; Example: !^MButton would be
; Alt and Control while pressing
; Middle Mouse Button
;
[Settings]
MouseHotkey=^MButton
KeyboardHotkey=!F8
PasteFilename=0
UseGlass=1
DesktopDir=C:\Utils

When I distribute compiled exe files most who use them are not familiar with AHK. By having the explanation I avoid having to translate "Control-F8" into "^F8" and can use the ini values as is.

"Some people, when confronted with a problem, think I know, I'll use regular expressions.  Now they have two problems."

- Jamie Zawinski