summaryrefslogtreecommitdiffstats
path: root/src/Utilities.cpp
diff options
context:
space:
mode:
authorWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2015-12-17 14:37:41 +0100
committerWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2016-01-05 11:15:17 +0100
commitfc86917da1a175c04e9bd2e5f0bedb0a48a81c26 (patch)
tree6996a3e2e2cfd110a566d4093e22ca746a52038d /src/Utilities.cpp
parent73fa0a8df8203288aca032c71caa5ff47d35a3e2 (diff)
downloadastra-fc86917da1a175c04e9bd2e5f0bedb0a48a81c26.tar.gz
astra-fc86917da1a175c04e9bd2e5f0bedb0a48a81c26.tar.bz2
astra-fc86917da1a175c04e9bd2e5f0bedb0a48a81c26.tar.xz
astra-fc86917da1a175c04e9bd2e5f0bedb0a48a81c26.zip
Replace boost::lexical_cast by stringstreams
This is to avoid the dependence of lexical_cast on the current locale. The stringstreams used for the new string parsing/output functions are explicitly imbued with the C/classic locale.
Diffstat (limited to 'src/Utilities.cpp')
-rw-r--r--src/Utilities.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/Utilities.cpp b/src/Utilities.cpp
index cb54e93..4b80503 100644
--- a/src/Utilities.cpp
+++ b/src/Utilities.cpp
@@ -28,4 +28,99 @@ $Id$
#include "astra/Utilities.h"
+#include <boost/algorithm/string.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/classification.hpp>
+#include <sstream>
+#include <locale>
+#include <iomanip>
+
+namespace astra {
+
+namespace StringUtil {
+
+int stringToInt(const std::string& s)
+{
+ double i;
+ std::istringstream iss(s);
+ iss.imbue(std::locale::classic());
+ iss >> i;
+ if (iss.fail() || !iss.eof())
+ throw bad_cast();
+ return i;
+
+}
+
+float stringToFloat(const std::string& s)
+{
+ return (float)stringToDouble(s);
+}
+
+double stringToDouble(const std::string& s)
+{
+ double f;
+ std::istringstream iss(s);
+ iss.imbue(std::locale::classic());
+ iss >> f;
+ if (iss.fail() || !iss.eof())
+ throw bad_cast();
+ return f;
+}
+
+template<> float stringTo(const std::string& s) { return stringToFloat(s); }
+template<> double stringTo(const std::string& s) { return stringToDouble(s); }
+
+std::vector<float> stringToFloatVector(const std::string &s)
+{
+ return stringToVector<float>(s);
+}
+
+std::vector<double> stringToDoubleVector(const std::string &s)
+{
+ return stringToVector<double>(s);
+}
+
+template<typename T>
+std::vector<T> stringToVector(const std::string& s)
+{
+ // split
+ std::vector<std::string> items;
+ boost::split(items, s, boost::is_any_of(",;"));
+
+ // init list
+ std::vector<T> out;
+ out.resize(items.size());
+
+ // loop elements
+ for (unsigned int i = 0; i < items.size(); i++) {
+ out[i] = stringTo<T>(items[i]);
+ }
+ return out;
+}
+
+
+std::string floatToString(float f)
+{
+ std::ostringstream s;
+ s.imbue(std::locale::classic());
+ s << std::setprecision(9) << f;
+ return s.str();
+}
+
+std::string doubleToString(double f)
+{
+ std::ostringstream s;
+ s.imbue(std::locale::classic());
+ s << std::setprecision(17) << f;
+ return s.str();
+}
+
+
+template<> std::string toString(float f) { return floatToString(f); }
+template<> std::string toString(double f) { return doubleToString(f); }
+
+
+}
+
+}