aboutsummaryrefslogtreecommitdiff
path: root/Converter/src/array.c
blob: 442a4b5a8f6c91ff39fa09b65039aa5bcc34fae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "../includes/array.h"
#include <malloc.h>

void array_init(array_t *arr)
{
  arr = malloc(sizeof(*arr));
  arr->sz_used = 0;
  arr->sz_allocated = 8;
  arr->data = malloc(arr->sz_allocated);
}

void array_append(array_t *arr, void *data)
{
  if (arr->sz_allocated == arr->sz_used)
  {
    arr->sz_allocated *= 2;
    arr->data = realloc(arr->data, arr->sz_allocated);
  }
  arr->data[arr->sz_used] = data;
  ++arr->sz_used;
}

void *array_read(array_t *arr, int index)
{
  if (index > arr->sz_used)
    return NULL;
  return arr->data[index];
}