summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/parser.h22
-rw-r--r--src/main.c11
-rw-r--r--src/parser.c44
3 files changed, 72 insertions, 5 deletions
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 <stdint.h>
#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 <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)++;
+}