ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
util.cxx
Go to the documentation of this file.
1 //
2 // util.cpp
3 // Utility functions needed here and there.
4 //
5 // Author: Jens Wilberg, Lukas Aymans, Christoph Dalitz
6 // Date: 2019-04-02
7 // License: see ../LICENSE
8 //
9 
10 #include "util.h"
11 
12 #include <sstream>
13 #include <stdexcept>
14 #include <string>
15 
16 //-------------------------------------------------------------------
17 // converts *str* to double.
18 // If str is not a number a invalid_argument exception is thrown.
19 //-------------------------------------------------------------------
20 double stod(const char *s)
21 {
22  double result;
23 
24  // remove leading and trailing white space
25  std::string str(s);
26  str.erase(0, str.find_first_not_of("\t\r\n "));
27  str.erase(str.find_last_not_of("\t\r\n ") + 1);
28 
29  // use istream for conversion
30  std::istringstream iss(str);
31 
32  iss >> std::ws >> result;
33  if (iss.fail() || !iss.eof()) {
34  throw std::invalid_argument("not a number");
35  }
36  return result;
37 }
stod
double stod(const char *s)
Definition: util.cxx:20
util.h