summaryrefslogtreecommitdiff
path: root/masm/strtab.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-09-11 12:06:09 -0400
committerFreya Murphy <freya@freyacat.org>2024-09-11 12:06:09 -0400
commite3d2e31377030e84066bed4bbc04bf6c56206305 (patch)
treed5e7428212606a0f64d5b2e628b3f507948b8e2f /masm/strtab.c
parentjoe (diff)
downloadmips-e3d2e31377030e84066bed4bbc04bf6c56206305.tar.gz
mips-e3d2e31377030e84066bed4bbc04bf6c56206305.tar.bz2
mips-e3d2e31377030e84066bed4bbc04bf6c56206305.zip
refactor
Diffstat (limited to '')
-rw-r--r--masm/strtab.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/masm/strtab.c b/masm/strtab.c
new file mode 100644
index 0000000..57d3d0e
--- /dev/null
+++ b/masm/strtab.c
@@ -0,0 +1,54 @@
+#include <merror.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "asm.h"
+
+int strtab_get_str(struct str_table *strtab, const char *str, size_t *res)
+{
+ for (size_t i = 0; i < strtab->size; i ++) {
+ if (strcmp(strtab->ptr + i, str) == 0) {
+ if (res != NULL)
+ *res = i;
+ return M_SUCCESS;
+ }
+ }
+
+ return M_ERROR;
+}
+
+int strtab_write_str(struct str_table *strtab, const char *str, size_t *res)
+{
+ if (strtab_get_str(strtab, str, res) == M_SUCCESS)
+ return M_SUCCESS;
+
+ size_t len = strlen(str);
+ char *new = realloc(strtab->ptr, strtab->size + len + 1);
+ if (new == NULL)
+ return M_ERROR;
+ strtab->ptr = new;
+ memcpy(strtab->ptr + strtab->size, str, len + 1);
+
+ if (res != NULL)
+ *res = strtab->size;
+
+ strtab->size += len + 1;
+ return M_SUCCESS;
+}
+
+int strtab_init(struct str_table *strtab)
+{
+ strtab->size = 1;
+ strtab->ptr = malloc(1);
+ if (strtab->ptr == NULL) {
+ ERROR("cannot alloc");
+ return M_ERROR;
+ }
+ *strtab->ptr = '\0';
+ return M_SUCCESS;
+}
+
+void strtab_free(struct str_table *strtab)
+{
+ free(strtab->ptr);
+}