aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordx <aryadevchavali1@gmail.com>2020-05-20 16:17:37 +0100
committerdx <aryadevchavali1@gmail.com>2020-05-20 16:17:37 +0100
commit505a5ac2a4c57d5daa03046366ac38a2e3182b94 (patch)
treed960a668cbc1ce7005495a2ca94469915014e426
parent1a70a39f9d23a2b4ef29e6c39034eb408ddff273 (diff)
downloadmdhtml-505a5ac2a4c57d5daa03046366ac38a2e3182b94.tar.gz
mdhtml-505a5ac2a4c57d5daa03046366ac38a2e3182b94.tar.bz2
mdhtml-505a5ac2a4c57d5daa03046366ac38a2e3182b94.zip
+dynamic array implementation
Very basic, and only for dynamic content
-rw-r--r--Converter/includes/array.h17
-rw-r--r--Converter/src/array.c28
2 files changed, 45 insertions, 0 deletions
diff --git a/Converter/includes/array.h b/Converter/includes/array.h
new file mode 100644
index 0000000..003b1fd
--- /dev/null
+++ b/Converter/includes/array.h
@@ -0,0 +1,17 @@
+#ifndef __ARRAY_H_
+#define __ARRAY_H_
+
+#include <stdlib.h>
+
+typedef struct
+{
+ void **data;
+ size_t sz_allocated;
+ size_t sz_used;
+} array_t;
+
+void array_init(array_t *arr);
+void array_append(array_t *arr, void *item);
+void *array_read(array_t *arr, int index);
+
+#endif // __ARRAY_H_
diff --git a/Converter/src/array.c b/Converter/src/array.c
new file mode 100644
index 0000000..442a4b5
--- /dev/null
+++ b/Converter/src/array.c
@@ -0,0 +1,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];
+}