From 5730d6c04258e192195bfbbbed76d68fd78ed458 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 12 Jan 2022 20:26:08 +0800 Subject: Implement a simple hash table. It is a very basic and simple hash table. It is so simple that I hesitate to call it a hash table. Anyways, I think it suffices for my purposes here. * Makefile.am: Add necessary files. * grammar.c (new_tnt_string): Formatting. * ht.c (new_ht): Constructor (destroy_ht): Destructor (ht_expand): Rehash (ht_insert, ht_delete, ht_find): Main functions. * list.c (add_to_list, list_assure_size): Modify the use of realloc. * test/check_ht.c: Ensure this is working correctly. * util.c (read_entire_file): Modify the use of realloc. --- src/util.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index 1af8e5e..fb18e23 100644 --- a/src/util.c +++ b/src/util.c @@ -7,7 +7,7 @@ If errors occur, return non-zero; otherwise the return value is zero. - + Note that *str should be already allocated. Note that the length of the string str is actually 1+length, and @@ -29,16 +29,18 @@ read_entire_file(const char *file_name, char **str, NUM *len) fseek(file, 0, SEEK_END); file_size = ftell(file); fseek(file, 0, SEEK_SET); - + *len = 1+file_size; - *str = realloc(*str, 1+file_size); - - if (*str == NULL) { + char *newstr = realloc(*str, 1+file_size); + + if (newstr == NULL) { fleprintf0("Cannot realloc\n"); return 1; } + *str = newstr; + fread(*str, sizeof(char), file_size, file); fclose(file); -- cgit v1.2.3-18-g5258