summaryrefslogtreecommitdiff
path: root/source/kit/xml.c
blob: ed11b1833d8410d2b0ac2e945e54ab225151edc5 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "xml.h"

#include "input_buffer.h"
#include <assert.h>

typedef struct {
  ib_t              last;
  kit_str_builder_t text;
  kit_da_xml_t      tags;
} kit_xml_intermediate_t;

static kit_xml_intermediate_t kit_xml_parse_buf_(
    ib_t begin, kit_allocator_t *alloc) {
  kit_xml_intermediate_t res;
  memset(&res, 0, sizeof res);

  ib_t last, spaces;
  memset(&last, 0, sizeof last);
  memset(&spaces, 0, sizeof spaces);

  ib_t tag_text = ib_until(begin, SZ("<"));
  last          = ib_copy(tag_text);

  DA_INIT(res.tags, 0, alloc);

  for (;;) {
    ib_t tagend_open = ib_exact(last, SZ("</"));
    ib_destroy(tagend_open);
    if (tagend_open.status == KIT_OK)
      break;

    ib_t tag_open = ib_exact(last, SZ("<"));

    if (tag_open.status != KIT_OK) {
      ib_destroy(tag_open);
      break;
    }

    xml_t tag;
    memset(&tag, 0, sizeof tag);

    spaces        = ib_any(tag_open, SZ(" \t\r\n"));
    ib_t tag_name = ib_none(spaces, SZ(" \t\r\n/>"));
    ib_destroy(spaces);

    DA_INIT(tag.properties, 0, alloc);

    ib_destroy(last);
    last = ib_copy(tag_name);

    for (;;) {
      spaces        = ib_any(last, SZ(" \t\r\n"));
      ib_t property = ib_none(spaces, SZ(" \t\r\n=/>"));
      ib_destroy(spaces);

      if (property.status != KIT_OK || property.data.size == 0) {
        ib_destroy(property);
        break;
      }

      spaces      = ib_any(property, SZ(" \t\r\n"));
      ib_t equals = ib_exact(spaces, SZ("="));
      ib_destroy(spaces);
      spaces          = ib_any(equals, SZ(" \t\r\n"));
      ib_t value_open = ib_exact(spaces, SZ("\""));
      ib_destroy(spaces);
      ib_t value_text  = ib_until(value_open, SZ("\""));
      ib_t value_close = ib_exact(value_text, SZ("\""));

      ib_destroy(last);
      last = value_close;

      if (last.status == KIT_OK) {
        i64 n = tag.properties.size;
        DA_RESIZE(tag.properties, n + 1);

        if (tag.properties.size != n + 1) {
          last.status = KIT_ERROR_BAD_ALLOC;
          DA_DESTROY(tag.properties);
        } else {
          //  move
          tag.properties.values[n].name = property.data;
          memset(&property.data, 0, sizeof property.data);

          //  move
          tag.properties.values[n].value = value_text.data;
          memset(&value_text.data, 0, sizeof value_text.data);
        }
      }

      ib_destroy(property);
      ib_destroy(equals);
      ib_destroy(value_open);
      ib_destroy(value_text);
    }

    spaces               = ib_any(last, SZ(" \t\r\n"));
    ib_t tag_close       = ib_exact(spaces, SZ(">"));
    ib_t tag_close_empty = ib_exact(spaces, SZ("/>"));
    ib_destroy(spaces);

    if (tag_close.status == KIT_OK) {
      kit_xml_intermediate_t im = kit_xml_parse_buf_(tag_close,
                                                     alloc);
      tag.text                  = im.text;
      tag.children              = im.tags;

      tagend_open = ib_exact(im.last, SZ("</"));
      ib_destroy(im.last);
      spaces           = ib_any(tagend_open, SZ(" \t\r\n"));
      ib_t tagend_name = ib_exact(spaces, WRAP_STR(tag_name.data));
      ib_destroy(spaces);
      spaces            = ib_any(tagend_name, SZ(" \t\r\n"));
      ib_t tagend_close = ib_exact(spaces, SZ(">"));
      ib_destroy(spaces);
      ib_destroy(tagend_open);
      ib_destroy(tagend_name);

      ib_destroy(last);
      last = tagend_close;

    } else if (tag_close_empty.status == KIT_OK) {
      ib_destroy(last);
      last = ib_copy(tag_close_empty);

      DA_INIT(tag.text, 0, alloc);
      DA_INIT(tag.children, 0, alloc);
    } else
      last.status = KIT_ERROR_INTERNAL;

    ib_t tag_tail = ib_until(last, SZ("<"));

    ib_destroy(last);
    last = ib_copy(tag_tail);

    if (last.status == KIT_OK) {
      i64 n = res.tags.size;
      DA_RESIZE(res.tags, n + 1);

      if (res.tags.size != n + 1) {
        last.status = KIT_ERROR_BAD_ALLOC;
        xml_destroy(&tag);
      } else {
        //  move
        tag.tag = tag_name.data;
        memset(&tag_name.data, 0, sizeof tag_name.data);

        //  move
        tag.tail = tag_tail.data;
        memset(&tag_tail.data, 0, sizeof tag_tail.data);

        res.tags.values[n] = tag;
      }
    } else
      xml_destroy(&tag);

    ib_destroy(tag_open);
    ib_destroy(tag_name);
    ib_destroy(tag_close);
    ib_destroy(tag_close_empty);
    ib_destroy(tag_tail);
  }

  if (last.status != KIT_OK) {
    for (i64 i = 0; i < res.tags.size; i++)
      xml_destroy(res.tags.values + i);
    DA_DESTROY(res.text);
    DA_DESTROY(res.tags);
  } else {
    //  move
    res.text = tag_text.data;
    memset(&tag_text.data, 0, sizeof tag_text.data);
  }

  ib_destroy(tag_text);

  res.last = last;
  return res;
}

