Skip to content

Commit

Permalink
Add a printing function + free arrays that are inside another array
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeldit committed Oct 25, 2024
1 parent 011ec9b commit f089481
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 36 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
CC=gcc
CFILES=src/main.c \
src/json_types.c \
src/printing.c \
src/storage.c

all: clean json-parser
Expand Down
34 changes: 8 additions & 26 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <stdio.h>

#include "printing.h"
#include "storage.h"

int main(int argc, char *argv[])
Expand All @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/printing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>

#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;
}
}
}
8 changes: 8 additions & 0 deletions src/printing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef PRINTING_H
#define PRINTING_H

#include "storage.h"

void arr_print(Array *a);

#endif // !PRINTING_H
16 changes: 13 additions & 3 deletions src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -374,6 +383,7 @@ void destroy_array(Array *a)
{
ArrArrLink *tmp = al;
al = al->next;
destroy_array(tmp->value);
free(tmp);
}
}
18 changes: 11 additions & 7 deletions src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

0 comments on commit f089481

Please sign in to comment.