From e657cd80127c564f29e225048267801102696efa Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 18 Oct 2024 15:17:20 +0200 Subject: [PATCH] Restarting ex01 --- cpp00/ex01/.clangd | 8 ++ cpp00/ex01/Makefile | 8 +- cpp00/ex01/includes/color.hpp | 47 +++++++++ cpp00/ex01/{ => includes}/phonebook.hpp | 59 ++++++----- cpp00/ex01/main.cpp | 127 ------------------------ cpp00/ex01/phonebook.cpp | 27 ----- cpp00/ex01/sources/main.cpp | 56 +++++++++++ cpp00/ex01/sources/phonebook.cpp | 33 ++++++ 8 files changed, 182 insertions(+), 183 deletions(-) create mode 100644 cpp00/ex01/.clangd create mode 100644 cpp00/ex01/includes/color.hpp rename cpp00/ex01/{ => includes}/phonebook.hpp (50%) delete mode 100644 cpp00/ex01/main.cpp delete mode 100644 cpp00/ex01/phonebook.cpp create mode 100644 cpp00/ex01/sources/main.cpp create mode 100644 cpp00/ex01/sources/phonebook.cpp diff --git a/cpp00/ex01/.clangd b/cpp00/ex01/.clangd new file mode 100644 index 0000000..a39ccfc --- /dev/null +++ b/cpp00/ex01/.clangd @@ -0,0 +1,8 @@ +CompileFlags: + Compiler: clang + Add: + - "-xc" + - "-I/usr/local/include" + - "-I/nix/store/pzmpyb9hxfv60vh0l67avr94h71nip1b-llvm-16.0.6-dev/include" + - "-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include" + - "-I/Users/raphael/Documents/piscine/cpp/cpp00/ex01/includes/" diff --git a/cpp00/ex01/Makefile b/cpp00/ex01/Makefile index 156f410..d142940 100644 --- a/cpp00/ex01/Makefile +++ b/cpp00/ex01/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/10/14 23:43:03 by rparodi ### ########.fr # +# Updated: 2024/10/17 15:36:20 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -21,14 +21,14 @@ RM = rm -rf # Flags # Mandatory flags for 42 cpp -CXXFLAGS = -Werror -Wextra -Wall -std=c++98 +CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ # Flags to debug and have the dependences (can be removed for correction) CXXFLAGS += -MMD -g3 # Flag to debug (TO REMOVE) CXXFLAGS += -D DEBUG=1 # Sources -SRC = ./phonebook.cpp \ - ./main.cpp +SRC = ./sources/phonebook.cpp \ + ./sources/main.cpp # Objects OBJDIRNAME = ./build diff --git a/cpp00/ex01/includes/color.hpp b/cpp00/ex01/includes/color.hpp new file mode 100644 index 0000000..cd7cfa9 --- /dev/null +++ b/cpp00/ex01/includes/color.hpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 15:27:54 by rparodi #+# #+# */ +/* Updated: 2024/10/17 15:48:22 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 diff --git a/cpp00/ex01/phonebook.hpp b/cpp00/ex01/includes/phonebook.hpp similarity index 50% rename from cpp00/ex01/phonebook.hpp rename to cpp00/ex01/includes/phonebook.hpp index 53d094f..a9d936f 100644 --- a/cpp00/ex01/phonebook.hpp +++ b/cpp00/ex01/includes/phonebook.hpp @@ -5,42 +5,51 @@ /* +:+ +:+ +:+ */ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/20 18:59:14 by rparodi #+# #+# */ -/* Updated: 2024/10/15 00:26:50 by rparodi ### ########.fr */ +/* Created: 2024/10/17 14:56:49 by rparodi #+# #+# */ +/* Updated: 2024/10/18 12:18:04 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef PHONEBOOK_H -#define PHONEBOOK_H +#ifndef PHONEBOOK_HPP +# define PHONEBOOK_HPP #include -#include +#include #include +#include +#include #include -#include +#include "color.hpp" -#define MENU_TEXT "You're in the main menu:\nHere's the command:\n\t- " << BLUE << "ADD: " << RESET << "To add a new contact\n\t- " BLUE << "SEARCH: " << RESET "To search a contact\n\t- " BLUE << "EXIT: " << RESET "to exit the program" -#define GOLD "\033[38;5;220m" -#define GREY "\033[0;90m" -#define RED "\033[31m" -#define GREEN "\033[32m" -#define BLUE "\033[34m" -#define RESET "\033[0m" +#define ERROR 1 +#define NO_ERROR 0 -#ifndef DEBUG -# define DEBUG 0 -#endif +std::string get_input(const char *str); -class contact { +class Contact +{ public: - int id; - std::string number; - std::string first_name; - std::string last_name; - std::string nickname; - std::string dark_secret; - struct tm *creation_time; - void _debug(void); + /*Contact();*/ + /*~Contact();*/ + void init_new(int id); + void print(); + private: + int _id; + std::string _first_name; + std::string _last_name; + std::string _nickname; + std::string _number; + std::string _secret; }; +class PhoneBook +{ + public: + void add(); + void search(); + private: + Contact _array_contact[8]; +}; + + #endif diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp deleted file mode 100644 index 12b6f2f..0000000 --- a/cpp00/ex01/main.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/20 19:42:14 by rparodi #+# #+# */ -/* Updated: 2024/10/15 00:50:51 by rparodi ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "phonebook.hpp" -#include -#include - -int get_older_contact(contact array_contact[8]) -{ - int ret = 0; - time_t remain = 0; - - for (int i = 0; i < 8; i++) - { - time_t tmp = mktime(array_contact[i].creation_time); - if (tmp == -1) - { - std::cerr << RED << "\nError: during the time process\n" << RESET << std::endl; - exit(1); - } - if (tmp > remain) - { - remain = mktime(array_contact[i].creation_time); - if (remain == -1) - { - std::cerr << RED << "\nError: during the time process\n" << RESET << std::endl; - exit(1); - } - ret = i; - } - } - return (ret); -} - -/** - * @brief Adding a new contact - * - * @param index index for the new contact - * @param array_contact the array asked by the subject - * @return true on error - */ -bool adding(int index, contact array_contact[8]) -{ - std::string input; - - if (index > 8 || index < 0) - return (true); - - std::cout << "Thanks to give the number of the new contact" << std::endl; - std::getline(std::cin, input); - if (input.length() != 10) - return (true); - array_contact[index].number = input; - - std::cout << "Thanks to give the nickname of the new contact" << std::endl; - std::getline(std::cin, input); - array_contact[index].nickname = input; - - std::cout << "Thanks to give the first_name of the new contact" << std::endl; - std::getline(std::cin, input); - array_contact[index].first_name = input; - - std::cout << "Thanks to give the last_name of the new contact" << std::endl; - std::getline(std::cin, input); - array_contact[index].last_name = input; - - std::cout << "Thanks to give the dark secret of the new contact" << std::endl; - std::getline(std::cin, input); - array_contact[index].dark_secret = input; - - array_contact[index].id = index; - time_t now = time(0); - array_contact[index].creation_time = localtime(&now); - - if (DEBUG) - array_contact[index]._debug(); - return (false); -} - -bool searching(contact contact[8], int index) -{ - if (index == 0) - return (true); - for (int i = 0; i < index; i++) - contact[i]._debug(); - return (false); -} - -int main(void) -{ - contact array_contact[8]; - int i; - std::string input; - - i = 0; - while (true) - { - std::cout << MENU_TEXT << std::endl; - std::getline(std::cin, input); - if (strcmp(input.c_str(), "ADD") == 0) - { - if (adding(i, array_contact)) - std::cerr << RED << "\nError: during the adding process\n" << RESET << std::endl; - else - i++; - } - else if (strcmp(input.c_str(), "SEARCH") == 0) - { - if (searching(array_contact, i)) - std::cerr << RED << "\nError: during the searching process\n" << RESET << std::endl; - } - else if (strcmp(input.c_str(), "EXIT") == 0) - return (0); - else - std::cerr << RED << "\nError: no option reconnise\n" << RESET << std::endl; - input.clear(); - } -} diff --git a/cpp00/ex01/phonebook.cpp b/cpp00/ex01/phonebook.cpp deleted file mode 100644 index 31c80a5..0000000 --- a/cpp00/ex01/phonebook.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* phonebook.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/20 19:50:03 by rparodi #+# #+# */ -/* Updated: 2024/10/15 00:32:03 by rparodi ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "phonebook.hpp" - -void contact::_debug() { - std::cout << GOLD << "\n\n\tDEBUG MODE ENABLE\n" << RESET << std::endl; - std::cout << GREY; - std::cout << "First Name:\t" << first_name << std::endl; - std::cout << "Last Name:\t" << last_name << std::endl; - std::cout << "Nickname:\t" << nickname << std::endl; - std::cout << "Number:\t\t" << number << std::endl; - std::cout << "Dark Secret:\t" << dark_secret << std::endl; - std::cout << "ID:\t\t" << id << std::endl; - std::cout << "Creation Hour:\t" << creation_time->tm_hour << ":" << creation_time->tm_min << ":" << creation_time->tm_sec << std::endl; - std::cout << RESET << std::endl; -} - diff --git a/cpp00/ex01/sources/main.cpp b/cpp00/ex01/sources/main.cpp new file mode 100644 index 0000000..422d4a9 --- /dev/null +++ b/cpp00/ex01/sources/main.cpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 14:55:18 by rparodi #+# #+# */ +/* Updated: 2024/10/18 12:19:07 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "phonebook.hpp" + +/** + * @brief return the input with std::getline() + * + * @param err_msg The message return in case of error + * @return the value of input + */ +std::string get_input(const char *err_msg) +{ + std::string input; + + try + { + if (!std::getline(std::cin, input)) + throw std::runtime_error("getline can't read the input !\n"); + } + catch (const std::exception& err) + { + std::cerr << CLR_BOLD_RED << "\nError:\t" << err.what() << CLR_RESET << CLR_RED << "(" << err_msg << ")" << CLR_RESET << std::endl; + exit(1); + } + return (input); +} + +int main() +{ + std::string input; + Contact array_contact[8]; + size_t i = 0; + + while (true) + { + input = get_input("SELECT MENU [main.cpp:38]"); + if (strcmp(input.c_str(), "ADD")) + { + std::cout << "adding" << std::endl; + array_contact[i].init_new(i); + i++; + } + if (strcmp(input.c_str(), "EXIT")) + exit(1); + } +} diff --git a/cpp00/ex01/sources/phonebook.cpp b/cpp00/ex01/sources/phonebook.cpp new file mode 100644 index 0000000..330c264 --- /dev/null +++ b/cpp00/ex01/sources/phonebook.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* phonebook.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 15:09:21 by rparodi #+# #+# */ +/* Updated: 2024/10/17 16:44:08 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "phonebook.hpp" + +void Contact::init_new(int id) +{ + _id = id; + _first_name = get_input("first_name [phonebook.cpp:18]"); + _last_name = get_input("last_name [phonebook.cpp:19]"); + _nickname = get_input("nickname [phonebook.cpp:20]"); + _number = get_input("number [phonebook.cpp:21]"); + _secret = get_input("secret [phonebook.cpp:22]"); +} + +void Contact::print() +{ + std::cout << "|" << std::setw(10) << _id << "|"; + std::cout << "|" << std::setw(10) << _first_name << "|"; + std::cout << "|" << std::setw(10) << _last_name << "|"; + std::cout << "|" << std::setw(10) << _nickname << "|"; + std::cout << "|" << std::setw(10) << _number << "|"; + std::cout << "|" << std::setw(10) << _secret << "|"; +}