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