Ini File Manager

Posts 1–15 of 20 · Page 1 of 2
Ini File Manager
Hi guys,

I was doing my business when I thought that release this little class could be useful for someone out there. Here you go (done in VS2012):

IniFile.h :
Code:
#include "stdafx.h"
#include <windows.h> 

class IniFile{
	public:
		IniFile(const char*); //constructor
		char* ReadValue(const char*,const char*);                     //general func to read the value
		bool WriteString(const char*,const char*,const char*);                    //func to write a string
		bool WriteInteger(const char*,const char*,int);             //func to write an integer
		bool WriteBool(const char*,const char*,bool);             //func to write a boolean
		bool WriteDouble(const char*, const char*, double);                         //func to write a double
	private:
		bool WriteiniString(const char*,const char*,const char*);                  //private func used to write all the previous types
		~IniFile(); //deconstructor
		const char* dir; //path of the file (assigned only once in the constructor)
};
IniFile.cpp :
Code:
#include "stdafx.h"
#include "IniFile.h"

IniFile::IniFile(const char* uri){
	dir = uri;                  //assign the path of the file
}
IniFile::~IniFile(){
	delete(dir);                //free the memory of the pointer on deconstruction
}
bool IniFile::WriteString(const char* section, const char* key, const char* value){
	return WriteiniString(section, key, value);
}
bool IniFile::WriteInteger(const char* section, const char* key, int value){
	char* buffer = new char[7];                     //buffer for the conversion from int to char*
	_itoa(value,buffer,10);                             //actually, the conversion
	return WriteiniString(section, key, buffer);
}
bool IniFile::WriteDouble(const char* section, const char* key, double value){
	const int numOfDigits = 6, bufferLength = 32;    //1st is n° of digits, 2nd is the buffer length
	char* buffer = new char[bufferLength];
	_gcvt(value,numOfDigits,buffer);                 //the conversion
	return WriteiniString(section, key, buffer);
}
bool IniFile::WriteBool(const char* section, const char* key, bool value){
	return value? WriteiniString(section, key, "true") : WriteiniString(section, key, "false");	
}
bool IniFile::WriteiniString(const char* section, const char* key, const char* value){
	return WritePrivateProfileStringA(section, key, value, dir);
}
char* IniFile::ReadValue(const char* key, const char* variable){
	const int length = 100;                          //max readable chars (you can edit the length)
	char* buffer = new char[length];             //buffer for the reading of the value
	GetPrivateProfileStringA(key,variable,NULL,buffer,length,dir);
	return buffer;
}
Usage :
Code:
#include "stdafx.h"
#include "IniFile.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	IniFile * file = new IniFile("D:\\test.ini");  //create the file

	file->WriteString("TEST", "String", "string");    //write some values
	file->WriteBool("TEST", "Bool", false);
	file->WriteInteger("TEST", "Int", 1234);
	file->WriteDouble("TEST", "Double", 56.78);

	cout<<file->ReadValue("TEST","String")<<endl;   //read and print them
	cout<<file->ReadValue("TEST","Bool")<<endl;   
	cout<<file->ReadValue("TEST","Int")<<endl;  
	cout<<file->ReadValue("TEST","Double")<<endl;   
	return 0;
}

Screenshot:
bool IniFile::WriteInteger(const char* section, const char* key, int value){
char* buffer = new char[7]; //buffer for the conversion from int to char*
_itoa(value,buffer,10); //actually, the conversion
return WriteiniString(section, key, buffer);
}


bool IniFile::WriteDouble(const char* section, const char* key, double value){
const int numOfDigits = 6, bufferLength = 32; //1st is n° of digits, 2nd is the buffer length
char* buffer = new char[bufferLength];
_gcvt(value,numOfDigits,buffer); //the conversion
return WriteiniString(section, key, buffer);
}
. .
Quote Originally Posted by abuckau907 View Post
. .
Simply because he is allocating memory on the heap ... He should call delete after that.
@Sixx93
Please only use the English language, others are not allowed.
Quote Originally Posted by Sixx93 View Post
Code:
                bool WriteString(const char*,const char*,const char*);                    //func to write a string
		bool WriteInteger(const char*,const char*,int);             //func to write an integer
		bool WriteBool(const char*,const char*,bool);             //func to write a boolean
		bool WriteDouble(const char*, const char*, double);
