/*
-----------------------------------------------------------------------
Copyright: 2010-2015, iMinds-Vision Lab, University of Antwerp
2014-2015, CWI, Amsterdam
Contact: astra@uantwerpen.be
Website: http://sf.net/projects/astra-toolbox
This file is part of the ASTRA Toolbox.
The ASTRA Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The ASTRA Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the ASTRA Toolbox. If not, see .
-----------------------------------------------------------------------
$Id$
*/
#include "astra/Utilities.h"
#include
#include
#include
#include
#include
#include
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 stringToFloatVector(const std::string &s)
{
return stringToVector(s);
}
std::vector stringToDoubleVector(const std::string &s)
{
return stringToVector(s);
}
template
std::vector stringToVector(const std::string& s)
{
// split
std::vector items;
boost::split(items, s, boost::is_any_of(",;"));
// init list
std::vector out;
out.resize(items.size());
// loop elements
for (unsigned int i = 0; i < items.size(); i++) {
out[i] = stringTo(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); }
}
}