#include #include using namespace std; //define the parameter data types #define RTS_ERROR 0 #define RTS_OK 1 #define RTS_STRING 2 #define RTS_DOUBLE 3 struct parameter { //name of the specified parameter char* name; int ID; char* char_value; double double_value; int type; bool set; }; class rtsParser { private: vector parameters; vector::iterator p_SearchParams(char* name); bool p_SetParameter(char* name, char* value); bool p_SetParameter(char* name, double value); public: void AddParameter(char* name, int ID, int type); void AddParameter(char* name, int type); void OutputParameters(); void Load(const char* filename); int GetDoubleParameter(char* name, double& result); int GetStringParameter(char* name, char*& result); }; vector::iterator rtsParser::p_SearchParams(char *name) { //search the parameters vector for a name matching the input parameter vector::iterator iter; //run through the vector for(iter = parameters.begin(); iter != parameters.end(); iter++) { //if the name matches the parameter name, return the iterator if(strcmp((*iter).name, name) == 0) return iter; } return parameters.end(); } bool rtsParser::p_SetParameter(char *name, char *value) { //sets the character value of a parameter //first, find the parameter vector::iterator iter; for(iter = parameters.begin(); iter != parameters.end(); iter++) { //if the parameter is found if(strcmp((*iter).name, name) == 0) { //if the parameter type is wrong, return false if((*iter).type != RTS_STRING) return false; //otherwise, set the parameter value (*iter).char_value = value; (*iter).set = true; return true; } } //if the parameter was not found, return false return false; } bool rtsParser::p_SetParameter(char *name, double value) { //sets the character value of a parameter //first, find the parameter vector::iterator iter; for(iter = parameters.begin(); iter != parameters.end(); iter++) { //if the parameter is found if(strcmp((*iter).name, name) == 0) { //if the parameter type is wrong, return false if((*iter).type != RTS_DOUBLE) return false; //otherwise, set the parameter value (*iter).double_value = value; (*iter).set = true; return true; } } //if the parameter was not found, return false return false; } void rtsParser::Load(const char* filename) { //loads a parameter file and fills the list with the parameter values //create an input stream and load the file ifstream infile; infile.open(filename); //create temporary variables to store values char param_name[1000]; vector::iterator iter; //for each line in the parameter file while(!infile.eof()) { //get the parameter name token infile.get(param_name, 100, ' '); //find the proper parameter in the parameter list iter = p_SearchParams(param_name); //test to make sure the parameter was found if(iter == parameters.end()) { cout<<"Error, could not find parameter: "<>equals; //cout<<"infile>>equals: "<::iterator iter = p_SearchParams(name); //check for errors if(iter == parameters.end()) { cout<<"Error finding parameter: "<::iterator iter = p_SearchParams(name); //check for errors if(iter == parameters.end()) { cout<<"Error finding parameter: "<::iterator iter; for(iter = parameters.begin(); iter!= parameters.end(); iter++) { //test to see if the parameter was set by the file if((*iter).set) { cout<<"name: "<<(*iter).name; if((*iter).type == RTS_DOUBLE) cout<<" value: "<<(*iter).double_value<