diff options
author | Freya Murphy <freya@freyacat.org> | 2024-09-09 12:41:49 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-09-09 12:41:49 -0400 |
commit | 2ed275821676a0d5baea6c7fd843d71c72c2342c (patch) | |
tree | 480297f28e5c42d02a47b3b94027a7abe507d010 /masm/strtbl.c | |
download | mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.tar.gz mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.tar.bz2 mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.zip |
initial mips32 (r2000ish mips32r6) assembler
Diffstat (limited to 'masm/strtbl.c')
-rw-r--r-- | masm/strtbl.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/masm/strtbl.c b/masm/strtbl.c new file mode 100644 index 0000000..b01bb92 --- /dev/null +++ b/masm/strtbl.c @@ -0,0 +1,49 @@ +#include <merror.h> +#include <string.h> +#include <stdlib.h> + +#include "asm.h" + +int strtbl_get_str(struct str_table *str_tbl, const char *str, size_t *res) +{ + for (size_t i = 0; i < str_tbl->size; i ++) { + if (strcmp(str_tbl->ptr + i, str) == 0) { + if (res != NULL) + *res = i; + return M_SUCCESS; + } + } + + return M_ERROR; +} + +int strtbl_write_str(struct str_table *str_tbl, const char *str, size_t *res) +{ + if (strtbl_get_str(str_tbl, str, res) == M_SUCCESS) + return M_SUCCESS; + + size_t len = strlen(str); + char *new = realloc(str_tbl->ptr, str_tbl->size + len + 1); + if (new == NULL) + return M_ERROR; + str_tbl->ptr = new; + memcpy(str_tbl->ptr + str_tbl->size, str, len + 1); + + if (res != NULL) + *res = str_tbl->size; + + str_tbl->size += len + 1; + return M_SUCCESS; +} + +void strtbl_init(struct str_table *str_tbl) +{ + str_tbl->size = 1; + str_tbl->ptr = malloc(1); + *str_tbl->ptr = '\0'; +} + +void strtbl_free(struct str_table *str_tbl) +{ + free(str_tbl->ptr); +} |