202 lines
5.9 KiB
C++
202 lines
5.9 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ScalarConverter.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/02/28 20:03:23 by aortigos #+# #+# */
|
|
/* Updated: 2026/02/28 20:03:23 by aortigos ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "ScalarConverter.hpp"
|
|
|
|
//////////////////
|
|
// Constructors //
|
|
//////////////////
|
|
|
|
ScalarConverter::ScalarConverter()
|
|
{
|
|
// std::cout << "ScalarConverter default constructor called" << std::endl;
|
|
}
|
|
|
|
ScalarConverter::ScalarConverter(const ScalarConverter &other)
|
|
{
|
|
*this = other;
|
|
// std::cout << "ScalarConverter copy constructor called" << std::endl;
|
|
}
|
|
|
|
ScalarConverter& ScalarConverter::operator=(const ScalarConverter &other)
|
|
{
|
|
if (this != &other)
|
|
{
|
|
// Copy attributes here
|
|
}
|
|
// std::cout << "ScalarConverter copy assignment operator called" << std::endl;
|
|
return (*this);
|
|
}
|
|
|
|
ScalarConverter::~ScalarConverter()
|
|
{
|
|
// std::cout << "ScalarConverter destructor called" << std::endl;
|
|
}
|
|
|
|
// Main function
|
|
void ScalarConverter::convert(const std::string &literal)
|
|
{
|
|
if (isSpecialCase(literal))
|
|
convertFromSpecialCase(literal);
|
|
else if (isChar(literal))
|
|
convertFromChar(literal);
|
|
else if(isFloat(literal))
|
|
convertFromFloat(literal);
|
|
else if(isDouble(literal))
|
|
convertFromDouble(literal);
|
|
else if (isInt(literal))
|
|
convertFromInt(literal);
|
|
else
|
|
std::cout << "Error: invalid literal" << std::endl;
|
|
}
|
|
|
|
// Checkers
|
|
|
|
bool ScalarConverter::isSpecialCase(const std::string &literal)
|
|
{
|
|
if (literal == "-inf" || literal == "+inf" || literal == "nan"
|
|
|| literal == "-inff" || literal == "+inff" || literal == "nanf")
|
|
return (true);
|
|
return (false);
|
|
}
|
|
|
|
bool ScalarConverter::isChar(const std::string &literal)
|
|
{
|
|
char c;
|
|
|
|
if (literal.length() != 3)
|
|
return (false);
|
|
c = literal[1];
|
|
if (literal[0] == '\'' && literal[2] == '\''
|
|
&& (c >= 32 && c < 127))
|
|
{
|
|
return (true);
|
|
}
|
|
return (false);
|
|
}
|
|
|
|
bool ScalarConverter::isInt(const std::string &literal)
|
|
{
|
|
char *end;
|
|
long nb;
|
|
|
|
nb = strtol(literal.c_str(), &end, 10);
|
|
if (*end != '\0')
|
|
return (false);
|
|
if (nb > INT_MAX || nb < INT_MIN)
|
|
return (false);
|
|
return (true);
|
|
}
|
|
|
|
bool ScalarConverter::isDouble(const std::string &literal)
|
|
{
|
|
char *end;
|
|
|
|
if (literal.find('.') == std::string::npos)
|
|
return (false);
|
|
strtod(literal.c_str(), &end);
|
|
if (*end != '\0')
|
|
return (false);
|
|
return (true);
|
|
}
|
|
|
|
bool ScalarConverter::isFloat(const std::string &literal)
|
|
{
|
|
if (literal[literal.length() - 1] != 'f')
|
|
return (false);
|
|
|
|
std::string withoutF = literal.substr(0, literal.length() - 1);
|
|
return isDouble(withoutF);
|
|
}
|
|
|
|
// Converters
|
|
|
|
void ScalarConverter::convertFromSpecialCase(const std::string &literal)
|
|
{
|
|
std::cout << "char: impossible" << std::endl;
|
|
std::cout << "int: impossible" << std::endl;
|
|
if (literal == "nanf" || literal == "+inff" || literal == "-inff")
|
|
{
|
|
std::cout << "float: " << literal << std::endl;
|
|
std::cout << "double: " << literal.substr(0, literal.length() - 1) << std::endl;
|
|
} else {
|
|
std::cout << "float: " << literal << "f" << std::endl;
|
|
std::cout << "double: " << literal << std::endl;
|
|
}
|
|
}
|
|
|
|
void ScalarConverter::convertFromChar(const std::string &literal)
|
|
{
|
|
char c;
|
|
|
|
c = literal[1];
|
|
std::cout << "char: '" << c << "'" << std::endl;
|
|
std::cout << "int: " << static_cast<int>(c) << std::endl;
|
|
std::cout << std::fixed << std::setprecision(1);
|
|
std::cout << "float: " << static_cast<float>(c) << "f" << std::endl;
|
|
std::cout << "double: " << static_cast<double>(c) << std::endl;
|
|
}
|
|
|
|
void ScalarConverter::convertFromInt(const std::string &literal)
|
|
{
|
|
int nb;
|
|
char *end;
|
|
|
|
nb = static_cast<int>(std::strtol(literal.c_str(), &end, 10));
|
|
if (nb < 0 || nb > 127)
|
|
std::cout << "char: impossible" << std::endl;
|
|
else if (nb < 32 || nb == 127)
|
|
std::cout << "char: Non displayable" << std::endl;
|
|
else
|
|
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
|
std::cout << "int: " << nb << std::endl;
|
|
std::cout << std::fixed << std::setprecision(1);
|
|
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
|
|
std::cout << "double: " << static_cast<double>(nb) << std::endl;
|
|
}
|
|
|
|
void ScalarConverter::convertFromFloat(const std::string &literal)
|
|
{
|
|
float nb;
|
|
char *end;
|
|
|
|
nb = std::strtof(literal.c_str(), &end);
|
|
if (nb < 0 || nb > 127)
|
|
std::cout << "char: impossible" << std::endl;
|
|
else if (nb < 32 || nb == 127)
|
|
std::cout << "char: Non displayable" << std::endl;
|
|
else
|
|
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
|
std::cout << "int: " << static_cast<int>(nb) << std::endl;
|
|
std::cout << std::fixed << std::setprecision(1);
|
|
std::cout << "float: " << nb << "f" << std::endl;
|
|
std::cout << "double: " << static_cast<double>(nb) << std::endl;
|
|
}
|
|
|
|
void ScalarConverter::convertFromDouble(const std::string &literal)
|
|
{
|
|
double nb;
|
|
char *end;
|
|
|
|
nb = std::strtod(literal.c_str(), &end);
|
|
if (nb < 0 || nb > 127)
|
|
std::cout << "char: impossible" << std::endl;
|
|
else if (nb < 32 || nb == 127)
|
|
std::cout << "char: Non displayable" << std::endl;
|
|
else
|
|
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
|
std::cout << "int: " << static_cast<int>(nb) << std::endl;
|
|
std::cout << std::fixed << std::setprecision(1);
|
|
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
|
|
std::cout << "double: " << nb << std::endl;
|
|
}
|