commit 8a78b30d089ab81d3c4d3acc8e936f75f2574d0f Author: aortigos Date: Sun Mar 1 00:28:26 2026 +0100 ex00 and ex01 diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..3416f4f --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,43 @@ +NAME = scalar + +SRC = main.cpp \ + ScalarConverter/ScalarConverter.cpp + +OBJ = $(SRC:.cpp=.o) + +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 + +GREEN = \033[0;32m +RED = \033[0;31m +RESET = \033[0m + +TOTAL := $(words $(SRC)) +COUNT = 0 + +all: $(NAME) + +$(NAME): $(OBJ) + @rm -f .build_start + @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) + @printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)" + +%.o: %.cpp + @if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi + @$(eval COUNT = $(shell echo $$(($(COUNT)+1)))) + @PERCENT=$$(($(COUNT)*100/$(TOTAL))); \ + BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \ + SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \ + printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL) + @$(CC) $(CFLAGS) -c $< -o $@ + +clean: + @printf "$(RED)Eliminando...\n$(RESET)" + @rm -f $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex00/ScalarConverter/ScalarConverter.cpp b/ex00/ScalarConverter/ScalarConverter.cpp new file mode 100644 index 0000000..6c4399c --- /dev/null +++ b/ex00/ScalarConverter/ScalarConverter.cpp @@ -0,0 +1,201 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScalarConverter.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(c) << std::endl; + std::cout << std::fixed << std::setprecision(1); + std::cout << "float: " << static_cast(c) << "f" << std::endl; + std::cout << "double: " << static_cast(c) << std::endl; +} + +void ScalarConverter::convertFromInt(const std::string &literal) +{ + int nb; + char *end; + + nb = static_cast(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(nb) << "'" << std::endl; + std::cout << "int: " << nb << std::endl; + std::cout << std::fixed << std::setprecision(1); + std::cout << "float: " << static_cast(nb) << "f" << std::endl; + std::cout << "double: " << static_cast(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(nb) << "'" << std::endl; + std::cout << "int: " << static_cast(nb) << std::endl; + std::cout << std::fixed << std::setprecision(1); + std::cout << "float: " << nb << "f" << std::endl; + std::cout << "double: " << static_cast(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(nb) << "'" << std::endl; + std::cout << "int: " << static_cast(nb) << std::endl; + std::cout << std::fixed << std::setprecision(1); + std::cout << "float: " << static_cast(nb) << "f" << std::endl; + std::cout << "double: " << nb << std::endl; +} diff --git a/ex00/ScalarConverter/ScalarConverter.hpp b/ex00/ScalarConverter/ScalarConverter.hpp new file mode 100644 index 0000000..db6c632 --- /dev/null +++ b/ex00/ScalarConverter/ScalarConverter.hpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScalarConverter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/02/28 20:03:23 by aortigos #+# #+# */ +/* Updated: 2026/02/28 20:03:23 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SCALARCONVERTER_HPP + +# define SCALARCONVERTER_HPP + +# include // STD:: +# include // INT_INT & INT_MAX +# include // strtol +# include + +class ScalarConverter +{ + private: + ScalarConverter(); + ScalarConverter(const ScalarConverter &other); + ScalarConverter& operator=(const ScalarConverter &other); + ~ScalarConverter(); + public: + // Main function + static void convert(const std::string &literal); + + // Checkers + static bool isSpecialCase(const std::string &literal); + static bool isChar(const std::string &literal); + static bool isInt(const std::string &literal); + static bool isFloat(const std::string &literal); + static bool isDouble(const std::string &literal); + + // Converters + static void convertFromSpecialCase(const std::string &literal); + static void convertFromChar(const std::string &literal); + static void convertFromInt(const std::string &literal); + static void convertFromFloat(const std::string &literal); + static void convertFromDouble(const std::string &literal); +}; + +#endif diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..e35ecd2 --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/02/28 23:48:07 by aortigos #+# #+# */ +/* Updated: 2026/02/28 23:48:07 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Serializer.hpp" + +////////////////// +// Constructors // +////////////////// + +Serializer::Serializer() +{ + // std::cout << "Serializer default constructor called" << std::endl; +} + +Serializer::Serializer(const Serializer &other) +{ + *this = other; + // std::cout << "Serializer copy constructor called" << std::endl; +} + +Serializer& Serializer::operator=(const Serializer &other) +{ + if (this != &other) + { + // Copy attributes here + } + // std::cout << "Serializer copy assignment operator called" << std::endl; + return (*this); +} + +Serializer::~Serializer() +{ + // std::cout << "Serializer destructor called" << std::endl; +} + +uintptr_t Serializer::serialize(Data *ptr) +{ + return (reinterpret_cast(ptr)); +} + +Data *Serializer::deserialize(uintptr_t raw) +{ + return (reinterpret_cast(raw)); +} \ No newline at end of file diff --git a/ex01/Serializer/Serializer.hpp b/ex01/Serializer/Serializer.hpp new file mode 100644 index 0000000..b7065c3 --- /dev/null +++ b/ex01/Serializer/Serializer.hpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Serializer.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/02/28 23:48:07 by aortigos #+# #+# */ +/* Updated: 2026/02/28 23:48:07 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SERIALIZER_HPP + +# define SERIALIZER_HPP + +# include +# include + +struct Data +{ + int age; + std::string name; +}; + +class Serializer +{ + private: + Serializer(); + Serializer(const Serializer &other); + Serializer& operator=(const Serializer &other); + ~Serializer(); + + public: + static uintptr_t serialize(Data *ptr); + static Data *deserialize(uintptr_t raw); + +}; + +#endif diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..a5a7ef1 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos age = 42; + original->name = "Hadi"; + + std::cout << "Puntero original: " << original << std::endl; + + uintptr_t serialized = Serializer::serialize(original); + std::cout << "Serializado (número): " << serialized << std::endl; + + Data *recovered = Serializer::deserialize(serialized); + std::cout << "Puntero recovered: " << recovered << std::endl; + + std::cout << "Son iguales: " << (original == recovered) << std::endl; + std::cout << "Age recovered: " << recovered->age << std::endl; + std::cout << "Name recovered: " << recovered->name << std::endl; + + delete original; +} \ No newline at end of file