Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML parser implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 22 | #include <assert.h> |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
| 24 | #include <stdint.h> |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Radek Krejci | 06a704e | 2015-04-22 14:50:49 +0200 | [diff] [blame] | 30 | #include "common.h" |
| 31 | #include "dict.h" |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 32 | #include "printer.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 33 | #include "tree_schema.h" |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 34 | #include "xml_private.h" |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 35 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 36 | #ifndef NDEBUG |
| 37 | unsigned int lineno, lws_lineno; |
| 38 | #define COUNTLINE(c) if ((c) == 0xa) {lineno++;} |
| 39 | #else |
Radek Krejci | 8dfe289 | 2015-05-28 15:04:25 +0200 | [diff] [blame] | 40 | #define lineno 0 |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 41 | #define COUNTLINE(C) |
| 42 | #endif |
| 43 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 44 | #define ign_xmlws(p) \ |
| 45 | while (is_xmlws(*p)) { \ |
| 46 | COUNTLINE(*p); \ |
| 47 | p++; \ |
| 48 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 49 | |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 50 | struct lyxml_ns * |
| 51 | lyxml_get_ns(struct lyxml_elem *elem, const char *prefix) |
| 52 | { |
| 53 | struct lyxml_attr *attr; |
| 54 | int len; |
| 55 | |
| 56 | if (!elem) { |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | if (!prefix) { |
| 61 | len = 0; |
| 62 | } else { |
| 63 | len = strlen(prefix) + 1; |
| 64 | } |
| 65 | |
| 66 | for (attr = elem->attr; attr; attr = attr->next) { |
| 67 | if (attr->type != LYXML_ATTR_NS) { |
| 68 | continue; |
| 69 | } |
| 70 | if (!attr->name) { |
| 71 | if (!len) { |
| 72 | /* default namespace found */ |
| 73 | if (!attr->value) { |
| 74 | /* empty default namespace -> no default namespace */ |
| 75 | return NULL; |
| 76 | } |
| 77 | return (struct lyxml_ns *)attr; |
| 78 | } |
| 79 | } else if (len && !memcmp(attr->name, prefix, len)) { |
| 80 | /* prefix found */ |
| 81 | return (struct lyxml_ns *)attr; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* go recursively */ |
| 86 | return lyxml_get_ns(elem->parent, prefix); |
| 87 | } |
| 88 | |
| 89 | struct lyxml_attr * |
| 90 | lyxml_dup_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr) |
| 91 | { |
| 92 | struct lyxml_attr *result, *a; |
| 93 | |
| 94 | if (!attr || !parent) { |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (attr->type == LYXML_ATTR_NS) { |
| 99 | /* this is correct, despite that all attributes seems like a standard |
| 100 | * attributes (struct lyxml_attr), some of them can be namespace |
| 101 | * definitions (and in that case they are struct lyxml_ns). |
| 102 | */ |
| 103 | result = (struct lyxml_attr *)calloc(1, sizeof (struct lyxml_ns)); |
| 104 | } else { |
| 105 | result = calloc(1, sizeof (struct lyxml_attr)); |
| 106 | } |
| 107 | result->value = lydict_insert(ctx, attr->value, 0); |
| 108 | result->name = lydict_insert(ctx, attr->name, 0); |
| 109 | result->type = attr->type; |
| 110 | |
| 111 | /* set namespace in case of standard attributes */ |
| 112 | if (result->type == LYXML_ATTR_STD && attr->ns) { |
| 113 | result->ns = lyxml_get_ns(parent, attr->ns->prefix); |
| 114 | } |
| 115 | |
| 116 | /* set parent pointer in case of namespace attribute */ |
| 117 | if (result->type == LYXML_ATTR_NS) { |
| 118 | ((struct lyxml_ns *)result)->parent = parent; |
| 119 | } |
| 120 | |
| 121 | /* put attribute into the parent's attributes list */ |
| 122 | if (parent->attr) { |
| 123 | /* go to the end of the list */ |
| 124 | for (a = parent->attr; a->next; a = a->next); |
| 125 | /* and append new attribute */ |
| 126 | a->next = result; |
| 127 | } else { |
| 128 | /* add the first attribute in the list */ |
| 129 | parent->attr = result; |
| 130 | } |
| 131 | |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | struct lyxml_elem * |
| 136 | lyxml_dup_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, struct lyxml_elem *parent, int recursive) |
| 137 | { |
| 138 | struct lyxml_elem *result, *child; |
| 139 | struct lyxml_attr *attr; |
| 140 | |
| 141 | if (!elem) { |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | result = calloc(1, sizeof *result); |
| 146 | result->content = lydict_insert(ctx, elem->content, 0); |
| 147 | result->name = lydict_insert(ctx, elem->name, 0); |
| 148 | result->flags = elem->flags; |
| 149 | #ifndef NDEBUG |
| 150 | result->line = elem->line; |
| 151 | #endif |
| 152 | result->prev = result; |
| 153 | |
| 154 | if (parent) { |
| 155 | lyxml_add_child(ctx, parent, result); |
| 156 | } |
| 157 | |
| 158 | /* namespace */ |
| 159 | if (elem->ns) { |
| 160 | result->ns = lyxml_get_ns(result, elem->ns->prefix); |
| 161 | } |
| 162 | |
| 163 | /* duplicate attributes */ |
| 164 | for (attr = elem->attr; attr; attr = attr->next) { |
| 165 | lyxml_dup_attr(ctx, result, attr); |
| 166 | } |
| 167 | |
| 168 | if (!recursive) { |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | /* duplicate children */ |
| 173 | LY_TREE_FOR(elem->child, child) { |
| 174 | lyxml_dup_elem(ctx, child, result, 1); |
| 175 | } |
| 176 | |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | static struct lyxml_ns * |
| 181 | lyxml_find_ns(struct lyxml_elem *elem, const char *prefix, const char *value) |
| 182 | { |
| 183 | int pref_match, val_match; |
| 184 | struct lyxml_attr *attr; |
| 185 | |
| 186 | if (!elem) { |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | for (attr = elem->attr; attr; attr = attr->next) { |
| 191 | if (attr->type != LYXML_ATTR_NS) { |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | pref_match = 0; |
| 196 | if (!prefix && !attr->name) { |
| 197 | pref_match = 1; |
| 198 | } |
| 199 | if (prefix && attr->name && !strcmp(attr->name, prefix)) { |
| 200 | pref_match = 1; |
| 201 | } |
| 202 | |
| 203 | val_match = 0; |
| 204 | if (!value && !attr->value) { |
| 205 | val_match = 1; |
| 206 | } |
| 207 | if (value && attr->value && !strcmp(attr->value, value)) { |
| 208 | val_match = 1; |
| 209 | } |
| 210 | |
| 211 | if (pref_match && val_match) { |
| 212 | return (struct lyxml_ns *)attr; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* go recursively */ |
| 217 | return lyxml_find_ns(elem->parent, prefix, value); |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | lyxml_correct_ns(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns) |
| 222 | { |
| 223 | const struct lyxml_ns *elem_ns; |
| 224 | struct lyxml_elem *elem_root, *ns_root, *tmp; |
| 225 | |
| 226 | LY_TREE_DFS_BEGIN(elem, tmp, elem) { |
| 227 | if (elem->ns) { |
| 228 | /* find the root of elem */ |
| 229 | for (elem_root = elem; elem_root->parent; elem_root = elem_root->parent); |
| 230 | |
| 231 | /* find the root of elem NS */ |
| 232 | for (ns_root = elem->ns->parent; ns_root->parent; ns_root = ns_root->parent); |
| 233 | |
| 234 | /* elem NS is defined outside elem subtree */ |
| 235 | if (ns_root != elem_root) { |
| 236 | if (copy_ns) { |
| 237 | elem_ns = elem->ns; |
| 238 | /* we may have already copied the NS over? */ |
| 239 | elem->ns = lyxml_find_ns(elem, elem_ns->prefix, elem_ns->value); |
| 240 | |
| 241 | /* we haven't copied it over, copy it now */ |
| 242 | if (!elem->ns) { |
| 243 | elem->ns = (struct lyxml_ns *)lyxml_dup_attr(ctx, elem, (struct lyxml_attr *)elem_ns); |
| 244 | } |
| 245 | } else { |
| 246 | elem->ns = NULL; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | LY_TREE_DFS_END(elem, tmp, elem) |
| 251 | } |
| 252 | } |
| 253 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 254 | void |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 255 | lyxml_unlink_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 256 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 257 | struct lyxml_elem *parent, *first; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 258 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 259 | if (!elem) { |
| 260 | return; |
| 261 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 262 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 263 | /* store pointers to important nodes */ |
| 264 | parent = elem->parent; |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 265 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 266 | /* unlink from parent */ |
| 267 | if (parent) { |
| 268 | if (parent->child == elem) { |
| 269 | /* we unlink the first child */ |
| 270 | /* update the parent's link */ |
| 271 | parent->child = elem->next; |
| 272 | } |
| 273 | /* forget about the parent */ |
| 274 | elem->parent = NULL; |
| 275 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 276 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 277 | /* unlink from siblings */ |
| 278 | if (elem->prev == elem) { |
| 279 | /* there are no more siblings */ |
| 280 | return; |
| 281 | } |
| 282 | if (elem->next) { |
| 283 | elem->next->prev = elem->prev; |
| 284 | } else { |
| 285 | /* unlinking the last element */ |
| 286 | if (parent) { |
| 287 | first = parent->child; |
| 288 | } else { |
| 289 | first = elem; |
| 290 | while (elem->prev->next) { |
| 291 | first = elem->prev; |
| 292 | } |
| 293 | } |
| 294 | first->prev = elem->prev; |
| 295 | } |
| 296 | if (elem->prev->next) { |
| 297 | elem->prev->next = elem->next; |
| 298 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 299 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 300 | /* clean up the unlinked element */ |
| 301 | elem->next = NULL; |
| 302 | elem->prev = elem; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 303 | |
| 304 | lyxml_correct_ns(ctx, elem, copy_ns); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 307 | void |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 308 | lyxml_free_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 309 | { |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 310 | struct lyxml_attr *aiter, *aprev; |
| 311 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 312 | if (!attr) { |
| 313 | return; |
| 314 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 315 | |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 316 | if (parent) { |
| 317 | /* unlink attribute from the parent's list of attributes */ |
| 318 | aprev = NULL; |
| 319 | for (aiter = parent->attr; aiter; aiter = aiter->next) { |
| 320 | if (aiter == attr) { |
| 321 | break; |
| 322 | } |
| 323 | aprev = aiter; |
| 324 | } |
| 325 | if (!aiter) { |
| 326 | /* attribute to remove not found */ |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | if (!aprev) { |
| 331 | /* attribute is first in parent's list of attributes */ |
| 332 | parent->attr = attr->next; |
| 333 | } else { |
| 334 | /* reconnect previous attribute to the next */ |
| 335 | aprev->next = attr->next; |
| 336 | } |
| 337 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 338 | lydict_remove(ctx, attr->name); |
| 339 | lydict_remove(ctx, attr->value); |
| 340 | free(attr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 343 | void |
| 344 | lyxml_free_attrs(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 345 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 346 | struct lyxml_attr *a, *next; |
| 347 | if (!elem || !elem->attr) { |
| 348 | return; |
| 349 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 350 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 351 | a = elem->attr; |
| 352 | do { |
| 353 | next = a->next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 354 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 355 | lydict_remove(ctx, a->name); |
| 356 | lydict_remove(ctx, a->value); |
| 357 | free(a); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 358 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 359 | a = next; |
| 360 | } while (a); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 363 | static void |
| 364 | lyxml_free_elem_(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 365 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 366 | struct lyxml_elem *e, *next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 367 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 368 | if (!elem) { |
| 369 | return; |
| 370 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 371 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 372 | lyxml_free_attrs(ctx, elem); |
| 373 | LY_TREE_FOR_SAFE(elem->child, next, e) { |
| 374 | lyxml_free_elem_(ctx, e); |
| 375 | } |
| 376 | lydict_remove(ctx, elem->name); |
| 377 | lydict_remove(ctx, elem->content); |
| 378 | free(elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 381 | API void |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 382 | lyxml_free_elem(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 383 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 384 | if (!elem) { |
| 385 | return; |
| 386 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 387 | |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 388 | lyxml_unlink_elem(ctx, elem, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 389 | lyxml_free_elem_(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 392 | const char * |
| 393 | lyxml_get_attr(struct lyxml_elem *elem, const char *name, const char *ns) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 394 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 395 | struct lyxml_attr *a; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 396 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 397 | assert(elem); |
| 398 | assert(name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 399 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 400 | for (a = elem->attr; a; a = a->next) { |
| 401 | if (a->type != LYXML_ATTR_STD) { |
| 402 | continue; |
| 403 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 404 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 405 | if (!strcmp(name, a->name)) { |
| 406 | if ((!ns && !a->ns) || (ns && a->ns && !strcmp(ns, a->ns->value))) { |
| 407 | return a->value; |
| 408 | } |
| 409 | } |
| 410 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 411 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 412 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 415 | int |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 416 | lyxml_add_child(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 417 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 418 | struct lyxml_elem *e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 419 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 420 | assert(parent); |
| 421 | assert(elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 422 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 423 | /* (re)link element to parent */ |
| 424 | if (elem->parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 425 | lyxml_unlink_elem(ctx, elem, 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 426 | } |
| 427 | elem->parent = parent; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 428 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 429 | /* link parent to element */ |
| 430 | if (parent->child) { |
| 431 | e = parent->child; |
| 432 | elem->prev = e->prev; |
| 433 | elem->next = NULL; |
| 434 | elem->prev->next = elem; |
| 435 | e->prev = elem; |
| 436 | } else { |
| 437 | parent->child = elem; |
| 438 | elem->prev = elem; |
| 439 | elem->next = NULL; |
| 440 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 441 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 442 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Michal Vasko | 3b85572 | 2015-08-28 16:01:18 +0200 | [diff] [blame] | 445 | int |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 446 | lyxml_getutf8(const char *buf, unsigned int *read, unsigned int line) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 447 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 448 | int c, aux; |
| 449 | int i; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 450 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 451 | c = buf[0]; |
| 452 | *read = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 453 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 454 | /* buf is NULL terminated string, so 0 means EOF */ |
| 455 | if (!c) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 456 | LOGVAL(LYE_EOF, line); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | *read = 1; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 460 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 461 | /* process character byte(s) */ |
| 462 | if ((c & 0xf8) == 0xf0) { |
| 463 | /* four bytes character */ |
| 464 | *read = 4; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 465 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 466 | c &= 0x07; |
| 467 | for (i = 1; i <= 3; i++) { |
| 468 | aux = buf[i]; |
| 469 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 470 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 471 | return 0; |
| 472 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 473 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 474 | c = (c << 6) | (aux & 0x3f); |
| 475 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 476 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 477 | if (c < 0x1000 || c > 0x10ffff) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 478 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | } else if ((c & 0xf0) == 0xe0) { |
| 482 | /* three bytes character */ |
| 483 | *read = 3; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 484 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 485 | c &= 0x0f; |
| 486 | for (i = 1; i <= 2; i++) { |
| 487 | aux = buf[i]; |
| 488 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 489 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 490 | return 0; |
| 491 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 492 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 493 | c = (c << 6) | (aux & 0x3f); |
| 494 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 495 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 496 | if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 497 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | } else if ((c & 0xe0) == 0xc0) { |
| 501 | /* two bytes character */ |
| 502 | *read = 2; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 503 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 504 | aux = buf[1]; |
| 505 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 506 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 510 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 511 | if (c < 0x80) { |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 512 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | } else if (!(c & 0x80)) { |
| 516 | /* one byte character */ |
| 517 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 518 | /* invalid character */ |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 519 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | } else { |
| 523 | /* invalid character */ |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 524 | LOGVAL(LYE_XML_INVAL, line, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 525 | return 0; |
| 526 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 527 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 528 | return c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 531 | /** |
| 532 | * Store UTF-8 character specified as 4byte integer into the dst buffer. |
| 533 | * Returns number of written bytes (4 max), expects that dst has enough space. |
| 534 | * |
| 535 | * UTF-8 mapping: |
| 536 | * 00000000 -- 0000007F: 0xxxxxxx |
| 537 | * 00000080 -- 000007FF: 110xxxxx 10xxxxxx |
| 538 | * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx |
| 539 | * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 540 | * |
| 541 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 542 | static unsigned int |
| 543 | pututf8(char *dst, int32_t value) |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 544 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 545 | if (value < 0x80) { |
| 546 | /* one byte character */ |
| 547 | dst[0] = value; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 548 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 549 | return 1; |
| 550 | } else if (value < 0x800) { |
| 551 | /* two bytes character */ |
| 552 | dst[0] = 0xc0 | (value >> 6); |
| 553 | dst[1] = 0x80 | (value & 0x3f); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 554 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 555 | return 2; |
| 556 | } else if (value < 0x10000) { |
| 557 | /* three bytes character */ |
| 558 | dst[0] = 0xe0 | (value >> 12); |
| 559 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 560 | dst[2] = 0x80 | (value & 0x3f); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 561 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 562 | return 3; |
| 563 | } else if (value < 0x200000) { |
| 564 | /* four bytes character */ |
| 565 | dst[0] = 0xf0 | (value >> 18); |
| 566 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 567 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 568 | dst[3] = 0x80 | (value & 0x3f); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 569 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 570 | return 4; |
| 571 | } else { |
| 572 | /* out of range */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 573 | LOGVAL(LYE_SPEC, lineno, "Invalid UTF-8 value 0x%08x", value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 574 | return 0; |
| 575 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 578 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 579 | static int |
| 580 | parse_ignore(const char *data, const char *endstr, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 581 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 582 | unsigned int slen; |
| 583 | const char *c = data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 584 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 585 | slen = strlen(endstr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 586 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 587 | while (*c && memcmp(c, endstr, slen)) { |
| 588 | COUNTLINE(*c); |
| 589 | c++; |
| 590 | } |
| 591 | if (!*c) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 592 | LOGVAL(LYE_XML_MISS, lineno, "closing sequence", endstr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 593 | return EXIT_FAILURE; |
| 594 | } |
| 595 | c += slen; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 596 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 597 | *len = c - data; |
| 598 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 601 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 602 | static char * |
| 603 | parse_text(const char *data, char delim, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 604 | { |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 605 | #define BUFSIZE 1024 |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 606 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 607 | char buf[BUFSIZE]; |
| 608 | char *result = NULL, *aux; |
| 609 | unsigned int r; |
| 610 | int o, size = 0; |
| 611 | int cdsect = 0; |
| 612 | int32_t n; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 613 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 614 | for (*len = o = 0; cdsect || data[*len] != delim; o++) { |
| 615 | if (!data[*len] || (!cdsect && !memcmp(&data[*len], "]]>", 2))) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 616 | LOGVAL(LYE_XML_INVAL, lineno, "element content, \"]]>\" found"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 617 | goto error; |
| 618 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 619 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 620 | loop: |
| 621 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 622 | if (o > BUFSIZE - 3) { |
| 623 | /* add buffer into the result */ |
| 624 | if (result) { |
| 625 | size = size + o; |
| 626 | aux = realloc(result, size + 1); |
| 627 | result = aux; |
| 628 | } else { |
| 629 | size = o; |
| 630 | result = malloc((size + 1) * sizeof *result); |
| 631 | } |
| 632 | memcpy(&result[size - o], buf, o); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 633 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 634 | /* write again into the beginning of the buffer */ |
| 635 | o = 0; |
| 636 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 637 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 638 | if (cdsect || !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 639 | /* CDSect */ |
| 640 | if (!cdsect) { |
| 641 | cdsect = 1; |
| 642 | *len += 9; |
| 643 | } |
| 644 | if (data[*len] && !memcmp(&data[*len], "]]>", 3)) { |
| 645 | *len += 3; |
| 646 | cdsect = 0; |
| 647 | o--; /* we don't write any data in this iteration */ |
| 648 | } else { |
| 649 | buf[o] = data[*len]; |
| 650 | (*len)++; |
| 651 | } |
| 652 | } else if (data[*len] == '&') { |
| 653 | (*len)++; |
| 654 | if (data[*len] != '#') { |
| 655 | /* entity reference - only predefined refs are supported */ |
| 656 | if (!memcmp(&data[*len], "lt;", 3)) { |
| 657 | buf[o] = '<'; |
| 658 | *len += 3; |
| 659 | } else if (!memcmp(&data[*len], "gt;", 3)) { |
| 660 | buf[o] = '>'; |
| 661 | *len += 3; |
| 662 | } else if (!memcmp(&data[*len], "amp;", 4)) { |
| 663 | buf[o] = '&'; |
| 664 | *len += 4; |
| 665 | } else if (!memcmp(&data[*len], "apos;", 5)) { |
| 666 | buf[o] = '\''; |
| 667 | *len += 5; |
| 668 | } else if (!memcmp(&data[*len], "quot;", 5)) { |
| 669 | buf[o] = '\"'; |
| 670 | *len += 5; |
| 671 | } else { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 672 | LOGVAL(LYE_XML_INVAL, lineno, "entity reference (only predefined references are supported)"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 673 | goto error; |
| 674 | } |
| 675 | } else { |
| 676 | /* character reference */ |
| 677 | (*len)++; |
| 678 | if (isdigit(data[*len])) { |
| 679 | for (n = 0; isdigit(data[*len]); (*len)++) { |
| 680 | n = (10 * n) + (data[*len] - '0'); |
| 681 | } |
| 682 | if (data[*len] != ';') { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 683 | LOGVAL(LYE_XML_INVAL, lineno, "character reference, missing semicolon"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 684 | goto error; |
| 685 | } |
| 686 | } else if (data[(*len)++] == 'x' && isxdigit(data[*len])) { |
| 687 | for (n = 0; isxdigit(data[*len]); (*len)++) { |
| 688 | if (isdigit(data[*len])) { |
| 689 | r = (data[*len] - '0'); |
| 690 | } else if (data[*len] > 'F') { |
| 691 | r = 10 + (data[*len] - 'a'); |
| 692 | } else { |
| 693 | r = 10 + (data[*len] - 'A'); |
| 694 | } |
| 695 | n = (16 * n) + r; |
| 696 | } |
| 697 | } else { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 698 | LOGVAL(LYE_XML_INVAL, lineno, "character reference"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 699 | goto error; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 700 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 701 | } |
| 702 | r = pututf8(&buf[o], n); |
| 703 | if (!r) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 704 | LOGVAL(LYE_XML_INVAL, lineno, "character reference value"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 705 | goto error; |
| 706 | } |
| 707 | o += r - 1; /* o is ++ in for loop */ |
| 708 | (*len)++; |
| 709 | } |
| 710 | } else { |
| 711 | buf[o] = data[*len]; |
| 712 | COUNTLINE(buf[o]); |
| 713 | (*len)++; |
| 714 | } |
| 715 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 716 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 717 | if (delim == '<' && !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 718 | /* ignore loop's end condition on beginning of CDSect */ |
| 719 | goto loop; |
| 720 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 721 | #undef BUFSIZE |
| 722 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 723 | if (o) { |
| 724 | if (result) { |
| 725 | size = size + o; |
| 726 | aux = realloc(result, size + 1); |
| 727 | result = aux; |
| 728 | } else { |
| 729 | size = o; |
| 730 | result = malloc((size + 1) * sizeof *result); |
| 731 | } |
| 732 | memcpy(&result[size - o], buf, o); |
| 733 | } |
| 734 | if (result) { |
| 735 | result[size] = '\0'; |
Radek Krejci | a526964 | 2015-07-20 19:04:11 +0200 | [diff] [blame] | 736 | } else { |
| 737 | size = 0; |
| 738 | result = strdup(""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 739 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 740 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 741 | return result; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 742 | |
| 743 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 744 | free(result); |
| 745 | return NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 748 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 749 | static struct lyxml_attr * |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 750 | parse_attr(struct ly_ctx *ctx, const char *data, unsigned int *len, struct lyxml_elem *parent) |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 751 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 752 | const char *c = data, *start, *delim; |
| 753 | char prefix[32]; |
| 754 | int uc; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 755 | struct lyxml_attr *attr = NULL, *a; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 756 | unsigned int size; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 757 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 758 | /* check if it is attribute or namespace */ |
| 759 | if (!memcmp(c, "xmlns", 5)) { |
| 760 | /* namespace */ |
| 761 | attr = calloc(1, sizeof (struct lyxml_ns)); |
| 762 | attr->type = LYXML_ATTR_NS; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 763 | ((struct lyxml_ns *)attr)->parent = parent; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 764 | c += 5; |
| 765 | if (*c != ':') { |
| 766 | /* default namespace, prefix will be empty */ |
| 767 | goto equal; |
| 768 | } |
| 769 | c++; /* go after ':' to the prefix value */ |
| 770 | } else { |
| 771 | /* attribute */ |
| 772 | attr = calloc(1, sizeof *attr); |
| 773 | attr->type = LYXML_ATTR_STD; |
| 774 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 775 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 776 | /* process name part of the attribute */ |
| 777 | start = c; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 778 | uc = lyxml_getutf8(c, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 779 | if (!is_xmlnamestartchar(uc)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 780 | LOGVAL(LYE_XML_INVAL, lineno, "NameStartChar of the attribute"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 781 | free(attr); |
| 782 | return NULL; |
| 783 | } |
| 784 | c += size; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 785 | uc = lyxml_getutf8(c, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 786 | while (is_xmlnamechar(uc)) { |
| 787 | if (attr->type == LYXML_ATTR_STD && *c == ':') { |
| 788 | /* attribute in a namespace */ |
| 789 | start = c + 1; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 790 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 791 | /* look for the prefix in namespaces */ |
| 792 | memcpy(prefix, data, c - data); |
| 793 | prefix[c - data] = '\0'; |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 794 | attr->ns = lyxml_get_ns(parent, prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 795 | } |
| 796 | c += size; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 797 | uc = lyxml_getutf8(c, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 798 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 799 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 800 | /* store the name */ |
| 801 | size = c - start; |
| 802 | attr->name = lydict_insert(ctx, start, size); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 803 | |
| 804 | equal: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 805 | /* check Eq mark that can be surrounded by whitespaces */ |
| 806 | ign_xmlws(c); |
| 807 | if (*c != '=') { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 808 | LOGVAL(LYE_XML_INVAL, lineno, "attribute definition, \"=\" expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 809 | goto error; |
| 810 | } |
| 811 | c++; |
| 812 | ign_xmlws(c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 813 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 814 | /* process value part of the attribute */ |
| 815 | if (!*c || (*c != '"' && *c != '\'')) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 816 | LOGVAL(LYE_XML_INVAL, lineno, "attribute value, \" or \' expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 817 | goto error; |
| 818 | } |
| 819 | delim = c; |
| 820 | attr->value = lydict_insert_zc(ctx, parse_text(++c, *delim, &size)); |
| 821 | if (ly_errno) { |
| 822 | goto error; |
| 823 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 824 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 825 | *len = c + size + 1 - data; /* +1 is delimiter size */ |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 826 | |
| 827 | /* put attribute into the parent's attributes list */ |
| 828 | if (parent->attr) { |
| 829 | /* go to the end of the list */ |
| 830 | for (a = parent->attr; a->next; a = a->next); |
| 831 | /* and append new attribute */ |
| 832 | a->next = attr; |
| 833 | } else { |
| 834 | /* add the first attribute in the list */ |
| 835 | parent->attr = attr; |
| 836 | } |
| 837 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 838 | return attr; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 839 | |
| 840 | error: |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 841 | lyxml_free_attr(ctx, NULL, attr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 842 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 843 | } |
| 844 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 845 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 846 | static struct lyxml_elem * |
| 847 | parse_elem(struct ly_ctx *ctx, const char *data, unsigned int *len, struct lyxml_elem *parent) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 848 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 849 | const char *c = data, *start, *e; |
| 850 | const char *lws; /* leading white space for handling mixed content */ |
| 851 | int uc; |
| 852 | char *str; |
| 853 | char prefix[32] = { 0 }; |
| 854 | unsigned int prefix_len = 0; |
| 855 | struct lyxml_elem *elem = NULL, *child; |
| 856 | struct lyxml_attr *attr; |
| 857 | unsigned int size; |
| 858 | int nons_flag = 0, closed_flag = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 859 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 860 | *len = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 862 | if (*c != '<') { |
| 863 | return NULL; |
| 864 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 865 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 866 | /* locate element name */ |
| 867 | c++; |
| 868 | e = c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 869 | |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 870 | uc = lyxml_getutf8(e, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 871 | if (!is_xmlnamestartchar(uc)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 872 | LOGVAL(LYE_XML_INVAL, lineno, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 873 | return NULL; |
| 874 | } |
| 875 | e += size; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 876 | uc = lyxml_getutf8(e, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 877 | while (is_xmlnamechar(uc)) { |
| 878 | if (*e == ':') { |
| 879 | if (prefix_len) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 880 | LOGVAL(LYE_XML_INVAL, lineno, "element name, multiple colons found"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 881 | goto error; |
| 882 | } |
| 883 | /* element in a namespace */ |
| 884 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 885 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 886 | /* look for the prefix in namespaces */ |
| 887 | memcpy(prefix, c, prefix_len = e - c); |
| 888 | prefix[prefix_len] = '\0'; |
| 889 | c = start; |
| 890 | } |
| 891 | e += size; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 892 | uc = lyxml_getutf8(e, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 893 | } |
| 894 | if (!*e) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 895 | LOGVAL(LYE_EOF, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 896 | return NULL; |
| 897 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 898 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 899 | /* allocate element structure */ |
| 900 | elem = calloc(1, sizeof *elem); |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 901 | #ifndef NDEBUG |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 902 | elem->line = lineno; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 903 | #endif |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 904 | elem->next = NULL; |
| 905 | elem->prev = elem; |
| 906 | if (parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 907 | lyxml_add_child(ctx, parent, elem); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 908 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 909 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 910 | /* store the name into the element structure */ |
| 911 | elem->name = lydict_insert(ctx, c, e - c); |
| 912 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 913 | |
| 914 | process: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 915 | ly_errno = 0; |
| 916 | ign_xmlws(c); |
| 917 | if (!memcmp("/>", c, 2)) { |
| 918 | /* we are done, it was EmptyElemTag */ |
| 919 | c += 2; |
| 920 | closed_flag = 1; |
| 921 | } else if (*c == '>') { |
| 922 | /* process element content */ |
| 923 | c++; |
| 924 | lws = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 925 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 926 | while (*c) { |
| 927 | if (!memcmp(c, "</", 2)) { |
| 928 | if (lws && !elem->child) { |
| 929 | /* leading white spaces were actually content */ |
| 930 | goto store_content; |
| 931 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 932 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 933 | /* Etag */ |
| 934 | c += 2; |
| 935 | /* get name and check it */ |
| 936 | e = c; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 937 | uc = lyxml_getutf8(e, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 938 | if (!is_xmlnamestartchar(uc)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 939 | LOGVAL(LYE_XML_INVAL, lineno, "NameStartChar of the attribute"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 940 | goto error; |
| 941 | } |
| 942 | e += size; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 943 | uc = lyxml_getutf8(e, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 944 | while (is_xmlnamechar(uc)) { |
| 945 | if (*e == ':') { |
| 946 | /* element in a namespace */ |
| 947 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 948 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 949 | /* look for the prefix in namespaces */ |
| 950 | if (memcmp(prefix, c, e - c)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 951 | LOGVAL(LYE_SPEC, lineno, |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 952 | "Mixed opening (%s) and closing element tags (different namespaces).", elem->name); |
| 953 | goto error; |
| 954 | } |
| 955 | c = start; |
| 956 | } |
| 957 | e += size; |
Michal Vasko | 076588e | 2015-10-06 15:39:57 +0200 | [diff] [blame] | 958 | uc = lyxml_getutf8(e, &size, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 959 | } |
| 960 | if (!*e) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 961 | LOGVAL(LYE_EOF, lineno); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 962 | goto error; |
| 963 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 964 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 965 | /* check that it corresponds to opening tag */ |
| 966 | size = e - c; |
| 967 | str = malloc((size + 1) * sizeof *str); |
| 968 | memcpy(str, c, e - c); |
| 969 | str[e - c] = '\0'; |
| 970 | if (size != strlen(elem->name) || memcmp(str, elem->name, size)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 971 | LOGVAL(LYE_SPEC, lineno, "Mixed opening (%s) and closing (%s) element tags.", elem->name, str); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 972 | free(str); |
| 973 | goto error; |
| 974 | } |
| 975 | free(str); |
| 976 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 977 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 978 | ign_xmlws(c); |
| 979 | if (*c != '>') { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 980 | LOGVAL(LYE_SPEC, lineno, "Close element tag \"%s\" contain additional data.", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 981 | goto error; |
| 982 | } |
| 983 | c++; |
| 984 | closed_flag = 1; |
| 985 | break; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 986 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 987 | } else if (!memcmp(c, "<?", 2)) { |
| 988 | if (lws) { |
| 989 | /* leading white spaces were only formatting */ |
| 990 | lws = NULL; |
| 991 | } |
| 992 | /* PI - ignore it */ |
| 993 | c += 2; |
| 994 | if (parse_ignore(c, "?>", &size)) { |
| 995 | goto error; |
| 996 | } |
| 997 | c += size; |
| 998 | } else if (!memcmp(c, "<!--", 4)) { |
| 999 | if (lws) { |
| 1000 | /* leading white spaces were only formatting */ |
| 1001 | lws = NULL; |
| 1002 | } |
| 1003 | /* Comment - ignore it */ |
| 1004 | c += 4; |
| 1005 | if (parse_ignore(c, "-->", &size)) { |
| 1006 | goto error; |
| 1007 | } |
| 1008 | c += size; |
| 1009 | } else if (!memcmp(c, "<![CDATA[", 9)) { |
| 1010 | /* CDSect */ |
| 1011 | goto store_content; |
| 1012 | } else if (*c == '<') { |
| 1013 | if (lws) { |
| 1014 | if (elem->flags & LYXML_ELEM_MIXED) { |
| 1015 | /* we have a mixed content */ |
| 1016 | goto store_content; |
| 1017 | } else { |
| 1018 | /* leading white spaces were only formatting */ |
| 1019 | lws = NULL; |
| 1020 | } |
| 1021 | } |
| 1022 | if (elem->content) { |
| 1023 | /* we have a mixed content */ |
| 1024 | child = calloc(1, sizeof *child); |
| 1025 | child->content = elem->content; |
| 1026 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1027 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1028 | elem->flags |= LYXML_ELEM_MIXED; |
| 1029 | } |
| 1030 | child = parse_elem(ctx, c, &size, elem); |
| 1031 | if (!child) { |
| 1032 | goto error; |
| 1033 | } |
| 1034 | c += size; /* move after processed child element */ |
| 1035 | } else if (is_xmlws(*c)) { |
| 1036 | lws = c; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 1037 | #ifndef NDEBUG |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1038 | lws_lineno = lineno; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 1039 | #endif |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1040 | ign_xmlws(c); |
| 1041 | } else { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1042 | store_content: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1043 | /* store text content */ |
| 1044 | if (lws) { |
| 1045 | /* process content including the leading white spaces */ |
| 1046 | c = lws; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 1047 | #ifndef NDEBUG |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1048 | lineno = lws_lineno; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 1049 | #endif |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1050 | lws = NULL; |
| 1051 | } |
| 1052 | elem->content = lydict_insert_zc(ctx, parse_text(c, '<', &size)); |
| 1053 | if (ly_errno) { |
| 1054 | goto error; |
| 1055 | } |
| 1056 | c += size; /* move after processed text content */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1057 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1058 | if (elem->child) { |
| 1059 | /* we have a mixed content */ |
| 1060 | child = calloc(1, sizeof *child); |
| 1061 | child->content = elem->content; |
| 1062 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1063 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1064 | elem->flags |= LYXML_ELEM_MIXED; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | } else { |
| 1069 | /* process attribute */ |
| 1070 | attr = parse_attr(ctx, c, &size, elem); |
| 1071 | if (!attr) { |
| 1072 | goto error; |
| 1073 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1074 | c += size; /* move after processed attribute */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1075 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1076 | /* check namespace */ |
| 1077 | if (attr->type == LYXML_ATTR_NS) { |
| 1078 | if (!prefix[0] && !attr->name) { |
| 1079 | if (attr->value) { |
| 1080 | /* default prefix */ |
| 1081 | elem->ns = (struct lyxml_ns *)attr; |
| 1082 | } else { |
| 1083 | /* xmlns="" -> no namespace */ |
| 1084 | nons_flag = 1; |
| 1085 | } |
| 1086 | } else if (prefix[0] && attr->name && !memcmp(attr->name, prefix, prefix_len + 1)) { |
| 1087 | /* matching namespace with prefix */ |
| 1088 | elem->ns = (struct lyxml_ns *)attr; |
| 1089 | } |
| 1090 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1091 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1092 | /* go back to finish element processing */ |
| 1093 | goto process; |
| 1094 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1095 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1096 | *len = c - data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1097 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1098 | if (!closed_flag) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1099 | LOGVAL(LYE_XML_MISS, lineno, "closing element tag", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1100 | goto error; |
| 1101 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1102 | |
Radek Krejci | 78a230a | 2015-07-07 17:04:40 +0200 | [diff] [blame] | 1103 | if (!elem->ns && !nons_flag && parent) { |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 1104 | elem->ns = lyxml_get_ns(parent, prefix_len ? prefix : NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1105 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1106 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1107 | return elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1108 | |
| 1109 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1110 | lyxml_free_elem(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1111 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1112 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 1115 | /* logs directly */ |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1116 | API struct lyxml_elem * |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1117 | lyxml_read(struct ly_ctx *ctx, const char *data, int UNUSED(options)) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1118 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1119 | const char *c = data; |
| 1120 | unsigned int len; |
| 1121 | struct lyxml_elem *root = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1122 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 1123 | #ifndef NDEBUG |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1124 | /* TODO: threads support */ |
| 1125 | lineno = 1; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 1126 | #endif |
| 1127 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1128 | /* process document */ |
| 1129 | while (*c) { |
| 1130 | if (is_xmlws(*c)) { |
| 1131 | /* skip whitespaces */ |
| 1132 | ign_xmlws(c); |
| 1133 | } else if (!memcmp(c, "<?", 2)) { |
| 1134 | /* XMLDecl or PI - ignore it */ |
| 1135 | c += 2; |
| 1136 | if (parse_ignore(c, "?>", &len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1137 | return NULL; |
| 1138 | } |
| 1139 | c += len; |
| 1140 | } else if (!memcmp(c, "<!--", 4)) { |
| 1141 | /* Comment - ignore it */ |
| 1142 | c += 2; |
| 1143 | if (parse_ignore(c, "-->", &len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1144 | return NULL; |
| 1145 | } |
| 1146 | c += len; |
| 1147 | } else if (!memcmp(c, "<!", 2)) { |
| 1148 | /* DOCTYPE */ |
| 1149 | /* TODO - standalone ignore counting < and > */ |
| 1150 | LOGERR(LY_EINVAL, "DOCTYPE not supported in XML documents."); |
| 1151 | return NULL; |
| 1152 | } else if (*c == '<') { |
| 1153 | /* element - process it in next loop to strictly follow XML |
| 1154 | * format |
| 1155 | */ |
| 1156 | break; |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1157 | } else { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1158 | LOGVAL(LYE_XML_INCHAR, lineno, c); |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1159 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1160 | } |
| 1161 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1162 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1163 | root = parse_elem(ctx, c, &len, NULL); |
| 1164 | if (!root) { |
| 1165 | return NULL; |
| 1166 | } |
| 1167 | c += len; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1168 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1169 | /* ignore the rest of document where can be comments, PIs and whitespaces, |
| 1170 | * note that we are not detecting syntax errors in these parts |
| 1171 | */ |
| 1172 | ign_xmlws(c); |
| 1173 | if (*c) { |
| 1174 | LOGWRN("There are some not parsed data:\n%s", c); |
| 1175 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1176 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1177 | return root; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1178 | } |
| 1179 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1180 | API struct lyxml_elem * |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1181 | lyxml_read_fd(struct ly_ctx *ctx, int fd, int UNUSED(options)) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1182 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1183 | if (fd == -1 || !ctx) { |
| 1184 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1185 | return NULL; |
| 1186 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1187 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1188 | LOGERR(LY_EINT, "%s function is not implemented", __func__); |
| 1189 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1192 | API struct lyxml_elem * |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1193 | lyxml_read_file(struct ly_ctx *ctx, const char *filename, int UNUSED(options)) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1194 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1195 | if (!filename || !ctx) { |
| 1196 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1197 | return NULL; |
| 1198 | } |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1199 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1200 | LOGERR(LY_EINT, "%s function is not implemented", __func__); |
| 1201 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1202 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1203 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1204 | int |
| 1205 | lyxml_dump_text(struct lyout *out, const char *text) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1206 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1207 | unsigned int i, n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1208 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1209 | if (!text) { |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1213 | for (i = n = 0; text[i]; i++) { |
| 1214 | switch (text[i]) { |
| 1215 | case '&': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1216 | n += ly_print(out, "&"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1217 | break; |
| 1218 | case '<': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1219 | n += ly_print(out, "<"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1220 | break; |
| 1221 | case '>': |
| 1222 | /* not needed, just for readability */ |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1223 | n += ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1224 | break; |
| 1225 | default: |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1226 | ly_write(out, &text[i], 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1227 | n++; |
| 1228 | } |
| 1229 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1230 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1231 | return n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1234 | static int |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1235 | dump_elem(struct lyout *out, struct lyxml_elem *e, int level, int options) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1236 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1237 | int size = 0; |
| 1238 | struct lyxml_attr *a; |
| 1239 | struct lyxml_elem *child; |
| 1240 | const char *delim, *delim_outer; |
| 1241 | int indent; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1242 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1243 | if (!e->name) { |
| 1244 | /* mixed content */ |
| 1245 | if (e->content) { |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1246 | return lyxml_dump_text(out, e->content); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1247 | } else { |
| 1248 | return 0; |
| 1249 | } |
| 1250 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1251 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1252 | delim = delim_outer = "\n"; |
| 1253 | indent = 2 * level; |
| 1254 | if ((e->flags & LYXML_ELEM_MIXED) || (e->parent && (e->parent->flags & LYXML_ELEM_MIXED))) { |
| 1255 | delim = ""; |
| 1256 | } |
| 1257 | if (e->parent && (e->parent->flags & LYXML_ELEM_MIXED)) { |
| 1258 | delim_outer = ""; |
| 1259 | indent = 0; |
| 1260 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1261 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1262 | if (!options || (options & LYXML_DUMP_OPEN)) { |
| 1263 | /* opening tag */ |
| 1264 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1265 | size += ly_print(out, "%*s<%s:%s", indent, "", e->ns->prefix, e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1266 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1267 | size += ly_print(out, "%*s<%s", indent, "", e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1268 | } |
| 1269 | } else if (options & LYXML_DUMP_CLOSE) { |
| 1270 | indent = 0; |
| 1271 | goto close; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1272 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1273 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1274 | /* attributes */ |
| 1275 | for (a = e->attr; a; a = a->next) { |
| 1276 | if (a->type == LYXML_ATTR_NS) { |
| 1277 | if (a->name) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1278 | size += ly_print(out, " xmlns:%s=\"%s\"", a->name, a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1279 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1280 | size += ly_print(out, " xmlns=\"%s\"", a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1281 | } |
| 1282 | } else if (a->ns && a->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1283 | size += ly_print(out, " %s:%s=\"%s\"", a->ns->prefix, a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1284 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1285 | size += ly_print(out, " %s=\"%s\"", a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1286 | } |
| 1287 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1288 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1289 | /* apply options */ |
| 1290 | if (options == (LYXML_DUMP_OPEN | LYXML_DUMP_CLOSE)) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1291 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1292 | return size; |
| 1293 | } else if (options & LYXML_DUMP_OPEN) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1294 | ly_print(out, ">"); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1295 | return ++size; |
| 1296 | } else if (options & LYXML_DUMP_ATTRS) { |
| 1297 | return size; |
| 1298 | } |
| 1299 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1300 | if (!e->child && !e->content) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1301 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1302 | return size; |
| 1303 | } else if (e->content) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1304 | ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1305 | size++; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1306 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1307 | size += lyxml_dump_text(out, e->content); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1308 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1309 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1310 | size += ly_print(out, "</%s:%s>%s", e->ns->prefix, e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1311 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1312 | size += ly_print(out, "</%s>%s", e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1313 | } |
| 1314 | return size; |
| 1315 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1316 | size += ly_print(out, ">%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1317 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1318 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1319 | /* go recursively */ |
| 1320 | LY_TREE_FOR(e->child, child) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1321 | size += dump_elem(out, child, level + 1, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1322 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1323 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1324 | close: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1325 | /* closing tag */ |
| 1326 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1327 | size += ly_print(out, "%*s</%s:%s>%s", indent, "", e->ns->prefix, e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1328 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1329 | size += ly_print(out, "%*s</%s>%s", indent, "", e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1330 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1331 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1332 | return size; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1335 | API int |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1336 | lyxml_dump(FILE *stream, struct lyxml_elem *elem, int options) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1337 | { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1338 | struct lyout out; |
| 1339 | |
| 1340 | if (!stream || !elem) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1341 | return 0; |
| 1342 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1343 | |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1344 | out.type = LYOUT_STREAM; |
| 1345 | out.method.f = stream; |
| 1346 | |
| 1347 | return dump_elem(&out, elem, 0, options); |
| 1348 | } |
| 1349 | |
| 1350 | API int |
| 1351 | lyxml_dump_fd(int fd, struct lyxml_elem *elem, int options) |
| 1352 | { |
| 1353 | struct lyout out; |
| 1354 | |
| 1355 | if (fd < 0 || !elem) { |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | out.type = LYOUT_FD; |
| 1360 | out.method.fd = fd; |
| 1361 | |
| 1362 | return dump_elem(&out, elem, 0, options); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1363 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1364 | |
| 1365 | API int |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame^] | 1366 | lyxml_dump_mem(char **strp, struct lyxml_elem *elem, int options) |
| 1367 | { |
| 1368 | struct lyout out; |
| 1369 | int r; |
| 1370 | |
| 1371 | if (!strp || !elem) { |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | out.type = LYOUT_MEMORY; |
| 1376 | out.method.mem.buf = NULL; |
| 1377 | out.method.mem.len = 0; |
| 1378 | out.method.mem.size = 0; |
| 1379 | |
| 1380 | r = dump_elem(&out, elem, 0, options); |
| 1381 | |
| 1382 | *strp = out.method.mem.buf; |
| 1383 | return r; |
| 1384 | } |
| 1385 | |
| 1386 | API int |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame] | 1387 | lyxml_dump_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, struct lyxml_elem *elem, int options) |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1388 | { |
| 1389 | struct lyout out; |
| 1390 | |
| 1391 | if (!writeclb || !elem) { |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | out.type = LYOUT_CALLBACK; |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame] | 1396 | out.method.clb.f = writeclb; |
| 1397 | out.method.clb.arg = arg; |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1398 | |
| 1399 | return dump_elem(&out, elem, 0, options); |
| 1400 | } |