more changes

This commit is contained in:
Julian Daube 2020-12-02 15:35:20 +01:00
parent 6b020756af
commit ab099339b2
4 changed files with 58 additions and 15 deletions

View File

@ -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) { 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; size_t index = hash % table->len;
if (table->data[index].hash == 0) { if (table->data[index].hash == 0) {

50
main.c
View File

@ -126,13 +126,14 @@ char fgetc_count(FILE *file) {
} }
int add_char(char ** str, char c) { int add_char(char ** str, char c) {
if (str == NULL || *str == NULL) { if (str == NULL) {
return 0; return 0;
} }
if (*str != NULL) {
int len = strlen(*str); int len = strlen(*str);
char *temp = realloc(*str, len + 1); char *temp = realloc(*str, len + 2);
if (temp == NULL) { if (temp == NULL) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -140,27 +141,63 @@ int add_char(char ** str, char c) {
*str = temp; *str = temp;
(*str)[len-1] = c; (*str)[len] = c;
(*str)[len] = 0; (*str)[len+1] = 0;
return 0; return 0;
} }
*str = malloc(2);
if (*str == NULL) {
errno = ENOMEM;
return -1;
}
(*str)[0] = c;
(*str)[1] = '\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) { void eval_macro(struct filelist * last, FILE * file) {
char * name = NULL; char * name = NULL;
strhash_t hash = 0; strhash_t hash = 0;
hashtable_iterator_t macro; hashtable_iterator_t macro = hashtable_end(last->macros);
char current = 0; char current = 0;
// read macro name // 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); hash = strhash_add(hash, current);
add_char(&name, current);
// try to find macro in table // try to find macro in table
macro = hashtable_get_hash(last->macros, hash); 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)) { if (!good(file)) {
return; return;
} }
@ -209,7 +246,6 @@ struct option long_opts[] = {
{}, // terminator {}, // terminator
}; };
void help(int argc, char ** args) { void help(int argc, char ** args) {
printf("%s [OPTIONS] file1 file2...\n" printf("%s [OPTIONS] file1 file2...\n"
"--output\n" "--output\n"

View File

@ -44,9 +44,6 @@ protected:
std::set<Path::Path> includes; std::set<Path::Path> includes;
}; };
#include <functional>
template <typename iterator> template <typename iterator>
std::string readTill(iterator &start, const iterator &end, std::function<bool(const iterator&)> limiter) { std::string readTill(iterator &start, const iterator &end, std::function<bool(const iterator&)> limiter) {
iterator current = start; iterator current = start;

View File

@ -11,7 +11,6 @@
struct hashtable table = {}; struct hashtable table = {};
struct entry nEntry; struct entry nEntry;
void testresize() { void testresize() {
init("resize"); init("resize");
int err = hashtable_resize(&table, 20); int err = hashtable_resize(&table, 20);
@ -52,6 +51,13 @@ void* test_copy(void * data) {
void testget() { void testget() {
init("get"); 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"); hashtable_iterator_t elem = hashtable_get(&table, "hi");
if (elem == hashtable_end(&table)) { if (elem == hashtable_end(&table)) {