Sign in to follow this  
ChJees

Code problems (Parser Making)

Recommended Posts

I am getting a 'Segmentation Fault' error from this piece of code, and i am frustrated from trying to solve it myself:
bool ParseCheck( string Text, string Text2 )
{
     //Check if special
     if ( Text == "Include" ){ ParseConfig( Text2 ); return 1; }
     return 0;
}

bool ParseConfig(string File)
{
    //Variables
    ifstream fConfigFile;
    string CurrentLine,fContent;
    //Open File
    fConfigFile.open( File.c_str() );
    
    //Check if open
    if( fConfigFile.is_open() )
    {
        //Add everything except comments to fContent
        while ( getline(fConfigFile,CurrentLine) )
        {
            //If '//' ignore.
            if ( ( CurrentLine.at(0) == '/' )  && ( CurrentLine.at(1) == '/' ) )
            {
                 //Ignore, pick next line
            }else{
                 //Add to global string
                 fContent.append(CurrentLine);
            }
        }
    
    }else{
        //Or else Fail!
        return 0;
    }
    fConfigFile.close(); //Close File
    
    //Start parsing and assigning variables (Also include other files.)
    istringstream ContentStream;
    ContentStream.str( "fgds = 213\n" );
    string prefix,suffix;
    int space_pos;
    bool special;
    
    //While loop...
    while ( getline(ContentStream,CurrentLine) )
    {
          //Step 1: Split '='
          StringTokenizer ConTok(CurrentLine,"=");
          prefix = ConTok.nextToken();
          suffix = ConTok.nextToken();
          
          //Step 2: Clean up spaces.
          
          //Prefix
          while ( space_pos = prefix.find(" ") )
          {
          prefix.erase( space_pos );
          }
          //Suffix
          space_pos = suffix.find_first_of(" ");
          if ( suffix.at( suffix.find_first_of(" ")-1 ) != ' ' ) { suffix.erase( space_pos ); }
          
          //Step 3: Examine if special, otherwise assign as variable.
          if ( ( special = ParseCheck( prefix, suffix ) ) == 0)
          {
               //Assign variable
               string tempt =  prefix + " = " + suffix;
               message = TTF_RenderText_Solid( font, tempt.c_str(), textColor ); //SDL Related
               //GlobalVariableHandler.add_variable(prefix,suffix);
          }
    }
}
Using this Tokenizer: http://www.partow.net/programming/stringtokenizer/index.html I code in C Plus Plus and uses Bloodshed Dev-C Plus Plus with it's default compiler. So could any experienced coders shed some light on what i have done wrong? (I comment pretty well by experience.) EDIT: I did not take in the class where everything is stored. (Avoid making the post more cluttered than it allready is and it is not relevant at the moment.)

Share this post


Link to post
Share on other sites
I haven't spotted the error in your code, but here are few tips that might help:

1. Pass strings by constant reference rather than by value (this will avoid some unnecessary overhead).

2. Use constructors where appropriate (for example, open your file object using its constructor rather than the 'open' function).

3. Let file objects close themselves when they go out of scope rather than closing them manually.

4. The Boost libraries include a collection of string algorithms that handle things like string trimming, simple parsing, and so on. The Boost libraries are quite reliable, so using them in place of your own code or other third-party libraries will (in most cases) reduce the potential for error.

5. The line 'while ( space_pos = prefix.find(" ") )' looks a little suspicious to me.

6. Declare and initialize variables at first use rather than declaring them at the top of the function.

7. C++ stream objects implicitly convert to bool; although in some cases such conversions can be a little dodgy, in this case I think it would be more idiomatic to write 'if (file)', rather than 'if (file.is_open())'.

8. Personally I don't like to use using directives even in .cpp files, but opinions may differ on this.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
when you run it through the debugger what specific line of code is causing the seg fault?

-me


No.

And interesting jyk, i will see what i can do.

Share this post


Link to post
Share on other sites
My guess would be the suffix space erasure. That could at least cause a seg-fault even if it's not in this instance. (iirc, it's been a little while since I've suffered through string manipulation in C++)

Share this post


Link to post
Share on other sites
Quote:
Original post by ChJees
Quote:
Original post by Palidine
when you run it through the debugger what specific line of code is causing the seg fault?

-me


No.

...


I do not understand your response here?

Share this post


Link to post
Share on other sites
I seem to have found out what the problem was... And it was NOT in that code!

When i deleted this:
message = TTF_RenderText_Solid( font, "Lowding...", textColor );
So did i no longer get a segmentation fault :S.

Here are related stuff to it:
font = TTF_OpenFont( "FreeSans.ttf", 28 );
SDL_Color textColor = { 0, 255, 255 };

Anyway, i fixed and will solve this SDL TTF stuff later. Thanks for all your help anyway :).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this