/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ScalarConverter.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bchanot +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/03 13:23:10 by bchanot #+# #+# */ /* Updated: 2026/01/05 13:45:35 by bchanot ### ########.fr */ /* */ /* ************************************************************************** */ #include "ScalarConverter.hpp" #include #include #include #include #include #include #include #include /* ** ------------------------------- CONSTRUCTOR -------------------------------- */ /* ** --------------------------------- OVERLOAD --------------------------------- */ ScalarConverter & ScalarConverter::operator=( ScalarConverter const & rhs ) { (void) rhs; //if ( this != &rhs ) //{ //this->_value = rhs.getValue(); //} return *this; } std::ostream & operator<<( std::ostream & o, ScalarConverter const & i ) { (void)i; //o << "Value = " << i.getValue(); return o; } /* ** --------------------------------- METHODS ---------------------------------- */ void ScalarConverter::convert(std::string str) { char *endptr; bool isPossible = false; double converted = strtod(str.c_str(), &endptr); if (errno != ERANGE && (*endptr == '\0' || (*endptr == 'f' && *(endptr + 1) == '\0'))) { isPossible = true; } else { isPossible = false; } if (!isPossible && str.length() == 1) { converted = static_cast(str[0]); isPossible = true; } ScalarConverter::_printChar(converted, isPossible); ScalarConverter::_printInt(converted, isPossible); ScalarConverter::_printFloat(converted, isPossible); ScalarConverter::_printDouble(converted, isPossible); } void ScalarConverter::_printChar(double const converted, bool isPossible) { if (!isPossible || std::isnan(converted) || converted > std::numeric_limits::max() || converted < std::numeric_limits::min()) std::cout << "char : Impossible" << std::endl; else if (!isprint(static_cast(converted))) std::cout << "char : Non displayable" << std::endl; else std::cout << "char : '" << static_cast(converted) << "'" << std::endl; } void ScalarConverter::_printInt(double const converted, bool isPossible) { if (!isPossible || std::isnan(converted) || converted > std::numeric_limits::max() || converted < std::numeric_limits::min()) std::cout << "int : Impossible" << std::endl; else std::cout << "int : " << static_cast(converted) << std::endl; } void ScalarConverter::_printFloat(double const converted, bool isPossible) { if (!isPossible || converted > std::numeric_limits::max() || converted < -std::numeric_limits::max()) std::cout << "float : Impossible" << std::endl; else if (std::isnan(converted)) std::cout << "float : nanf" << std::endl; else std::cout << "float : " << std::fixed << std::setprecision(2) << static_cast(converted) << "f" << std::endl; } void ScalarConverter::_printDouble(double const converted, bool isPossible) { if (!isPossible) std::cout << "double : Impossible" << std::endl; else std::cout << "double : " << std::fixed << std::setprecision(2) << static_cast(converted) << std::endl; } /* ** --------------------------------- ACCESSOR --------------------------------- */ /* ************************************************************************** */