So, I have been having a lot of problems with this program for the past few days. Made a lot of progress tonight and I think I am almost done, but need some help with the vector. Basically the program has a bunch of params (things like GoalWidth, and MaxShootingForce) all set to default values of 0.0. Now, I have to read an .ini file and store the contents to a vector<string> lines. Next, I need to change that vector into a vector of KV (key/value pairs) by splitting lines at the first blank space and storing the left hand side as the key and the right hand side as the value (this is done in util.cpp). Then, back in params.cpp, I need to create a new vector<string> KV and then call Util::toKV to populate KV with lines. This is my problem. The goal is for the output to display a "before and after". Before will list everything set to zero, and the after will display each member variable with the correct value from the .ini file. Right now the code is set up in main to do this, its just a matter of getting the vector to work I think.
Below you will find some of the code. In "util.cpp" I am not 100% sure if my Util::toKV and Util::findValue are correct or not. I think they are, but I can't run the code to check them. Same thing goes with getDoubleValue, getIntValue, getBoolValue. Again, I think they are right, but not sure. The biggest problem is with "params.cpp" and applyFromFile. I have created the empty vector<string> KV, but its calling the Util::toKV that is causing me problems. Any help with this would be great! Only have a few more hours to get everything working right so I am going to keep messing around with it to see if I can get it to work right. I am hoping it is just something simply I am missing. Been staring at this code for hours so it would not surprise me.
util.cpp:
params.cpp:
Below you will find some of the code. In "util.cpp" I am not 100% sure if my Util::toKV and Util::findValue are correct or not. I think they are, but I can't run the code to check them. Same thing goes with getDoubleValue, getIntValue, getBoolValue. Again, I think they are right, but not sure. The biggest problem is with "params.cpp" and applyFromFile. I have created the empty vector<string> KV, but its calling the Util::toKV that is causing me problems. Any help with this would be great! Only have a few more hours to get everything working right so I am going to keep messing around with it to see if I can get it to work right. I am hoping it is just something simply I am missing. Been staring at this code for hours so it would not surprise me.
util.cpp:
bool Util::toKV( const vector<string> &lines, vector<KV> &kv, string &errMsg ) { for(int i = 0, n = lines.size(); i < n; i ++) { int index = Util::indexOf(lines[i], '""'); if(index == -1) { errMsg = "No space found in: [" + lines[i] + "]"; return(false); } string mKey = Util::trim(lines[i].substr(0, index)); string mValue = Util::trim(lines[i].substr(index + 1)); KV theKV(mKey, mValue); kv.push_back(theKV); } return(true); } bool Util::findValue( const string &key, const vector<KV> &kv, string &value, bool ignoreCase ) { string theKey = key; if(ignoreCase) theKey = toLowerCase(theKey); for(int i = 0, n = kv.size(); i < n; i ++) { string kvKey = kv[i].mKey; if(ignoreCase) kvKey = toLowerCase(kvKey); if(theKey == kvKey) { value = kv[i].mValue; return(true); } } return(false); } bool Util::getIntValue(const string &s, int *valuePtr) { char *endPtr = 0; unsigned long result = strtoul(s.c_str(), &endPtr, 10); if(*endPtr != '\0') return(false); *valuePtr = (int)result; return(true); } bool Util::getDoubleValue(const string &s, double *valuePtr) { char *endPtr = 0; int result = strtod(s.c_str(), &endPtr); if(*endPtr != '\0') return(false); *valuePtr = (double)result; return(true); } bool Util::getBoolValue(const string &value, bool *valuePtr) { char *endPtr = 0; unsigned long result = strtoul(value.c_str(), &endPtr, 10); if(*endPtr != '\0') return(false); *valuePtr = (bool)result; return(true); }
params.cpp:
bool Params::applyFromFile(const string &fileName, string &errMsg) { vector<string> lines; if(!Util::getLines("params.ini", lines, errMsg)) { cout << errMsg << endl; return(1); } lines = Util::trimLines(lines); lines = Util::removeBlanks(lines); lines = Util::removeComments(lines); vector<string> KV; KV = Util::toKV(lines, kv, errMsg); //for(int i = 0, n = lines.size(); i < n; i ++) //KV.push_back(lines[i]); string theValue; if(!Util::findValue("GoalWidth", kv, theValue, errMsg)) return(false); if(!Util::getDoubleValue(theValue, &GoalWidth)) return(false); return(true); }