#include #include #include #include #include #include "exception.h" #include "parse.h" using namespace JFK; using std::string; string JFK::argument(int index, const string& objstring) // Returns index-th argument of any object string // arguments look like x=0.5343 etc... { int spacecount = 0; int start = 0; int fin; // Get start/final position of index-th substing for (fin = 0; fin < objstring.length() && spacecount <= index; fin++) { if (objstring[fin] == ' ') { // Two spaces != two sep. spaces if (fin!=0 && objstring[fin - 1] != ' ') spacecount++; if (spacecount <= index) start = fin + 1; // One after Space else fin--; } } if (spacecount < index || start >= fin) return ""; // Argument not available else return objstring.substr(start, fin - start); } string JFK::identifier(const string& arg) { // Identifier : substring before "=" or complete string if no "=" return arg.substr(0, arg.find("=")); } string JFK::value(const string& arg) { // Value : substring after "=" or nothing if no "=" if(arg.find("=") >= arg.length()) return ""; else return arg.substr(arg.find("=") + 1); } double JFK::strtodbl(const string& s) { std::istringstream i(s); double x; if (i >> x) return x; else throw JFK::exception(s + " not a value"); } int JFK::strtoint(const string& s) { std::istringstream i(s); int x; if (i >> x) return x; else throw JFK::exception(s + " not a value"); }