INIFileHandler Module

BlitzMax Forums/BlitzMax Programming/INIFileHandler Module

Muttley(Posted 2007) [#1]
Hi All,

I've finally finished off my free INIFileHandler module.

It allows you to create and manipulate data stored in INI-style configuration files. Not all features of the INI file "standards" are supported, but almost all of the more useful ones are.

At the moment there is only one simple example program, but I will be adding a few more over the next couple of weeks.

Details Here

Hope it comes in useful for somebody and let me know if you find any problems with it or have any questions.

Cheers

Muttley


Russell(Posted 2007) [#2]
Long live .ini/.pref files!

(Death to the super-behemoth bloat monger Windows Registry!)

Russell


Muttley(Posted 2007) [#3]
Hehe.

I mainly wrote it as I found XML configs too confusing to design. ;)


Big&(Posted 2007) [#4]
Nice little mod Muttley :)

(Came across it on YakYak first, totally missed in on here!)


Muttley(Posted 2007) [#5]
Cheers Big&.

What's your nick over at YakYak then? There seems to be a lot of love for Blitz over at YakYak.

Gonna re-launch my Log File Handler next I think. :)


Big&(Posted 2007) [#6]
I dont post there at all. 100% lurker :)


TaskMaster(Posted 2007) [#7]
Just took a look at this. One hting you might want to add to your get functions is a default.

So it would be like this:

Local width:Int = configFile.GetIntValue( "Graphics", "ScreenX", 800)

This way, when you are loading your parameters, if they don't exist, you get the default returned. Makes it much easier on the programmer using the ini file. Instead of checking that I got values and if not, setting the variable to a default value, you just get a defauklt returned if it does not exist.


Muttley(Posted 2007) [#8]
I see what you mean.

Would you want the section/parameter and value created on the fly if it doesn't exist as well then?

Muttley


TaskMaster(Posted 2007) [#9]
No, I don't think so, since it is the default, no reason for it to be in the ini file at all...

And, the next time the ini file gets written, the parameter would be written anyway.


Muttley(Posted 2007) [#10]
But if it's not in the INI file, then when the INI file is rewritten it will be left out. You (as a programmer) would never know it wasn't in the INI file as if you'd requested a parameter with a default and that default is returned you'd only know that you'd got a value correctly, not that the parameter/value didn't actually exist. So you wouldn't know to add it manually, IYSWIM.


TaskMaster(Posted 2007) [#11]
I see what you mean, but if it is just the default, then it doesn't really need to be in the INI file.

I generally write the whole INI file when I save it anyway. Not just the pieces that change. Much easier to just call one function and write it all than to try and remember what has changed and needs to be written.


Muttley(Posted 2007) [#12]
The whole INI file is rewritten when saved, which is why I would need to add the section/paramater with the default value if it doesn't exist.


TaskMaster(Posted 2007) [#13]
Yes, but if it is the default, it does not need to exist.

And if it changes, the programmer should be writing it.

Does that make sense?


Muttley(Posted 2007) [#14]
OK, try this one.

If the default is returned because it doesn't exist. How does the programmer know it doesn't exist to re-add it (say, for example, the user has accidently deleted a line in the INI file)?


TaskMaster(Posted 2007) [#15]
Every time my program reads the INI, it will get the default for any parameter that does not exist. Which is fine, it just means the user has not changed it. If the user changes it, then the new value will get written.

If it is set to the default, then it does not matter whether it exists or not.

No different than if you play with the registry, some values don't exist because the system is using the default. If you want to change it, then you add the parameter. If you want the default back, you can just delete the parameter.


Muttley(Posted 2007) [#16]
OK, I get what you mean now. I prefer to have everything in the INI file from the start TBH, but maybe I can add that as an option :)


TaskMaster(Posted 2007) [#17]
Yeah, if you are planning to have an INI file that you want the user to edit by hand (possibly), then you would want all of the options there. But, if the INI file is only for your program, then non-default items are not necessary.

You and I were looking at it from different aspects.

I guess you could always make the default an optional parameter.


Muttley(Posted 2007) [#18]
I've just released version 1.03.

You can now pass default values to the Get* methods which is used if the Parameter doesn't currently exist in the INI file. Optionally you can also now use the CreateMissingEntries() method to have those default values automatically added to the INI file if required (along with any required Sections) which can then be saved out.

Thanks to TaskMaster for the idea.

Details and download link here


TaskMaster(Posted 2007) [#19]
Nice. Thanks for making the addition. I have already started using it. :)


Muttley(Posted 2007) [#20]
NP. If you come with any other suggestions, just let me know. :)


Raz(Posted 2007) [#21]
Nice one :) Will be nice to use a proper system instead of bodging something myself! ta!


Muttley(Posted 2009) [#22]
Version 1.1.0 now released.

This is mainly a bug-fix and code tidy-up release, although I have also added lots of unit tests using Brucey's fantastic bah.maxunit module.

Download from: http://code.google.com/p/muttmod/downloads/list


MGE(Posted 2009) [#23]
Excellent. Thanks Muttley!