Skip to content

Commit

Permalink
Fix my_strtol
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Dec 17, 2023
1 parent 1d8bcc0 commit 94a6557
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/my/io/my_printf/flag_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/my/std/convert_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions lib/my/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions lib/my/string/strtol.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ 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;
if (index_in(str[i], base) == -1)
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);
}
5 changes: 2 additions & 3 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/parser/room.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 94a6557

Please sign in to comment.