Skip to content

Commit

Permalink
fix: when a float is like 42.00 it was considered as an int
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisLanganay committed Mar 29, 2023
1 parent ca5ffd1 commit 95977d9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/jp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ SRC += jp_writeint.c
SRC += jp_writebool.c
SRC += jp_writetab.c
SRC += jp_writearray.c
SRC += my_is_a_float.c


INC_DIR := ../../includes
Expand Down
1 change: 1 addition & 0 deletions lib/jp/jp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
int my_nbrlen(long int nb);
double my_getnbr_float(char *str);
int my_strlen(char const *str);
int my_is_a_float(char *str);
long int my_compute_power_rec(long int nb, int p);
int my_strcmp(char const *s1, char const *s2);

Expand Down
3 changes: 2 additions & 1 deletion lib/jp/reader/getvalue/jp_getvalue_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ void jp_getvalue_int(char **str, parsed_data_t *data)
{
int nb = my_getnbr(*str);
double nb_float = my_getnbr_float(*str);
int is_float = my_is_a_float(*str);

for (; **str == '.' || **str == '-' ||
(**str >= '0' && **str <= '9'); *str += 1);

if (nb_float != nb) {
if (is_float == 1) {
data->type = p_float;
data->value.p_float = nb_float;
} else {
Expand Down
15 changes: 15 additions & 0 deletions lib/jp/utils/my_is_a_float.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
** EPITECH PROJECT, 2023
** json-parser
** File description:
** my_is_a_float
*/

int my_is_a_float(char *str)
{
for (int i = 0; str[i] != ','; i++) {
if (str[i] == '.')
return 1;
}
return 0;
}
2 changes: 2 additions & 0 deletions tests/do_not_modify.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"x": 5632,
"y": 65131
},
"float": 24.68,
"float2": 42.0,
"alive": true,
"inventory": {
"items": [
Expand Down
11 changes: 11 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,14 @@ Test(json_parser, Write_Simple_Int) {
jp_write("tests.json", data);
free(data);
}

Test(json_parser, Read_Float) {
parsed_data_t *data = jp_parse("tests.json");
float my_float = jp_search(data, "float")->value.p_float;
cr_assert(my_float == (float)24.68);

my_float = jp_search(data, "float2")->value.p_float;
cr_assert(my_float == (float)42.0);

free(data);
}
2 changes: 2 additions & 0 deletions tests/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"x": 5632,
"y": 65131
},
"float": 0,
"float2": 0,
"alive": true,
"inventory": {
"items": [
Expand Down

0 comments on commit 95977d9

Please sign in to comment.