diff options
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..4cd26fa --- /dev/null +++ b/src/parser.c @@ -0,0 +1,44 @@ +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +#include "include/parser.h" + +int blen; + +static void addbyte(struct byte *bytes, int *pos, enum btype type, char *value); + +struct byte *parse(uint8_t *buf) +{ + int len = 2; + struct byte *bytes = calloc(len, sizeof(struct byte)); + for (int i = 0, j = 0; buf[i]; ++i) { + if (j >= len) + len *= 2; + bytes = realloc(bytes, len * sizeof(struct byte)); + switch (buf[i]) { + case '\xb': + addbyte(bytes, &j, BEGIN, NULL); + break; + case '\xe': + addbyte(bytes, &j, END, NULL); + break; + } + blen = j; + } + return bytes; +} + +static void addbyte(struct byte *bytes, int *pos, enum btype type, char *value) +{ + bytes[*pos].type = type; + + if (value != NULL) { + bytes[*pos].value = calloc(strlen(value) + 1, sizeof(char)); + strcpy(bytes[*pos].value, value); + } + else + bytes[*pos].value = NULL; + + (*pos)++; +} |