Code: Select all
[Section]
Id=value
;comment here just make sure it doesn't have a equal sign
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;
}