Introduction:
Hi and thank you in advance. My name is Mikkel and i'm somewhat new to c++.
I actually came here about a month ago to ask how to make a simple cfg parser.
But after reading the rules i decided to try learning on my own and post my results instead.
I'm now done with the most important function in my program and it work's!!!
Fucking nice feeling :)
anyhow in my experience i have probably done a poor job as i carry over some bad habits from my time as a php scriptor.
I was wondering if someone would mind looking over my code and give me some pointers as to how i can optimize it?
what this code does / is supposed to do.
it parses a file line by line checking if certain criteria are met, and then add's the given information to a map called cfgEntries.
void scfgpars::getFileData()
{
std::string* output; //points to either id or value.
std::string id, value;
while(std::getline(file, line)) //go through file line by line.
{
output = &id; //set output to point to id.
if(line.find("=") != std::string::npos && line.at(0) != '=') // if the line has an equals
// and it is not the first char
// else clear for next line.
{
int Ecounter = 0; // equals counter
for (unsigned i=0; i<line.length(); ++i) //go through all the chars in the line.
{
char linechar = line.at(i); // current char.
if(linechar == '=') // if equals change output pointer to value and add to Ecounter.
{
Ecounter++;
output = &value;
}
if(Ecounter > 1) // if more then one equals invalidate line and clear for next line.
{
id.clear();
value.clear();
break;
}
if(linechar != ' ' && linechar != '=') // if it is not an equals or a space.
*output += linechar; // add char to output
}
if(Ecounter == 1) //add value to map with id as id.
{
cfgEntries [id] = value;
}
id.clear();
value.clear();
} else {
id.clear();
value.clear();
}
}
}
so i made a few changes.
added another map that represents each line of the file as i figured out i could not edit a file directly using fstreams.
instead a make a pair in the cfgEntris map that holds the entry / value and the corresponding line number from the file.
the new map cfgFile then holds each line in order with the linenumber as the id.
This way i can add changes to both cfgFile and cfgEntries by reference and then rewrite the cfg file by iterating through the cfgFile map.
I would like to post all the code here but it has suddenly become quite large :)
I will add the getFileData here and post a pastebin to the whole thing.
On a side-note: when am i supposed to use constant's. I'm having difficulty figuring out when exactly to use it.
Damn that's some nice code.
Thanks a lot JLBorges. Super nice with the http references.
I might have to leave out the c++11 stuff as i'm using code::blocks with gcc version 4.7.1.
i would like to use v4.8.1 with all the new 11 features but i'm working with SFML and encountering a lot of compiler errors when using the new compiler.
I even bought the c++ 11 primer book to learn from and i'm stuck in c++ 03. at least i think that's the standard i'm using.
anyway thanks a lot.
ps. you removed my pointer stuff :'( *sniff*
std::string* buffer;
std::string id, value;
That pointer was defining for me. I learned how pointers work with that. or i think i did :)