summaryrefslogtreecommitdiff
path: root/src/include/xml/decode.c
blob: f072ae7691fec9daef0298fa5c699c7db5642bf7 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdlib.h>
#include <string.h>

#include "xml.h"

static char *gettag(char *xml, int *pos);

map_t *decode(char *xml, int *len)
{
  map_t *decoded = calloc(1, sizeof(map_t));
  *len = 1;
  
  int closed = 0;
  char *title = NULL, *tmp;
  for (int i = 0, j = 0; i < strlen(xml); ++i) {
    switch (xml[i]) {
      case '<':
        ++i;
        if (xml[i] == '/') {
          ++i;
          if (!strcmp(title, (tmp = gettag(xml, &i))))
            closed = 1;
          else {
            free(tmp);
            return NULL;
          }
          free(tmp);
        } else {
          if (title != NULL)
            free(title);
          closed = 0;
          title = gettag(xml, &i);
        }
        printf("%s\n", title);
        break;
      case '>':
        // TODO: make this recursive
        if (xml[i + 1] == '<')
          printf("nested tag\n");
        else
          ;
        break;
    }
  }
  free(title);
  return decoded;
}

static char *gettag(char *xml, int *pos)
{
  int len, i;
  for (i = *pos, len = 0; xml[i] != '>'; ++i, ++len) ;
  char *title = calloc(len + 1, sizeof(char));
  strncpy(title, xml + *pos, len);
  *pos += i - 2;
  return title;
}