From 9d476992bccfe40de31c81680668237dd2a6316e Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 15 Oct 2024 00:15:21 +0200 Subject: [PATCH] update: adding colors and the debug mode --- .gitignore | 5 +++++ cpp00/ex01/Makefile | 4 ++-- cpp00/ex01/main.cpp | 42 ++++++++++++++++++++++++++++------------ cpp00/ex01/phonebook.cpp | 8 ++++++-- cpp00/ex01/phonebook.hpp | 16 +++++++++++---- 5 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66319c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build/ +*.d +*.o +phonebook +megaphone diff --git a/cpp00/ex01/Makefile b/cpp00/ex01/Makefile index 9c2da95..156f410 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 19:07:56 by rparodi ### ########.fr # +# Updated: 2024/10/14 23:43:03 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -25,7 +25,7 @@ CXXFLAGS = -Werror -Wextra -Wall -std=c++98 # Flags to debug and have the dependences (can be removed for correction) CXXFLAGS += -MMD -g3 # Flag to debug (TO REMOVE) -# CXXFLAGS += -D DEBUG=1 +CXXFLAGS += -D DEBUG=1 # Sources SRC = ./phonebook.cpp \ ./main.cpp diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp index d8afc9e..d44daf9 100644 --- a/cpp00/ex01/main.cpp +++ b/cpp00/ex01/main.cpp @@ -6,17 +6,20 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/20 19:42:14 by rparodi #+# #+# */ -/* Updated: 2024/10/14 19:06:54 by rparodi ### ########.fr */ +/* Updated: 2024/10/15 00:14:12 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "phonebook.hpp" #include -#include -#include -#include -#include +/** + * @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; @@ -47,9 +50,20 @@ bool adding(int index, contact array_contact[8]) 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].print(); + 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); } @@ -64,14 +78,18 @@ int main(void) { std::cout << MENU_TEXT << std::endl; std::getline(std::cin, input); - if (strcmp(input.c_str(), "ADD:") == 0) + if (strcmp(input.c_str(), "ADD") == 0) + { if (adding(i, array_contact)) - std::cerr << "Error: during the adding process" << std::endl; - if (strcmp(input.c_str(), "SEARCH:") == 0) - /*if (searching(array_contact))*/; - if (strcmp(input.c_str(), "EXIT:") == 0) + std::cerr << RED << "\nError: during the adding process\n" << RESET << std::endl; + else + i++; + } + if (strcmp(input.c_str(), "SEARCH") == 0) + if (searching(array_contact, i)) + std::cerr << RED << "\nError: during the searching process\n" << RESET << std::endl; + if (strcmp(input.c_str(), "EXIT") == 0) return (0); input.clear(); - i++; } } diff --git a/cpp00/ex01/phonebook.cpp b/cpp00/ex01/phonebook.cpp index a52f150..bf8a69f 100644 --- a/cpp00/ex01/phonebook.cpp +++ b/cpp00/ex01/phonebook.cpp @@ -6,18 +6,22 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/20 19:50:03 by rparodi #+# #+# */ -/* Updated: 2024/10/14 18:52:45 by rparodi ### ########.fr */ +/* Updated: 2024/10/15 00:13:03 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "phonebook.hpp" +#include -void contact::print(void) { +void contact::print(void) { + std::cout << GREY; + std::cout << "Creation Hour:" << creation_time << std::endl; std::cout << "First Name:" << first_name << std::endl; std::cout << "Last Name:" << last_name << std::endl; std::cout << "Nickname:" << nickname << std::endl; std::cout << "Number:" << number << std::endl; std::cout << "Dark Secret:" << dark_secret << std::endl; std::cout << "ID:" << id << std::endl; + std::cout << RESET; } diff --git a/cpp00/ex01/phonebook.hpp b/cpp00/ex01/phonebook.hpp index c008e67..06d1502 100644 --- a/cpp00/ex01/phonebook.hpp +++ b/cpp00/ex01/phonebook.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/20 18:59:14 by rparodi #+# #+# */ -/* Updated: 2024/10/14 19:05:17 by rparodi ### ########.fr */ +/* Updated: 2024/10/15 00:14:00 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,17 @@ #define PHONEBOOK_H #include +#include #include +#include +#include -#define MENU_TEXT "You're in the main menu:\nHere's the command:\n\t- ADD: To add a new contact\n\t- SEARCH: To search a contact\n\t- EXIT: to exit the program" +#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 GREY "\033[0;90m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define BLUE "\033[34m" +#define RESET "\033[0m" #ifndef DEBUG # define DEBUG 0 @@ -30,8 +38,8 @@ class contact { std::string last_name; std::string nickname; std::string dark_secret; - - void print(void); + struct tm *creation_time; + void _debug(void); }; #endif