]> git.tdb.fi Git - libs/core.git/commitdiff
Use lexical_cast instead of stringstream in GetOpt
authorMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 09:00:07 +0000 (12:00 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 09:12:54 +0000 (12:12 +0300)
source/core/getopt.h

index d45e888692ea9f96d2947180f19de75f6652eabb..bb1bd1568f6dff403843e6bd57492bc0832fb21d 100644 (file)
@@ -9,10 +9,10 @@ Distributed under the LGPL
 #define MSP_CORE_GETOPT_H_
 
 #include <list>
-#include <sstream>
 #include <stdexcept>
 #include <string>
 #include <vector>
+#include <msp/strings/lexicalcast.h>
 
 namespace Msp {
 
@@ -79,13 +79,14 @@ private:
 
                virtual void store(const std::string &a)
                {
-                       T tmp;
-                       std::istringstream ss(a);
-                       ss>>tmp;
-                       if(ss.fail())
-                               throw usage_error("Invalid argument for --"+lng);
-
-                       data = tmp;
+                       try
+                       {
+                               data = lexical_cast<T>(a);
+                       }
+                       catch(const lexical_error &e)
+                       {
+                               throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+                       }
                }
        private:
                T &data;
@@ -102,13 +103,14 @@ private:
 
                virtual void store(const std::string &a)
                {
-                       typename T::value_type tmp;
-                       std::istringstream ss(a);
-                       ss>>tmp;
-                       if(ss.fail())
-                               throw usage_error("Invalid argument for --"+lng);
-
-                       data.push_back(tmp);
+                       try
+                       {
+                               data.push_back(lexical_cast<typename T::value_type>(a));
+                       }
+                       catch(const lexical_error &e)
+                       {
+                               throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+                       }
                }
        private:
                T &data;