kit_xml_parse_result_t kit_xml_parse(kit_is_handle_t  is,
                                     kit_allocator_t *alloc) {
  ib_t                   ib = ib_wrap(is, alloc);
  kit_xml_intermediate_t im = kit_xml_parse_buf_(ib, alloc);
  ib_destroy(ib);

  kit_xml_parse_result_t res;
  memset(&res, 0, sizeof res);

  res.status = im.last.status;
  ib_destroy(im.last);

  if (res.status != KIT_OK)
    return res;

  if (im.text.size == 0 && im.tags.size == 1) {
    res.xml = im.tags.values[0];
    DA_DESTROY(im.text);
    DA_DESTROY(im.tags);
    return res;
  }

  DA_INIT(res.xml.tag, 0, alloc);
  DA_INIT(res.xml.tail, 0, alloc);
  DA_INIT(res.xml.properties, 0, alloc);

  res.xml.text     = im.text;
  res.xml.children = im.tags;

  return res;
}

kit_xml_print_result_t kit_xml_print(kit_xml_t       *xml,
                                     kit_allocator_t *alloc) {
  assert(xml != NULL);

  kit_xml_print_result_t result;
  memset(&result, 0, sizeof result);

  result.status = KIT_ERROR_NOT_IMPLEMENTED;
  return result;
}

void kit_xml_destroy(kit_xml_t *xml) {
  assert(xml != NULL);
  if (xml == NULL)
    return;

  for (i64 i = 0; i < xml->properties.size; i++) {
    DA_DESTROY(xml->properties.values[i].name);
    DA_DESTROY(xml->properties.values[i].value);
  }

  for (i64 i = 0; i < xml->children.size; i++)
    kit_xml_destroy(xml->children.values + i);

  DA_DESTROY(xml->tag);
  DA_DESTROY(xml->text);
  DA_DESTROY(xml->tail);

  DA_DESTROY(xml->properties);
  DA_DESTROY(xml->children);
}