CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    20

    help with my file parser

    Hi Guys,
    I am writing a file parser for one of my code projects.. I have attached the code for my project
    Code:
    namespace Xlib {
    	/*!Templating the class */
    	template <class T>
    	class Parser;
    	
    	template <class T>
    	class FileParser {
     
    		public:
    			/*!	Default Constructor of the class	
    			/param filename the name of the file that needs to be parsed
    			*/
    			FileParser(char *filename);
    			/*!	Parsing the file
    			/param line the line which has been read
    			/param delimiters the delimiters to the file.
    				*/
    			void Parse(std::string& line,std::string& delimiters);
    			/*! Searches the value of the passed in query*/
    			void QueryKey(const char* key);
    			
    			/*! Get the key values
    			/brief can u used for newer data types athat are not recognized
    			*/
    			std::vector<std::string>& get_AsString(){return token;}
    			
    			/*! Searches the Text file and returns the Query vector*/
    			std::vector<std::string>& SearchandRetrieve(std::string& key){ QueryKey(key);	return get_AsString();}
    			
    			/* get values if u are searching for something specific..
    	
    			*/
    			
    			/*! Returns the second item of the 
    			/param type the index of the element u want to retrieve.. 
    			/brief say.. if u have Gravity : 9.8 0 2 it returns 9.8 if index is 1 0 if index is 2 and 2 if the index is 3
    			/brief Requires query key to have been called before without which it would throw up junk or crash
    			//TODO: 	Redo it so that we can automate the Query Key process*/
    			T get_ValueasmyType(int index=1,std::ios_base& (*f)(std::ios_base&)=std::dec);
                   
        
    			
    		private:
    			/*!	File reader	*/
    			std::fstream Parser;
    			
    			/*! The value of the parsed out variable */
    			T* out;
    					
    			/*! A vector of string to recieve the parsed value */
    			std::vector<std::string>  token;
    			
    		protected:
    			/*	Nothing Protected */
     
    	};//End of the class function; 
    	
    	//--------------------
    	// Convenient typedefs
    	//--------------------
    	/*!defines the type to be float*/
    	typedef Parser<float>	Parsef;
    	/*!defines the type to be int*/
    	typedef Parser<int>	Parsei;
    	/*!defines the type to be float*/
    	typedef Parser<double>	Parsed;
    	/*!defines the type to be float*/
    	typedef Parser<Xlib::vec3d>	Vector;
    
    };//End of the namespace
     
     
    
    #endif
    My cpp file
    Code:
    #include "Parser.h"
    #include <iostream>
    #include "UTIL.h"
    
    #include <cstring>
    //Include header Files here 
    //For reading in thetext
    
     
    using namespace std;
    using namespace Xlib;
    
    template<class T>
    FileParser<T>::FileParser(char *filename){
    	Parser.open(filename,ios::in);
    	if(!Parser.is_open())
    		{
    			Xlib::Error("Cant Open Parsing file");
    			exit(EXIT_FAILURE);
    		}
    }
    
    
    template<class T>
    void FileParser<T>::QueryKey(const char* key){
    	
    	string buffer;
    	string line;
    	int strlength= strlen(key);
    	token.clear();
    	//TODO::: 
    	while(!Parser.eof())
    	{
    		buffer.clear();
    		getline(Parser,line,'\n');
    		//can use getline with the line input and buffer output with delimiters
    		buffer= line.substr(0,5);
    		
    		if(buffer==key)
    		{
    			Parse(line,": ");				
    			return;
    		}
    		
    		Error("The Search Query was not found.. Quit application for now.");
    		DevMsg("This query is un available :", key);
    		DevMsg("Try setting all values to Default May be i can go");
    		exit(EXIT_FAILURE);
    	}
    	
    }
    
    
    template<class T>
    void FileParser<T>::Parse(string& line,string& delimiters){
    	unsigned int pos=line.find_first_not_of(delimiters,0);
    	unsigned int lastpos=line.find_first_of(delimiters,pos);
    	
    	while(string::npos!= pos|| string::npos != lastpos)
    		{
    		token.push_back(line.substr(pos,lastpos-pos));
    		pos=line.find_first_not_of(delimiters,lastpos);
    		lastpos=line.find_first_of(delimiters,pos);		
    		}	
    }
    
    
    
    
    
    template<class T>
    T FileParser<T>::get_ValueasmyType(int index,std::ios_base& (*f)(std::ios_base&)){
    	if(token.size()!=(index+1)){
    		Error("Reading in the correct number of arguments", Parser, 75);
    		return 0;
    	}
    	
    	T i;
    	if(quickstrConvert<T>(i,token[1],f)){
    		return i;
    	}
    	else 
    		Error("Error Found in conversion", Parser, 75);
    		return 0;
    }
    I tried calling this function in the main loop like what I am giving now
    Code:
    	Parser myparse("1.log");
    	myparse.QueryKey("Gravity");
    	DevMsg("Testing ",myparse.get_ValueasmyType());
    My text file 1.log is a simple test file which has jus this one line
    Gravity:9.8


    But This throws me errors like this
    main.cpp: In function 'int main(int, char**)':
    main.cpp:190: error: missing template arguments before 'myparse'
    main.cpp:190: error: expected `;' before 'myparse'
    main.cpp:191: error: 'myparse' was not declared in this scope

    Can someone help me with where I have gone wrong?? And Ya DevMsg is a simple overload for cout statement i have wrote..

    Is there somewhere I have gone wrong????

    Regards

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: help with my file parser

    Hello

    Please, write the smallest program that compiles (or the one that duplicates the error).
    The error messages look somewhat familar, and I'm going to take a wild guess here that
    it's probablly due to misplaced semi-colon, curly braces, incorrect header includes,
    or the issue with a scope resolution.

    For example, namespace doesn't end with a ;
    that's one error you can correct.
    std::ifstream's open() takes a const char*, so you might consider changing it.
    I think it's a good practice to put things where they belong.
    I'd consider putting all those typedefs in a seperate file,
    possibly enclosed in the same namespace, or where Parser is declared,
    so in this way you don't clutter FileParser which shows no relationship with Parser.
    But what's bothering me the most is how you're using using directives to define your template class...

    Edit:
    Code:
    template<class T>
    T FileParser<T>::get_ValueasmyType(int index,std::ios_base& (*f)(std::ios_base&)){
    	if(token.size()!=(index+1)){
    		Error("Reading in the correct number of arguments", Parser, 75);
    		return 0;
    	}
    	
    	T i;
    	if(quickstrConvert<T>(i,token[1],f)){
    		return i;
    	}
    	else 
    		Error("Error Found in conversion", Parser, 75);
    		return 0;
    }
    I think, you might want to go over the return types for the method above.
    Last edited by potatoCode; April 28th, 2009 at 10:57 PM.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: help with my file parser

    Quote Originally Posted by sundar0206 View Post
    Code:
    	Parser myparse("1.log");
    You declare Parser as a template class. You don't define Parser anywhere. You try to instantiate an object of that class without specifying the template argument?

    It looks like you have not quite understoodd the concept of templates, yet, so I recommend to go back and do some reading and/or work on a real small example with a template class first.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: help with my file parser

    Besides that, you also define the template in another cpp file.
    Thanks for your help.

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: help with my file parser

    yes, templates have to be implemented in the header ( or #included if you would like to still keep the implementation in a seperate file.)

    then you need to instantiate with something like,
    Code:
    Parser<int> my ParserObj;

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured