summaryrefslogtreecommitdiff
path: root/src/parser.c
blob: 4cd26fa278560c32689ce03d3fe6c4ea1fd6febe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)++;
}