1 #include <cmath>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <sstream>
5 #include <string>
6 
7 #include "exception.h"
8 #include "parse.h"
9 
10 using namespace JFK;
11 using std::string;
12 
13 string
argument(int index,const string & objstring)14 JFK::argument(int index, const string& objstring)
15 // Returns index-th argument of any object string
16 // arguments look like x=0.5343 etc...
17 {
18     int spacecount = 0;
19     int start = 0;
20     int fin;
21 
22     // Get start/final position of index-th substing
23     for (fin = 0; fin < objstring.length() && spacecount <= index; fin++)
24     {
25         if (objstring[fin] == ' ')
26         {
27             // Two spaces != two sep. spaces
28             if (fin!=0 && objstring[fin - 1] != ' ')
29                 spacecount++;
30 
31             if (spacecount <= index)
32                 start = fin + 1; // One after Space
33             else
34                 fin--;
35         }
36     }
37 
38     if (spacecount < index || start >= fin)
39         return ""; // Argument not available
40     else
41         return objstring.substr(start, fin - start);
42 }
43 
44 string
identifier(const string & arg)45 JFK::identifier(const string& arg)
46 {
47     // Identifier : substring before "=" or complete string if no "="
48     return arg.substr(0, arg.find("="));
49 }
50 
51 string
value(const string & arg)52 JFK::value(const string& arg)
53 {
54     // Value : substring after "=" or nothing if no "="
55     if(arg.find("=") >= arg.length())
56         return "";
57     else
58         return arg.substr(arg.find("=") + 1);
59 }
60 
61 double
strtodbl(const string & s)62 JFK::strtodbl(const string& s)
63 {
64     std::istringstream i(s);
65     double x;
66     if (i >> x)
67         return x;
68     else
69         throw JFK::exception(s + " not a value");
70 }
71 
72 int
strtoint(const string & s)73 JFK::strtoint(const string& s)
74 {
75     std::istringstream i(s);
76     int x;
77     if (i >> x)
78         return x;
79     else
80         throw JFK::exception(s + " not a value");
81 }
82