ScalarConverter.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ScalarConverter.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2026/01/03 13:23:10 by bchanot #+# #+# */
  9. /* Updated: 2026/01/05 13:45:35 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ScalarConverter.hpp"
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <limits>
  16. #include <string>
  17. #include <ctype.h>
  18. #include <cerrno>
  19. #include <cstdlib>
  20. #include <cmath>
  21. /*
  22. ** ------------------------------- CONSTRUCTOR --------------------------------
  23. */
  24. /*
  25. ** --------------------------------- OVERLOAD ---------------------------------
  26. */
  27. ScalarConverter & ScalarConverter::operator=( ScalarConverter const & rhs )
  28. {
  29. (void) rhs;
  30. //if ( this != &rhs )
  31. //{
  32. //this->_value = rhs.getValue();
  33. //}
  34. return *this;
  35. }
  36. std::ostream & operator<<( std::ostream & o, ScalarConverter const & i )
  37. {
  38. (void)i;
  39. //o << "Value = " << i.getValue();
  40. return o;
  41. }
  42. /*
  43. ** --------------------------------- METHODS ----------------------------------
  44. */
  45. void ScalarConverter::convert(std::string str) {
  46. char *endptr;
  47. bool isPossible = false;
  48. double converted = strtod(str.c_str(), &endptr);
  49. if (errno != ERANGE && (*endptr == '\0' || (*endptr == 'f' && *(endptr + 1) == '\0'))) {
  50. isPossible = true;
  51. } else {
  52. isPossible = false;
  53. }
  54. if (!isPossible && str.length() == 1) {
  55. converted = static_cast<char>(str[0]);
  56. isPossible = true;
  57. }
  58. ScalarConverter::_printChar(converted, isPossible);
  59. ScalarConverter::_printInt(converted, isPossible);
  60. ScalarConverter::_printFloat(converted, isPossible);
  61. ScalarConverter::_printDouble(converted, isPossible);
  62. }
  63. void ScalarConverter::_printChar(double const converted, bool isPossible) {
  64. if (!isPossible || std::isnan(converted) || converted > std::numeric_limits<char>::max() || converted < std::numeric_limits<char>::min())
  65. std::cout << "char : Impossible" << std::endl;
  66. else if (!isprint(static_cast<char>(converted)))
  67. std::cout << "char : Non displayable" << std::endl;
  68. else
  69. std::cout << "char : '" << static_cast<char>(converted) << "'" << std::endl;
  70. }
  71. void ScalarConverter::_printInt(double const converted, bool isPossible) {
  72. if (!isPossible || std::isnan(converted) || converted > std::numeric_limits<int>::max() || converted < std::numeric_limits<int>::min())
  73. std::cout << "int : Impossible" << std::endl;
  74. else
  75. std::cout << "int : " << static_cast<int>(converted) << std::endl;
  76. }
  77. void ScalarConverter::_printFloat(double const converted, bool isPossible) {
  78. if (!isPossible || converted > std::numeric_limits<float>::max() || converted < -std::numeric_limits<float>::max())
  79. std::cout << "float : Impossible" << std::endl;
  80. else if (std::isnan(converted))
  81. std::cout << "float : nanf" << std::endl;
  82. else
  83. std::cout << "float : " << std::fixed << std::setprecision(2) << static_cast<float>(converted) << "f" << std::endl;
  84. }
  85. void ScalarConverter::_printDouble(double const converted, bool isPossible) {
  86. if (!isPossible)
  87. std::cout << "double : Impossible" << std::endl;
  88. else
  89. std::cout << "double : " << std::fixed << std::setprecision(2) << static_cast<double>(converted) << std::endl;
  90. }
  91. /*
  92. ** --------------------------------- ACCESSOR ---------------------------------
  93. */
  94. /* ************************************************************************** */