summaryrefslogtreecommitdiff
path: root/hax_table.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--hax_table.c40
1 files changed, 24 insertions, 16 deletions
diff --git a/hax_table.c b/hax_table.c
index 0b89a35..c2c82a3 100644
--- a/hax_table.c
+++ b/hax_table.c
@@ -102,7 +102,7 @@ static inline size_t search(struct table tbl, struct string name, char *exists)
}
}
-int set_table_index(struct table *tbl, struct string name, void *ptr) {
+int set_table_index(struct table *tbl, struct string name, union table_ptr ptr) {
char exists;
size_t index = search(*tbl, name, &exists);
@@ -132,11 +132,10 @@ int set_table_index(struct table *tbl, struct string name, void *ptr) {
return 0;
}
-void * get_table_index(struct table tbl, struct string name) {
- char exists;
- size_t index = search(tbl, name, &exists);
- if (!exists)
- return 0;
+union table_ptr get_table_index(struct table tbl, struct string name, char *exists) {
+ size_t index = search(tbl, name, exists);
+ if (!*exists)
+ return (union table_ptr){0};
return tbl.array[index].ptr;
}
@@ -147,14 +146,13 @@ char has_table_index(struct table tbl, struct string name) {
return exists;
}
-void * remove_table_index(struct table *tbl, struct string name) {
- char exists;
- size_t index = search(*tbl, name, &exists);
+union table_ptr get_and_remove_table_index(struct table *tbl, struct string name, char *exists) {
+ size_t index = search(*tbl, name, exists);
- if (!exists)
- return 0;
+ if (!*exists)
+ return (union table_ptr){0};
- void *ptr = tbl->array[index].ptr;
+ union table_ptr ptr = tbl->array[index].ptr;
free(tbl->array[index].name.data);
memmove(&(tbl->array[index]), &(tbl->array[index+1]), (tbl->len - index - 1) * sizeof(*(tbl->array)));
@@ -168,6 +166,12 @@ void * remove_table_index(struct table *tbl, struct string name) {
return ptr;
}
+void remove_table_index(struct table *tbl, struct string name) {
+ char exists;
+ get_and_remove_table_index(tbl, name, &exists);
+ return;
+}
+
void clear_table(struct table *tbl) {
for (size_t i = 0; i < tbl->len; i++)
free(tbl->array[i].name.data);
@@ -181,10 +185,14 @@ size_t get_table_offset(struct table tbl, struct string name, char *exists) {
}
// TODO: Proper lookup
-void * get_table_prefix(struct table tbl, struct string name) {
- for (size_t i = 0; i < tbl.len; i++)
- if (tbl.array[i].name.len <= name.len && memcmp(tbl.array[i].name.data, name.data, tbl.array[i].name.len) == 0)
+union table_ptr get_table_prefix(struct table tbl, struct string name, char *exists) {
+ for (size_t i = 0; i < tbl.len; i++) {
+ if (tbl.array[i].name.len <= name.len && memcmp(tbl.array[i].name.data, name.data, tbl.array[i].name.len) == 0) {
+ *exists = 1;
return tbl.array[i].ptr;
+ }
+ }
- return 0;
+ *exists = 0;
+ return (union table_ptr){0};
}