72 lines
1.3 KiB
C++
72 lines
1.3 KiB
C++
#include "ScalarConverter.hpp"
|
|
#include "PrintValue.hpp"
|
|
|
|
ScalarConverter::ScalarConverter(void) {
|
|
}
|
|
|
|
ScalarConverter::ScalarConverter(const ScalarConverter &cpy) {
|
|
if (this != &cpy) {
|
|
*this = cpy;
|
|
}
|
|
}
|
|
|
|
ScalarConverter::~ScalarConverter(void) {
|
|
}
|
|
|
|
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) {
|
|
return *this;
|
|
}
|
|
|
|
bool _isNan(const std::string &str) {
|
|
if (str == "nan" || str == "nanf" || str == "-inf" || str == "-inff" ||
|
|
str == "+inf" || str == "+inff" || str == "inf" || str == "inff")
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
e_type _getType(const std::string &s)
|
|
{
|
|
if (_isNan(s))
|
|
return NAN;
|
|
|
|
if (s.length() == 1 && !isdigit(s[0]))
|
|
return CHAR;
|
|
|
|
char *err;
|
|
|
|
(void)strtol(s.c_str(), &err, 10);
|
|
if (*err == 0)
|
|
return INT;
|
|
(void)strtod(s.c_str(), &err);
|
|
if (*err == 0)
|
|
return DOUBLE;
|
|
if (*err == 'f' && *(err + 1) == 0)
|
|
return FLOAT;
|
|
return NONE;
|
|
|
|
}
|
|
|
|
void ScalarConverter::convert(const std::string &s)
|
|
{
|
|
switch (_getType(s))
|
|
{
|
|
case CHAR:
|
|
_printChar(s);
|
|
break;
|
|
case INT:
|
|
_printInt(s);
|
|
break;
|
|
case FLOAT:
|
|
_printFloat(s);
|
|
break;
|
|
case DOUBLE:
|
|
_printDouble(s);
|
|
break;
|
|
case NAN:
|
|
_printNan(s);
|
|
break;
|
|
default:
|
|
std::cerr << "'" << s << "' does not convert to any of the availables types" << std::endl;
|
|
}
|
|
}
|