From 55dc897da6e81f2a26cfc7e66ac942824773498b Mon Sep 17 00:00:00 2001 From: JSDurand Date: Tue, 4 Jan 2022 11:51:58 +0800 Subject: temporary commit Now we can read grammars from a file. But we need to check if it works for reading strings still. --- src/list.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'src/list.c') diff --git a/src/list.c b/src/list.c index d823100..143c693 100644 --- a/src/list.c +++ b/src/list.c @@ -5,8 +5,8 @@ struct List_s { void **array; /* the array of elements */ - NUM len; /* the length of the array */ - NUM capacity; /* the number of pre-allocated bytes + NUM len; /* the length of the array */ + NUM capacity; /* the number of pre-allocated bytes for the array */ }; @@ -183,14 +183,14 @@ list_assure_size(List *ls, NUM size) if (array == NULL) return 1; - for (NUM i = 0; i < ls->capacity; i++) + for (NUM i = 0; i < ls->len; i++) *(array+i) = *(ls->array+i); ls->array = realloc(ls->array, sizeof(void*) * size); if (ls->array == NULL) return 1; - for (NUM i = 0; i < ls->capacity; i++) + for (NUM i = 0; i < ls->len; i++) *(ls->array+i) = *(array+i); free(array); @@ -206,6 +206,8 @@ list_assure_size(List *ls, NUM size) unsigned char list_set_length(List *ls, NUM len) { + list_assure_size(ls, len); + if (ls->len >= len) return 0; NUM *array = MYALLOC(NUM, len - ls->len); @@ -240,6 +242,17 @@ list_to_array(List *ls, NUM element_bytes, NUM *num) return array; } +List * +array_to_list(void **array, NUM size) +{ + List *ls = MYALLOC(List, 1); + + ls->array = array; + ls->len = ls->capacity = size; + + return ls; +} + void destroy_list(List *ls, unsigned char all_free_p) { @@ -250,8 +263,29 @@ destroy_list(List *ls, unsigned char all_free_p) if (all_free_p == 2) free(*(ls->array)); + ls->len = (ls->capacity = 0); + free(ls->array); free(ls); +} - ls->len = (ls->capacity = 0); +void +destroy_list_free_all(void *ls) +{ + List *list = (List *) ls; + destroy_list(list, 1); +} + +void +destroy_list_free_first(void *ls) +{ + List *list = (List *) ls; + destroy_list(list, 2); +} + +void +destroy_list_no_free(void *ls) +{ + List *list = (List *) ls; + destroy_list(list, 0); } -- cgit v1.2.3-18-g5258