Basic .txt ini reader.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Basic .txt ini reader.

Post by wildrj »

Well i needed a basic ini reader for my project so i decided to make one :/. It supports basic ini formatting..

Code: Select all

[Section]
Id=value
;comment here just make sure it doesn't have a equal sign
The only thing diffrent is the constructor it takes a file and a "badge"
the badge is just something you put at the top of your ini file to make sure its the right one.

So here ya guys go

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> 

class cIniReader{

	public:
		cIniReader(std::string file,std::string badge);
		~cIniReader();

		int getInt(std::string id);
		std::string getStr(std::string id);
		bool valueFind(std::string str, std::string id);

	private:
		
		bool checkBadge();

		std::string iniFile;
		std::string iniBadge;
		std::string iniValueTemp;
};

Code: Select all

#include "CIniReader.h"

cIniReader::cIniReader(std::string file, std::string badge)
{

	iniFile = file;
	iniBadge = badge;
	std::string iniValueTemp = "";
}

cIniReader::~cIniReader()
{

}

int cIniReader::getInt(std::string id)
{
	std::string temp;
	bool checked = false;

	if(checkBadge())
	{
		std::ifstream myfile(iniFile.c_str());
		while(checked != true && myfile.eof() == false)
		{	
			std::getline(myfile,temp);
			if(valueFind(temp,id) != false)
			{
				checked = true;
			}
		}

		if(checked == true)
		{
			return atoi(iniValueTemp.c_str());
		}
		else
		{
			return 0;
		}

	}
	else
	{
		return 0;
	}

}


std::string cIniReader::getStr(std::string id)
{
	std::string temp = "";
	bool checked = false;

	if(checkBadge())
	{
		std::ifstream myfile(iniFile.c_str());
		while(checked != true && myfile.eof() == false)
		{	
			std::getline(myfile,temp);
			if(valueFind(temp,id) != false)
			{
				checked = true;
			}
		}

		if(checked == true)
		{
			return iniValueTemp;
		}
		else
		{
			return "";
		}

	}
	else
	{
		return "";
	}

}

bool cIniReader::valueFind(std::string str,std::string id)
{
	std::string temp = "";
	size_t found;

	found = str.find("=");

	if(found != std::string::npos)
	{
		temp = str.substr(0,found);
		if(temp == id)
		{
			iniValueTemp = str.substr(found + 1);
			return true;
		}

	}
	else
	{
		return false;
	}

}

bool cIniReader::checkBadge()
{
	std::ifstream myfile(iniFile.c_str());
	std::string badge;

	if(myfile.is_open())
	{
		std::getline(myfile,badge);
		myfile.close();
		if(badge == iniBadge)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}


And heres how to use it

Code: Select all

#include <iostream>
#include <string>
#include "CIniReader.h"
int main(){
	int test;
	std::string test2;
	cIniReader read("test.ini", "[BadgeHere]");
	test = read.getInt("Test");
	test2 = read.getStr("Test2");
	std::cout<<"Test =" << test <<std::endl;
	std::cout<<"Test2 =" << test2 <<std::endl;
	std::cin.get();
	return 0;
}
http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

seems interesting that you didnt use the irrlicht file system methods (seeing as you are using irrlicht).

But its yet another snippet someone might find useful, thanks
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Post by wildrj »

Re usable code :).. So the reader can be used for something other then irrlicht related games :D
http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yeah, but now this code is not usable for e.g. zip file content.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Nice small class.
However is there a reason why you parse the whole file when you read a value?
You could store the content of the file in your object so you don't have to read the file over and over again.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Post Reply