feat: finishing the ex00
This commit is contained in:
parent
647c777037
commit
089d6bff84
5 changed files with 292 additions and 0 deletions
127
cpp06/ex01/Makefile
Normal file
127
cpp06/ex01/Makefile
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||
# Updated: 2025/03/24 16:45:14 by rparodi ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
# Variables
|
||||
|
||||
# Name
|
||||
NAME = serialize
|
||||
|
||||
# Commands
|
||||
CXX = c++
|
||||
RM = rm -rf
|
||||
|
||||
# Flags
|
||||
# Mandatory flags for 42
|
||||
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
|
||||
# Flags to have the dependences (can be removed for correction)
|
||||
CXXFLAGS += -MMD
|
||||
# Flags to have the debug (can be removed for correction)
|
||||
DEBUG = -g3
|
||||
# DEBUG += -fsanitize=address
|
||||
CXXFLAGS += $(DEBUG)
|
||||
|
||||
# Sources
|
||||
SRC = sources/Serializer.cpp \
|
||||
main.cpp
|
||||
|
||||
# Objects
|
||||
OBJDIRNAME = ./build
|
||||
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
|
||||
|
||||
# Colors
|
||||
GREEN = \033[32m
|
||||
GREY = \033[0;90m
|
||||
RED = \033[0;31m
|
||||
GOLD = \033[38;5;220m
|
||||
END = \033[0m
|
||||
|
||||
# Rules
|
||||
|
||||
# All (make all)
|
||||
all: header $(NAME) footer
|
||||
|
||||
# Bonus (make bonus)
|
||||
bonus: header $(OBJ) footer
|
||||
@mkdir -p $(OBJDIRNAME)
|
||||
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
|
||||
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
|
||||
|
||||
# Clean (make clean)
|
||||
clean:
|
||||
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
|
||||
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
|
||||
@$(RM) $(OBJDIRNAME)
|
||||
|
||||
# Clean (make fclean)
|
||||
fclean: clean
|
||||
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
|
||||
@$(RM) $(NAME)
|
||||
@echo ""
|
||||
|
||||
# Restart (make re)
|
||||
re: header fclean all
|
||||
|
||||
# Dependences for all
|
||||
$(NAME): $(OBJ)
|
||||
@mkdir -p $(OBJDIRNAME)
|
||||
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
|
||||
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
|
||||
|
||||
# Creating the objects
|
||||
$(OBJDIRNAME)/%.o: %.cpp
|
||||
@mkdir -p $(dir $@)
|
||||
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
|
||||
@$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
# Header
|
||||
header:
|
||||
@clear
|
||||
@printf '\n\n'
|
||||
@printf '$(GOLD) ******* ****** ******* $(END)\n'
|
||||
@printf '$(GOLD) ****** *** ******* $(END)\n'
|
||||
@printf '$(GOLD) ******* * ******* $(END)\n'
|
||||
@printf '$(GOLD) ****** ******* $(END)\n'
|
||||
@printf '$(GOLD) ******* ******* $(END)\n'
|
||||
@printf '$(GOLD) ******************* ******* * $(END)\n'
|
||||
@printf '$(GOLD) ******************* ******* *** $(END)\n'
|
||||
@printf '$(GOLD) ****** ******* ****** $(END)\n'
|
||||
@printf '$(GOLD) ****** $(END)\n'
|
||||
@printf '$(GOLD) ****** $(END)\n'
|
||||
@printf '$(GREY) Made by rparodi$(END)\n\n'
|
||||
|
||||
# Footer
|
||||
footer:
|
||||
@printf "\n"
|
||||
@printf "$(GOLD) ,_ _,$(END)\n"
|
||||
@printf "$(GOLD) | \\___//|$(END)\n"
|
||||
@printf "$(GOLD) |=6 6=|$(END)\n"
|
||||
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
|
||||
@printf "$(GOLD) ) \` ( ,$(END)\n"
|
||||
@printf "$(GOLD) / \\ (('$(END)\n"
|
||||
@printf "$(GOLD) | | ))$(END)\n"
|
||||
@printf "$(GOLD) /| | | |\\_//$(END)\n"
|
||||
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
|
||||
@printf "$(GOLD) '\"' '\"'$(END)\n"
|
||||
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
|
||||
|
||||
clangd:
|
||||
@echo -e \
|
||||
"CompileFlags:\n" \
|
||||
" Add:\n" \
|
||||
" - \"-std=c++98 -Wall -Wextra -Werror\"\n" \
|
||||
" - \"-I"$(shell pwd)"/includes\"\n" \
|
||||
" - \"-xc++\"\n" \
|
||||
> .clangd
|
||||
|
||||
# Phony
|
||||
.PHONY: all bonus clean fclean re clangd
|
||||
-include ${OBJ:.o=.d}
|
||||
34
cpp06/ex01/includes/Serializer.hpp
Normal file
34
cpp06/ex01/includes/Serializer.hpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Serializer.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/24 15:56:16 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/25 13:27:10 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "color.hpp"
|
||||
|
||||
struct Data {
|
||||
std::string string;
|
||||
int integer;
|
||||
};
|
||||
|
||||
class Serializer {
|
||||
public:
|
||||
static uintptr_t serialize(Data* ptr);
|
||||
static Data* deserialize(uintptr_t raw);
|
||||
private:
|
||||
Serializer();
|
||||
Serializer(const Serializer ©);
|
||||
Serializer& operator=(const Serializer &assign);
|
||||
~Serializer();
|
||||
};
|
||||
47
cpp06/ex01/includes/color.hpp
Normal file
47
cpp06/ex01/includes/color.hpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* color.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/17 15:27:54 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/24 13:58:15 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef COLOR_HPP
|
||||
#define COLOR_HPP
|
||||
|
||||
#define CLR_RESET "\033[0m"
|
||||
|
||||
#define CLR_BLACK "\033[0;30m"
|
||||
#define CLR_RED "\033[0;31m"
|
||||
#define CLR_GREEN "\033[0;32m"
|
||||
#define CLR_YELLOW "\033[0;33m"
|
||||
#define CLR_BLUE "\033[0;34m"
|
||||
#define CLR_MAGENTA "\033[0;35m"
|
||||
#define CLR_CYAN "\033[0;36m"
|
||||
#define CLR_WHITE "\033[0;37m"
|
||||
#define CLR_GOLD "\033[38;5;220m"
|
||||
#define CLR_GREY "\033[38;5;240m"
|
||||
|
||||
#define CLR_LIGHT_BLACK "\033[0;90m"
|
||||
#define CLR_LIGHT_RED "\033[0;91m"
|
||||
#define CLR_LIGHT_GREEN "\033[0;92m"
|
||||
#define CLR_LIGHT_YELLOW "\033[0;93m"
|
||||
#define CLR_LIGHT_BLUE "\033[0;94m"
|
||||
#define CLR_LIGHT_MAGENTA "\033[0;95m"
|
||||
#define CLR_LIGHT_CYAN "\033[0;96m"
|
||||
#define CLR_LIGHT_WHITE "\033[0;97m"
|
||||
|
||||
#define CLR_BOLD_BLACK "\033[1;30m"
|
||||
#define CLR_BOLD_RED "\033[1;31m"
|
||||
#define CLR_BOLD_GREEN "\033[1;32m"
|
||||
#define CLR_BOLD_YELLOW "\033[1;33m"
|
||||
#define CLR_BOLD_BLUE "\033[1;34m"
|
||||
#define CLR_BOLD_MAGENTA "\033[1;35m"
|
||||
#define CLR_BOLD_CYAN "\033[1;36m"
|
||||
#define CLR_BOLD_WHITE "\033[1;37m"
|
||||
|
||||
#endif
|
||||
38
cpp06/ex01/main.cpp
Normal file
38
cpp06/ex01/main.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/24 15:54:05 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/25 13:59:15 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Serializer.hpp"
|
||||
|
||||
void is_worked(int integer, std::string string, std::string name)
|
||||
{
|
||||
Data data;
|
||||
data.integer = integer;
|
||||
data.string = string;
|
||||
uintptr_t serialized = Serializer::serialize(&data);
|
||||
Data* deserialized;
|
||||
try {
|
||||
deserialized = Serializer::deserialize(serialized);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << CLR_RED << e.what() << CLR_RESET << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << CLR_YELLOW << "\t[ Testing " << name << " ]" << CLR_RESET << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "Serialized:" << std::endl << "\t" << CLR_GOLD << serialized << CLR_RESET << std::endl;
|
||||
std::cout << CLR_BLUE << "Deserialized:" << std::endl << "\t" << CLR_GOLD << deserialized->string << std::endl << "\t" << deserialized->integer << std::endl << CLR_RESET << std::endl;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
is_worked(42, "Student at", "42 / Student at");
|
||||
is_worked(1337, "Leet code", "1337 / leet code");
|
||||
is_worked(0, "", "NULL");
|
||||
return 0;
|
||||
}
|
||||
46
cpp06/ex01/sources/Serializer.cpp
Normal file
46
cpp06/ex01/sources/Serializer.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Serializer.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/24 16:49:50 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/25 13:57:12 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Serializer.hpp"
|
||||
|
||||
Serializer::Serializer(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serializer::Serializer(Serializer const & src) {
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
Serializer::~Serializer(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serializer & Serializer::operator=(Serializer const & rhs) {
|
||||
if (this != &rhs) {
|
||||
*this = rhs;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
uintptr_t Serializer::serialize(Data* ptr) {
|
||||
return reinterpret_cast<uintptr_t>(ptr);
|
||||
}
|
||||
|
||||
Data* Serializer::deserialize(uintptr_t raw) {
|
||||
Data *tmp = reinterpret_cast<Data*>(raw);
|
||||
if (!tmp) {
|
||||
throw std::runtime_error("Error: Deserialize failed !");
|
||||
return NULL;
|
||||
}
|
||||
return (tmp);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue