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

56
main.c
View File

@ -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"

View File

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

View File

@ -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)) {