From 73efe8f2d78c780ddf8be80909572807fab71ef7 Mon Sep 17 00:00:00 2001 From: bpc2003 Date: Thu, 6 Mar 2025 15:24:31 -0500 Subject: Got started on parser --- src/include/parser.h | 22 ++++++++++++++++++++++ src/main.c | 11 ++++++----- src/parser.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/include/parser.h create mode 100644 src/parser.c (limited to 'src') diff --git a/src/include/parser.h b/src/include/parser.h new file mode 100644 index 0000000..f4a6c77 --- /dev/null +++ b/src/include/parser.h @@ -0,0 +1,22 @@ +#ifndef PARSER_H +#define PARSER_H + +extern int blen; + +enum btype { + BEGIN = 1, + END, OBJ, + + COLON, + + IDENTIFIER, VALUE +}; + +struct byte { + enum btype type; + char *value; +}; + +struct byte *parse(unsigned char *buf); + +#endif diff --git a/src/main.c b/src/main.c index 8e4bab4..64c5de2 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include "include/fileops.h" +#include "include/parser.h" int main(int argc, char **argv) { @@ -13,12 +14,12 @@ int main(int argc, char **argv) uint8_t *buf = readdb(filename); if (buf == NULL) exit(0); - - printf("[ "); - for (int i = 0; buf[i]; ++i) { - printf("%d ", buf[i]); + struct byte *bytes = parse(buf); + for (int i = 0; i < blen; ++i) { + printf("%d\n", bytes[i].type); } - printf("]\n"); + + free(bytes); free(buf); exit(0); } 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 +#include +#include + +#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)++; +} -- cgit v1.2.3