Original Post
I want to have some criticism about my Config parser about it's performance and the look of it. And i am thinking of slapping it together in a class later and be able to set values. Language: C Plus Plus Compiler: The standard one with Bloodshed Dev-C Plus Plus And this is somewhat related to a earlier topic i made. (A problem wich i solved now.) Anyway, beware of page height rape: main.cpp include.h ini.cpp External Dependencies: http://www.partow.net/programming/stringtokenizer/ http://www.boost.org/ Oh and here is a example config file:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <StringTokenizer/StringTokenizer.cpp>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <chjees/include.h>
using namespace std;
using namespace boost;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//gVariables
int main(int argc, char *argv[])
{
int work = ParseConfig("test.ini");
if ( work = 1){ cout << "Success!"; }
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
/*
Make sure to include these:
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <StringTokenizer/StringTokenizer.cpp>
#include <boost/algorithm/string.hpp>
#include <map>
*/
//Defined: gVariables
//Function: ParseConfig(string File)
#ifndef _CHJEES_CONFIG
#define _CHJEES_CONFIG 1
#include "chjees/ini.cpp"
#endif
using namespace std;
using namespace boost;
//Global variable map.
map<string, string> gVariables;
//Rest
int ParseConfig(string File); //Prototype
bool ParseCheck( string Text, string Text2 )
{
//Check if special
if ( Text == "Include" ){ ParseConfig( Text2 ); return 1; }
return 0;
}
int ParseConfig(string File)
{
//Variables
fstream fConfigFile( File.c_str(), ios::in );
string CurrentLine;
string fContent;
//Open File
//Check if open
if( fConfigFile )
{
//Add everything except comments to fContent
while ( getline(fConfigFile,CurrentLine) ) //getline(fConfigFile,CurrentLine)
{
//If '//' ignore.
if ( (CurrentLine.empty() == 1) || (( CurrentLine.at(0) == '/' ) && ( CurrentLine.at(1) == '/' )) )
{
//Ignore, pick next line
// cout << "Ignored" << endl;
}else{
//Add to global string
fContent += CurrentLine + "\n";
//fContent.append(fContent);
}
// cout << CurrentLine << endl << fContent << endl;
// system("PAUSE");cout << endl;
}
fConfigFile.close(); //Close File
}else{
//Or else Fail!
return 0;
}
//Start parsing and assigning variables (Also include other files.)
stringstream ContentStream;
ContentStream.str(fContent);
string prefix,suffix;
int space_pos;
bool special;
//While loop...
while ( getline(ContentStream,CurrentLine) )
{
//Check if not empty
if ( !CurrentLine.empty() )
{
//Is not empty, continue
//Step 1: Split '='
StringTokenizer ConTok(CurrentLine,"=");
prefix = ConTok.nextToken();
suffix = ConTok.nextToken();
//Step 2: Clean up spaces.
//Prefix
trim_left(prefix);
trim_right(prefix);
//Suffix
trim_left(suffix);
//Check if not special, otherwise add to a map.
special = ParseCheck(prefix,suffix);
if (special == 0) { gVariables[prefix] = suffix; }
}
}
return 1;
}
//Test
Cheese = 1
Lalala = zomg string! [Edited by - ChJees on February 11, 2008 3:05:17 PM]