Suggestion : use a  lexing and parsing tool like visual parse. Hard code parser 
can be problematic.

Mickey M.
Construction Partner Inc.
http://www.constructionpartner.com

--- On Fri, 10/31/08, Tyler Littlefield <[EMAIL PROTECTED]> wrote:

From: Tyler Littlefield <[EMAIL PROTECTED]>
Subject: [c-prog] c++ code help please?
To: [email protected]
Date: Friday, October 31, 2008, 9:50 PM






Hello list,
I'm working on a basic config parser.
I've got the following code; my config file looks like:
docroot="/home/ tyler/docroot/ "
The parser is able to pick that out, but it's unable (sadly) to tell it's 
the last line; it just runs in a loop.
I know the code is kinda messy--I'll clean it up once it's running smoother.
Any suggestions would be great.
here's the main function, below that will be the definition for the Var 
struct.
//code:
map<string,Var> ParseFile(string FileName)
{
map <string,Var> parsed; //will hold the parsed data
FILE *input=fopen( FileName. c_str()," r");
if (input==NULL)
{
cout << "Opening of file failed!" << endl;
return parsed;
}
rewind(input) ; //make sure the pointer is at the top of the file
string line; //holds the line to be parsed.
string name; //the name of the variable.
string val; //the value of the variable.
string temp; //will be used for stripping out whitespace
Var variable; //the actual variable.
char v; //used to get a char from the file.
string::iterator top; //will be used to count.
string::iterator tracker; //another counter/placeholder iterator.
size_t index; //used to find '"' in strings.
size_t fq; //holds the position of the first quote.
size_t lq; //holds the position of the last quote.
//we'll run until end of file is reached:
while (feof(input) ==0)
{
line="";
//runs until feof or end of line
//will also set the variable line up for parsing
while (((int)v!=10) &&((int)v! =13)&&(feof( input)==0) )
{
if (feof(input) !=0)
{
break;
}
v=fgetc(input) ;
line+=v;
}
top = line.begin() ;
//checks to see if we've got a comment.
if (*top=='#')
{
continue;
}
//will run until we see '='
//will split the variable and value apart.
for (top=line.begin( );top<line. end();top+ +)
{
if (*top=='=')
{
name="";
//we found the name; lets take it off the line variable.
for (tracker=line. begin();tracker< top;tracker+ +)
{
name+=*tracker;
}
top++; //skip the '=' sign.
val="";
//retrieve the rest of the string.
for (tracker=top; tracker<line. end();tracker+ +)
{
val+=*tracker;
}
break;
}
}
temp="";
//now we'll run through the name and strip off whitespace.
for (top=name.begin( );top<name. end();top+ +)
{
if (*top!=' ')
{
temp+=*top;
}
}
name=temp;
cout << "Name set to: " << name << endl;
//now we find out what kind of variable val holds:
index=val.find( '"');
if (index==string: :npos)
{
//quote wasn't found, we've got ourselves an integer, boys and girls.
//we'll strip the spaces out:
temp="";
for (top=val.begin( );top<val. end();top+ +)
{
if (*top!=' ')
{
temp+=*top;
}
}
val=temp;
//we'll initialize the variable, and clean it up
variable.type= TYPE_INT;
variable.str= "";
variable.integer= atoi(val. c_str());
parsed[name. c_str()]= variable;
cout << "Adding integer. " << variable.integer << endl;
continue;
}
else
{
fq=val.find_ first_of( '"');
lq=val.find_ last_of(' "');
top=(val.begin( )+fq);
temp="";
tracker=(val. begin()+lq) ;
for (top=top;top< tracker;top+ +)
{
temp+=*top;
}
cout << "Setting string variable. " << val << endl;
variable.str= val;
variable.type= TYPE_STR;
variable.integer= 0;
parsed[name. c_str()]= variable;
}
}
return parsed;
}
//and now the var struct:
typedef enum {
TYPE_INT,
TYPE_STR
} TYPE_VAR; //the types of variables.

//this is the basic variable found in the configuration file.
typedef struct _Var
{
string str;
int integer;
TYPE_VAR type;
} Var;

Thanks,
Tyler Littlefield
email: [EMAIL PROTECTED] com
web: tysdomain-com
Visit for quality software and web design.
skype: st8amnd2005

 














      

Reply via email to