Files
CPP_Module_06/ex00/ScalarConverter.cpp

90 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ScalarConverter.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 19:59:17 by adjoly #+# #+# */
/* Updated: 2025/04/09 10:31:41 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScalarConverter.hpp"
#include "GetType.hpp"
#include "PrintNb.hpp"
#include "StrTo.hpp"
void _log(std::string emoji, std::string what, std::string who,
std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "" << what << ": " << str << std::endl;
else
std::cout << "" << emoji << "" << what << "(" << who << "): " << str
<< std::endl;
#else
(void)emoji, (void)what, (void)who, (void)str;
#endif
}
ScalarConverter::ScalarConverter(void) {
_log("", "ScalarConverter", "", "default constructor called");
}
ScalarConverter::ScalarConverter(const ScalarConverter &cpy) {
_log("", "ScalarConverter", "", "copy constructor called");
if (this != &cpy) {
*this = cpy;
}
}
ScalarConverter::~ScalarConverter(void) {
_log("", "ScalarConverter", "", "destructor called");
}
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) {
return *this;
}
void _ConvertNan(const std::string &str) {
std::string nb;
if (str[0] == 'n')
nb = "nan";
else if (str[0] == '-')
nb = "-inf";
else
nb = "+inf";
std::cout << "char: impossible" << std::endl;
std::cout << "int: impossible" << std::endl;
std::cout << "float: " << nb << "f" << std::endl;
std::cout << "double: " << nb << std::endl;
}
void ScalarConverter::convert(const std::string &s) {
nbType type = GetType(s);
switch (type) {
case CHAR:
_Convert<char>(s);
break;
case INT:
_Convert<int>(s);
break;
case FLOAT:
_Convert<float>(s);
break;
case DOUBLE:
_Convert<double>(s);
break;
case NAN:
_ConvertNan(s);
break;
case NONE:
std::cout << s << " does not convert to any type" << std::endl;
break;
};
}