diff --git a/hashtable.c b/hashtable.c index ceab16d..367a036 100644 --- a/hashtable.c +++ b/hashtable.c @@ -55,6 +55,10 @@ hashtable_iterator_t hashtable_next(struct hashtable * table, hashtable_iterator } hashtable_iterator_t hashtable_get_hash(struct hashtable * table, entry_hash_t hash) { + if (!table->len) { + return hashtable_end(table); + } + size_t index = hash % table->len; if (table->data[index].hash == 0) { diff --git a/main.c b/main.c index 9e3d29d..5a8fe17 100644 --- a/main.c +++ b/main.c @@ -126,41 +126,78 @@ char fgetc_count(FILE *file) { } int add_char(char ** str, char c) { - if (str == NULL || *str == NULL) { + if (str == NULL) { return 0; } - int len = strlen(*str); + if (*str != NULL) { + int len = strlen(*str); - char *temp = realloc(*str, len + 1); - if (temp == NULL) { + char *temp = realloc(*str, len + 2); + if (temp == NULL) { + errno = ENOMEM; + return -1; + } + + *str = temp; + + (*str)[len] = c; + (*str)[len+1] = 0; + return 0; + } + + *str = malloc(2); + if (*str == NULL) { errno = ENOMEM; return -1; } - *str = temp; + (*str)[0] = c; + (*str)[1] = '\0'; - (*str)[len-1] = c; - (*str)[len] = 0; return 0; } +int macro_limiter(char c) { + switch(c) { + case '\\': + // next macro + return 1; + case '{': + // arguments + return 2; + case '[': + // optional arguments + return 3; + } + + return 0; +} void eval_macro(struct filelist * last, FILE * file) { char * name = NULL; strhash_t hash = 0; - hashtable_iterator_t macro; + hashtable_iterator_t macro = hashtable_end(last->macros); char current = 0; // read macro name - while(good(file) && current != '\\' && macro == hashtable_end(last->macros)) { + while(good(file) && !macro_limiter(current) && macro == hashtable_end(last->macros)) { + current = fgetc_count(file); + hash = strhash_add(hash, current); + add_char(&name, current); + // try to find macro in table macro = hashtable_get_hash(last->macros, hash); } + if (macro == hashtable_end(last->macros)) { + // no macro with that name found + printf("%d:%d:macro not found: %s\n", line, pos, name); + } + if (!good(file)) { return; } @@ -209,7 +246,6 @@ struct option long_opts[] = { {}, // terminator }; - void help(int argc, char ** args) { printf("%s [OPTIONS] file1 file2...\n" "--output\n" diff --git a/main_old.cpp b/main_old.cpp index cd57bb3..60d785b 100644 --- a/main_old.cpp +++ b/main_old.cpp @@ -44,9 +44,6 @@ protected: std::set includes; }; -#include - - template std::string readTill(iterator &start, const iterator &end, std::function limiter) { iterator current = start; @@ -137,7 +134,7 @@ InputExtractor::List InputExtractor::Include(Path::Path path) { } cout << endl; - + // add to include list includes.insert(path); diff --git a/test_hashtable.c b/test_hashtable.c index 35d8b5c..0dbbe91 100644 --- a/test_hashtable.c +++ b/test_hashtable.c @@ -11,7 +11,6 @@ struct hashtable table = {}; struct entry nEntry; - void testresize() { init("resize"); int err = hashtable_resize(&table, 20); @@ -52,6 +51,13 @@ void* test_copy(void * data) { void testget() { init("get"); + hashtable_clear(&table); + + if (hashtable_get(&table, "hi") != hashtable_end(&table)) { + fail("found entry in empty table"); + } + + hashtable_add(&table, nEntry); hashtable_iterator_t elem = hashtable_get(&table, "hi"); if (elem == hashtable_end(&table)) {