Hi Guys,
I am writing a file parser for one of my code projects.. I have attached the code for my project
My cpp fileCode: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
I tried calling this function in the main loop like what I am giving nowCode:
#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;
}
My text file 1.log is a simple test file which has jus this one lineCode:Parser myparse("1.log");
myparse.QueryKey("Gravity");
DevMsg("Testing ",myparse.get_ValueasmyType());
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

