From d97af9e3b657d36084346beb5f965c6b0c8eab5c Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 11 Apr 2025 16:06:27 +0200 Subject: [PATCH] feat(09): parsing of the float finish --- cpp09/ex00/Makefile | 2 +- cpp09/ex00/main.cpp | 45 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/cpp09/ex00/Makefile b/cpp09/ex00/Makefile index b9915fd..34faa10 100644 --- a/cpp09/ex00/Makefile +++ b/cpp09/ex00/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2025/04/11 12:35:05 by rparodi ### ########.fr # +# Updated: 2025/04/11 15:55:49 by rparodi ### ########.fr # # # # **************************************************************************** # diff --git a/cpp09/ex00/main.cpp b/cpp09/ex00/main.cpp index 1e0a39c..fdeddaf 100644 --- a/cpp09/ex00/main.cpp +++ b/cpp09/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/08 12:15:32 by rparodi #+# #+# */ -/* Updated: 2025/04/11 15:07:21 by rparodi ### ########.fr */ +/* Updated: 2025/04/11 16:06:02 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ enum error_code { NEGATIVE, NO_FORMAT, NO_DATE, + NO_FLOAT, NO_LIMIT, TOO_LARGE }; @@ -28,6 +29,13 @@ typedef struct s_value { float value; } value; +value convertValue (enum error_code error, float value) { + s_value to_return; + to_return.reason = error; + to_return.value = value; + return to_return; +} + bool is_valid_format_date(std::string str) { regex_t regex; @@ -41,6 +49,7 @@ bool is_valid_format_date(std::string str) { regfree(®ex); return true; } + std::string check_date(std::string name, enum error_code *error_code) { if (!is_valid_format_date(name)) { *error_code = NO_FORMAT; @@ -70,11 +79,31 @@ std::string check_date(std::string name, enum error_code *error_code) { return (name); } -value convertValue (enum error_code error, float value) { - s_value to_return; - to_return.reason = error; - to_return.value = value; - return to_return; +float check_value(std::string value, enum error_code *error_code) { + bool has_dot = false; + if (value.at(0) == '-') { + *error_code = NEGATIVE; + return 0; + } + for (size_t i = 0; i < value.length(); i++) { + if (value[i] == '.') { + if (has_dot == true) { + *error_code = NO_FLOAT; + return 0; + } + has_dot = true; + continue; + } + if (isdigit(value[i]) == false) { + *error_code = NO_FLOAT; + return 0; + } + } + if (value.size() >= 4 || atof(value.c_str()) > 1000) { + *error_code = TOO_LARGE; + return 0; + } + return (atof(value.c_str())); } std::mapparse_input(std::string name) { @@ -96,7 +125,7 @@ std::mapparse_input(std::string name) { } else { tmpDate = check_date(tmpLine.substr(0, limit), &tmpError); if (tmpError == NO_ERROR) { - tmpValue = std::atof(tmpLine.substr(limit + 3).c_str()); + tmpValue = check_value(tmpLine.substr(limit + 3).c_str(), &tmpError); } } to_ret.insert(std::pair(tmpDate, convertValue(tmpError, tmpValue))); @@ -118,7 +147,7 @@ int main(int argc, char *argv[]) { exit( 1); } if (access("data.csv", F_OK)) { - std::cerr << CLR_RED << "The file `data.csv` have to exist (to take it from the intra make get_db)" << CLR_RESET << std::endl; + std::cerr << CLR_RED << "The file `data.csv` have to exist (to take it from the intra `make get_db`)" << CLR_RESET << std::endl; exit(1); } if (access("data.csv", R_OK)) {