From 94a6557b7c7452fec0f06e12031b4b9a5247a1d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Sun, 17 Dec 2023 15:18:40 +0100 Subject: [PATCH] Fix my_strtol --- lib/my/io/my_printf/flag_parser.c | 4 ++-- lib/my/std/convert_base.c | 2 +- lib/my/string.h | 4 ++-- lib/my/string/strtol.c | 14 +++++++------- src/parser/parser.c | 5 ++--- src/parser/room.c | 6 +++--- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/my/io/my_printf/flag_parser.c b/lib/my/io/my_printf/flag_parser.c index 611a5fd..a251625 100644 --- a/lib/my/io/my_printf/flag_parser.c +++ b/lib/my/io/my_printf/flag_parser.c @@ -54,7 +54,7 @@ void get_flags(printf_t *pr, printf_args_t *arg) arg->flags = (struct flags_s){ 0 }; get_simple_flags(pr, arg); if (IS_NUM(pr->format[pr->index]) || pr->format[pr->index] == '-') { - arg->flags.padding = my_strtol(pr->format + pr->index, NULL); + arg->flags.padding = my_strtol(pr->format + pr->index); arg->flags.padded = true; pr->index += ( (int)my_nbr_len_base(arg->flags.padding, BASE_DEC) @@ -72,7 +72,7 @@ bool get_precision(printf_t *pr, printf_args_t *arg) arg->precision = default_precision; if (pr->format[pr->index] != '.') return true; - tmp = my_strtol(pr->format + pr->index + 1, NULL); + tmp = my_strtol(pr->format + pr->index + 1); if (tmp <= 0 && pr->format[pr->index + 1] == '-') return false; pr->index += 1; diff --git a/lib/my/std/convert_base.c b/lib/my/std/convert_base.c index ea083f5..9008d17 100644 --- a/lib/my/std/convert_base.c +++ b/lib/my/std/convert_base.c @@ -9,5 +9,5 @@ char *convert_base(char const *nbr, char const *base_from, char const *base_to) { - return my_nbr_to_base(my_strtol_base(nbr, NULL, base_from), base_to); + return my_nbr_to_base(my_strtol_base(nbr, base_from), base_to); } diff --git a/lib/my/string.h b/lib/my/string.h index ea64e88..6408b80 100644 --- a/lib/my/string.h +++ b/lib/my/string.h @@ -59,8 +59,8 @@ int my_showmem(char const *str, int size); int my_showstr(char const *str); int my_strcmp(char const *s1, char const *s2); int my_strncmp(char const *s1, char const *s2, size_t n); -long my_strtol(const char *str, const char **endptr); -long my_strtol_base(const char *str, const char **endptr, char const *base); +long my_strtol(const char *str); +long my_strtol_base(const char *str, char const *base); size_t my_memfind(const void *arr, const void *target, size_t n); size_t my_strlen(char const *str); size_t write_int_base(long long nbr, char const *base, char *buf); diff --git a/lib/my/string/strtol.c b/lib/my/string/strtol.c index b388895..2de1664 100644 --- a/lib/my/string/strtol.c +++ b/lib/my/string/strtol.c @@ -30,15 +30,15 @@ bool is_valid(char c, bool *negative, const char *base) return false; } -long my_strtol_base(const char *str, const char **endptr, char const *base) +long my_strtol_base(const char *str, char const *base) { long res = 0; long len = (long)my_strlen(base); bool negative = false; size_t i = 0; - if (str == NULL || len == 0) - return 0; + if (str == NULL || base == 0) + return INT64_MAX; for (; str[i]; i++) { if (!is_valid(str[i], &negative, base)) break; @@ -46,12 +46,12 @@ long my_strtol_base(const char *str, const char **endptr, char const *base) continue; res = res * len + -index_in(str[i], base); } - if (endptr != NULL) - *endptr = str + i; + if (str[i]) + return INT64_MAX; return (negative) ? res : -res; } -long my_strtol(const char *str, const char **endptr) +long my_strtol(const char *str) { - return my_strtol_base(str, endptr, BASE_DEC); + return my_strtol_base(str, BASE_DEC); } diff --git a/src/parser/parser.c b/src/parser/parser.c index 072b3b3..691a240 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -38,10 +38,9 @@ int get_command(char *line, anthill_t *anthill) static int get_ants_num(char *line, anthill_t *anthill) { - const char *endptr; - long value = my_strtol(line, &endptr); + long value = my_strtol(line); - if (endptr == line) + if (value == INT64_MAX) return RET_ERROR; anthill->ants = value; anthill->step = ST_ROOMS; diff --git a/src/parser/room.c b/src/parser/room.c index a10dc8e..91cdbff 100644 --- a/src/parser/room.c +++ b/src/parser/room.c @@ -45,10 +45,10 @@ room_t *get_room_data(char *line) *ptr_x = '\0'; *ptr_y = '\0'; room.name = dup; - room.x = my_strtol(ptr_x + 1, NULL); - room.y = my_strtol(ptr_y + 1, NULL); + room.x = my_strtol(ptr_x + 1); + room.y = my_strtol(ptr_y + 1); room.type = ROOM_NORMAL; - if (line == ptr_x || ptr_x == ptr_y) + if (room.x == INT64_MAX || room.y == INT64_MAX) return NULL; return &room; }