diff --git a/Makefile b/Makefile index 2787f0e..1e391c1 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ CC=gcc CFILES=src/main.c \ src/json_types.c \ + src/printing.c \ src/storage.c all: clean json-parser diff --git a/src/main.c b/src/main.c index 03d9e7e..013ccf8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,4 @@ -#include - +#include "printing.h" #include "storage.h" int main(int argc, char *argv[]) @@ -26,30 +25,13 @@ int main(int argc, char *argv[]) arr_add_double(&a, 0.5); arr_add_double(&a, 55.789); - for (int i = 0; i < a.size; ++i) - { - ArrayLink al = array_get(&a, i); - switch (al.type) - { - case T_STR: - printf("%s\n", al.strv.str ? al.strv.str : ""); - break; - case T_INT: - printf("%d\n", al.integerv); - break; - case T_DOUBLE: - printf("%f\n", al.doublev); - break; - case T_BOOL: - printf("%s\n", al.boolv ? "true" : "false"); - break; - case T_NULL: - printf("null\n"); - break; - case T_ARR: - break; - } - } + Array b = init_array(2); + arr_add_int(&b, 9); + arr_add_int(&b, 8); + + arr_add_arr(&a, &b); + + arr_print(&a); destroy_array(&a); return 0; diff --git a/src/printing.c b/src/printing.c new file mode 100644 index 0000000..777595e --- /dev/null +++ b/src/printing.c @@ -0,0 +1,33 @@ +#include + +#include "storage.h" + +void arr_print(Array *a) +{ + int size = a->size; + for (int i = 0; i < size; ++i) + { + ArrayLink al = array_get(a, i); + switch (al.type) + { + case T_STR: + printf("%s\n", al.strv.str ? al.strv.str : ""); + break; + case T_INT: + printf("%d\n", al.integerv); + break; + case T_DOUBLE: + printf("%f\n", al.doublev); + break; + case T_BOOL: + printf("%s\n", al.boolv ? "true" : "false"); + break; + case T_NULL: + printf("null\n"); + break; + case T_ARR: + arr_print(al.arrayv); + break; + } + } +} diff --git a/src/printing.h b/src/printing.h new file mode 100644 index 0000000..ce5a59a --- /dev/null +++ b/src/printing.h @@ -0,0 +1,8 @@ +#ifndef PRINTING_H +#define PRINTING_H + +#include "storage.h" + +void arr_print(Array *a); + +#endif // !PRINTING_H diff --git a/src/storage.c b/src/storage.c index 69642fd..05c5fce 100644 --- a/src/storage.c +++ b/src/storage.c @@ -4,9 +4,18 @@ Array init_array(int size) { - return (Array){ .size = size, .insert_idx = 0 }; + char *indexes_types = calloc(size, sizeof(char)); + if (!indexes_types) + { + size = 0; + } + return ( + Array){ .size = size, .insert_idx = 0, .indexes_types = indexes_types }; } +/******************************************************************************* +** ADDS ** +*******************************************************************************/ void arr_add_str(Array *a, String value) { if (!a) @@ -181,7 +190,7 @@ void arr_add_null(Array *a) a->indexes_types[a->insert_idx++] = T_NULL; } -void arr_add_array(Array *a, Array *value) +void arr_add_arr(Array *a, Array *value) { if (!a || !value) { @@ -327,7 +336,7 @@ ArrayLink array_get(Array *a, int index) void destroy_array(Array *a) { - // free(a->indexes_types); + free(a->indexes_types); StrArrLink *sl = a->strings_head; while (sl) @@ -374,6 +383,7 @@ void destroy_array(Array *a) { ArrArrLink *tmp = al; al = al->next; + destroy_array(tmp->value); free(tmp); } } diff --git a/src/storage.h b/src/storage.h index d230898..3b4382a 100644 --- a/src/storage.h +++ b/src/storage.h @@ -81,7 +81,7 @@ struct array_linked_list // Stores for each index the type of the value, allowing us to not have to // search inside all linked lists - char indexes_types[10]; + char *indexes_types; // Pointers to the head of each linked list StrArrLink *strings_head; @@ -100,16 +100,20 @@ struct array_linked_list ArrArrLink *arrays_tail; }; +/** +** \brief The size of the returned array is 0, it means that an error occured +** when allocating the indexes_types array +*/ Array init_array(int size); void arr_add_str(Array *a, String value); -void arr_add_int(Array *ial, int value); -void arr_add_double(Array *ial, double value); -void arr_add_bool(Array *ial, char value); -void arr_add_null(Array *ial); -void arr_add_arr(Array *ial, Array *value); +void arr_add_int(Array *a, int value); +void arr_add_double(Array *a, double value); +void arr_add_bool(Array *a, char value); +void arr_add_null(Array *a); +void arr_add_arr(Array *a, Array *value); ArrayLink array_get(Array *a, int index); -void destroy_array(Array *ial); +void destroy_array(Array *a); #endif // !STORAGE_H