1 /* Copyright (c) 2013-2014 Yoran Heling
2
3 Permission is hereby granted, free of charge, to any person obtaining
4 a copy of this software and associated documentation files (the
5 "Software"), to deal in the Software without restriction, including
6 without limitation the rights to use, copy, modify, merge, publish,
7 distribute, sublicense, and/or sell copies of the Software, and to
8 permit persons to whom the Software is furnished to do so, subject to
9 the following conditions:
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef YXML_H
24 #define YXML_H
25
26 #include <stdint.h>
27 #include <stddef.h>
28
29 #if defined(_MSC_VER) && !defined(__cplusplus) && !defined(inline)
30 #define inline __inline
31 #endif
32
33 /* Full API documentation for this library can be found in the "yxml.pod" file
34 * in the yxml git repository, or online at http://dev.yorhel.nl/yxml/man */
35
36 typedef enum {
37 YXML_EEOF = -5, /* Unexpected EOF */
38 YXML_EREF = -4, /* Invalid character or entity reference (&whatever;) */
39 YXML_ECLOSE = -3, /* Close tag does not match open tag (<Tag> .. </OtherTag>) */
40 YXML_ESTACK = -2, /* Stack overflow (too deeply nested tags or too long element/attribute name) */
41 YXML_ESYN = -1, /* Syntax error (unexpected byte) */
42 YXML_OK = 0, /* Character consumed, no new token present */
43 YXML_ELEMSTART = 1, /* Start of an element: '<Tag ..' */
44 YXML_CONTENT = 2, /* Element content */
45 YXML_ELEMEND = 3, /* End of an element: '.. />' or '</Tag>' */
46 YXML_ATTRSTART = 4, /* Attribute: 'Name=..' */
47 YXML_ATTRVAL = 5, /* Attribute value */
48 YXML_ATTREND = 6, /* End of attribute '.."' */
49 YXML_PISTART = 7, /* Start of a processing instruction */
50 YXML_PICONTENT = 8, /* Content of a PI */
51 YXML_PIEND = 9 /* End of a processing instruction */
52 } yxml_ret_t;
53
54 /* When, exactly, are tokens returned?
55 *
56 * <TagName
57 * '>' ELEMSTART
58 * '/' ELEMSTART, '>' ELEMEND
59 * ' ' ELEMSTART
60 * '>'
61 * '/', '>' ELEMEND
62 * Attr
63 * '=' ATTRSTART
64 * "X ATTRVAL
65 * 'Y' ATTRVAL
66 * 'Z' ATTRVAL
67 * '"' ATTREND
68 * '>'
69 * '/', '>' ELEMEND
70 *
71 * </TagName
72 * '>' ELEMEND
73 */
74
75
76 typedef struct {
77 /* PUBLIC (read-only) */
78
79 /* Name of the current element, zero-length if not in any element. Changed
80 * after YXML_ELEMSTART. The pointer will remain valid up to and including
81 * the next non-YXML_ATTR* token, the pointed-to buffer will remain valid
82 * up to and including the YXML_ELEMEND for the corresponding element. */
83 char *elem;
84
85 /* The last read character(s) of an attribute value (YXML_ATTRVAL), element
86 * data (YXML_CONTENT), or processing instruction (YXML_PICONTENT). Changed
87 * after one of the respective YXML_ values is returned, and only valid
88 * until the next yxml_parse() call. Usually, this string only consists of
89 * a single byte, but multiple bytes are returned in the following cases:
90 * - "<?SomePI ?x ?>": The two characters "?x"
91 * - "<![CDATA[ ]x ]]>": The two characters "]x"
92 * - "<![CDATA[ ]]x ]]>": The three characters "]]x"
93 * - "&#N;" and "&#xN;", where dec(n) > 127. The referenced Unicode
94 * character is then encoded in multiple UTF-8 bytes.
95 */
96 char data[8];
97
98 /* Name of the current attribute. Changed after YXML_ATTRSTART, valid up to
99 * and including the next YXML_ATTREND. */
100 char *attr;
101
102 /* Name/target of the current processing instruction, zero-length if not in
103 * a PI. Changed after YXML_PISTART, valid up to (but excluding)
104 * the next YXML_PIEND. */
105 char *pi;
106
107 /* Line number, byte offset within that line, and total bytes read. These
108 * values refer to the position _after_ the last byte given to
109 * yxml_parse(). These are useful for debugging and error reporting. */
110 uint64_t byte;
111 uint64_t total;
112 uint32_t line;
113
114
115 /* PRIVATE */
116 int state;
117 unsigned char *stack; /* Stack of element names + attribute/PI name, separated by \0. Also starts with a \0. */
118 size_t stacksize, stacklen;
119 unsigned reflen;
120 unsigned quote;
121 int nextstate; /* Used for '@' state remembering and for the "string" consuming state */
122 unsigned ignore;
123 unsigned char *string;
124 } yxml_t;
125
126
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130
131 void yxml_init(yxml_t *, void *, size_t);
132
133
134 yxml_ret_t yxml_parse(yxml_t *, int);
135
136
137 /* May be called after the last character has been given to yxml_parse().
138 * Returns YXML_OK if the XML document is valid, YXML_EEOF otherwise. Using
139 * this function isn't really necessary, but can be used to detect documents
140 * that don't end correctly. In particular, an error is returned when the XML
141 * document did not contain a (complete) root element, or when the document
142 * ended while in a comment or processing instruction. */
143 yxml_ret_t yxml_eof(yxml_t *);
144
145 #ifdef __cplusplus
146 }
147 #endif
148
149
150 /* Returns the length of the element name (x->elem), attribute name (x->attr),
151 * or PI name (x->pi). This function should ONLY be used directly after the
152 * YXML_ELEMSTART, YXML_ATTRSTART or YXML_PISTART (respectively) tokens have
153 * been returned by yxml_parse(), calling this at any other time may not give
154 * the correct results. This function should also NOT be used on strings other
155 * than x->elem, x->attr or x->pi. */
yxml_symlen(yxml_t * x,const char * s)156 static inline size_t yxml_symlen(yxml_t *x, const char *s) {
157 return (x->stack + x->stacklen) - (const unsigned char*)s;
158 }
159
160 #endif
161
162 /* vim: set noet sw=4 ts=4: */
163