SpinParser  1.0
InputParser.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include <iostream>
11 #include <sstream>
12 #include "boost/regex.hpp"
13 #include "lib/Log.hpp"
14 
15 namespace InputParser
16 {
24  inline double stringToDouble(const std::string &input)
25  {
26  //debug output
27  boost::regex number("[-\\.\\d]+");
28  bool debugFlag = !boost::regex_match(input, number);
29  std::string parsedString(input);
30 
31  boost::regex squareRoot("sqrt\\(([\\.\\d]+)\\)");
32  boost::regex multDiv("([\\.\\d]+)([\\*/])([\\.\\d]+)");
33  boost::smatch match;
34  std::stringstream replacement;
35  replacement << std::fixed << std::setprecision(20);
36  //parse sqrt
37  while (boost::regex_search(parsedString, match, squareRoot))
38  {
39  replacement.str("");
40  replacement << match.prefix() << sqrt(std::stod(match.str(1))) << match.suffix();
41  parsedString = replacement.str();
42  }
43  //parse multiplication and division
44  while (boost::regex_search(parsedString, match, multDiv))
45  {
46  replacement.str("");
47  replacement << match.prefix();
48  if (match.str(2) == "*") replacement << std::stod(match.str(1)) * std::stod(match.str(3));
49  else replacement << std::stod(match.str(1)) / std::stod(match.str(3));
50  replacement << match.suffix();
51  parsedString = replacement.str();
52  }
53 
54  if (debugFlag) Log::log << Log::LogLevel::Debug << "parsed input string " << input << " to " << parsedString << Log::endl;
55  return std::stod(parsedString);
56  }
57 
65  inline float stringToFloat(const std::string &input)
66  {
67  return float(stringToDouble(input));
68  }
69 }
InputParser::stringToFloat
float stringToFloat(const std::string &input)
Parse input string to double. Resolves simple multiplications, divisions and sqrt() expressions....
Definition: InputParser.hpp:65
Log.hpp
Lightweight logging interface with output filtering.
InputParser::stringToDouble
double stringToDouble(const std::string &input)
Parse input string to double. Resolves simple multiplications, divisions and sqrt() expressions....
Definition: InputParser.hpp:24