| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ScalarConverter.cpp :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2026/01/03 13:23:10 by bchanot #+# #+# */
- /* Updated: 2026/01/05 13:45:35 by bchanot ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ScalarConverter.hpp"
- #include <iostream>
- #include <iomanip>
- #include <limits>
- #include <string>
- #include <ctype.h>
- #include <cerrno>
- #include <cstdlib>
- #include <cmath>
- /*
- ** ------------------------------- 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<char>(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<char>::max() || converted < std::numeric_limits<char>::min())
- std::cout << "char : Impossible" << std::endl;
- else if (!isprint(static_cast<char>(converted)))
- std::cout << "char : Non displayable" << std::endl;
- else
- std::cout << "char : '" << static_cast<char>(converted) << "'" << std::endl;
- }
- void ScalarConverter::_printInt(double const converted, bool isPossible) {
- if (!isPossible || std::isnan(converted) || converted > std::numeric_limits<int>::max() || converted < std::numeric_limits<int>::min())
- std::cout << "int : Impossible" << std::endl;
- else
- std::cout << "int : " << static_cast<int>(converted) << std::endl;
- }
- void ScalarConverter::_printFloat(double const converted, bool isPossible) {
- if (!isPossible || converted > std::numeric_limits<float>::max() || converted < -std::numeric_limits<float>::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<float>(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<double>(converted) << std::endl;
- }
- /*
- ** --------------------------------- ACCESSOR ---------------------------------
- */
- /* ************************************************************************** */
|