nova verzija tcp+udp->serial com

Committer:
bosko001
Date:
Sun Apr 05 12:56:41 2020 +0000
Revision:
2:45b351b4fc2a
Child:
4:7abcf4543282
n0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 2:45b351b4fc2a 1 #define dxml_NOMMAP
bosko001 2:45b351b4fc2a 2
bosko001 2:45b351b4fc2a 3 #include <stdlib.h>
bosko001 2:45b351b4fc2a 4 #include <stdio.h>
bosko001 2:45b351b4fc2a 5 #include <stdarg.h>
bosko001 2:45b351b4fc2a 6 #include <string.h>
bosko001 2:45b351b4fc2a 7 #include <ctype.h>
bosko001 2:45b351b4fc2a 8 //#include <unistd.h>
bosko001 2:45b351b4fc2a 9 //#include <sys/types.h>
bosko001 2:45b351b4fc2a 10 #ifndef dxml_NOMMAP
bosko001 2:45b351b4fc2a 11 #include <sys/mman.h>
bosko001 2:45b351b4fc2a 12 #endif // dxml_NOMMAP
bosko001 2:45b351b4fc2a 13 //#include <stat.h>
bosko001 2:45b351b4fc2a 14 #include "dxml.h"
bosko001 2:45b351b4fc2a 15
bosko001 2:45b351b4fc2a 16 #define dxml_WS "\t\r\n " // whitespace
bosko001 2:45b351b4fc2a 17 #define dxml_ERRL 128 // maximum error string length
bosko001 2:45b351b4fc2a 18
bosko001 2:45b351b4fc2a 19 char* strdup(const char* literal)
bosko001 2:45b351b4fc2a 20 {
bosko001 2:45b351b4fc2a 21 auto l_len = strlen(literal);
bosko001 2:45b351b4fc2a 22 auto n = new char[l_len + 1];
bosko001 2:45b351b4fc2a 23 strncpy(n, literal, l_len + 1);
bosko001 2:45b351b4fc2a 24 return n;
bosko001 2:45b351b4fc2a 25 }
bosko001 2:45b351b4fc2a 26
bosko001 2:45b351b4fc2a 27 typedef struct dxml_root *dxml_root_t;
bosko001 2:45b351b4fc2a 28 struct dxml_root { // additional data for the root tag
bosko001 2:45b351b4fc2a 29 struct dxml xml; // is a super-struct built on top of dxml struct
bosko001 2:45b351b4fc2a 30 dxml_t cur; // current xml tree insertion point
bosko001 2:45b351b4fc2a 31 char *m; // original xml string
bosko001 2:45b351b4fc2a 32 size_t len; // length of allocated memory for mmap, -1 for malloc
bosko001 2:45b351b4fc2a 33 char *u; // UTF-8 conversion of string if original was UTF-16
bosko001 2:45b351b4fc2a 34 char *s; // start of work area
bosko001 2:45b351b4fc2a 35 char *e; // end of work area
bosko001 2:45b351b4fc2a 36 char **ent; // general entities (ampersand sequences)
bosko001 2:45b351b4fc2a 37 char ***attr; // default attributes
bosko001 2:45b351b4fc2a 38 char ***pi; // processing instructions
bosko001 2:45b351b4fc2a 39 short standalone; // non-zero if <?xml standalone="yes"?>
bosko001 2:45b351b4fc2a 40 char err[dxml_ERRL]; // error string
bosko001 2:45b351b4fc2a 41 };
bosko001 2:45b351b4fc2a 42
bosko001 2:45b351b4fc2a 43 char *dxml_NIL[] = { NULL }; // empty, null terminated array of strings
bosko001 2:45b351b4fc2a 44
bosko001 2:45b351b4fc2a 45 // returns the first child tag with the given name or NULL if not found
bosko001 2:45b351b4fc2a 46 dxml_t dxml_child(dxml_t xml, const char *name)
bosko001 2:45b351b4fc2a 47 {
bosko001 2:45b351b4fc2a 48 xml = (xml) ? xml->child : NULL;
bosko001 2:45b351b4fc2a 49 while (xml && strcmp(name, xml->name)) xml = xml->sibling;
bosko001 2:45b351b4fc2a 50 return xml;
bosko001 2:45b351b4fc2a 51 }
bosko001 2:45b351b4fc2a 52
bosko001 2:45b351b4fc2a 53 // returns the Nth tag with the same name in the same subsection or NULL if not
bosko001 2:45b351b4fc2a 54 // found
bosko001 2:45b351b4fc2a 55 dxml_t dxml_idx(dxml_t xml, int idx)
bosko001 2:45b351b4fc2a 56 {
bosko001 2:45b351b4fc2a 57 for (; xml && idx; idx--) xml = xml->next;
bosko001 2:45b351b4fc2a 58 return xml;
bosko001 2:45b351b4fc2a 59 }
bosko001 2:45b351b4fc2a 60
bosko001 2:45b351b4fc2a 61 // returns the value of the requested tag attribute or NULL if not found
bosko001 2:45b351b4fc2a 62 const char *dxml_attr(dxml_t xml, const char *attr)
bosko001 2:45b351b4fc2a 63 {
bosko001 2:45b351b4fc2a 64 int i = 0, j = 1;
bosko001 2:45b351b4fc2a 65 dxml_root_t root = (dxml_root_t)xml;
bosko001 2:45b351b4fc2a 66
bosko001 2:45b351b4fc2a 67 if (! xml || ! xml->attr) return NULL;
bosko001 2:45b351b4fc2a 68 while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2;
bosko001 2:45b351b4fc2a 69 if (xml->attr[i]) return xml->attr[i + 1]; // found attribute
bosko001 2:45b351b4fc2a 70
bosko001 2:45b351b4fc2a 71 while (root->xml.parent) root = (dxml_root_t)root->xml.parent; // root tag
bosko001 2:45b351b4fc2a 72 for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++);
bosko001 2:45b351b4fc2a 73 if (! root->attr[i]) return NULL; // no matching default attributes
bosko001 2:45b351b4fc2a 74 while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3;
bosko001 2:45b351b4fc2a 75 return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default
bosko001 2:45b351b4fc2a 76 }
bosko001 2:45b351b4fc2a 77
bosko001 2:45b351b4fc2a 78 // same as dxml_get but takes an already initialized va_list
bosko001 2:45b351b4fc2a 79 dxml_t dxml_vget(dxml_t xml, va_list ap)
bosko001 2:45b351b4fc2a 80 {
bosko001 2:45b351b4fc2a 81 char *name = va_arg(ap, char *);
bosko001 2:45b351b4fc2a 82 int idx = -1;
bosko001 2:45b351b4fc2a 83
bosko001 2:45b351b4fc2a 84 if (name && *name) {
bosko001 2:45b351b4fc2a 85 idx = va_arg(ap, int);
bosko001 2:45b351b4fc2a 86 xml = dxml_child(xml, name);
bosko001 2:45b351b4fc2a 87 }
bosko001 2:45b351b4fc2a 88 return (idx < 0) ? xml : dxml_vget(dxml_idx(xml, idx), ap);
bosko001 2:45b351b4fc2a 89 }
bosko001 2:45b351b4fc2a 90
bosko001 2:45b351b4fc2a 91 // Traverses the xml tree to retrieve a specific subtag. Takes a variable
bosko001 2:45b351b4fc2a 92 // length list of tag names and indexes. The argument list must be terminated
bosko001 2:45b351b4fc2a 93 // by either an index of -1 or an empty string tag name. Example:
bosko001 2:45b351b4fc2a 94 // title = dxml_get(library, "shelf", 0, "book", 2, "title", -1);
bosko001 2:45b351b4fc2a 95 // This retrieves the title of the 3rd book on the 1st shelf of library.
bosko001 2:45b351b4fc2a 96 // Returns NULL if not found.
bosko001 2:45b351b4fc2a 97 dxml_t dxml_get(dxml_t xml, ...)
bosko001 2:45b351b4fc2a 98 {
bosko001 2:45b351b4fc2a 99 va_list ap;
bosko001 2:45b351b4fc2a 100 dxml_t r;
bosko001 2:45b351b4fc2a 101
bosko001 2:45b351b4fc2a 102 va_start(ap, xml);
bosko001 2:45b351b4fc2a 103 r = dxml_vget(xml, ap);
bosko001 2:45b351b4fc2a 104 va_end(ap);
bosko001 2:45b351b4fc2a 105 return r;
bosko001 2:45b351b4fc2a 106 }
bosko001 2:45b351b4fc2a 107
bosko001 2:45b351b4fc2a 108 // returns a null terminated array of processing instructions for the given
bosko001 2:45b351b4fc2a 109 // target
bosko001 2:45b351b4fc2a 110 const char **dxml_pi(dxml_t xml, const char *target)
bosko001 2:45b351b4fc2a 111 {
bosko001 2:45b351b4fc2a 112 dxml_root_t root = (dxml_root_t)xml;
bosko001 2:45b351b4fc2a 113 int i = 0;
bosko001 2:45b351b4fc2a 114
bosko001 2:45b351b4fc2a 115 if (! root) return (const char **)dxml_NIL;
bosko001 2:45b351b4fc2a 116 while (root->xml.parent) root = (dxml_root_t)root->xml.parent; // root tag
bosko001 2:45b351b4fc2a 117 while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
bosko001 2:45b351b4fc2a 118 return (const char **)((root->pi[i]) ? root->pi[i] + 1 : dxml_NIL);
bosko001 2:45b351b4fc2a 119 }
bosko001 2:45b351b4fc2a 120
bosko001 2:45b351b4fc2a 121 // set an error string and return root
bosko001 2:45b351b4fc2a 122 dxml_t dxml_err(dxml_root_t root, char *s, const char *err, ...)
bosko001 2:45b351b4fc2a 123 {
bosko001 2:45b351b4fc2a 124 va_list ap;
bosko001 2:45b351b4fc2a 125 int line = 1;
bosko001 2:45b351b4fc2a 126 char *t, fmt[dxml_ERRL];
bosko001 2:45b351b4fc2a 127
bosko001 2:45b351b4fc2a 128 for (t = root->s; t < s; t++) if (*t == '\n') line++;
bosko001 2:45b351b4fc2a 129 snprintf(fmt, dxml_ERRL, "[error near line %d]: %s", line, err);
bosko001 2:45b351b4fc2a 130
bosko001 2:45b351b4fc2a 131 va_start(ap, err);
bosko001 2:45b351b4fc2a 132 vsnprintf(root->err, dxml_ERRL, fmt, ap);
bosko001 2:45b351b4fc2a 133 va_end(ap);
bosko001 2:45b351b4fc2a 134
bosko001 2:45b351b4fc2a 135 return &root->xml;
bosko001 2:45b351b4fc2a 136 }
bosko001 2:45b351b4fc2a 137
bosko001 2:45b351b4fc2a 138 // Recursively decodes entity and character references and normalizes new lines
bosko001 2:45b351b4fc2a 139 // ent is a null terminated array of alternating entity names and values. set t
bosko001 2:45b351b4fc2a 140 // to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
bosko001 2:45b351b4fc2a 141 // for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
bosko001 2:45b351b4fc2a 142 // attribute normalization. Returns s, or if the decoded string is longer than
bosko001 2:45b351b4fc2a 143 // s, returns a malloced string that must be freed.
bosko001 2:45b351b4fc2a 144 char *dxml_decode(char *s, char **ent, char t)
bosko001 2:45b351b4fc2a 145 {
bosko001 2:45b351b4fc2a 146 char *e, *r = s, *m = s;
bosko001 2:45b351b4fc2a 147 long b, c, d, l;
bosko001 2:45b351b4fc2a 148
bosko001 2:45b351b4fc2a 149 for (; *s; s++) { // normalize line endings
bosko001 2:45b351b4fc2a 150 while (*s == '\r') {
bosko001 2:45b351b4fc2a 151 *(s++) = '\n';
bosko001 2:45b351b4fc2a 152 if (*s == '\n') memmove(s, (s + 1), strlen(s));
bosko001 2:45b351b4fc2a 153 }
bosko001 2:45b351b4fc2a 154 }
bosko001 2:45b351b4fc2a 155
bosko001 2:45b351b4fc2a 156 for (s = r; ; ) {
bosko001 2:45b351b4fc2a 157 while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++;
bosko001 2:45b351b4fc2a 158
bosko001 2:45b351b4fc2a 159 if (! *s) break;
bosko001 2:45b351b4fc2a 160 else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference
bosko001 2:45b351b4fc2a 161 if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16
bosko001 2:45b351b4fc2a 162 else c = strtol(s + 2, &e, 10); // base 10
bosko001 2:45b351b4fc2a 163 if (! c || *e != ';') { s++; continue; } // not a character ref
bosko001 2:45b351b4fc2a 164
bosko001 2:45b351b4fc2a 165 if (c < 0x80) *(s++) = c; // US-ASCII subset
bosko001 2:45b351b4fc2a 166 else { // multi-byte UTF-8 sequence
bosko001 2:45b351b4fc2a 167 for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
bosko001 2:45b351b4fc2a 168 b = (b - 2) / 5; // number of bytes in payload
bosko001 2:45b351b4fc2a 169 *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
bosko001 2:45b351b4fc2a 170 while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
bosko001 2:45b351b4fc2a 171 }
bosko001 2:45b351b4fc2a 172
bosko001 2:45b351b4fc2a 173 memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
bosko001 2:45b351b4fc2a 174 }
bosko001 2:45b351b4fc2a 175 else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) ||
bosko001 2:45b351b4fc2a 176 (*s == '%' && t == '%')) { // entity reference
bosko001 2:45b351b4fc2a 177 for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b]));
bosko001 2:45b351b4fc2a 178 b += 2); // find entity in entity list
bosko001 2:45b351b4fc2a 179
bosko001 2:45b351b4fc2a 180 if (ent[b++]) { // found a match
bosko001 2:45b351b4fc2a 181 if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) {
bosko001 2:45b351b4fc2a 182 l = (d = (s - r)) + c + strlen(e); // new length
bosko001 2:45b351b4fc2a 183 r = (char*)((r == m) ? strcpy((char*)malloc(l), r) : realloc(r, l));
bosko001 2:45b351b4fc2a 184 e = strchr((s = r + d), ';'); // fix up pointers
bosko001 2:45b351b4fc2a 185 }
bosko001 2:45b351b4fc2a 186
bosko001 2:45b351b4fc2a 187 memmove(s + c, e + 1, strlen(e)); // shift rest of string
bosko001 2:45b351b4fc2a 188 strncpy(s, ent[b], c); // copy in replacement text
bosko001 2:45b351b4fc2a 189 }
bosko001 2:45b351b4fc2a 190 else s++; // not a known entity
bosko001 2:45b351b4fc2a 191 }
bosko001 2:45b351b4fc2a 192 else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' ';
bosko001 2:45b351b4fc2a 193 else s++; // no decoding needed
bosko001 2:45b351b4fc2a 194 }
bosko001 2:45b351b4fc2a 195
bosko001 2:45b351b4fc2a 196 if (t == '*') { // normalize spaces for non-cdata attributes
bosko001 2:45b351b4fc2a 197 for (s = r; *s; s++) {
bosko001 2:45b351b4fc2a 198 if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1);
bosko001 2:45b351b4fc2a 199 while (*s && *s != ' ') s++;
bosko001 2:45b351b4fc2a 200 }
bosko001 2:45b351b4fc2a 201 if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space
bosko001 2:45b351b4fc2a 202 }
bosko001 2:45b351b4fc2a 203 return r;
bosko001 2:45b351b4fc2a 204 }
bosko001 2:45b351b4fc2a 205
bosko001 2:45b351b4fc2a 206 // called when parser finds start of new tag
bosko001 2:45b351b4fc2a 207 void dxml_open_tag(dxml_root_t root, char *name, char **attr)
bosko001 2:45b351b4fc2a 208 {
bosko001 2:45b351b4fc2a 209 dxml_t xml = root->cur;
bosko001 2:45b351b4fc2a 210
bosko001 2:45b351b4fc2a 211 if (xml->name) xml = dxml_add_child(xml, name, strlen(xml->txt));
bosko001 2:45b351b4fc2a 212 else xml->name = name; // first open tag
bosko001 2:45b351b4fc2a 213
bosko001 2:45b351b4fc2a 214 xml->attr = attr;
bosko001 2:45b351b4fc2a 215 root->cur = xml; // update tag insertion point
bosko001 2:45b351b4fc2a 216 }
bosko001 2:45b351b4fc2a 217
bosko001 2:45b351b4fc2a 218 // called when parser finds character content between open and closing tag
bosko001 2:45b351b4fc2a 219 void dxml_char_content(dxml_root_t root, char *s, size_t len, char t)
bosko001 2:45b351b4fc2a 220 {
bosko001 2:45b351b4fc2a 221 dxml_t xml = root->cur;
bosko001 2:45b351b4fc2a 222 char *m = s;
bosko001 2:45b351b4fc2a 223 size_t l;
bosko001 2:45b351b4fc2a 224
bosko001 2:45b351b4fc2a 225 if (! xml || ! xml->name || ! len) return; // sanity check
bosko001 2:45b351b4fc2a 226
bosko001 2:45b351b4fc2a 227 s[len] = '\0'; // null terminate text (calling functions anticipate this)
bosko001 2:45b351b4fc2a 228 len = strlen(s = dxml_decode(s, root->ent, t)) + 1;
bosko001 2:45b351b4fc2a 229
bosko001 2:45b351b4fc2a 230 if (! *(xml->txt)) xml->txt = s; // initial character content
bosko001 2:45b351b4fc2a 231 else { // allocate our own memory and make a copy
bosko001 2:45b351b4fc2a 232 xml->txt = (char*)((xml->flags & dxml_TXTM) // allocate some space
bosko001 2:45b351b4fc2a 233 ? realloc(xml->txt, (l = strlen(xml->txt)) + len)
bosko001 2:45b351b4fc2a 234 : strcpy((char*)malloc((l = strlen(xml->txt)) + len), xml->txt));
bosko001 2:45b351b4fc2a 235 strcpy(xml->txt + l, s); // add new char content
bosko001 2:45b351b4fc2a 236 if (s != m) free(s); // free s if it was malloced by dxml_decode()
bosko001 2:45b351b4fc2a 237 }
bosko001 2:45b351b4fc2a 238
bosko001 2:45b351b4fc2a 239 if (xml->txt != m) dxml_set_flag(xml, dxml_TXTM);
bosko001 2:45b351b4fc2a 240 }
bosko001 2:45b351b4fc2a 241
bosko001 2:45b351b4fc2a 242 // called when parser finds closing tag
bosko001 2:45b351b4fc2a 243 dxml_t dxml_close_tag(dxml_root_t root, char *name, char *s)
bosko001 2:45b351b4fc2a 244 {
bosko001 2:45b351b4fc2a 245 if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name))
bosko001 2:45b351b4fc2a 246 return dxml_err(root, s, "unexpected closing tag </%s>", name);
bosko001 2:45b351b4fc2a 247
bosko001 2:45b351b4fc2a 248 root->cur = root->cur->parent;
bosko001 2:45b351b4fc2a 249 return NULL;
bosko001 2:45b351b4fc2a 250 }
bosko001 2:45b351b4fc2a 251
bosko001 2:45b351b4fc2a 252 // checks for circular entity references, returns non-zero if no circular
bosko001 2:45b351b4fc2a 253 // references are found, zero otherwise
bosko001 2:45b351b4fc2a 254 int dxml_ent_ok(char *name, char *s, char **ent)
bosko001 2:45b351b4fc2a 255 {
bosko001 2:45b351b4fc2a 256 int i;
bosko001 2:45b351b4fc2a 257
bosko001 2:45b351b4fc2a 258 for (; ; s++) {
bosko001 2:45b351b4fc2a 259 while (*s && *s != '&') s++; // find next entity reference
bosko001 2:45b351b4fc2a 260 if (! *s) return 1;
bosko001 2:45b351b4fc2a 261 if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref.
bosko001 2:45b351b4fc2a 262 for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2);
bosko001 2:45b351b4fc2a 263 if (ent[i] && ! dxml_ent_ok(name, ent[i + 1], ent)) return 0;
bosko001 2:45b351b4fc2a 264 }
bosko001 2:45b351b4fc2a 265 }
bosko001 2:45b351b4fc2a 266
bosko001 2:45b351b4fc2a 267 // called when the parser finds a processing instruction
bosko001 2:45b351b4fc2a 268 void dxml_proc_inst(dxml_root_t root, char *s, size_t len)
bosko001 2:45b351b4fc2a 269 {
bosko001 2:45b351b4fc2a 270 int i = 0, j = 1;
bosko001 2:45b351b4fc2a 271 char *target = s;
bosko001 2:45b351b4fc2a 272
bosko001 2:45b351b4fc2a 273 s[len] = '\0'; // null terminate instruction
bosko001 2:45b351b4fc2a 274 if (*(s += strcspn(s, dxml_WS))) {
bosko001 2:45b351b4fc2a 275 *s = '\0'; // null terminate target
bosko001 2:45b351b4fc2a 276 s += strspn(s + 1, dxml_WS) + 1; // skip whitespace after target
bosko001 2:45b351b4fc2a 277 }
bosko001 2:45b351b4fc2a 278
bosko001 2:45b351b4fc2a 279 if (! strcmp(target, "xml")) { // <?xml ... ?>
bosko001 2:45b351b4fc2a 280 if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10,
bosko001 2:45b351b4fc2a 281 dxml_WS "='\"") + 10, "yes", 3)) root->standalone = 1;
bosko001 2:45b351b4fc2a 282 return;
bosko001 2:45b351b4fc2a 283 }
bosko001 2:45b351b4fc2a 284
bosko001 2:45b351b4fc2a 285 if (! root->pi[0]) *(root->pi = (char***)malloc(sizeof(char **))) = NULL; //first pi
bosko001 2:45b351b4fc2a 286
bosko001 2:45b351b4fc2a 287 while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
bosko001 2:45b351b4fc2a 288 if (! root->pi[i]) { // new target
bosko001 2:45b351b4fc2a 289 root->pi = (char***)realloc(root->pi, sizeof(char **) * (i + 2));
bosko001 2:45b351b4fc2a 290 root->pi[i] = (char**)malloc(sizeof(char *) * 3);
bosko001 2:45b351b4fc2a 291 root->pi[i][0] = target;
bosko001 2:45b351b4fc2a 292 root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list
bosko001 2:45b351b4fc2a 293 root->pi[i][2] = strdup(""); // empty document position list
bosko001 2:45b351b4fc2a 294 }
bosko001 2:45b351b4fc2a 295
bosko001 2:45b351b4fc2a 296 while (root->pi[i][j]) j++; // find end of instruction list for this target
bosko001 2:45b351b4fc2a 297 root->pi[i] = (char**)realloc(root->pi[i], sizeof(char *) * (j + 3));
bosko001 2:45b351b4fc2a 298 root->pi[i][j + 2] = (char*)realloc(root->pi[i][j + 1], j + 1);
bosko001 2:45b351b4fc2a 299 strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
bosko001 2:45b351b4fc2a 300 root->pi[i][j + 1] = NULL; // null terminate pi list for this target
bosko001 2:45b351b4fc2a 301 root->pi[i][j] = s; // set instruction
bosko001 2:45b351b4fc2a 302 }
bosko001 2:45b351b4fc2a 303
bosko001 2:45b351b4fc2a 304 // called when the parser finds an internal doctype subset
bosko001 2:45b351b4fc2a 305 short dxml_internal_dtd(dxml_root_t root, char *s, size_t len)
bosko001 2:45b351b4fc2a 306 {
bosko001 2:45b351b4fc2a 307 char q, *c, *t, *n = NULL, *v, **ent, **pe;
bosko001 2:45b351b4fc2a 308 int i, j;
bosko001 2:45b351b4fc2a 309
bosko001 2:45b351b4fc2a 310 pe = (char**)memcpy(malloc(sizeof(dxml_NIL)), dxml_NIL, sizeof(dxml_NIL));
bosko001 2:45b351b4fc2a 311
bosko001 2:45b351b4fc2a 312 for (s[len] = '\0'; s; ) {
bosko001 2:45b351b4fc2a 313 while (*s && *s != '<' && *s != '%') s++; // find next declaration
bosko001 2:45b351b4fc2a 314
bosko001 2:45b351b4fc2a 315 if (! *s) break;
bosko001 2:45b351b4fc2a 316 else if (! strncmp(s, "<!ENTITY", 8)) { // parse entity definitions
bosko001 2:45b351b4fc2a 317 c = s += strspn(s + 8, dxml_WS) + 8; // skip white space separator
bosko001 2:45b351b4fc2a 318 n = s + strspn(s, dxml_WS "%"); // find name
bosko001 2:45b351b4fc2a 319 *(s = n + strcspn(n, dxml_WS)) = ';'; // append ; to name
bosko001 2:45b351b4fc2a 320
bosko001 2:45b351b4fc2a 321 v = s + strspn(s + 1, dxml_WS) + 1; // find value
bosko001 2:45b351b4fc2a 322 if ((q = *(v++)) != '"' && q != '\'') { // skip externals
bosko001 2:45b351b4fc2a 323 s = strchr(s, '>');
bosko001 2:45b351b4fc2a 324 continue;
bosko001 2:45b351b4fc2a 325 }
bosko001 2:45b351b4fc2a 326
bosko001 2:45b351b4fc2a 327 for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
bosko001 2:45b351b4fc2a 328 ent = (char**)realloc(ent, (i + 3) * sizeof(char *)); // space for next ent
bosko001 2:45b351b4fc2a 329 if (*c == '%') pe = ent;
bosko001 2:45b351b4fc2a 330 else root->ent = ent;
bosko001 2:45b351b4fc2a 331
bosko001 2:45b351b4fc2a 332 *(++s) = '\0'; // null terminate name
bosko001 2:45b351b4fc2a 333 if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value
bosko001 2:45b351b4fc2a 334 ent[i + 1] = dxml_decode(v, pe, '%'); // set value
bosko001 2:45b351b4fc2a 335 ent[i + 2] = NULL; // null terminate entity list
bosko001 2:45b351b4fc2a 336 if (! dxml_ent_ok(n, ent[i + 1], ent)) { // circular reference
bosko001 2:45b351b4fc2a 337 if (ent[i + 1] != v) free(ent[i + 1]);
bosko001 2:45b351b4fc2a 338 dxml_err(root, v, "circular entity declaration &%s", n);
bosko001 2:45b351b4fc2a 339 break;
bosko001 2:45b351b4fc2a 340 }
bosko001 2:45b351b4fc2a 341 else ent[i] = n; // set entity name
bosko001 2:45b351b4fc2a 342 }
bosko001 2:45b351b4fc2a 343 else if (! strncmp(s, "<!ATTLIST", 9)) { // parse default attributes
bosko001 2:45b351b4fc2a 344 t = s + strspn(s + 9, dxml_WS) + 9; // skip whitespace separator
bosko001 2:45b351b4fc2a 345 if (! *t) { dxml_err(root, t, "unclosed <!ATTLIST"); break; }
bosko001 2:45b351b4fc2a 346 if (*(s = t + strcspn(t, dxml_WS ">")) == '>') continue;
bosko001 2:45b351b4fc2a 347 else *s = '\0'; // null terminate tag name
bosko001 2:45b351b4fc2a 348 for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
bosko001 2:45b351b4fc2a 349
bosko001 2:45b351b4fc2a 350 while (*(n = ++s + strspn(s, dxml_WS)) && *n != '>') {
bosko001 2:45b351b4fc2a 351 if (*(s = n + strcspn(n, dxml_WS))) *s = '\0'; // attr name
bosko001 2:45b351b4fc2a 352 else { dxml_err(root, t, "malformed <!ATTLIST"); break; }
bosko001 2:45b351b4fc2a 353
bosko001 2:45b351b4fc2a 354 s += strspn(s + 1, dxml_WS) + 1; // find next token
bosko001 2:45b351b4fc2a 355 c = strdup((strncmp(s, "CDATA", 5)) ? "*" : " "); // is it cdata?
bosko001 2:45b351b4fc2a 356 if (! strncmp(s, "NOTATION", 8))
bosko001 2:45b351b4fc2a 357 s += strspn(s + 8, dxml_WS) + 8;
bosko001 2:45b351b4fc2a 358 s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, dxml_WS);
bosko001 2:45b351b4fc2a 359 if (! s) { dxml_err(root, t, "malformed <!ATTLIST"); break; }
bosko001 2:45b351b4fc2a 360
bosko001 2:45b351b4fc2a 361 s += strspn(s, dxml_WS ")"); // skip white space separator
bosko001 2:45b351b4fc2a 362 if (! strncmp(s, "#FIXED", 6))
bosko001 2:45b351b4fc2a 363 s += strspn(s + 6, dxml_WS) + 6;
bosko001 2:45b351b4fc2a 364 if (*s == '#') { // no default value
bosko001 2:45b351b4fc2a 365 s += strcspn(s, dxml_WS ">") - 1;
bosko001 2:45b351b4fc2a 366 if (*c == ' ') continue; // cdata is default, nothing to do
bosko001 2:45b351b4fc2a 367 v = NULL;
bosko001 2:45b351b4fc2a 368 }
bosko001 2:45b351b4fc2a 369 else if ((*s == '"' || *s == '\'') && // default value
bosko001 2:45b351b4fc2a 370 (s = strchr(v = s + 1, *s))) *s = '\0';
bosko001 2:45b351b4fc2a 371 else { dxml_err(root, t, "malformed <!ATTLIST"); break; }
bosko001 2:45b351b4fc2a 372
bosko001 2:45b351b4fc2a 373 if (! root->attr[i]) { // new tag name
bosko001 2:45b351b4fc2a 374 root->attr = (char***)((! i) ? malloc(2 * sizeof(char **))
bosko001 2:45b351b4fc2a 375 : realloc(root->attr,
bosko001 2:45b351b4fc2a 376 (i + 2) * sizeof(char **)));
bosko001 2:45b351b4fc2a 377 root->attr[i] = (char**)malloc(2 * sizeof(char *));
bosko001 2:45b351b4fc2a 378 root->attr[i][0] = t; // set tag name
bosko001 2:45b351b4fc2a 379 root->attr[i][1] = (char *)(root->attr[i + 1] = NULL);
bosko001 2:45b351b4fc2a 380 }
bosko001 2:45b351b4fc2a 381
bosko001 2:45b351b4fc2a 382 for (j = 1; root->attr[i][j]; j += 3); // find end of list
bosko001 2:45b351b4fc2a 383 root->attr[i] = (char**)realloc(root->attr[i],
bosko001 2:45b351b4fc2a 384 (j + 4) * sizeof(char *));
bosko001 2:45b351b4fc2a 385
bosko001 2:45b351b4fc2a 386 root->attr[i][j + 3] = NULL; // null terminate list
bosko001 2:45b351b4fc2a 387 root->attr[i][j + 2] = c; // is it cdata?
bosko001 2:45b351b4fc2a 388 root->attr[i][j + 1] = (v) ? dxml_decode(v, root->ent, *c)
bosko001 2:45b351b4fc2a 389 : NULL;
bosko001 2:45b351b4fc2a 390 root->attr[i][j] = n; // attribute name
bosko001 2:45b351b4fc2a 391 }
bosko001 2:45b351b4fc2a 392 }
bosko001 2:45b351b4fc2a 393 else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); // comments
bosko001 2:45b351b4fc2a 394 else if (! strncmp(s, "<?", 2)) { // processing instructions
bosko001 2:45b351b4fc2a 395 if ((s = strstr(c = s + 2, "?>")))
bosko001 2:45b351b4fc2a 396 dxml_proc_inst(root, c, s++ - c);
bosko001 2:45b351b4fc2a 397 }
bosko001 2:45b351b4fc2a 398 else if (*s == '<') s = strchr(s, '>'); // skip other declarations
bosko001 2:45b351b4fc2a 399 else if (*(s++) == '%' && ! root->standalone) break;
bosko001 2:45b351b4fc2a 400 }
bosko001 2:45b351b4fc2a 401
bosko001 2:45b351b4fc2a 402 free(pe);
bosko001 2:45b351b4fc2a 403 return ! *root->err;
bosko001 2:45b351b4fc2a 404 }
bosko001 2:45b351b4fc2a 405
bosko001 2:45b351b4fc2a 406 // Converts a UTF-16 string to UTF-8. Returns a new string that must be freed
bosko001 2:45b351b4fc2a 407 // or NULL if no conversion was needed.
bosko001 2:45b351b4fc2a 408 char *dxml_str2utf8(char **s, size_t *len)
bosko001 2:45b351b4fc2a 409 {
bosko001 2:45b351b4fc2a 410 char *u;
bosko001 2:45b351b4fc2a 411 size_t l = 0, sl, max = *len;
bosko001 2:45b351b4fc2a 412 long c, d;
bosko001 2:45b351b4fc2a 413 int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
bosko001 2:45b351b4fc2a 414
bosko001 2:45b351b4fc2a 415 if (be == -1) return NULL; // not UTF-16
bosko001 2:45b351b4fc2a 416
bosko001 2:45b351b4fc2a 417 u = (char*)malloc(max);
bosko001 2:45b351b4fc2a 418 for (sl = 2; sl < *len - 1; sl += 2) {
bosko001 2:45b351b4fc2a 419 c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE
bosko001 2:45b351b4fc2a 420 : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE
bosko001 2:45b351b4fc2a 421 if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half
bosko001 2:45b351b4fc2a 422 d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)
bosko001 2:45b351b4fc2a 423 : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF);
bosko001 2:45b351b4fc2a 424 c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;
bosko001 2:45b351b4fc2a 425 }
bosko001 2:45b351b4fc2a 426
bosko001 2:45b351b4fc2a 427 while (l + 6 > max) u = (char*)realloc(u, max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 428 if (c < 0x80) u[l++] = c; // US-ASCII subset
bosko001 2:45b351b4fc2a 429 else { // multi-byte UTF-8 sequence
bosko001 2:45b351b4fc2a 430 for (b = 0, d = c; d; d /= 2) b++; // bits in c
bosko001 2:45b351b4fc2a 431 b = (b - 2) / 5; // bytes in payload
bosko001 2:45b351b4fc2a 432 u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head
bosko001 2:45b351b4fc2a 433 while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
bosko001 2:45b351b4fc2a 434 }
bosko001 2:45b351b4fc2a 435 }
bosko001 2:45b351b4fc2a 436 return *s = (char*)realloc(u, *len = l);
bosko001 2:45b351b4fc2a 437 }
bosko001 2:45b351b4fc2a 438
bosko001 2:45b351b4fc2a 439 // frees a tag attribute list
bosko001 2:45b351b4fc2a 440 void dxml_free_attr(char **attr) {
bosko001 2:45b351b4fc2a 441 int i = 0;
bosko001 2:45b351b4fc2a 442 char *m;
bosko001 2:45b351b4fc2a 443
bosko001 2:45b351b4fc2a 444 if (! attr || attr == dxml_NIL) return; // nothing to free
bosko001 2:45b351b4fc2a 445 while (attr[i]) i += 2; // find end of attribute list
bosko001 2:45b351b4fc2a 446 m = attr[i + 1]; // list of which names and values are malloced
bosko001 2:45b351b4fc2a 447 for (i = 0; m[i]; i++) {
bosko001 2:45b351b4fc2a 448 if (m[i] & dxml_NAMEM) free(attr[i * 2]);
bosko001 2:45b351b4fc2a 449 if (m[i] & dxml_TXTM) free(attr[(i * 2) + 1]);
bosko001 2:45b351b4fc2a 450 }
bosko001 2:45b351b4fc2a 451 free(m);
bosko001 2:45b351b4fc2a 452 free(attr);
bosko001 2:45b351b4fc2a 453 }
bosko001 2:45b351b4fc2a 454
bosko001 2:45b351b4fc2a 455 // parse the given xml string and return an dxml structure
bosko001 2:45b351b4fc2a 456 dxml_t dxml_parse_str(char *s, size_t len)
bosko001 2:45b351b4fc2a 457 {
bosko001 2:45b351b4fc2a 458 dxml_root_t root = (dxml_root_t)dxml_new(NULL);
bosko001 2:45b351b4fc2a 459 char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning
bosko001 2:45b351b4fc2a 460 int l, i, j;
bosko001 2:45b351b4fc2a 461
bosko001 2:45b351b4fc2a 462 root->m = s;
bosko001 2:45b351b4fc2a 463 if (! len) return dxml_err(root, NULL, "root tag missing");
bosko001 2:45b351b4fc2a 464 root->u = dxml_str2utf8(&s, &len); // convert utf-16 to utf-8
bosko001 2:45b351b4fc2a 465 root->e = (root->s = s) + len; // record start and end of work area
bosko001 2:45b351b4fc2a 466
bosko001 2:45b351b4fc2a 467 e = s[len - 1]; // save end char
bosko001 2:45b351b4fc2a 468 s[len - 1] = '\0'; // turn end char into null terminator
bosko001 2:45b351b4fc2a 469
bosko001 2:45b351b4fc2a 470 while (*s && *s != '<') s++; // find first tag
bosko001 2:45b351b4fc2a 471 if (! *s) return dxml_err(root, s, "root tag missing");
bosko001 2:45b351b4fc2a 472
bosko001 2:45b351b4fc2a 473 for (; ; ) {
bosko001 2:45b351b4fc2a 474 attr = (char **)dxml_NIL;
bosko001 2:45b351b4fc2a 475 d = ++s;
bosko001 2:45b351b4fc2a 476
bosko001 2:45b351b4fc2a 477 if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag
bosko001 2:45b351b4fc2a 478 if (! root->cur)
bosko001 2:45b351b4fc2a 479 return dxml_err(root, d, "markup outside of root element");
bosko001 2:45b351b4fc2a 480
bosko001 2:45b351b4fc2a 481 s += strcspn(s, dxml_WS "/>");
bosko001 2:45b351b4fc2a 482 while (isspace(*s)) *(s++) = '\0'; // null terminate tag name
bosko001 2:45b351b4fc2a 483
bosko001 2:45b351b4fc2a 484 if (*s && *s != '/' && *s != '>') // find tag in default attr list
bosko001 2:45b351b4fc2a 485 for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
bosko001 2:45b351b4fc2a 486
bosko001 2:45b351b4fc2a 487 for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib
bosko001 2:45b351b4fc2a 488 attr = (char**)((l) ? realloc(attr, (l + 4) * sizeof(char *))
bosko001 2:45b351b4fc2a 489 : malloc(4 * sizeof(char *))); // allocate space
bosko001 2:45b351b4fc2a 490 attr[l + 3] = (char*)((l) ? realloc(attr[l + 1], (l / 2) + 2)
bosko001 2:45b351b4fc2a 491 : malloc(2)); // mem for list of maloced vals
bosko001 2:45b351b4fc2a 492 strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced
bosko001 2:45b351b4fc2a 493 attr[l + 2] = NULL; // null terminate list
bosko001 2:45b351b4fc2a 494 attr[l + 1] = ""; // temporary attribute value
bosko001 2:45b351b4fc2a 495 attr[l] = s; // set attribute name
bosko001 2:45b351b4fc2a 496
bosko001 2:45b351b4fc2a 497 s += strcspn(s, dxml_WS "=/>");
bosko001 2:45b351b4fc2a 498 if (*s == '=' || isspace(*s)) {
bosko001 2:45b351b4fc2a 499 *(s++) = '\0'; // null terminate tag attribute name
bosko001 2:45b351b4fc2a 500 q = *(s += strspn(s, dxml_WS "="));
bosko001 2:45b351b4fc2a 501 if (q == '"' || q == '\'') { // attribute value
bosko001 2:45b351b4fc2a 502 attr[l + 1] = ++s;
bosko001 2:45b351b4fc2a 503 while (*s && *s != q) s++;
bosko001 2:45b351b4fc2a 504 if (*s) *(s++) = '\0'; // null terminate attribute val
bosko001 2:45b351b4fc2a 505 else {
bosko001 2:45b351b4fc2a 506 dxml_free_attr(attr);
bosko001 2:45b351b4fc2a 507 return dxml_err(root, d, "missing %c", q);
bosko001 2:45b351b4fc2a 508 }
bosko001 2:45b351b4fc2a 509
bosko001 2:45b351b4fc2a 510 for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3);
bosko001 2:45b351b4fc2a 511 attr[l + 1] = dxml_decode(attr[l + 1], root->ent, (a
bosko001 2:45b351b4fc2a 512 && a[j]) ? *a[j + 2] : ' ');
bosko001 2:45b351b4fc2a 513 if (attr[l + 1] < d || attr[l + 1] > s)
bosko001 2:45b351b4fc2a 514 attr[l + 3][l / 2] = dxml_TXTM; // value malloced
bosko001 2:45b351b4fc2a 515 }
bosko001 2:45b351b4fc2a 516 }
bosko001 2:45b351b4fc2a 517 while (isspace(*s)) s++;
bosko001 2:45b351b4fc2a 518 }
bosko001 2:45b351b4fc2a 519
bosko001 2:45b351b4fc2a 520 if (*s == '/') { // self closing tag
bosko001 2:45b351b4fc2a 521 *(s++) = '\0';
bosko001 2:45b351b4fc2a 522 if ((*s && *s != '>') || (! *s && e != '>')) {
bosko001 2:45b351b4fc2a 523 if (l) dxml_free_attr(attr);
bosko001 2:45b351b4fc2a 524 return dxml_err(root, d, "missing >");
bosko001 2:45b351b4fc2a 525 }
bosko001 2:45b351b4fc2a 526 dxml_open_tag(root, d, attr);
bosko001 2:45b351b4fc2a 527 dxml_close_tag(root, d, s);
bosko001 2:45b351b4fc2a 528 }
bosko001 2:45b351b4fc2a 529 else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag
bosko001 2:45b351b4fc2a 530 *s = '\0'; // temporarily null terminate tag name
bosko001 2:45b351b4fc2a 531 dxml_open_tag(root, d, attr);
bosko001 2:45b351b4fc2a 532 *s = q;
bosko001 2:45b351b4fc2a 533 }
bosko001 2:45b351b4fc2a 534 else {
bosko001 2:45b351b4fc2a 535 if (l) dxml_free_attr(attr);
bosko001 2:45b351b4fc2a 536 return dxml_err(root, d, "missing >");
bosko001 2:45b351b4fc2a 537 }
bosko001 2:45b351b4fc2a 538 }
bosko001 2:45b351b4fc2a 539 else if (*s == '/') { // close tag
bosko001 2:45b351b4fc2a 540 s += strcspn(d = s + 1, dxml_WS ">") + 1;
bosko001 2:45b351b4fc2a 541 if (! (q = *s) && e != '>') return dxml_err(root, d, "missing >");
bosko001 2:45b351b4fc2a 542 *s = '\0'; // temporarily null terminate tag name
bosko001 2:45b351b4fc2a 543 if (dxml_close_tag(root, d, s)) return &root->xml;
bosko001 2:45b351b4fc2a 544 if (isspace(*s = q)) s += strspn(s, dxml_WS);
bosko001 2:45b351b4fc2a 545 }
bosko001 2:45b351b4fc2a 546 else if (! strncmp(s, "!--", 3)) { // xml comment
bosko001 2:45b351b4fc2a 547 if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) ||
bosko001 2:45b351b4fc2a 548 (! *s && e != '>')) return dxml_err(root, d, "unclosed <!--");
bosko001 2:45b351b4fc2a 549 }
bosko001 2:45b351b4fc2a 550 else if (! strncmp(s, "![CDATA[", 8)) { // cdata
bosko001 2:45b351b4fc2a 551 if ((s = strstr(s, "]]>")))
bosko001 2:45b351b4fc2a 552 dxml_char_content(root, d + 8, (s += 2) - d - 10, 'c');
bosko001 2:45b351b4fc2a 553 else return dxml_err(root, d, "unclosed <![CDATA[");
bosko001 2:45b351b4fc2a 554 }
bosko001 2:45b351b4fc2a 555 else if (! strncmp(s, "!DOCTYPE", 8)) { // dtd
bosko001 2:45b351b4fc2a 556 for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' ||
bosko001 2:45b351b4fc2a 557 *(s + strspn(s + 1, dxml_WS) + 1) != '>')));
bosko001 2:45b351b4fc2a 558 l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
bosko001 2:45b351b4fc2a 559 if (! *s && e != '>')
bosko001 2:45b351b4fc2a 560 return dxml_err(root, d, "unclosed <!DOCTYPE");
bosko001 2:45b351b4fc2a 561 d = (l) ? strchr(d, '[') + 1 : d;
bosko001 2:45b351b4fc2a 562 if (l && ! dxml_internal_dtd(root, d, s++ - d)) return &root->xml;
bosko001 2:45b351b4fc2a 563 }
bosko001 2:45b351b4fc2a 564 else if (*s == '?') { // <?...?> processing instructions
bosko001 2:45b351b4fc2a 565 do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
bosko001 2:45b351b4fc2a 566 if (! s || (! *s && e != '>'))
bosko001 2:45b351b4fc2a 567 return dxml_err(root, d, "unclosed <?");
bosko001 2:45b351b4fc2a 568 else dxml_proc_inst(root, d + 1, s - d - 2);
bosko001 2:45b351b4fc2a 569 }
bosko001 2:45b351b4fc2a 570 else return dxml_err(root, d, "unexpected <");
bosko001 2:45b351b4fc2a 571
bosko001 2:45b351b4fc2a 572 if (! s || ! *s) break;
bosko001 2:45b351b4fc2a 573 *s = '\0';
bosko001 2:45b351b4fc2a 574 d = ++s;
bosko001 2:45b351b4fc2a 575 if (*s && *s != '<') { // tag character content
bosko001 2:45b351b4fc2a 576 while (*s && *s != '<') s++;
bosko001 2:45b351b4fc2a 577 if (*s) dxml_char_content(root, d, s - d, '&');
bosko001 2:45b351b4fc2a 578 else break;
bosko001 2:45b351b4fc2a 579 }
bosko001 2:45b351b4fc2a 580 else if (! *s) break;
bosko001 2:45b351b4fc2a 581 }
bosko001 2:45b351b4fc2a 582
bosko001 2:45b351b4fc2a 583 if (! root->cur) return &root->xml;
bosko001 2:45b351b4fc2a 584 else if (! root->cur->name) return dxml_err(root, d, "root tag missing");
bosko001 2:45b351b4fc2a 585 else return dxml_err(root, d, "unclosed tag <%s>", root->cur->name);
bosko001 2:45b351b4fc2a 586 }
bosko001 2:45b351b4fc2a 587
bosko001 2:45b351b4fc2a 588 // Wrapper for dxml_parse_str() that accepts a file stream. Reads the entire
bosko001 2:45b351b4fc2a 589 // stream into memory and then parses it. For xml files, use dxml_parse_file()
bosko001 2:45b351b4fc2a 590 // or dxml_parse_fd()
bosko001 2:45b351b4fc2a 591 dxml_t dxml_parse_fp(FILE *fp)
bosko001 2:45b351b4fc2a 592 {
bosko001 2:45b351b4fc2a 593 dxml_root_t root;
bosko001 2:45b351b4fc2a 594 size_t l, len = 0;
bosko001 2:45b351b4fc2a 595 char *s;
bosko001 2:45b351b4fc2a 596
bosko001 2:45b351b4fc2a 597 if (! (s = (char*)malloc(dxml_BUFSIZE))) return NULL;
bosko001 2:45b351b4fc2a 598 do {
bosko001 2:45b351b4fc2a 599 len += (l = fread((s + len), 1, dxml_BUFSIZE, fp));
bosko001 2:45b351b4fc2a 600 if (l == dxml_BUFSIZE) s = (char*)realloc(s, len + dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 601 } while (s && l == dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 602
bosko001 2:45b351b4fc2a 603 if (! s) return NULL;
bosko001 2:45b351b4fc2a 604 root = (dxml_root_t)dxml_parse_str(s, len);
bosko001 2:45b351b4fc2a 605 root->len = -1; // so we know to free s in dxml_free()
bosko001 2:45b351b4fc2a 606 return &root->xml;
bosko001 2:45b351b4fc2a 607 }
bosko001 2:45b351b4fc2a 608 //
bosko001 2:45b351b4fc2a 609 //// A wrapper for dxml_parse_str() that accepts a file descriptor. First
bosko001 2:45b351b4fc2a 610 //// attempts to mem map the file. Failing that, reads the file into memory.
bosko001 2:45b351b4fc2a 611 //// Returns NULL on failure.
bosko001 2:45b351b4fc2a 612 //dxml_t dxml_parse_fd(int fd)
bosko001 2:45b351b4fc2a 613 //{
bosko001 2:45b351b4fc2a 614 // dxml_root_t root;
bosko001 2:45b351b4fc2a 615 // struct stat st;
bosko001 2:45b351b4fc2a 616 // size_t l;
bosko001 2:45b351b4fc2a 617 // void *m;
bosko001 2:45b351b4fc2a 618 //
bosko001 2:45b351b4fc2a 619 // if (fd < 0) return NULL;
bosko001 2:45b351b4fc2a 620 // fstat(fd, &st);
bosko001 2:45b351b4fc2a 621 //
bosko001 2:45b351b4fc2a 622 //#ifndef dxml_NOMMAP
bosko001 2:45b351b4fc2a 623 // l = (st.st_size + sysconf(_SC_PAGESIZE) - 1) & ~(sysconf(_SC_PAGESIZE) -1);
bosko001 2:45b351b4fc2a 624 // if ((m = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
bosko001 2:45b351b4fc2a 625 // MAP_FAILED) {
bosko001 2:45b351b4fc2a 626 // madvise(m, l, MADV_SEQUENTIAL); // optimize for sequential access
bosko001 2:45b351b4fc2a 627 // root = (dxml_root_t)dxml_parse_str(m, st.st_size);
bosko001 2:45b351b4fc2a 628 // madvise(m, root->len = l, MADV_NORMAL); // put it back to normal
bosko001 2:45b351b4fc2a 629 // }
bosko001 2:45b351b4fc2a 630 // else { // mmap failed, read file into memory
bosko001 2:45b351b4fc2a 631 //#endif // dxml_NOMMAP
bosko001 2:45b351b4fc2a 632 // l = read(fd, m = malloc(st.st_size), st.st_size);
bosko001 2:45b351b4fc2a 633 // root = (dxml_root_t)dxml_parse_str((char*)m, l);
bosko001 2:45b351b4fc2a 634 // root->len = -1; // so we know to free s in dxml_free()
bosko001 2:45b351b4fc2a 635 //#ifndef dxml_NOMMAP
bosko001 2:45b351b4fc2a 636 // }
bosko001 2:45b351b4fc2a 637 //#endif // dxml_NOMMAP
bosko001 2:45b351b4fc2a 638 // return &root->xml;
bosko001 2:45b351b4fc2a 639 //}
bosko001 2:45b351b4fc2a 640
bosko001 2:45b351b4fc2a 641 // Encodes ampersand sequences appending the results to *dst, reallocating *dst
bosko001 2:45b351b4fc2a 642 // if length excedes max. a is non-zero for attribute encoding. Returns *dst
bosko001 2:45b351b4fc2a 643 char *dxml_ampencode(const char *s, size_t len, char **dst, size_t *dlen,
bosko001 2:45b351b4fc2a 644 size_t *max, short a)
bosko001 2:45b351b4fc2a 645 {
bosko001 2:45b351b4fc2a 646 const char *e;
bosko001 2:45b351b4fc2a 647
bosko001 2:45b351b4fc2a 648 for (e = s + len; s != e; s++) {
bosko001 2:45b351b4fc2a 649 while (*dlen + 10 > *max) *dst = (char*)realloc(*dst, *max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 650
bosko001 2:45b351b4fc2a 651 switch (*s) {
bosko001 2:45b351b4fc2a 652 case '\0': return *dst;
bosko001 2:45b351b4fc2a 653 case '&': *dlen += sprintf(*dst + *dlen, "&amp;"); break;
bosko001 2:45b351b4fc2a 654 case '<': *dlen += sprintf(*dst + *dlen, "&lt;"); break;
bosko001 2:45b351b4fc2a 655 case '>': *dlen += sprintf(*dst + *dlen, "&gt;"); break;
bosko001 2:45b351b4fc2a 656 case '"': *dlen += sprintf(*dst + *dlen, (a) ? "&quot;" : "\""); break;
bosko001 2:45b351b4fc2a 657 case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "&#xA;" : "\n"); break;
bosko001 2:45b351b4fc2a 658 case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "&#x9;" : "\t"); break;
bosko001 2:45b351b4fc2a 659 case '\r': *dlen += sprintf(*dst + *dlen, "&#xD;"); break;
bosko001 2:45b351b4fc2a 660 default: (*dst)[(*dlen)++] = *s;
bosko001 2:45b351b4fc2a 661 }
bosko001 2:45b351b4fc2a 662 }
bosko001 2:45b351b4fc2a 663 return *dst;
bosko001 2:45b351b4fc2a 664 }
bosko001 2:45b351b4fc2a 665
bosko001 2:45b351b4fc2a 666 // Recursively converts each tag to xml appending it to *s. Reallocates *s if
bosko001 2:45b351b4fc2a 667 // its length excedes max. start is the location of the previous tag in the
bosko001 2:45b351b4fc2a 668 // parent tag's character content. Returns *s.
bosko001 2:45b351b4fc2a 669 char *dxml_toxml_r(dxml_t xml, char **s, size_t *len, size_t *max,
bosko001 2:45b351b4fc2a 670 size_t start, char ***attr)
bosko001 2:45b351b4fc2a 671 {
bosko001 2:45b351b4fc2a 672 int i, j;
bosko001 2:45b351b4fc2a 673 char *txt = (xml->parent) ? xml->parent->txt : strdup("");
bosko001 2:45b351b4fc2a 674 size_t off = 0;
bosko001 2:45b351b4fc2a 675
bosko001 2:45b351b4fc2a 676 // parent character content up to this tag
bosko001 2:45b351b4fc2a 677 *s = dxml_ampencode(txt + start, xml->off - start, s, len, max, 0);
bosko001 2:45b351b4fc2a 678
bosko001 2:45b351b4fc2a 679 while (*len + strlen(xml->name) + 4 > *max) // reallocate s
bosko001 2:45b351b4fc2a 680 *s = (char*)realloc(*s, *max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 681
bosko001 2:45b351b4fc2a 682 *len += sprintf(*s + *len, "<%s", xml->name); // open tag
bosko001 2:45b351b4fc2a 683 for (i = 0; xml->attr[i]; i += 2) { // tag attributes
bosko001 2:45b351b4fc2a 684 if (dxml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
bosko001 2:45b351b4fc2a 685 while (*len + strlen(xml->attr[i]) + 7 > *max) // reallocate s
bosko001 2:45b351b4fc2a 686 *s = (char*)realloc(*s, *max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 687
bosko001 2:45b351b4fc2a 688 *len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
bosko001 2:45b351b4fc2a 689 dxml_ampencode(xml->attr[i + 1], -1, s, len, max, 1);
bosko001 2:45b351b4fc2a 690 *len += sprintf(*s + *len, "\"");
bosko001 2:45b351b4fc2a 691 }
bosko001 2:45b351b4fc2a 692
bosko001 2:45b351b4fc2a 693 for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
bosko001 2:45b351b4fc2a 694 for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes
bosko001 2:45b351b4fc2a 695 if (! attr[i][j + 1] || dxml_attr(xml, attr[i][j]) != attr[i][j + 1])
bosko001 2:45b351b4fc2a 696 continue; // skip duplicates and non-values
bosko001 2:45b351b4fc2a 697 while (*len + strlen(attr[i][j]) + 7 > *max) // reallocate s
bosko001 2:45b351b4fc2a 698 *s = (char*)realloc(*s, *max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 699
bosko001 2:45b351b4fc2a 700 *len += sprintf(*s + *len, " %s=\"", attr[i][j]);
bosko001 2:45b351b4fc2a 701 dxml_ampencode(attr[i][j + 1], -1, s, len, max, 1);
bosko001 2:45b351b4fc2a 702 *len += sprintf(*s + *len, "\"");
bosko001 2:45b351b4fc2a 703 }
bosko001 2:45b351b4fc2a 704 *len += sprintf(*s + *len, ">");
bosko001 2:45b351b4fc2a 705
bosko001 2:45b351b4fc2a 706 *s = (xml->child) ? dxml_toxml_r(xml->child, s, len, max, 0, attr) //child
bosko001 2:45b351b4fc2a 707 : dxml_ampencode(xml->txt, -1, s, len, max, 0); //data
bosko001 2:45b351b4fc2a 708
bosko001 2:45b351b4fc2a 709 while (*len + strlen(xml->name) + 4 > *max) // reallocate s
bosko001 2:45b351b4fc2a 710 *s = (char*)realloc(*s, *max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 711
bosko001 2:45b351b4fc2a 712 *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
bosko001 2:45b351b4fc2a 713
bosko001 2:45b351b4fc2a 714 while (txt[off] && off < xml->off) off++; // make sure off is within bounds
bosko001 2:45b351b4fc2a 715 return (xml->ordered) ? dxml_toxml_r(xml->ordered, s, len, max, off, attr)
bosko001 2:45b351b4fc2a 716 : dxml_ampencode(txt + off, -1, s, len, max, 0);
bosko001 2:45b351b4fc2a 717 }
bosko001 2:45b351b4fc2a 718
bosko001 2:45b351b4fc2a 719 // Converts an dxml structure back to xml. Returns a string of xml data that
bosko001 2:45b351b4fc2a 720 // must be freed.
bosko001 2:45b351b4fc2a 721 char *dxml_toxml(dxml_t xml)
bosko001 2:45b351b4fc2a 722 {
bosko001 2:45b351b4fc2a 723 dxml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL;
bosko001 2:45b351b4fc2a 724 dxml_root_t root = (dxml_root_t)xml;
bosko001 2:45b351b4fc2a 725 size_t len = 0, max = dxml_BUFSIZE;
bosko001 2:45b351b4fc2a 726 char *s = strcpy((char*)malloc(max), ""), *t, *n;
bosko001 2:45b351b4fc2a 727 int i, j, k;
bosko001 2:45b351b4fc2a 728
bosko001 2:45b351b4fc2a 729 if (! xml || ! xml->name) return (char*)realloc(s, len + 1);
bosko001 2:45b351b4fc2a 730 while (root->xml.parent) root = (dxml_root_t)root->xml.parent; // root tag
bosko001 2:45b351b4fc2a 731
bosko001 2:45b351b4fc2a 732 for (i = 0; ! p && root->pi[i]; i++) { // pre-root processing instructions
bosko001 2:45b351b4fc2a 733 for (k = 2; root->pi[i][k - 1]; k++);
bosko001 2:45b351b4fc2a 734 for (j = 1; (n = root->pi[i][j]); j++) {
bosko001 2:45b351b4fc2a 735 if (root->pi[i][k][j - 1] == '>') continue; // not pre-root
bosko001 2:45b351b4fc2a 736 while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
bosko001 2:45b351b4fc2a 737 s = (char*)realloc(s, max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 738 len += sprintf(s + len, "<?%s%s%s?>\n", t, *n ? " " : "", n);
bosko001 2:45b351b4fc2a 739 }
bosko001 2:45b351b4fc2a 740 }
bosko001 2:45b351b4fc2a 741
bosko001 2:45b351b4fc2a 742 xml->parent = xml->ordered = NULL;
bosko001 2:45b351b4fc2a 743 s = dxml_toxml_r(xml, &s, &len, &max, 0, root->attr);
bosko001 2:45b351b4fc2a 744 xml->parent = p;
bosko001 2:45b351b4fc2a 745 xml->ordered = o;
bosko001 2:45b351b4fc2a 746
bosko001 2:45b351b4fc2a 747 for (i = 0; ! p && root->pi[i]; i++) { // post-root processing instructions
bosko001 2:45b351b4fc2a 748 for (k = 2; root->pi[i][k - 1]; k++);
bosko001 2:45b351b4fc2a 749 for (j = 1; (n = root->pi[i][j]); j++) {
bosko001 2:45b351b4fc2a 750 if (root->pi[i][k][j - 1] == '<') continue; // not post-root
bosko001 2:45b351b4fc2a 751 while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
bosko001 2:45b351b4fc2a 752 s = (char*)realloc(s, max += dxml_BUFSIZE);
bosko001 2:45b351b4fc2a 753 len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
bosko001 2:45b351b4fc2a 754 }
bosko001 2:45b351b4fc2a 755 }
bosko001 2:45b351b4fc2a 756 return (char*)realloc(s, len + 1);
bosko001 2:45b351b4fc2a 757 }
bosko001 2:45b351b4fc2a 758
bosko001 2:45b351b4fc2a 759 // free the memory allocated for the dxml structure
bosko001 2:45b351b4fc2a 760 void dxml_free(dxml_t xml)
bosko001 2:45b351b4fc2a 761 {
bosko001 2:45b351b4fc2a 762 dxml_root_t root = (dxml_root_t)xml;
bosko001 2:45b351b4fc2a 763 int i, j;
bosko001 2:45b351b4fc2a 764 char **a, *s;
bosko001 2:45b351b4fc2a 765
bosko001 2:45b351b4fc2a 766 if (! xml) return;
bosko001 2:45b351b4fc2a 767 dxml_free(xml->child);
bosko001 2:45b351b4fc2a 768 dxml_free(xml->ordered);
bosko001 2:45b351b4fc2a 769
bosko001 2:45b351b4fc2a 770 if (! xml->parent) { // free root tag allocations
bosko001 2:45b351b4fc2a 771 for (i = 10; root->ent[i]; i += 2) // 0 - 9 are default entites (<>&"')
bosko001 2:45b351b4fc2a 772 if ((s = root->ent[i + 1]) < root->s || s > root->e) free(s);
bosko001 2:45b351b4fc2a 773 free(root->ent); // free list of general entities
bosko001 2:45b351b4fc2a 774
bosko001 2:45b351b4fc2a 775 for (i = 0; (a = root->attr[i]); i++) {
bosko001 2:45b351b4fc2a 776 for (j = 1; a[j++]; j += 2) // free malloced attribute values
bosko001 2:45b351b4fc2a 777 if (a[j] && (a[j] < root->s || a[j] > root->e)) free(a[j]);
bosko001 2:45b351b4fc2a 778 free(a);
bosko001 2:45b351b4fc2a 779 }
bosko001 2:45b351b4fc2a 780 if (root->attr[0]) free(root->attr); // free default attribute list
bosko001 2:45b351b4fc2a 781
bosko001 2:45b351b4fc2a 782 for (i = 0; root->pi[i]; i++) {
bosko001 2:45b351b4fc2a 783 for (j = 1; root->pi[i][j]; j++);
bosko001 2:45b351b4fc2a 784 free(root->pi[i][j + 1]);
bosko001 2:45b351b4fc2a 785 free(root->pi[i]);
bosko001 2:45b351b4fc2a 786 }
bosko001 2:45b351b4fc2a 787 if (root->pi[0]) free(root->pi); // free processing instructions
bosko001 2:45b351b4fc2a 788
bosko001 2:45b351b4fc2a 789 if (root->len == -1) free(root->m); // malloced xml data
bosko001 2:45b351b4fc2a 790 #ifndef dxml_NOMMAP
bosko001 2:45b351b4fc2a 791 else if (root->len) munmap(root->m, root->len); // mem mapped xml data
bosko001 2:45b351b4fc2a 792 #endif // dxml_NOMMAP
bosko001 2:45b351b4fc2a 793 if (root->u) free(root->u); // utf8 conversion
bosko001 2:45b351b4fc2a 794 }
bosko001 2:45b351b4fc2a 795
bosko001 2:45b351b4fc2a 796 dxml_free_attr(xml->attr); // tag attributes
bosko001 2:45b351b4fc2a 797 if ((xml->flags & dxml_TXTM)) free(xml->txt); // character content
bosko001 2:45b351b4fc2a 798 if ((xml->flags & dxml_NAMEM)) free(xml->name); // tag name
bosko001 2:45b351b4fc2a 799 free(xml);
bosko001 2:45b351b4fc2a 800 }
bosko001 2:45b351b4fc2a 801
bosko001 2:45b351b4fc2a 802 // return parser error message or empty string if none
bosko001 2:45b351b4fc2a 803 const char *dxml_error(dxml_t xml)
bosko001 2:45b351b4fc2a 804 {
bosko001 2:45b351b4fc2a 805 while (xml && xml->parent) xml = xml->parent; // find root tag
bosko001 2:45b351b4fc2a 806 return (xml) ? ((dxml_root_t)xml)->err : "";
bosko001 2:45b351b4fc2a 807 }
bosko001 2:45b351b4fc2a 808
bosko001 2:45b351b4fc2a 809 // returns a new empty dxml structure with the given root tag name
bosko001 2:45b351b4fc2a 810 dxml_t dxml_new(const char *name)
bosko001 2:45b351b4fc2a 811 {
bosko001 2:45b351b4fc2a 812 static char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
bosko001 2:45b351b4fc2a 813 "apos;", "&#39;", "amp;", "&#38;", NULL };
bosko001 2:45b351b4fc2a 814 dxml_root_t root = (dxml_root_t)memset(malloc(sizeof(struct dxml_root)),
bosko001 2:45b351b4fc2a 815 '\0', sizeof(struct dxml_root));
bosko001 2:45b351b4fc2a 816 root->xml.name = (char *)name;
bosko001 2:45b351b4fc2a 817 root->cur = &root->xml;
bosko001 2:45b351b4fc2a 818 strcpy(root->err, root->xml.txt = "");
bosko001 2:45b351b4fc2a 819 root->ent = (char**)memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
bosko001 2:45b351b4fc2a 820 root->attr = root->pi = (char ***)(root->xml.attr = dxml_NIL);
bosko001 2:45b351b4fc2a 821 return &root->xml;
bosko001 2:45b351b4fc2a 822 }
bosko001 2:45b351b4fc2a 823
bosko001 2:45b351b4fc2a 824 // inserts an existing tag into an dxml structure
bosko001 2:45b351b4fc2a 825 dxml_t dxml_insert(dxml_t xml, dxml_t dest, size_t off)
bosko001 2:45b351b4fc2a 826 {
bosko001 2:45b351b4fc2a 827 dxml_t cur, prev, head;
bosko001 2:45b351b4fc2a 828
bosko001 2:45b351b4fc2a 829 xml->next = xml->sibling = xml->ordered = NULL;
bosko001 2:45b351b4fc2a 830 xml->off = off;
bosko001 2:45b351b4fc2a 831 xml->parent = dest;
bosko001 2:45b351b4fc2a 832
bosko001 2:45b351b4fc2a 833 if ((head = dest->child)) { // already have sub tags
bosko001 2:45b351b4fc2a 834 if (head->off <= off) { // not first subtag
bosko001 2:45b351b4fc2a 835 for (cur = head; cur->ordered && cur->ordered->off <= off;
bosko001 2:45b351b4fc2a 836 cur = cur->ordered);
bosko001 2:45b351b4fc2a 837 xml->ordered = cur->ordered;
bosko001 2:45b351b4fc2a 838 cur->ordered = xml;
bosko001 2:45b351b4fc2a 839 }
bosko001 2:45b351b4fc2a 840 else { // first subtag
bosko001 2:45b351b4fc2a 841 xml->ordered = head;
bosko001 2:45b351b4fc2a 842 dest->child = xml;
bosko001 2:45b351b4fc2a 843 }
bosko001 2:45b351b4fc2a 844
bosko001 2:45b351b4fc2a 845 for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name);
bosko001 2:45b351b4fc2a 846 prev = cur, cur = cur->sibling); // find tag type
bosko001 2:45b351b4fc2a 847 if (cur && cur->off <= off) { // not first of type
bosko001 2:45b351b4fc2a 848 while (cur->next && cur->next->off <= off) cur = cur->next;
bosko001 2:45b351b4fc2a 849 xml->next = cur->next;
bosko001 2:45b351b4fc2a 850 cur->next = xml;
bosko001 2:45b351b4fc2a 851 }
bosko001 2:45b351b4fc2a 852 else { // first tag of this type
bosko001 2:45b351b4fc2a 853 if (prev && cur) prev->sibling = cur->sibling; // remove old first
bosko001 2:45b351b4fc2a 854 xml->next = cur; // old first tag is now next
bosko001 2:45b351b4fc2a 855 for (cur = head, prev = NULL; cur && cur->off <= off;
bosko001 2:45b351b4fc2a 856 prev = cur, cur = cur->sibling); // new sibling insert point
bosko001 2:45b351b4fc2a 857 xml->sibling = cur;
bosko001 2:45b351b4fc2a 858 if (prev) prev->sibling = xml;
bosko001 2:45b351b4fc2a 859 }
bosko001 2:45b351b4fc2a 860 }
bosko001 2:45b351b4fc2a 861 else dest->child = xml; // only sub tag
bosko001 2:45b351b4fc2a 862
bosko001 2:45b351b4fc2a 863 return xml;
bosko001 2:45b351b4fc2a 864 }
bosko001 2:45b351b4fc2a 865
bosko001 2:45b351b4fc2a 866 // Adds a child tag. off is the offset of the child tag relative to the start
bosko001 2:45b351b4fc2a 867 // of the parent tag's character content. Returns the child tag.
bosko001 2:45b351b4fc2a 868 dxml_t dxml_add_child(dxml_t xml, const char *name, size_t off)
bosko001 2:45b351b4fc2a 869 {
bosko001 2:45b351b4fc2a 870 dxml_t child;
bosko001 2:45b351b4fc2a 871
bosko001 2:45b351b4fc2a 872 if (! xml) return NULL;
bosko001 2:45b351b4fc2a 873 child = (dxml_t)memset(malloc(sizeof(struct dxml)), '\0',
bosko001 2:45b351b4fc2a 874 sizeof(struct dxml));
bosko001 2:45b351b4fc2a 875 child->name = (char *)name;
bosko001 2:45b351b4fc2a 876 child->attr = dxml_NIL;
bosko001 2:45b351b4fc2a 877 child->txt = "";
bosko001 2:45b351b4fc2a 878
bosko001 2:45b351b4fc2a 879 return dxml_insert(child, xml, off);
bosko001 2:45b351b4fc2a 880 }
bosko001 2:45b351b4fc2a 881
bosko001 2:45b351b4fc2a 882 // sets the character content for the given tag and returns the tag
bosko001 2:45b351b4fc2a 883 dxml_t dxml_set_txt(dxml_t xml, const char *txt)
bosko001 2:45b351b4fc2a 884 {
bosko001 2:45b351b4fc2a 885 if (! xml) return NULL;
bosko001 2:45b351b4fc2a 886 if (xml->flags & dxml_TXTM) free(xml->txt); // existing txt was malloced
bosko001 2:45b351b4fc2a 887 xml->flags &= ~dxml_TXTM;
bosko001 2:45b351b4fc2a 888 xml->txt = (char *)txt;
bosko001 2:45b351b4fc2a 889 return xml;
bosko001 2:45b351b4fc2a 890 }
bosko001 2:45b351b4fc2a 891
bosko001 2:45b351b4fc2a 892 // Sets the given tag attribute or adds a new attribute if not found. A value
bosko001 2:45b351b4fc2a 893 // of NULL will remove the specified attribute. Returns the tag given.
bosko001 2:45b351b4fc2a 894 dxml_t dxml_set_attr(dxml_t xml, const char *name, const char *value)
bosko001 2:45b351b4fc2a 895 {
bosko001 2:45b351b4fc2a 896 int l = 0, c;
bosko001 2:45b351b4fc2a 897
bosko001 2:45b351b4fc2a 898 if (! xml) return NULL;
bosko001 2:45b351b4fc2a 899 while (xml->attr[l] && strcmp(xml->attr[l], name)) l += 2;
bosko001 2:45b351b4fc2a 900 if (! xml->attr[l]) { // not found, add as new attribute
bosko001 2:45b351b4fc2a 901 if (! value) return xml; // nothing to do
bosko001 2:45b351b4fc2a 902 if (xml->attr == dxml_NIL) { // first attribute
bosko001 2:45b351b4fc2a 903 xml->attr = (char**)malloc(4 * sizeof(char *));
bosko001 2:45b351b4fc2a 904 xml->attr[1] = strdup(""); // empty list of malloced names/vals
bosko001 2:45b351b4fc2a 905 }
bosko001 2:45b351b4fc2a 906 else xml->attr = (char**)realloc(xml->attr, (l + 4) * sizeof(char *));
bosko001 2:45b351b4fc2a 907
bosko001 2:45b351b4fc2a 908 xml->attr[l] = (char *)name; // set attribute name
bosko001 2:45b351b4fc2a 909 xml->attr[l + 2] = NULL; // null terminate attribute list
bosko001 2:45b351b4fc2a 910 xml->attr[l + 3] = (char*)realloc(xml->attr[l + 1],
bosko001 2:45b351b4fc2a 911 (c = strlen(xml->attr[l + 1])) + 2);
bosko001 2:45b351b4fc2a 912 strcpy(xml->attr[l + 3] + c, " "); // set name/value as not malloced
bosko001 2:45b351b4fc2a 913 if (xml->flags & dxml_DUP) xml->attr[l + 3][c] = dxml_NAMEM;
bosko001 2:45b351b4fc2a 914 }
bosko001 2:45b351b4fc2a 915 else if (xml->flags & dxml_DUP) free((char *)name); // name was strduped
bosko001 2:45b351b4fc2a 916
bosko001 2:45b351b4fc2a 917 for (c = l; xml->attr[c]; c += 2); // find end of attribute list
bosko001 2:45b351b4fc2a 918 if (xml->attr[c + 1][l / 2] & dxml_TXTM) free(xml->attr[l + 1]); //old val
bosko001 2:45b351b4fc2a 919 if (xml->flags & dxml_DUP) xml->attr[c + 1][l / 2] |= dxml_TXTM;
bosko001 2:45b351b4fc2a 920 else xml->attr[c + 1][l / 2] &= ~dxml_TXTM;
bosko001 2:45b351b4fc2a 921
bosko001 2:45b351b4fc2a 922 if (value) xml->attr[l + 1] = (char *)value; // set attribute value
bosko001 2:45b351b4fc2a 923 else { // remove attribute
bosko001 2:45b351b4fc2a 924 if (xml->attr[c + 1][l / 2] & dxml_NAMEM) free(xml->attr[l]);
bosko001 2:45b351b4fc2a 925 memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
bosko001 2:45b351b4fc2a 926 xml->attr = (char**)realloc(xml->attr, (c + 2) * sizeof(char *));
bosko001 2:45b351b4fc2a 927 memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
bosko001 2:45b351b4fc2a 928 (c / 2) - (l / 2)); // fix list of which name/vals are malloced
bosko001 2:45b351b4fc2a 929 }
bosko001 2:45b351b4fc2a 930 xml->flags &= ~dxml_DUP; // clear strdup() flag
bosko001 2:45b351b4fc2a 931 return xml;
bosko001 2:45b351b4fc2a 932 }
bosko001 2:45b351b4fc2a 933
bosko001 2:45b351b4fc2a 934 // sets a flag for the given tag and returns the tag
bosko001 2:45b351b4fc2a 935 dxml_t dxml_set_flag(dxml_t xml, short flag)
bosko001 2:45b351b4fc2a 936 {
bosko001 2:45b351b4fc2a 937 if (xml) xml->flags |= flag;
bosko001 2:45b351b4fc2a 938 return xml;
bosko001 2:45b351b4fc2a 939 }
bosko001 2:45b351b4fc2a 940
bosko001 2:45b351b4fc2a 941 // removes a tag along with its subtags without freeing its memory
bosko001 2:45b351b4fc2a 942 dxml_t dxml_cut(dxml_t xml)
bosko001 2:45b351b4fc2a 943 {
bosko001 2:45b351b4fc2a 944 dxml_t cur;
bosko001 2:45b351b4fc2a 945
bosko001 2:45b351b4fc2a 946 if (! xml) return NULL; // nothing to do
bosko001 2:45b351b4fc2a 947 if (xml->next) xml->next->sibling = xml->sibling; // patch sibling list
bosko001 2:45b351b4fc2a 948
bosko001 2:45b351b4fc2a 949 if (xml->parent) { // not root tag
bosko001 2:45b351b4fc2a 950 cur = xml->parent->child; // find head of subtag list
bosko001 2:45b351b4fc2a 951 if (cur == xml) xml->parent->child = xml->ordered; // first subtag
bosko001 2:45b351b4fc2a 952 else { // not first subtag
bosko001 2:45b351b4fc2a 953 while (cur->ordered != xml) cur = cur->ordered;
bosko001 2:45b351b4fc2a 954 cur->ordered = cur->ordered->ordered; // patch ordered list
bosko001 2:45b351b4fc2a 955
bosko001 2:45b351b4fc2a 956 cur = xml->parent->child; // go back to head of subtag list
bosko001 2:45b351b4fc2a 957 if (strcmp(cur->name, xml->name)) { // not in first sibling list
bosko001 2:45b351b4fc2a 958 while (strcmp(cur->sibling->name, xml->name))
bosko001 2:45b351b4fc2a 959 cur = cur->sibling;
bosko001 2:45b351b4fc2a 960 if (cur->sibling == xml) { // first of a sibling list
bosko001 2:45b351b4fc2a 961 cur->sibling = (xml->next) ? xml->next
bosko001 2:45b351b4fc2a 962 : cur->sibling->sibling;
bosko001 2:45b351b4fc2a 963 }
bosko001 2:45b351b4fc2a 964 else cur = cur->sibling; // not first of a sibling list
bosko001 2:45b351b4fc2a 965 }
bosko001 2:45b351b4fc2a 966
bosko001 2:45b351b4fc2a 967 while (cur->next && cur->next != xml) cur = cur->next;
bosko001 2:45b351b4fc2a 968 if (cur->next) cur->next = cur->next->next; // patch next list
bosko001 2:45b351b4fc2a 969 }
bosko001 2:45b351b4fc2a 970 }
bosko001 2:45b351b4fc2a 971 xml->ordered = xml->sibling = xml->next = NULL;
bosko001 2:45b351b4fc2a 972 return xml;
bosko001 2:45b351b4fc2a 973 }
bosko001 2:45b351b4fc2a 974
bosko001 2:45b351b4fc2a 975 #ifdef dxml_TEST // test harness
bosko001 2:45b351b4fc2a 976 int main(int argc, char **argv)
bosko001 2:45b351b4fc2a 977 {
bosko001 2:45b351b4fc2a 978 dxml_t xml;
bosko001 2:45b351b4fc2a 979 char *s;
bosko001 2:45b351b4fc2a 980 int i;
bosko001 2:45b351b4fc2a 981
bosko001 2:45b351b4fc2a 982 if (argc != 2) return fprintf(stderr, "usage: %s xmlfile\n", argv[0]);
bosko001 2:45b351b4fc2a 983
bosko001 2:45b351b4fc2a 984 xml = dxml_parse_file(argv[1]);
bosko001 2:45b351b4fc2a 985 printf("%s\n", (s = dxml_toxml(xml)));
bosko001 2:45b351b4fc2a 986 free(s);
bosko001 2:45b351b4fc2a 987 i = fprintf(stderr, "%s", dxml_error(xml));
bosko001 2:45b351b4fc2a 988 dxml_free(xml);
bosko001 2:45b351b4fc2a 989 return (i) ? 1 : 0;
bosko001 2:45b351b4fc2a 990 }
bosko001 2:45b351b4fc2a 991 #endif // dxml_TEST