Problem with a file parser.

This is a discussion on Problem with a file parser. within the C++ Programming forums, part of the General Programming Boards category; I have a parser class that I use to parse setting files, now the parser works great except when I ...

  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 06:42 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    It would help to actually see the source file associated with the header file you describe above. Could you maybe use an STL map instead? Why is everything static? That may be the problem. Only one instance of all the class's member variable exist if they are declared as static regardless of how many classes you instantiate. When your second class is created and destroyed it could be interferring with or resetting the data members cooresponding to the first class.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by hk_mp5kpdw
    Why is everything static? That may be the problem. Only one instance of all the class's member variable exist if they are declared as static regardless of how many classes you instantiate. When your second class is created and destroyed it could be interferring with or resetting the data members cooresponding to the first class.
    I think thats the problem. Lets see if I can fix that.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    I'm having a little problem, when I remove the static keyword from spairs and parse_data the compiler complains that I cannot use for example
    Code:
    spairs[i].property = (char*) malloc(MAX_BLOCK_PROPLENGTH*sizeof(char));
    The compiler says:
    error C2109: subscript requires array or pointer type
    How can I fix that?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Try to post ALL of your code, just giving us the header file with the class description isn't going to cut it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by hk_mp5kpdw
    Try to post ALL of your code, just giving us the header file with the class description isn't going to cut it.
    I can't post the whole code, the code doesn't belong to me. Besides, what you said in the first post was right, the problem is in the following two lines
    Code:
    static spair_t * spairs;
    static parse_file_t * parse_data;
    So I just need to know how not to make it static without getting the C2109 error.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Well, static member functions are designed in part to operate without an object of the class being instantiated. They can only access/modify static data members (or variables local to the static function itself) since those static data members are always in existence without an object being specifically instantiated. Changing a data member from static to non-static without touching the rest of the code means that a static function could now be attempting to access a non-static data member which doesn't exist and the compiler will complain. Any function that touches a non-static data member must be non-static itself.

    You could be talking about significant code changes. Without seeing the full code and being able to break it apart and see why the class was designed the way it is, it is going to be difficult for me to give you much in the way of advice. The only thing you may be able to do is only ever parse one file at a time in sequence, no two files being parsed at the same time. So, instead of changing the class, you may need to change the flow of the rest of your program to suit the capabilities/limitations of the class.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by hk_mp5kpdw
    Any function that touches a non-static data member must be non-static itself.
    That's what I needed to know! I didn't know that and with that I was able to fix the problem, I just made everything non static (I don't know why it was static in the first place) and it works great.

    Quote Originally Posted by hk_mp5kpdw
    You could be talking about significant code changes.
    I just needed to remove the static keyword from all the functions and variables, and it works great.

    Thanks for helping me!

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21