From 168d8e1548af1564b1b788eedddbebae2a707841 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 30 Dec 2021 14:28:02 -0600 Subject: [PATCH] Created a list structure for strings. Should work for splitting lines of text up just before they're tokenized. --- includes/list.h | 20 ++++++++++++++++++++ src/list.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 16 +++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 includes/list.h create mode 100644 src/list.c diff --git a/includes/list.h b/includes/list.h new file mode 100644 index 0000000..3db4453 --- /dev/null +++ b/includes/list.h @@ -0,0 +1,20 @@ +#ifndef LIST_H +#define LIST_H + +#include +#include + +#define LISTDEFAULTSIZE 4 + +typedef struct { + char** root; + int size; + int capacity; +} List; + +List* CreateList(void); +int AddListItem(char *, List *); +void PrintList(List*); +void DestroyList(List*); + +#endif \ No newline at end of file diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..76ce53a --- /dev/null +++ b/src/list.c @@ -0,0 +1,43 @@ +#include "../includes/list.h" + +List* CreateList() { + List *new = malloc(sizeof(List)); + new->root = malloc(sizeof(char*) * LISTDEFAULTSIZE); + new->size = 0; + new->capacity = LISTDEFAULTSIZE; + + return new; +} + +int AddListItem(char *value, List* list) { + + if (list->capacity < list->size + 1) { + list->root = realloc(list->root, sizeof(char*) * list->capacity * 2); + list->capacity = list->capacity * 2; + } + + list->root[list->size] = value; + list->size++; + + return 0; +} + +void PrintList(List* list) { + List *current = list; + + printf("Size: %d; Capacity: %d\n", list->size, list->capacity); + + for(int i = 0; i < list->size; i++) { + printf("%d: %s\n", i, list->root[i]); + } +} + +void DestroyList(List* list) { + + for(int i = 0; i < list->size; i++) { + free(list->root[i]); + } + + free(list->root); + free(list); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index f68bbc2..1446042 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include #include #include +#include "../includes/list.h" typedef struct opcode { char op[255]; @@ -24,7 +25,20 @@ int main(int argc, char* args[]) { // print_file(args[i]); // } - print_file(args[1]); + //print_file(args[1]); + char str[16]; + List *list = CreateList(); + + for (int i = 0; i < 64; i++) { + sprintf(str, "%d", i); + AddListItem(str, list); + + if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); + } + + PrintList(list); + + free(list); } void print_file(char* file_path) {