(C++) Class for reading Windows-INI-like files

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

addKeyToSection will add the key even if it exists.
Call setValue to change the value of an already existing key.

Going to change this behavior of addKeyToSection.
I didn't take a look on the empty lines yet but I defenetly will
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Yay! it works. Now just those empty lines and its all done.

Thanks a LOT! I can finally complete this part of my project.

Edit:
I just don't like to see 800X600 and I prefer 800x600 so I changed in get and set 2d value to read and write "x" instead of "X". Are there any more places I should change so it will follow this?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

There is another problem that if I wanna save a u32 value (Bits for example from the SIrrlichtCreationParameters struct) I need to cast it which a proper method can help me avoid it..
CIniFileReaderStub::setValue(..,..,u32 value)...

Edit:
And if we're at it, the tutorials at your site needs an update as they wont compile, simple typo reasons but still..

By the way, when was the last time you updated the docs?

Do I see a "Release 2.1 (Bug Fix)"..? :lol:
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Just as I promised release 2.1 is out.
It can be found at http://btbsoft.org (I change the hoster because of these annoying ads.

Thanks MasterGod for the bug reports.

And no I didn't fix this empty line "bug". I'm trying to remove the stdlib dependencies in the next release so that the class could probably be included in Irrlicht so it doesn't make sense to fix this now.
Last edited by Sylence on Sat Dec 27, 2008 8:07 pm, edited 1 time in total.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Np. Listen, I have a suggestion about the design. I think that if you're serious about inserting this class into Irrlicht this change has to come.
Making a template class tValue for values and then every time you work with a value its tValue type. You see where I'm heading? Cause lets say I wanna make methods to work with 12 more types, its gonna be insane to add it this way.
And it would help logically to have 1 method to Get, 1 method to Set and 1 method to Create (e.g addKeyToSection which creates new one if one doesn't exist and edit an existing one instead of having both that and setValue which now has no reason to be used - why using it if I can create and edit with addKeyToSection...)
I'm sure the chances of getting This class into irrlicht are much more greater.

P.S
Considering myself a beginner in C++ "Vs" the irrlicht community I'll still be glad to help if I can.
Also I think I'll do it anyways not just to practice templates but for the benefits I've talked about -While I'll be glad to submit and if you agree with some changes make it official.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

As far as my understanding of templates goes (which is not very far) you would still have to create a get method for every single type because the compiler can't know wheather you want the returned value to be an int or a float for example.

However the idea sounds nice. If this can be done it would be super to have the class designed that way
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Sylence wrote:As far as my understanding of templates goes (which is not very far) you would still have to create a get method for every single type because the compiler can't know wheather you want the returned value to be an int or a float for example.
Well, as far is My understanding of templates goes (which is also not very far) that is exactly what it fixes, by returning value of type tValue, in run-time it gets the right type so it has nothing to do with the compiler.
Edit: I think I'm wrong about the run-time thing but I'm pretty sure the compiler wont be the problem.

Can someone with better knowledge of template clarify that?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Yes. It will NOT be runtime. Also, if you code it properly, it'll return the proper type. But you know, there's a better way ;)

Declare 2 template var above the function, and, pass them BOTH to the function (which you'll make void). Is it ugly? yeah. Functional and efficient? yeah. Plus you can always mask it. Just pass result as a reference and modify it in the function ;)
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

As I previously said I don't know templates very well and I thought about the idea today.
I figured out one problem: How to convert the data that comes from the file into templates if you don't know what type the user wants to have.
float int double etc. are not that problem but I can't image how to convert the data into dimension2d or other types that have more than one value.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
MickeyKnox
Posts: 58
Joined: Tue Apr 10, 2007 7:49 pm
Location: Karlsruhe

Post by MickeyKnox »

I wrote a Makefile for the Ini File Reader, that builds a static library.
If you like it, please feel free to distribute it with your code.

Code: Select all

all:
	$(CXX) -I../irrlicht-1.4/include/ -c *.cpp
	ar r libinireader.a *.o
Please note, i didn't respected your directory structure, but rather threw
everything in one single directory.
xray
Posts: 231
Joined: Fri Feb 02, 2007 1:06 pm
Location: Germany, Munich
Contact:

Post by xray »

I cant get it to work here! Im using the following code to generate a ini file:

Code: Select all


...
	// checking rocket.ini for different parameters
	IIniFileReader* ini = createIniFileReader("rocket.ini");
	
	// if ini file doesnt excists, create a new one with standard parameters
	if (!ini->load()) {
		
		ini->drop();
		ini = createIniFileReaderEmpty();
		ini->setSaveFileName("rocket.ini");
		
		// device section
		ini->addSection("video");

		ini->addKeyToSection("video", "AA", "0");
		ini->addKeyToSection("video", "Bits", "32");
		
		ini->save();
		
		ini->drop();
		
	}
...

The problem is that every time on the second addKeyToSection, my game crashes. If I only set one addKeyToSection, everything works fine and the ini file appears with their values. So the array push_back call makes problems after every second addKeyToSection:

Code: Select all


	Sections[i]->Keys.push_back(k);

Any suggestion Sylence ?
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I believe the reason is that you don't have an empty line at the end of your ini file.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
xray
Posts: 231
Joined: Fri Feb 02, 2007 1:06 pm
Location: Germany, Munich
Contact:

Post by xray »

There is still no ini file at the beginning, so it couldnt be a matter of a missing empty line. What I want is to create a new ini file, and put all standard parameters in it. So if I comment the line:

Code: Select all

      ini->addKeyToSection("video", "Bits", "32");
out it works perfect and I get the following ini file:

Code: Select all

[video]
AA=0
But with the second addKeyToSection it crashes.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

First try to make manually the same ini file and see if it works. Then just add an empty line at the end and see if it works.
Second, if it didn't help, debug it..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

uh bad typo in the addKeyToSection() method

Code: Select all

void CIniFileReaderStub::addKeyToSection(const c8 *section, const c8 *name, const c8 *value)
{
	SKey* k = new SKey;
	strncpy(k->Name,name,64);
	if(value)
		strncpy(k->Value,value,64);

	for(u32 i=0; i<Sections.size(); ++i)
	{
		if( strcmp(Sections[i]->Name,section) == 0)
		{
			for(u32 j=0; j<Sections[i]->Keys.size(); ++j)
			{
				if( strcmp(Sections[i]->Keys[j]->Name,name) == 0)
				{
					strncpy(Sections[i]->Keys[j]->Value,value,64);
					return;
				}
			}

			Sections[i]->Keys.push_back(k);
			break;
		}
	}
}
this is what it should be.
wrote an i instead of an j :oops:
The archive on my website is updated too
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Post Reply