Thread: Problem with a file parser.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Problem with a file parser.

    I have a parser class that I use to parse setting files, now the parser works great except when I have have two parser running at the same time. The code of the parser.h looks like this (btw, I wrote part of the parser, not the whole thing):
    Code:
    #ifndef __PARSER_H__
    #define __PARSER_H__
    
    #define MAXTOKEN				1024
    #define MAX_BLOCK_PROPLENGTH	64 /*!< Maximum length of a property. */
    #define MAX_BLOCK_VALLENGTH		256 /*!< Maximum length of a value. */
    
    #include "../Math/Math.h"
    
    /**
    * property-value pair.
    */
    typedef struct
    {
    	char *property;
    	char *val;
    } spair_t;
    
    /**
    * Group of spairs.
    */
    typedef struct
    {
    	int firstpair; /**< First spair. */  
    	int numpairs; /**< Number of spairs. */ 
    } parse_file_t;
    
    /**
    * Parser class.
    * The parser class provides methods to parse files. This is specially used
    * for config parsing.
    */
    
    class Parser
    {
    public:
    	~Parser(); /**< Destructor. */
    
    	static int	parseFile(const char *name);
    	static void StartParseBuffer(const unsigned char *buffer, const int size);
    	static void StartParseString(const char *string);
    
    	static bool GetToken(bool crossline);
    	static int GetOffset(void);
    	static void GoTo(const int offset);
    
    	static char token[MAXTOKEN];				/**< buffer for tokens */
    	static int scriptline;
    	static int num_blocks;
    
    	static int num_spairs; /**< Number of spairs. */
    
    	static spair_t * spairs;
    	static parse_file_t * parse_data;
    
    	//! Gets the value corresponding to a property in an spair.
    	/*!
    	\param spair The spair number.
    	\param property The property of spair value.
    	\return A pointer to the spair value string.
    	*/
    	static char* spair_value(int spair, char *property);
    	static char* spair_value_ex(int spair, const char *property);
    
    	//! Gets the value corresponding to a property in an spair.
    	/*!
    	\param spair The spair number.
    	\param property The property of spair value as a float value.
    	\return The requested float value.
    	*/
    	static float spair_float(int spair, char *property);
    
    	//! Gets the value corresponding to a property in an spair.
    	/*!
    	\param spair The spair number.
    	\param property The property of spair value as a int value.
    	\return The requested int value.
    	*/
    	static int spair_int(int spair, char *property);
    
    	//! Gets the value corresponding to a property in an spair.
    	/*!
    	\param spair The spair number.
    	\param property The property of spair value as a int value.
    	\return The requested boolean value.
    	*/
    	static bool	spair_bool(int spair, char *property);
    
    	//! Gets the value corresponding to a property in an spair.
    	/*!
    	\param spair The spair number.
    	\param property The property of spair value as a int value.
    	\return The requested VECTOR3D value.
    	*/
    	static vec3 spair_vec3(int spair, char *property);
    
    	//! Gets the value corresponding to a property in an spair.
    	/*!
    	\param spair The spair number.
    	\param property The property of spair value as a int value.
    	\return The requested COLOR value.
    	*/
    	static vec4 spair_vec4(int spair, char *property);
    
    protected:
    
    	static const unsigned char *buffer;
    	static const unsigned char *buf_start;
    	static const unsigned char *buf_end;
    };
    
    #endif	/* __PARSER_H__ */
    And to create a parser I use
    Code:
    Parser *conf_parser = new Parser;
    conf_parser->parseFile(filename);
    I can get the proper values with no problems at all in conf_parser. But if while I have conf_parser I create another Parser that new parser will parse the right values then I delete that new parser and go back to keep parsing with the first parser and all the values in spairs and parse_data are gone so my query functions always fail to give me the right values. What can I do ?
    Last edited by Hulag; 03-17-2005 at 07:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM