From 64695b9deb270354508bcc1de63c16855e48b8e0 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 12 Apr 2025 18:09:15 +0200 Subject: [PATCH] feat(09): now key in number of the line --- cpp09/ex00/Makefile | 19 +------- cpp09/ex00/main.cpp | 107 +++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 75 deletions(-) diff --git a/cpp09/ex00/Makefile b/cpp09/ex00/Makefile index 6f1de3a..96dafe3 100644 --- a/cpp09/ex00/Makefile +++ b/cpp09/ex00/Makefile @@ -6,11 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -<<<<<<< Updated upstream -# Updated: 2025/04/11 15:55:49 by rparodi ### ########.fr # -======= -# Updated: 2025/04/12 12:53:27 by rparodi ### ########.fr # ->>>>>>> Stashed changes +# Updated: 2025/04/12 16:01:22 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -37,8 +33,6 @@ CXXFLAGS += $(DEBUG) SRC = BitcoinExchange.cpp \ main.cpp -<<<<<<< Updated upstream -======= INC_DIR = . CPPFLAGS = $(addprefix -I, $(INC_DIR)) -MMD -MP @@ -47,7 +41,6 @@ CPPFLAGS = $(addprefix -I, $(INC_DIR)) -MMD -MP # DEBUG = -g3 # CPPFLAGS += $(DEBUG) ->>>>>>> Stashed changes # Objects OBJDIRNAME = ./build OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o)) @@ -131,15 +124,6 @@ footer: @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' clangd: -<<<<<<< Updated upstream - @echo -e \ - "CompileFlags:\n" \ - " Add:\n" \ - " - \"-std=c++98 -Wall -Wextra -Werror\"\n" \ - " - \"-I"$(shell pwd)"/includes\"\n" \ - " - \"-xc++\"\n" \ - > .clangd -======= @printf "CompileFlags:\n" > ./.clangd @printf " Add:\n" >> ./.clangd @printf " - \"-std=c++98 -Wall -Wextra -Werror\"\n" >> ./.clangd @@ -150,7 +134,6 @@ clangd: done @printf "\n" >> ./.clangd @printf '$(GREY) Now parsing settings is set in $(END)$(GREEN)./.clangd$(END)\n' ->>>>>>> Stashed changes # Phony .PHONY: all clean fclean re get_db clangd diff --git a/cpp09/ex00/main.cpp b/cpp09/ex00/main.cpp index 6e742c0..b868578 100644 --- a/cpp09/ex00/main.cpp +++ b/cpp09/ex00/main.cpp @@ -6,17 +6,15 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/08 12:15:32 by rparodi #+# #+# */ -<<<<<<< Updated upstream -/* Updated: 2025/04/11 16:06:02 by rparodi ### ########.fr */ -======= -/* Updated: 2025/04/12 15:58:47 by rparodi ### ########.fr */ ->>>>>>> Stashed changes +/* Updated: 2025/04/12 18:07:03 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "BitcoinExchange.hpp" #include #include +#include +#include enum error_code { NO_ERROR = 0, @@ -31,68 +29,72 @@ enum error_code { typedef struct s_value { enum error_code reason; float value; + std::string date; } value; -value convertValue (enum error_code error, float value) { +value convertValue (enum error_code error, float value, std::string date) { s_value to_return; to_return.reason = error; to_return.value = value; + to_return.date = date; return to_return; } -<<<<<<< Updated upstream -bool is_valid_format_date(std::string str) { - regex_t regex; -======= std::string error_code_to_string(enum error_code code) { switch (code) { - case NO_ERROR: return "NO_ERROR"; - case NEGATIVE: return "NEGATIVE"; - case NO_FORMAT: return "NO_FORMAT"; - case NO_LIMIT: return "NO_LIMIT"; - case TOO_LARGE: return "TOO_LARGE"; - default: return "UNKNOWN"; + case NO_ERROR: + return "NO_ERROR"; + case NEGATIVE: + return "NEGATIVE"; + case NO_FORMAT: + return "NO_FORMAT"; + case NO_DATE: + return "NO_DATE"; + case NO_LIMIT: + return "NO_LIMIT"; + case NO_FLOAT: + return "NO_FLOAT"; + case TOO_LARGE: + return "TOO_LARGE"; + default: + return "UNKNOWN"; } } -void debug_print_map(const std::map >& data) { - std::cout << "========== Debug: Parsed Entries ==========" << std::endl; - - // En-tĂȘtes de colonnes +void debug_print_map(const std::map& data) { + std::cout << "===== Debug: Contents of map =====" << std::endl; std::cout << std::left - << std::setw(8) << "Line" - << std::setw(15) << "Date" + << std::setw(15) << "Key" << std::setw(10) << "Value" + << std::setw(15) << "Date" << std::setw(15) << "Reason" << std::endl; - std::cout << std::string(48, '-') << std::endl; - - // Parcours de la map extĂ©rieure (par ligne) - for (size_t i = 0; i < data.size(); ++i) { - size_t line = it->first; - const std::map& inner = it[line]->second; - const value& val = inner_it->second; + std::cout << std::string(40, '-') << std::endl; + for (std::map::const_iterator it = data.begin(); it != data.end(); ++it) { std::cout << std::left - << std::setw(8) << line - << std::setw(15) << date - << std::setw(10) << std::fixed << std::setprecision(2) << val.value - << std::setw(15) << error_code_to_string(val.reason) + << std::setw(15) << it->first + << std::setw(10) << std::fixed << std::setprecision(2) << it->second.value + << std::setw(15) << std::fixed << it->second.date + << std::setw(15) << error_code_to_string(it->second.reason) << std::endl; - } - std::cout << "===========================================" << std::endl; + std::cout << "===================================" << std::endl; } ->>>>>>> Stashed changes +std::string itoa_home(int i) { + std::ostringstream oss; + oss << "0000" << i; + return oss.str(); +} bool is_valid_format_date(std::string str) { regex_t regex; - int ret = regcomp(®ex, "^[0-9]{4}-[0-9]-[0-9]{2}$", REG_EXTENDED); + int ret = regcomp(®ex, "^[0-9]{4}-[0-9]{2}-[0-9]{2}$", REG_EXTENDED); if (ret) return false; @@ -159,42 +161,32 @@ float check_value(std::string value, enum error_code *error_code) { return (atof(value.c_str())); } -std::mapparse_input(std::string name) { +std::mapparse_input(std::string name) { enum error_code tmpError; std::ifstream file(name.c_str()); - std::map to_ret; + std::map to_ret; std::string tmpLine; std::string tmpDate; float tmpValue = 0; + size_t line = 0; while (std::getline(file, tmpLine)) { + line++; tmpError = NO_ERROR; + tmpDate.clear(); + tmpValue = 0; std::size_t limit = tmpLine.find(" | "); if (limit == std::string::npos) { tmpError = NO_FORMAT; -<<<<<<< Updated upstream -======= - to_ret[line].insert(std::make_pair(tmpLine, convertValue(line, tmpError, tmpValue))); - continue; ->>>>>>> Stashed changes + to_ret.insert(std::make_pair(line, convertValue(tmpError, tmpValue, tmpLine))); } else { tmpDate = check_date(tmpLine.substr(0, limit), &tmpError); - if (tmpError == NO_ERROR) { - tmpValue = check_value(tmpLine.substr(limit + 3).c_str(), &tmpError); - } + tmpValue = check_value(tmpLine.substr(limit + 3).c_str(), &tmpError); + to_ret.insert(std::make_pair(line, convertValue(tmpError, tmpValue, tmpDate))); } -<<<<<<< Updated upstream - to_ret.insert(std::pair(tmpDate, convertValue(tmpError, tmpValue))); -======= - if (tmpError != NO_ERROR) - to_ret[line].insert(std::make_pair(itoa_home(line), convertValue(line, tmpError, tmpValue))); - else - to_ret[line].insert(std::make_pair(tmpLine, convertValue(line, tmpError, tmpValue))); - line++; ->>>>>>> Stashed changes } return to_ret; } @@ -220,5 +212,6 @@ int main(int argc, char *argv[]) { std::cerr << CLR_RED << "The program have to read on the file `data.csv`" << CLR_RESET << std::endl; exit( 1); } - std::map user_db = parse_input(argv[1]); + std::map user_db = parse_input(argv[1]); + debug_print_map(user_db); }