| 1 |
#include "allegro5/allegro.h" |
| 2 |
#include <ctype.h> |
| 3 |
|
| 4 |
#include "xml.h" |
| 5 |
|
| 6 |
typedef struct { |
| 7 |
XmlState state; |
| 8 |
bool closing; |
| 9 |
ALLEGRO_USTR *value; |
| 10 |
int (*callback)(XmlState state, char const *value, void *u); |
| 11 |
void *u; |
| 12 |
} XmlParser; |
| 13 |
|
| 14 |
static void scalar(XmlParser *x) { |
| 15 |
x->callback(x->state, al_cstr(x->value), x->u); |
| 16 |
al_ustr_assign_cstr(x->value, ""); |
| 17 |
} |
| 18 |
|
| 19 |
static void opt_scalar(XmlParser *x) { |
| 20 |
if (al_ustr_size(x->value)) { |
| 21 |
scalar(x); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
static void discard_scalar(XmlParser *x) { |
| 26 |
al_ustr_assign_cstr(x->value, ""); |
| 27 |
} |
| 28 |
|
| 29 |
static void close_tag(XmlParser *x) { |
| 30 |
x->state = Outside; |
| 31 |
x->closing = false; |
| 32 |
} |
| 33 |
|
| 34 |
static void add_char(XmlParser *x, char c) { |
| 35 |
char s[] = {c, 0}; |
| 36 |
al_ustr_append_cstr(x->value, s); |
| 37 |
} |
| 38 |
|
| 39 |
static void create_tag(XmlParser *x) { |
| 40 |
scalar(x); |
| 41 |
} |
| 42 |
|
| 43 |
static void open_tag(XmlParser *x) { |
| 44 |
x->state = Outside; |
| 45 |
} |
| 46 |
|
| 47 |
void _al_xml_parse(ALLEGRO_FILE *f, |
| 48 |
int (*callback)(XmlState state, char const *value, void *u), |
| 49 |
void *u) |
| 50 |
{ |
| 51 |
XmlParser x_; |
| 52 |
XmlParser *x = &x_; |
| 53 |
x->value = al_ustr_new(""); |
| 54 |
x->state = Outside; |
| 55 |
x->closing = false; |
| 56 |
x->callback = callback; |
| 57 |
x->u = u; |
| 58 |
|
| 59 |
while (true) { |
| 60 |
int c = al_fgetc(f); |
| 61 |
if (c < 0) { |
| 62 |
break; |
| 63 |
} |
| 64 |
if (x->state == Outside) { |
| 65 |
if (c == '<') { |
| 66 |
opt_scalar(x); |
| 67 |
x->state = ElementName; |
| 68 |
continue; |
| 69 |
} |
| 70 |
} |
| 71 |
else if (x->state == ElementName) { |
| 72 |
if (c == '/') { |
| 73 |
x->closing = true; |
| 74 |
continue; |
| 75 |
} |
| 76 |
else if (c == '>') { |
| 77 |
if (x->closing) { |
| 78 |
discard_scalar(x); |
| 79 |
close_tag(x); |
| 80 |
} |
| 81 |
else { |
| 82 |
create_tag(x); |
| 83 |
open_tag(x); |
| 84 |
} |
| 85 |
continue; |
| 86 |
} |
| 87 |
else if (isspace(c)) { |
| 88 |
create_tag(x); |
| 89 |
x->state = AttributeName; |
| 90 |
continue; |
| 91 |
} |
| 92 |
} |
| 93 |
else if (x->state == AttributeName) { |
| 94 |
if (isspace(c)) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
else if (c == '/') { |
| 98 |
x->closing = true; |
| 99 |
continue; |
| 100 |
} |
| 101 |
else if (c == '?') { |
| 102 |
x->closing = true; |
| 103 |
continue; |
| 104 |
} |
| 105 |
else if (c == '>') { |
| 106 |
if (x->closing) { |
| 107 |
close_tag(x); |
| 108 |
} |
| 109 |
else { |
| 110 |
open_tag(x); |
| 111 |
} |
| 112 |
continue; |
| 113 |
} |
| 114 |
else if (c == '=') { |
| 115 |
scalar(x); |
| 116 |
x->state = AttributeStart; |
| 117 |
continue; |
| 118 |
} |
| 119 |
} |
| 120 |
else if (x->state == AttributeStart) { |
| 121 |
if (c == '"') { |
| 122 |
x->state = AttributeValue; |
| 123 |
} |
| 124 |
continue; |
| 125 |
} |
| 126 |
else if (x->state == AttributeValue) { |
| 127 |
if (c == '"') { |
| 128 |
scalar(x); |
| 129 |
x->state = AttributeName; |
| 130 |
continue; |
| 131 |
} |
| 132 |
} |
| 133 |
add_char(x, c); |
| 134 |
} |
| 135 |
|
| 136 |
al_fclose(f); |
| 137 |
al_ustr_free(x->value); |
| 138 |
} |