Why dont you just use template functions, much easier and you dont need to create multiple functions for each type

Also instead of using the older C type functions (PrivateProfileString) use the output/input file streams (from <fstream>)

Consider freeing the memory you allocate on the heap (Already mentioned but yea)

fstream makes file handling very very very easy simple
Quote Originally Posted by Yemiez View Post
Also instead of using the older C type functions (PrivateProfileString) use the output/input file streams (from <fstream>)
Because if he read the file into a stream he would have to manually parse it and find the content he needs based on the keys. The C functions work so no fixes needed (You supply a key and what not and it parses it for you).
Quote Originally Posted by ~BlackMaster~ View Post
Because if he read the file into a stream he would have to manually parse it and find the content he needs based on the keys. The C functions work so no fixes needed (You supply a key and what not and it parses it for you).
Manual parsing is even better and easier to update later on.
Quote Originally Posted by user590177 View Post


Manual parsing is even better and easier to update later on.
1. Why is it "better"?
2. Doesn't sound like he would require "updating". He's using the .ini format which is already defined by Microsoft.
Quote Originally Posted by ~BlackMaster~ View Post
1. Why is it "better"?
2. Doesn't sound like he would require "updating". He's using the .ini format which is already defined by Microsoft.
1. Allows you to parse the data the way you want it to.
2. Indeed, thus it is limited only to the ini format whereas a manual parsing might open doors for more features. Ie, encryption or file storage within the config file.
Quote Originally Posted by Yemiez View Post
Also instead of using the older C type functions (PrivateProfileString)
Correct.
PrivateProfileString API was deprecated a long time ago.

Quote Originally Posted by Yemiez View Post
Consider freeing the memory you allocate on the heap (Already mentioned but yea)
No.
I said to call delete after that. But they have important difference, one of them is that free won't run an object destructor, delete will.

Quote Originally Posted by Yemiez View Post
fstream makes file handling very very very easy simple
It's a normal method. fstream is very practical.
A good idea is stringstream, similar to fstream but works on strings internally instead of a file or user input.

Quote Originally Posted by user590177 View Post


Manual parsing is even better and easier to update later on.
Why do you say random stuff?

Quote Originally Posted by ~BlackMaster~ View Post
Because if he read the file into a stream he would have to manually parse it and find the content he needs based on the keys.
Correct.

Quote Originally Posted by ~BlackMaster~ View Post
The C functions work so no fixes needed
What are you talking about? However ... as already said, no. PrivateProfileString API was deprecated a long time ago.
@Sixx93 show your 'new' code please
Quote Originally Posted by maestro1994 View Post
show your 'new' code please
Show some of your previous work please.
I told to Sixx93 to show his code, then I'll see if he did something wrong and advise him something.
Quote Originally Posted by user590177 View Post


Show some of your previous work please.
Sorry, but I don't need to prove myself from a pseudo-guy.
This isn't my main forum, so I'm not so much active.
I made just a tutorial: http://www.mpgh.net/forum/showthread.php?t=1023313
People note programming levels/experiences of a guy as he talks, not from his projects.
When I have time, I help someone.
Another italian forum is my main forum, where there aren't only 4-5 people with nice programming skills like here, but the 90% of users on the forum, completely italian.
Have a nice day.
Thanks for all the suggestions. I've managed the heap now (just allocated the space once and made it free in the deconstructor).

As BlackMaster said, I used PrivateProfileString just to not manually handle the stream itself, I wanted to make a fast and simple class just to reach the goal. It's not meant to be super customizable or extremly flexible.
Since I needed to store some info I decided to write this class, then I decided to share it.
Quote Originally Posted by maestro1994 View Post
No.
I said to call delete after that. But they have important difference, one of them is that free won't run an object destructor, delete will.
I know, its still called freeing the memory tho
Quote Originally Posted by Yemiez View Post
I know, its still called freeing the memory tho
The problem is that free is a thing, and delete is another thing ... Don't confuse them.
On the rest, you are right
Posts 1–15 of 20 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us