summaryrefslogtreecommitdiffstats
path: root/src/Utilities.cpp
diff options
context:
space:
mode:
authorWillem Jan Palenstijn <wjp@usecode.org>2016-01-18 17:47:53 +0100
committerWillem Jan Palenstijn <wjp@usecode.org>2016-01-18 17:47:53 +0100
commit530aa4f96e9d0fe40ccd0bb9ae1363792a49467c (patch)
tree41ea934e76ad38f221a38469c6add9152f3a0884 /src/Utilities.cpp
parent34bab2b0caa6ee955d5d2f7882a6cd36cf142536 (diff)
parentfc86917da1a175c04e9bd2e5f0bedb0a48a81c26 (diff)
downloadastra-530aa4f96e9d0fe40ccd0bb9ae1363792a49467c.tar.gz
astra-530aa4f96e9d0fe40ccd0bb9ae1363792a49467c.tar.bz2
astra-530aa4f96e9d0fe40ccd0bb9ae1363792a49467c.tar.xz
astra-530aa4f96e9d0fe40ccd0bb9ae1363792a49467c.zip
Merge pull request #105 from wjp/strings
Replace boost::lexical_cast by stringstreams
Diffstat (limited to 'src/Utilities.cpp')
-rw-r--r--src/Utilities.cpp156
1 files changed, 77 insertions, 79 deletions
diff --git a/src/Utilities.cpp b/src/Utilities.cpp
index 3f65e9a..4b80503 100644
--- a/src/Utilities.cpp
+++ b/src/Utilities.cpp
@@ -28,101 +28,99 @@ $Id$
#include "astra/Utilities.h"
-using namespace std;
-using namespace astra;
+#include <boost/algorithm/string.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/classification.hpp>
-//-----------------------------------------------------------------------------
-// Trim Whitespace Characters
-void StringUtil::trim(std::string& _sString, bool _bLeft, bool _bRight)
+#include <sstream>
+#include <locale>
+#include <iomanip>
+
+namespace astra {
+
+namespace StringUtil {
+
+int stringToInt(const std::string& s)
{
- // trim right
- if (_bRight)
- _sString.erase(_sString.find_last_not_of(" \t\r") + 1);
+ double i;
+ std::istringstream iss(s);
+ iss.imbue(std::locale::classic());
+ iss >> i;
+ if (iss.fail() || !iss.eof())
+ throw bad_cast();
+ return i;
- // trim left
- if (_bLeft)
- _sString.erase(0, _sString.find_first_not_of(" \t\r"));
}
-//-----------------------------------------------------------------------------
-// Split String
-vector<string> StringUtil::split(const string& _sString, const string& _sDelims)
+
+float stringToFloat(const std::string& s)
{
- std::vector<string> ret;
-
- size_t start, pos;
- start = 0;
- do {
- pos = _sString.find_first_of(_sDelims, start);
- if (pos == start) {
- // Do nothing
- start = pos + 1;
- } else if (pos == string::npos) {
- // Copy the rest of the string
- ret.push_back(_sString.substr(start));
- break;
- } else {
- // Copy up to newt delimiter
- ret.push_back(_sString.substr(start, pos - start));
- start = pos + 1;
- }
-
- // Parse up to next real data (in case there are two delims after each other)
- start = _sString.find_first_not_of(_sDelims, start);
- } while (pos != string::npos);
-
- return ret;
+ return (float)stringToDouble(s);
}
-//-----------------------------------------------------------------------------
-// Cast string to int
-bool StringUtil::toInt(const string& _sString, int& _iValue)
+
+double stringToDouble(const std::string& s)
{
- std::istringstream ss(_sString);
- ss >> _iValue;
- return !ss.fail();
+ double f;
+ std::istringstream iss(s);
+ iss.imbue(std::locale::classic());
+ iss >> f;
+ if (iss.fail() || !iss.eof())
+ throw bad_cast();
+ return f;
}
-//-----------------------------------------------------------------------------
-// Cast string to float
-bool StringUtil::toFloat32(const string& _sString, float32& _fValue)
+
+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)
{
- std::istringstream ss(_sString);
- ss >> _fValue;
- return !ss.fail();
+ return stringToVector<float>(s);
}
-//-----------------------------------------------------------------------------
-// Convert string to Lower Case
-void StringUtil::toLowerCase(std::string& _sString)
+
+std::vector<double> stringToDoubleVector(const std::string &s)
{
- std::transform(_sString.begin(),
- _sString.end(),
- _sString.begin(),
- ::tolower);
+ return stringToVector<double>(s);
}
-//-----------------------------------------------------------------------------
-// Convert string to Upper Case
-void StringUtil::toUpperCase(std::string& _sString)
+
+template<typename T>
+std::vector<T> stringToVector(const std::string& s)
{
- std::transform(_sString.begin(),
- _sString.end(),
- _sString.begin(),
- ::toupper);
-}
-//-----------------------------------------------------------------------------
+ // 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;
+}
-//-----------------------------------------------------------------------------
-// Get Extension
-string FileSystemUtil::getExtension(string& _sFilename)
+std::string floatToString(float f)
{
- string sExtension = "";
- for (int i = _sFilename.length() - 1; 0 < i; i--) {
- if (_sFilename[i] == '.') {
- std::transform(sExtension.begin(),sExtension.end(),sExtension.begin(),::tolower);
- return sExtension;
- }
- sExtension = _sFilename[i] + sExtension;
- }
- return "";
+ 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); }
+
+
+}
+
}
-//-----------------------------------------------------------------------------