Browse Source

[3015] Rewrote to use boost::lexical cast to convert string to values

Kazunori Fujiwara 11 years ago
parent
commit
0202fc5aea
2 changed files with 14 additions and 28 deletions
  1. 10 26
      src/lib/cc/data.cc
  2. 4 2
      src/lib/cc/data.h

+ 10 - 26
src/lib/cc/data.cc

@@ -28,6 +28,7 @@
 #include <climits>
 
 #include <boost/algorithm/string.hpp> // for iequals
+#include <boost/lexical_cast.hpp>
 
 #include <cmath>
 
@@ -90,7 +91,7 @@ Element::getValue(std::map<std::string, ConstElementPtr>&) const {
 }
 
 bool
-Element::setValue(const int64_t) {
+Element::setValue(const long long int) {
     return (false);
 }
 
@@ -399,38 +400,21 @@ numberFromStringstream(std::istream& in, int& pos) {
 //
 ElementPtr
 fromStringstreamNumber(std::istream& in, int& pos) {
-    long long int i;
-    double d = 0.0;
-    bool is_double = false;
-    char* endptr;
-
     std::string number = numberFromStringstream(in, pos);
 
-    errno = 0;
-    i = strtoll(number.c_str(), &endptr, 10);
-    if (*endptr != '\0') {
-        const char* ptr;
-        errno = 0;
-        d = strtod(ptr = number.c_str(), &endptr);
-        is_double = true;
-        if (*endptr != '\0' || ptr == endptr) {
-            isc_throw(JSONError, std::string("Bad number: ") + number);
-        } else {
-            if (errno != 0) {
-                isc_throw(JSONError, std::string("Number overflow: ") + number);
-            }
+    if (number.find_first_of(".eE") < number.size()) {
+        try {
+            return (Element::create(boost::lexical_cast<double>(number)));
+        } catch (const boost::bad_lexical_cast&) {
+            isc_throw(JSONError, std::string("Number overflow: ") + number);
         }
     } else {
-        if ((i == LLONG_MAX || i == LLONG_MIN) && errno != 0) {
+        try {
+            return (Element::create(boost::lexical_cast<int64_t>(number)));
+        } catch (const boost::bad_lexical_cast&) {
             isc_throw(JSONError, std::string("Number overflow: ") + number);
         }
     }
-
-    if (is_double) {
-        return (Element::create(d));
-    } else {
-        return (Element::create(i));
-    }
 }
 
 ElementPtr

+ 4 - 2
src/lib/cc/data.h

@@ -167,7 +167,9 @@ public:
     /// is of the correct type
     ///
     //@{
-    virtual bool setValue(const int64_t v);
+    virtual bool setValue(const long long int v);
+    bool setValue(const long int i) { return (setValue(static_cast<long long int>(i))); };
+    bool setValue(const int i) { return (setValue(static_cast<long long int>(i))); };
     virtual bool setValue(const double v);
     virtual bool setValue(const bool t);
     virtual bool setValue(const std::string& v);
@@ -381,7 +383,7 @@ public:
     using Element::getValue;
     bool getValue(int64_t& t) const { t = i; return (true); }
     using Element::setValue;
-    bool setValue(int64_t v) { i = v; return (true); }
+    bool setValue(long long int v) { i = v; return (true); }
     void toJSON(std::ostream& ss) const;
     bool equals(const Element& other) const;
 };