Simple INI Reader and Writer

Monkey Forums/Monkey Code/Simple INI Reader and Writer

Goodlookinguy(Posted 2012) [#1]
Edit 3/5/13: I'm not sure what I was talking about in the below edit...but I hope I haven't made any changes to it since this...I think.
Edit 7/13/12: I'm reworking several parts of this at the moment. The next stable version of it will be between 7/13-7/21 probably.

I ported a BlitzMAX INI reader and writer for some of my stuff. Decided it didn't have enough features and added some. I figured it was worth sharing since there weren't any other INI type file readers.

I'm heading to bed at the time of this posting, so I'll just leave this list of methods, functions, and constants until I come back. When I come back later, I'll document and explain anything that needs to be explained.




Test
[monkeycode]Import cIni

Function Main:Int()
Local iniData:String = "[Data]~nValue 1 = 123456789~nValue 2 = 9.123456~nValue 3 = 15, 7.5, 3.75"
Local ini:Ini = Ini.Load(iniData)

Print ini.Get("Value 1", "Data") + ini.Get("Value 2", "Data")
Print String(ini.GetInt("Value 1", "Data"))
Print String(ini.GetFloat("Value 1", "Data"))
Print String(ini.GetFloat("Value 2", "Data"))
Print String(ini.GetInt("Value 2", "Data"))
Print String(ini.GetInt("Value 1", "Data") + ini.GetFloat("Value 2", "Data"))
Print ini.Get("Value 3", "Data")
Print "==================================="

Local intArray:Int[] = ini.GetIntArray("Value 3", "Data")
Local floatArray:Float[] = ini.GetFloatArray("Value 3", "Data")

For Local intVal:Int = EachIn intArray
Print "Int -> " + String(intVal)
Next

For Local floatVal:Float = EachIn floatArray
Print "Flt -> " + String(floatVal)
Next

Return 0
End[/monkeycode]


Methods, Functions, and Constants

[monkeycode]'- Methods
* ContainsKey:Bool( key:String, section:String )
* ContainsSection:Bool( section:String )
* Add:Void( key:String, value:String, section:String )
* Set:Void( key:String, value:String, section:String )
* Get:String( key:String, section:String, defaultValue:String )
* GetInt:Int( key:String, section:String, defaultValue:Int )
* GetFloat:Float( key:String, section:String, defaultValue:Float )
* GetIntArray:Int[]( key:String, section:String, defaultValue:Int[] )
* GetFloatArray:Float[]( key:String, section:String, defaultValue:Float[] )
* GetAsFile:String()
* GetSections:String[]()
* GetKeys:String[]( section:String )
* Merge:Void( data:String, mergeMode:Bool, useQuotes:Bool )
* Merge:Void( ini:Ini, mergeMode:Bool )

'- Functions
* Load:Ini( data:String, useQuotes:Bool )
* Create:Ini( useQuotes:Bool )

'- Consts
* DefaultSection:String
'-- How to handle quote reading
* AcceptQuotes:Bool '<-- Take only the contents wrapped in the quotes if in quotes
* IgnoreQuotes:Bool '<-- Take everything including quotes at beginning and end
* UseQuotes:Bool
* NoQuotes:Bool
'-- Merge Modes
* IgnoreSetKeys:Bool
* OverwriteKeys:Bool[/monkeycode]


degac(Posted 2012) [#2]
Nice to share.
I wrote a similar thing for Bmax (years ago): I will add something about 'arrays-keys' (like KEYS=1,2,3) for multiple keys-value.


[VOLOFORCE](Posted 2016) [#3]
I am sorry... but how exactly does this work?

Like say, I want to read an INI-file.

In which directory do I have to put it?

Then, how do I save? Or create a new one?


Goodlookinguy(Posted 2017) [#4]
This reads and writes INI files in memory. You have to handle the saving and loading since there are numerous systems people have designed for virtually saving data and loading it.


[VOLOFORCE](Posted 2017) [#5]
Oh, ok. Now I see, thanks. I was just confused by this. Mostly use JSON but INI can be handy.