summaryrefslogtreecommitdiff
path: root/src/str.c
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-01-04 11:51:58 +0800
committerJSDurand <mmemmew@gmail.com>2022-01-04 11:51:58 +0800
commit55dc897da6e81f2a26cfc7e66ac942824773498b (patch)
treefce0d7d57832907c991d551833bf5eecde947dd2 /src/str.c
parent53b8b6ffab5a968db75e9babddf4e2dbb2c688a3 (diff)
temporary commit
Now we can read grammars from a file. But we need to check if it works for reading strings still.
Diffstat (limited to 'src/str.c')
-rw-r--r--src/str.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/str.c b/src/str.c
new file mode 100644
index 0000000..46dc4a8
--- /dev/null
+++ b/src/str.c
@@ -0,0 +1,86 @@
+#include "str.h"
+#include "str_partial.h"
+#include <string.h>
+
+H_ATTR
+str_info
+_info_getter(str *s, UNUM n)
+{
+ return (str_info) { (NUM) *(s->data+n), 1 };
+}
+
+/* H_ATTR
+ * NUM
+ * _lenner(str *s)
+ * {
+ * return s->size;
+ * } */
+
+str *
+new_str(char *s, NUM size)
+{
+ str *str_pointer = MYALLOC(str, 1);
+
+ str_pointer->size = size;
+ str_pointer->data = s;
+ str_pointer->getter = _info_getter;
+ /* str_pointer->lenner = _lenner; */
+
+ return str_pointer;
+}
+
+void
+destroy_str(str *s, int flag)
+{
+ if (flag) free(s->data);
+
+ s->size = 0;
+
+ free(s);
+}
+
+H_ATTR
+str_info
+get_info(str *s, UNUM n)
+{
+ return s->getter(s, n);
+}
+
+H_ATTR
+NUM
+str_length(str *s)
+{
+ return s->size;
+}
+
+void
+change_str_content(str *s, char *str, NUM size)
+{
+ s->data = str;
+ s->size = size;
+}
+
+unsigned char
+copy_str(str *dest, str *source)
+{
+ if (str_length(dest) < str_length(source))
+ return 1;
+
+ dest->size = source->size;
+
+ memcpy(dest->data, source->data, source->size);
+
+ return 0;
+}
+
+char *
+get_data(str *s)
+{
+ return s->data;
+}
+
+void
+str_set_length(str *s, UNUM size)
+{
+ s->size = size;
+}