Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 2 | /** |
| 3 | * @file tree_data.c |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 5 | * @brief Manipulation with libyang data structures |
| 6 | * |
| 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of the Company nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this |
| 20 | * software without specific prior written permission. |
| 21 | */ |
| 22 | #define _GNU_SOURCE |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 23 | #define _XOPEN_SOURCE 700 |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
| 26 | #include <ctype.h> |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 27 | #include <limits.h> |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 28 | #include <stdarg.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 29 | #include <stdlib.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 30 | #include <sys/types.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 31 | #include <sys/mman.h> |
| 32 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 35 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 36 | #include <errno.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 38 | #include "libyang.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 39 | #include "common.h" |
| 40 | #include "context.h" |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 41 | #include "tree_data.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 42 | #include "parser.h" |
| 43 | #include "resolve.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 44 | #include "xml_internal.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 45 | #include "tree_internal.h" |
| 46 | #include "validation.h" |
| 47 | |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 48 | static struct lyd_node * |
| 49 | lyd_parse_(struct ly_ctx *ctx, const struct lys_node *parent, const char *data, LYD_FORMAT format, int options) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 50 | { |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 51 | struct lyxml_elem *xml; |
| 52 | struct lyd_node *result = NULL; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 53 | int xmlopt = LYXML_READ_MULTIROOT; |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 54 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 55 | if (!ctx || !data) { |
| 56 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 60 | if (options & LYD_OPT_NOSIBLINGS) { |
| 61 | xmlopt = 0; |
| 62 | } |
| 63 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 64 | switch (format) { |
| 65 | case LYD_XML: |
Radek Krejci | 452fb95 | 2015-10-02 16:07:46 +0200 | [diff] [blame] | 66 | case LYD_XML_FORMAT: |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 67 | xml = lyxml_read_data(ctx, data, xmlopt); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 68 | result = lyd_parse_xml(ctx, &xml, options, parent); |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 69 | lyxml_free(ctx, xml); |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 70 | break; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 71 | case LYD_JSON: |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 72 | result = lyd_parse_json(ctx, parent, data, options); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 73 | break; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 74 | default: |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 75 | /* error */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 76 | return NULL; |
| 77 | } |
| 78 | |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 79 | return result; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 82 | static struct lyd_node * |
| 83 | lyd_parse_data_(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, va_list ap) |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 84 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 85 | const struct lys_node *rpc = NULL; |
| 86 | |
| 87 | if (lyp_check_options(options)) { |
| 88 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | if (options & LYD_OPT_RPCREPLY) { |
| 93 | rpc = va_arg(ap, struct lys_node*); |
| 94 | if (!rpc || (rpc->nodetype != LYS_RPC)) { |
| 95 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return lyd_parse_(ctx, rpc, data, format, options); |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | API struct lyd_node * |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 104 | lyd_parse_data(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...) |
| 105 | { |
| 106 | va_list ap; |
| 107 | struct lyd_node *result; |
| 108 | |
| 109 | va_start(ap, options); |
| 110 | result = lyd_parse_data_(ctx, data, format, options, ap); |
| 111 | va_end(ap); |
| 112 | |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | API struct lyd_node * |
| 117 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...) |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 118 | { |
| 119 | struct lyd_node *ret; |
| 120 | struct stat sb; |
| 121 | char *data; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 122 | va_list ap; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 123 | |
| 124 | if (!ctx || (fd == -1)) { |
| 125 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | if (fstat(fd, &sb) == -1) { |
| 130 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
Radek Krejci | 3006be0 | 2015-12-17 11:24:33 +0100 | [diff] [blame] | 134 | data = mmap(NULL, sb.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 135 | if (data == MAP_FAILED) { |
| 136 | LOGERR(LY_ESYS, "Mapping file descriptor into memory failed."); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 140 | va_start(ap, options); |
| 141 | ret = lyd_parse_data_(ctx, data, format, options, ap); |
| 142 | |
| 143 | va_end(ap); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 144 | munmap(data, sb.st_size); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 145 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | API struct lyd_node * |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 150 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...) |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 151 | { |
| 152 | int fd; |
| 153 | struct lyd_node *ret; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 154 | va_list ap; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 155 | |
| 156 | if (!ctx || !path) { |
| 157 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | fd = open(path, O_RDONLY); |
| 162 | if (fd == -1) { |
| 163 | LOGERR(LY_ESYS, "Failed to open data file \"%s\" (%s).", path, strerror(errno)); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 167 | va_start(ap, options); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 168 | ret = lyd_parse_fd(ctx, fd, format, options); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 169 | |
| 170 | va_end(ap); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 171 | close(fd); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 172 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 177 | lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 178 | { |
| 179 | struct lyd_node *ret; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 180 | const struct lys_node *snode = NULL, *siblings; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 181 | |
| 182 | if ((!parent && !module) || !name) { |
| 183 | ly_errno = LY_EINVAL; |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | if (!parent) { |
| 188 | siblings = module->data; |
| 189 | } else { |
Michal Vasko | a5ef4d7 | 2015-09-29 16:05:21 +0200 | [diff] [blame] | 190 | if (!parent->schema) { |
| 191 | return NULL; |
| 192 | } |
| 193 | siblings = parent->schema->child; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 196 | if (lys_get_data_sibling(module, siblings, name, LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC, &snode) |
| 197 | || !snode) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 202 | if (!ret) { |
| 203 | LOGMEM; |
| 204 | return NULL; |
| 205 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 206 | ret->schema = (struct lys_node *)snode; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 207 | ret->prev = ret; |
| 208 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 209 | if (lyd_insert(parent, ret)) { |
| 210 | lyd_free(ret); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 218 | static struct lyd_node * |
| 219 | lyd_create_leaf(const struct lys_node *schema, const char *val_str) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 220 | { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 221 | int found; |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 222 | struct lyd_node_leaf_list *ret; |
| 223 | struct lys_type *stype, *type; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 224 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 225 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 226 | if (!ret) { |
| 227 | LOGMEM; |
| 228 | return NULL; |
| 229 | } |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 230 | ret->schema = (struct lys_node *)schema; |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 231 | ret->prev = (struct lyd_node *)ret; |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 232 | ret->value_str = lydict_insert(schema->module->ctx, val_str, 0); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 233 | |
| 234 | /* resolve the type correctly */ |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 235 | stype = &((struct lys_node_leaf *)schema)->type; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 236 | if (stype->base == LY_TYPE_UNION) { |
| 237 | found = 0; |
Radek Krejci | f738849 | 2015-10-30 16:12:53 +0100 | [diff] [blame] | 238 | type = NULL; |
| 239 | while ((type = lyp_get_next_union_type(stype, type, &found))) { |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 240 | ret->value_type = type->base; |
| 241 | if (!lyp_parse_value(ret, type, 1, NULL, UINT_MAX)) { |
| 242 | /* success! */ |
| 243 | break; |
| 244 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 245 | found = 0; |
Radek Krejci | f738849 | 2015-10-30 16:12:53 +0100 | [diff] [blame] | 246 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 247 | |
| 248 | if (!type) { |
| 249 | /* fail */ |
| 250 | ly_errno = LY_EINVAL; |
| 251 | lyd_free((struct lyd_node *)ret); |
| 252 | return NULL; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 253 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 254 | } else { |
| 255 | ret->value_type = stype->base; |
| 256 | if (lyp_parse_value(ret, stype, 1, NULL, 0)) { |
| 257 | lyd_free((struct lyd_node *)ret); |
Michal Vasko | 6a9dd1f | 2015-10-15 15:09:11 +0200 | [diff] [blame] | 258 | ly_errno = LY_EINVAL; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 259 | return NULL; |
| 260 | } |
| 261 | } |
| 262 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 263 | return (struct lyd_node *)ret; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | API struct lyd_node * |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 267 | lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 268 | { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 269 | struct lyd_node *ret; |
| 270 | const struct lys_node *snode = NULL, *siblings; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 271 | |
Michal Vasko | 14d8877 | 2015-12-03 10:54:59 +0100 | [diff] [blame] | 272 | if ((!parent && !module) || !name) { |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 273 | ly_errno = LY_EINVAL; |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | if (!parent) { |
| 278 | siblings = module->data; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 279 | } else { |
| 280 | if (!parent->schema) { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 281 | ly_errno = LY_EINVAL; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | siblings = parent->schema->child; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 287 | if (lys_get_data_sibling(module, siblings, name, LYS_LEAFLIST | LYS_LEAF, &snode) || !snode) { |
| 288 | ly_errno = LY_EINVAL; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 289 | return NULL; |
| 290 | } |
| 291 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 292 | ret = lyd_create_leaf(snode, val_str); |
| 293 | if (!ret) { |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | /* connect to parent */ |
| 298 | if (parent) { |
| 299 | if (lyd_insert(parent, ret)) { |
| 300 | lyd_free(ret); |
| 301 | return NULL; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return ret; |
| 306 | |
| 307 | } |
| 308 | |
| 309 | static struct lyd_node * |
| 310 | lyd_create_anyxml(const struct lys_node *schema, const char *val_xml) |
| 311 | { |
| 312 | struct lyd_node_anyxml *ret; |
| 313 | struct lyxml_elem *root; |
| 314 | char *xml; |
| 315 | |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 316 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 317 | if (!ret) { |
| 318 | LOGMEM; |
| 319 | return NULL; |
| 320 | } |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 321 | ret->schema = (struct lys_node *)schema; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 322 | ret->prev = (struct lyd_node *)ret; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 323 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 324 | /* store the anyxml data together with the anyxml element */ |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 325 | asprintf(&xml, "<%s>%s</%s>", schema->name, (val_xml ? val_xml : ""), schema->name); |
| 326 | root = lyxml_read_data(schema->module->ctx, xml, 0); |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 327 | free(xml); |
| 328 | if (!root) { |
| 329 | lyd_free((struct lyd_node *)ret); |
| 330 | return NULL; |
Michal Vasko | 14d8877 | 2015-12-03 10:54:59 +0100 | [diff] [blame] | 331 | } |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 332 | |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 333 | /* remove the root */ |
| 334 | ret->value = root->child; |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 335 | lyxml_unlink_elem(schema->module->ctx, root->child, 1); |
| 336 | lyxml_free(schema->module->ctx, root); |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 337 | |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 338 | return (struct lyd_node *)ret; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 341 | API struct lyd_node * |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 342 | lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_xml) |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 343 | { |
| 344 | struct lyd_node *ret; |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 345 | const struct lys_node *siblings, *snode; |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 346 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 347 | if ((!parent && !module) || !name) { |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 348 | ly_errno = LY_EINVAL; |
| 349 | return NULL; |
| 350 | } |
| 351 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 352 | if (!parent) { |
| 353 | siblings = module->data; |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 354 | } else { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 355 | if (!parent->schema) { |
| 356 | return NULL; |
| 357 | } |
| 358 | siblings = parent->schema->child; |
| 359 | } |
| 360 | |
| 361 | if (lys_get_data_sibling(module, siblings, name, LYS_ANYXML, &snode) || !snode) { |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | ret = lyd_create_anyxml(snode, val_xml); |
| 366 | if (!ret) { |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | /* connect to parent */ |
| 371 | if (parent) { |
| 372 | if (lyd_insert(parent, ret)) { |
| 373 | lyd_free(ret); |
| 374 | return NULL; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | API struct lyd_node * |
| 382 | lyd_output_new(const struct lys_node *schema) |
| 383 | { |
| 384 | struct lyd_node *ret; |
| 385 | |
| 386 | if (!schema || !(schema->nodetype & (LYS_CONTAINER | LYS_LIST)) |
| 387 | || !lys_parent(schema) || (lys_parent(schema)->nodetype != LYS_OUTPUT)) { |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 388 | ly_errno = LY_EINVAL; |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 393 | if (!ret) { |
| 394 | LOGMEM; |
| 395 | return NULL; |
| 396 | } |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 397 | ret->schema = (struct lys_node *)schema; |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 398 | ret->prev = ret; |
| 399 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame^] | 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | API struct lyd_node * |
| 404 | lyd_output_new_leaf(const struct lys_node *schema, const char *val_str) |
| 405 | { |
| 406 | if (!schema || (schema->nodetype != LYS_LEAF) |
| 407 | || !lys_parent(schema) || (lys_parent(schema)->nodetype != LYS_OUTPUT)) { |
| 408 | ly_errno = LY_EINVAL; |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | return lyd_create_leaf(schema, val_str); |
| 413 | } |
| 414 | |
| 415 | API struct lyd_node * |
| 416 | lyd_output_new_anyxml(const struct lys_node *schema, const char *val_xml) |
| 417 | { |
| 418 | if (!schema || (schema->nodetype != LYS_ANYXML) |
| 419 | || !lys_parent(schema) || (lys_parent(schema)->nodetype != LYS_OUTPUT)) { |
| 420 | ly_errno = LY_EINVAL; |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | return lyd_create_anyxml(schema, val_xml); |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 427 | API int |
| 428 | lyd_insert(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 429 | { |
| 430 | struct lys_node *sparent; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 431 | struct lyd_node *iter; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 432 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 433 | if (!node || !parent) { |
| 434 | ly_errno = LY_EINVAL; |
| 435 | return EXIT_FAILURE; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Michal Vasko | 72d65c9 | 2015-12-07 14:02:35 +0100 | [diff] [blame] | 438 | /* check placing the node to the appropriate place according to the schema (if LYS_OUTPUT is returned, |
| 439 | * the parent's schema will never match and it fails as it should) */ |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 440 | for (sparent = lys_parent(node->schema); |
Michal Vasko | 72d65c9 | 2015-12-07 14:02:35 +0100 | [diff] [blame] | 441 | sparent && !(sparent->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_OUTPUT | LYS_NOTIF)); |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 442 | sparent = lys_parent(sparent)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 443 | if (sparent != parent->schema) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 444 | return EXIT_FAILURE; |
| 445 | } |
| 446 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 447 | if (node->parent || node->prev->next) { |
| 448 | lyd_unlink(node); |
| 449 | } |
| 450 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 451 | if (!parent->child) { |
| 452 | /* add as the only child of the parent */ |
| 453 | parent->child = node; |
| 454 | } else { |
| 455 | /* add as the last child of the parent */ |
| 456 | parent->child->prev->next = node; |
| 457 | node->prev = parent->child->prev; |
| 458 | for (iter = node; iter->next; iter = iter->next); |
| 459 | parent->child->prev = iter; |
| 460 | } |
Michal Vasko | 9cc2d0a | 2015-10-14 15:49:07 +0200 | [diff] [blame] | 461 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 462 | LY_TREE_FOR(node, iter) { |
| 463 | iter->parent = parent; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return EXIT_SUCCESS; |
| 467 | } |
| 468 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 469 | static int |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 470 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, int before) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 471 | { |
| 472 | struct lys_node *par1, *par2; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 473 | struct lyd_node *iter, *last; |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 474 | |
| 475 | if (sibling == node) { |
| 476 | return EXIT_SUCCESS; |
| 477 | } |
| 478 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 479 | /* check placing the node to the appropriate place according to the schema */ |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 480 | for (par1 = lys_parent(sibling->schema); |
Radek Krejci | 2b7bb49 | 2015-12-02 15:46:29 +0100 | [diff] [blame] | 481 | par1 && !(par1->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 482 | par1 = lys_parent(par1)); |
| 483 | for (par2 = lys_parent(node->schema); |
Radek Krejci | 2b7bb49 | 2015-12-02 15:46:29 +0100 | [diff] [blame] | 484 | par2 && !(par2->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 485 | par2 = lys_parent(par2)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 486 | if (par1 != par2) { |
| 487 | ly_errno = LY_EINVAL; |
| 488 | return EXIT_FAILURE; |
| 489 | } |
| 490 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 491 | if (node->parent || node->prev->next) { |
| 492 | lyd_unlink(node); |
| 493 | } |
| 494 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 495 | LY_TREE_FOR(node, iter) { |
| 496 | iter->parent = sibling->parent; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 497 | last = iter; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 500 | if (before) { |
| 501 | if (sibling->prev->next) { |
| 502 | /* adding into the list */ |
| 503 | sibling->prev->next = node; |
| 504 | } else if (sibling->parent) { |
| 505 | /* at the beginning */ |
| 506 | sibling->parent->child = node; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 507 | } |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 508 | node->prev = sibling->prev; |
| 509 | sibling->prev = last; |
| 510 | last->next = sibling; |
| 511 | } else { |
| 512 | if (sibling->next) { |
| 513 | /* adding into a middle - fix the prev pointer of the node after inserted nodes */ |
| 514 | last->next = sibling->next; |
| 515 | sibling->next->prev = last; |
| 516 | } else { |
| 517 | /* at the end - fix the prev pointer of the first node */ |
| 518 | if (sibling->parent) { |
| 519 | sibling->parent->child->prev = last; |
| 520 | } else { |
| 521 | for (iter = sibling; iter->prev->next; iter = iter->prev); |
| 522 | iter->prev = last; |
| 523 | } |
| 524 | } |
| 525 | sibling->next = node; |
| 526 | node->prev = sibling; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 527 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 528 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 529 | return EXIT_SUCCESS; |
| 530 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 531 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 532 | API int |
| 533 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 534 | { |
| 535 | if (!node || !sibling || lyd_insert_sibling(sibling, node, 1)) { |
| 536 | ly_errno = LY_EINVAL; |
| 537 | return EXIT_FAILURE; |
| 538 | } |
| 539 | |
| 540 | return EXIT_SUCCESS; |
| 541 | } |
| 542 | |
| 543 | API int |
| 544 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 545 | { |
| 546 | if (!node || !sibling || lyd_insert_sibling(sibling, node, 0)) { |
| 547 | ly_errno = LY_EINVAL; |
| 548 | return EXIT_FAILURE; |
| 549 | } |
| 550 | |
| 551 | return EXIT_SUCCESS; |
| 552 | } |
| 553 | |
| 554 | API int |
| 555 | lyd_validate(struct lyd_node *node, int options) |
| 556 | { |
| 557 | struct lyd_node *next, *iter, *to_free = NULL; |
| 558 | |
| 559 | ly_errno = 0; |
| 560 | LY_TREE_DFS_BEGIN(node, next, iter) { |
| 561 | if (to_free) { |
| 562 | lyd_free(to_free); |
| 563 | to_free = NULL; |
| 564 | } |
| 565 | |
Radek Krejci | f03ec15 | 2015-12-03 13:18:37 +0100 | [diff] [blame] | 566 | if (lyv_data_context(iter, options, 0, NULL)) { |
| 567 | return EXIT_FAILURE; |
| 568 | } |
| 569 | if (lyv_data_content(iter, options, 0, NULL)) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 570 | if (ly_errno) { |
| 571 | return EXIT_FAILURE; |
| 572 | } else { |
| 573 | /* safe deferred removal */ |
| 574 | to_free = iter; |
Radek Krejci | efe6a14 | 2015-12-03 14:01:00 +0100 | [diff] [blame] | 575 | if (iter == node) { |
| 576 | /* removing the whole subtree */ |
| 577 | break; |
| 578 | } |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 579 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 580 | } |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 581 | |
| 582 | LY_TREE_DFS_END(node, next, iter); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 585 | if (to_free) { |
| 586 | lyd_free(to_free); |
| 587 | to_free = NULL; |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | return EXIT_SUCCESS; |
| 591 | } |
| 592 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 593 | /* create an attribute copy */ |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 594 | static struct lyd_attr * |
| 595 | lyd_dup_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr) |
| 596 | { |
| 597 | struct lyd_attr *ret; |
| 598 | |
| 599 | /* allocate new attr */ |
| 600 | if (!parent->attr) { |
| 601 | parent->attr = malloc(sizeof *parent->attr); |
| 602 | ret = parent->attr; |
| 603 | } else { |
| 604 | for (ret = parent->attr; ret->next; ret = ret->next); |
| 605 | ret->next = malloc(sizeof *ret); |
| 606 | ret = ret->next; |
| 607 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 608 | if (!ret) { |
| 609 | LOGMEM; |
| 610 | return NULL; |
| 611 | } |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 612 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 613 | /* fill new attr except */ |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 614 | ret->next = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 615 | ret->module = attr->module; |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 616 | ret->name = lydict_insert(ctx, attr->name, 0); |
| 617 | ret->value = lydict_insert(ctx, attr->value, 0); |
| 618 | |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 619 | return ret; |
| 620 | } |
| 621 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 622 | API int |
| 623 | lyd_unlink(struct lyd_node *node) |
| 624 | { |
| 625 | struct lyd_node *iter; |
| 626 | |
| 627 | if (!node) { |
| 628 | ly_errno = LY_EINVAL; |
| 629 | return EXIT_FAILURE; |
| 630 | } |
| 631 | |
| 632 | /* unlink from siblings */ |
| 633 | if (node->prev->next) { |
| 634 | node->prev->next = node->next; |
| 635 | } |
| 636 | if (node->next) { |
| 637 | node->next->prev = node->prev; |
| 638 | } else { |
| 639 | /* unlinking the last node */ |
Radek Krejci | 3263631 | 2016-01-07 13:49:48 +0100 | [diff] [blame] | 640 | if (node->parent) { |
| 641 | iter = node->parent->child; |
| 642 | } else { |
| 643 | iter = node->prev; |
| 644 | while (iter->prev != node) { |
| 645 | iter = iter->prev; |
| 646 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 647 | } |
| 648 | /* update the "last" pointer from the first node */ |
| 649 | iter->prev = node->prev; |
| 650 | } |
| 651 | |
| 652 | /* unlink from parent */ |
| 653 | if (node->parent) { |
| 654 | if (node->parent->child == node) { |
| 655 | /* the node is the first child */ |
| 656 | node->parent->child = node->next; |
| 657 | } |
| 658 | node->parent = NULL; |
| 659 | } |
| 660 | |
| 661 | node->next = NULL; |
| 662 | node->prev = node; |
| 663 | |
| 664 | return EXIT_SUCCESS; |
| 665 | } |
| 666 | |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 667 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 668 | lyd_dup(const struct lyd_node *node, int recursive) |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 669 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 670 | const struct lyd_node *next, *elem; |
| 671 | struct lyd_node *ret, *parent, *new_node; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 672 | struct lyd_attr *attr; |
| 673 | struct lyd_node_leaf_list *new_leaf; |
| 674 | struct lyd_node_anyxml *new_axml; |
| 675 | struct lys_type *type; |
| 676 | |
| 677 | if (!node) { |
| 678 | ly_errno = LY_EINVAL; |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | ret = NULL; |
| 683 | parent = NULL; |
| 684 | |
| 685 | /* LY_TREE_DFS */ |
| 686 | for (elem = next = node; elem; elem = next) { |
| 687 | |
| 688 | /* fill specific part */ |
| 689 | switch (elem->schema->nodetype) { |
| 690 | case LYS_LEAF: |
| 691 | case LYS_LEAFLIST: |
| 692 | new_leaf = malloc(sizeof *new_leaf); |
| 693 | new_node = (struct lyd_node *)new_leaf; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 694 | if (!new_node) { |
| 695 | LOGMEM; |
| 696 | return NULL; |
| 697 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 698 | |
| 699 | new_leaf->value = ((struct lyd_node_leaf_list *)elem)->value; |
| 700 | new_leaf->value_str = lydict_insert(elem->schema->module->ctx, |
| 701 | ((struct lyd_node_leaf_list *)elem)->value_str, 0); |
| 702 | new_leaf->value_type = ((struct lyd_node_leaf_list *)elem)->value_type; |
| 703 | /* bits type must be treated specially */ |
| 704 | if (new_leaf->value_type == LY_TYPE_BITS) { |
| 705 | for (type = &((struct lys_node_leaf *)elem->schema)->type; type->der->module; type = &type->der->type) { |
| 706 | if (type->base != LY_TYPE_BITS) { |
| 707 | LOGINT; |
Michal Vasko | d80e6c7 | 2015-10-15 09:37:01 +0200 | [diff] [blame] | 708 | lyd_free(new_node); |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 709 | lyd_free(ret); |
| 710 | return NULL; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | new_leaf->value.bit = malloc(type->info.bits.count * sizeof *new_leaf->value.bit); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 715 | if (!new_leaf->value.bit) { |
| 716 | LOGMEM; |
| 717 | lyd_free(new_node); |
| 718 | lyd_free(ret); |
| 719 | return NULL; |
| 720 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 721 | memcpy(new_leaf->value.bit, ((struct lyd_node_leaf_list *)elem)->value.bit, |
| 722 | type->info.bits.count * sizeof *new_leaf->value.bit); |
| 723 | } |
| 724 | break; |
| 725 | case LYS_ANYXML: |
| 726 | new_axml = malloc(sizeof *new_axml); |
| 727 | new_node = (struct lyd_node *)new_axml; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 728 | if (!new_node) { |
| 729 | LOGMEM; |
| 730 | return NULL; |
| 731 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 732 | |
| 733 | new_axml->value = lyxml_dup_elem(elem->schema->module->ctx, ((struct lyd_node_anyxml *)elem)->value, |
| 734 | NULL, 1); |
| 735 | break; |
| 736 | case LYS_CONTAINER: |
| 737 | case LYS_LIST: |
| 738 | case LYS_NOTIF: |
| 739 | case LYS_RPC: |
| 740 | new_node = malloc(sizeof *new_node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 741 | if (!new_node) { |
| 742 | LOGMEM; |
| 743 | return NULL; |
| 744 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 745 | new_node->child = NULL; |
| 746 | break; |
| 747 | default: |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 748 | lyd_free(ret); |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 749 | LOGINT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 750 | return NULL; |
| 751 | } |
| 752 | |
| 753 | /* fill common part */ |
| 754 | new_node->schema = elem->schema; |
| 755 | new_node->attr = NULL; |
| 756 | LY_TREE_FOR(elem->attr, attr) { |
| 757 | lyd_dup_attr(elem->schema->module->ctx, new_node, attr); |
| 758 | } |
| 759 | new_node->next = NULL; |
| 760 | new_node->prev = new_node; |
| 761 | new_node->parent = NULL; |
| 762 | |
| 763 | if (!ret) { |
| 764 | ret = new_node; |
| 765 | } |
| 766 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 767 | if (lyd_insert(parent, new_node)) { |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 768 | lyd_free(ret); |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 769 | LOGINT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 770 | return NULL; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | if (!recursive) { |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | /* LY_TREE_DFS_END */ |
| 779 | /* select element for the next run - children first */ |
| 780 | next = elem->child; |
| 781 | /* child exception for lyd_node_leaf and lyd_node_leaflist */ |
| 782 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 783 | next = NULL; |
| 784 | } |
| 785 | if (!next) { |
| 786 | /* no children, so try siblings */ |
| 787 | next = elem->next; |
| 788 | } else { |
| 789 | parent = new_node; |
| 790 | } |
| 791 | while (!next) { |
| 792 | /* no siblings, go back through parents */ |
| 793 | elem = elem->parent; |
| 794 | if (elem->parent == node->parent) { |
| 795 | break; |
| 796 | } |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 797 | if (!parent) { |
Michal Vasko | c8f5d80 | 2015-10-23 10:14:39 +0200 | [diff] [blame] | 798 | lyd_free(ret); |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 799 | LOGINT; |
Michal Vasko | c8f5d80 | 2015-10-23 10:14:39 +0200 | [diff] [blame] | 800 | return NULL; |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 801 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 802 | parent = parent->parent; |
| 803 | /* parent is already processed, go to its sibling */ |
| 804 | next = elem->next; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 811 | API void |
| 812 | lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 813 | { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 814 | struct lyd_attr *iter; |
| 815 | |
| 816 | if (!ctx || !attr) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 817 | return; |
| 818 | } |
| 819 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 820 | if (parent) { |
| 821 | if (parent->attr == attr) { |
| 822 | if (recursive) { |
| 823 | parent->attr = NULL; |
| 824 | } else { |
| 825 | parent->attr = attr->next; |
| 826 | } |
| 827 | } else { |
| 828 | for (iter = parent->attr; iter->next != attr; iter = iter->next); |
| 829 | if (iter->next) { |
| 830 | if (recursive) { |
| 831 | iter->next = NULL; |
| 832 | } else { |
| 833 | iter->next = attr->next; |
| 834 | } |
| 835 | } |
| 836 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 837 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 838 | |
| 839 | if (!recursive) { |
| 840 | attr->next = NULL; |
| 841 | } |
| 842 | |
| 843 | for(iter = attr; iter; ) { |
| 844 | attr = iter; |
| 845 | iter = iter->next; |
| 846 | |
| 847 | lydict_remove(ctx, attr->name); |
| 848 | lydict_remove(ctx, attr->value); |
| 849 | free(attr); |
| 850 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Michal Vasko | fd76bd1 | 2015-09-24 15:49:57 +0200 | [diff] [blame] | 853 | struct lyd_node * |
| 854 | lyd_attr_parent(struct lyd_node *root, struct lyd_attr *attr) |
| 855 | { |
| 856 | struct lyd_node *next, *elem; |
| 857 | struct lyd_attr *node_attr; |
| 858 | |
| 859 | LY_TREE_DFS_BEGIN(root, next, elem) { |
| 860 | for (node_attr = elem->attr; node_attr; node_attr = node_attr->next) { |
| 861 | if (node_attr == attr) { |
| 862 | return elem; |
| 863 | } |
| 864 | } |
| 865 | LY_TREE_DFS_END(root, next, elem) |
| 866 | } |
| 867 | |
| 868 | return NULL; |
| 869 | } |
| 870 | |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 871 | API struct lyd_attr * |
| 872 | lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value) |
| 873 | { |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 874 | struct lyd_attr *a, *iter; |
| 875 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 876 | const struct lys_module *module; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 877 | const char *p; |
| 878 | char *aux; |
| 879 | |
| 880 | if (!parent || !name || !value) { |
| 881 | return NULL; |
| 882 | } |
| 883 | ctx = parent->schema->module->ctx; |
| 884 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 885 | if ((p = strchr(name, ':'))) { |
| 886 | /* search for the namespace */ |
| 887 | aux = strndup(name, p - name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 888 | if (!aux) { |
| 889 | LOGMEM; |
| 890 | return NULL; |
| 891 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 892 | module = ly_ctx_get_module(ctx, aux, NULL); |
| 893 | free(aux); |
| 894 | name = p + 1; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 895 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 896 | if (!module) { |
| 897 | /* module not found */ |
Radek Krejci | a008ca9 | 2015-10-30 15:52:05 +0100 | [diff] [blame] | 898 | LOGERR(LY_EINVAL, "Attribute prefix does not match any schema in the context."); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 899 | return NULL; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 900 | } |
| 901 | } else { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 902 | /* no prefix -> module is the same as for the parent */ |
| 903 | module = parent->schema->module; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 904 | } |
| 905 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 906 | a = malloc(sizeof *a); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 907 | if (!a) { |
| 908 | LOGMEM; |
| 909 | return NULL; |
| 910 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 911 | a->module = (struct lys_module *)module; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 912 | a->next = NULL; |
| 913 | a->name = lydict_insert(ctx, name, 0); |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 914 | a->value = lydict_insert(ctx, value, 0); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 915 | |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 916 | if (!parent->attr) { |
| 917 | parent->attr = a; |
| 918 | } else { |
| 919 | for (iter = parent->attr; iter->next; iter = iter->next); |
| 920 | iter->next = a; |
| 921 | } |
| 922 | |
| 923 | return a; |
| 924 | } |
| 925 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 926 | API void |
| 927 | lyd_free(struct lyd_node *node) |
| 928 | { |
| 929 | struct lyd_node *next, *child; |
| 930 | |
| 931 | if (!node) { |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | if (!(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
| 936 | /* free children */ |
| 937 | LY_TREE_FOR_SAFE(node->child, next, child) { |
| 938 | lyd_free(child); |
| 939 | } |
| 940 | } else if (node->schema->nodetype == LYS_ANYXML) { |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 941 | lyxml_free(node->schema->module->ctx, ((struct lyd_node_anyxml *)node)->value); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 942 | } else { |
| 943 | /* free value */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 944 | switch(((struct lyd_node_leaf_list *)node)->value_type) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 945 | case LY_TYPE_BINARY: |
| 946 | case LY_TYPE_STRING: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 947 | lydict_remove(node->schema->module->ctx, ((struct lyd_node_leaf_list *)node)->value.string); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 948 | break; |
| 949 | case LY_TYPE_BITS: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 950 | if (((struct lyd_node_leaf_list *)node)->value.bit) { |
| 951 | free(((struct lyd_node_leaf_list *)node)->value.bit); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 952 | } |
| 953 | break; |
| 954 | default: |
| 955 | /* TODO nothing needed : LY_TYPE_BOOL, LY_TYPE_DEC64*/ |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | lyd_unlink(node); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 961 | lyd_free_attr(node->schema->module->ctx, node, node->attr, 1); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 962 | free(node); |
| 963 | } |
| 964 | |
Radek Krejci | 8146840 | 2016-01-07 13:52:40 +0100 | [diff] [blame] | 965 | API void |
| 966 | lyd_free_withsiblings(struct lyd_node *node) |
| 967 | { |
| 968 | struct lyd_node *iter, *aux; |
| 969 | |
| 970 | if (!node) { |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | /* optimization - avoid freeing (unlinking) the last node of the siblings list */ |
| 975 | /* so, first, free the node's predecessors to the beginning of the list ... */ |
| 976 | for(iter = node->prev; iter->next; iter = aux) { |
| 977 | aux = iter->prev; |
| 978 | lyd_free(iter); |
| 979 | } |
| 980 | /* ... then, the node is the first in the siblings list, so free them all */ |
| 981 | LY_TREE_FOR_SAFE(node, aux, iter) { |
| 982 | lyd_free(iter); |
| 983 | } |
| 984 | } |
| 985 | |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 986 | API char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 987 | lyxml_serialize(const struct lyxml_elem *anyxml) |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 988 | { |
| 989 | FILE *stream; |
| 990 | char *buf; |
| 991 | size_t buf_size; |
| 992 | |
| 993 | if (!anyxml) { |
| 994 | ly_errno = LY_EINVAL; |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | stream = open_memstream(&buf, &buf_size); |
| 999 | if (!stream) { |
| 1000 | ly_errno = LY_ESYS; |
| 1001 | return NULL; |
| 1002 | } |
Michal Vasko | 70f92c3 | 2015-12-10 09:57:24 +0100 | [diff] [blame] | 1003 | if (lyxml_dump_file(stream, anyxml, 0) == 0) { |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 1004 | free(buf); |
| 1005 | buf = NULL; |
| 1006 | ly_errno = LY_EINVAL; |
| 1007 | } |
| 1008 | fclose(stream); |
| 1009 | |
| 1010 | return buf; |
| 1011 | } |
| 1012 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1013 | int |
| 1014 | lyd_compare(struct lyd_node *first, struct lyd_node *second, int unique) |
| 1015 | { |
| 1016 | struct lys_node_list *slist; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1017 | const struct lys_node *snode = NULL; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1018 | struct lyd_node *diter; |
| 1019 | const char *val1, *val2; |
| 1020 | int i, j; |
| 1021 | |
| 1022 | assert(first); |
| 1023 | assert(second); |
| 1024 | |
| 1025 | if (first->schema != second->schema) { |
| 1026 | return 1; |
| 1027 | } |
| 1028 | |
| 1029 | switch (first->schema->nodetype) { |
| 1030 | case LYS_LEAFLIST: |
| 1031 | /* compare values */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1032 | if (((struct lyd_node_leaf_list *)first)->value_str == ((struct lyd_node_leaf_list *)second)->value_str) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | return 1; |
| 1036 | case LYS_LIST: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1037 | slist = (struct lys_node_list *)first->schema; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1038 | |
| 1039 | if (unique) { |
| 1040 | /* compare unique leafs */ |
| 1041 | for (i = 0; i < slist->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1042 | for (j = 0; j < slist->unique[i].expr_size; j++) { |
| 1043 | /* first */ |
| 1044 | diter = resolve_data_nodeid(slist->unique[i].expr[j], first->child); |
| 1045 | if (diter) { |
| 1046 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 1047 | } else { |
| 1048 | /* use default value */ |
Radek Krejci | 3eb09b6 | 2015-12-16 15:22:19 +0100 | [diff] [blame] | 1049 | if (resolve_schema_nodeid(slist->unique[i].expr[j], first->schema->child, first->schema->module, LYS_LEAF, &snode)) { |
| 1050 | /* error, but unique expression was checked when the schema was parsed */ |
| 1051 | return -1; |
| 1052 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1053 | val1 = ((struct lys_node_leaf *)snode)->dflt; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1054 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1055 | |
| 1056 | /* second */ |
| 1057 | diter = resolve_data_nodeid(slist->unique[i].expr[j], second->child); |
| 1058 | if (diter) { |
| 1059 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 1060 | } else { |
| 1061 | /* use default value */ |
Radek Krejci | 3eb09b6 | 2015-12-16 15:22:19 +0100 | [diff] [blame] | 1062 | if (resolve_schema_nodeid(slist->unique[i].expr[j], second->schema->child, second->schema->module, LYS_LEAF, &snode)) { |
| 1063 | /* error, but unique expression was checked when the schema was parsed */ |
| 1064 | return -1; |
| 1065 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1066 | val2 = ((struct lys_node_leaf *)snode)->dflt; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1067 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1068 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1069 | if (val1 != val2) { |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1073 | if (j && j == slist->unique[i].expr_size) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1074 | /* all unique leafs are the same in this set */ |
| 1075 | return 0; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* compare keys */ |
| 1081 | for (i = 0; i < slist->keys_size; i++) { |
| 1082 | snode = (struct lys_node *)slist->keys[i]; |
| 1083 | val1 = val2 = NULL; |
| 1084 | LY_TREE_FOR(first->child, diter) { |
| 1085 | if (diter->schema == snode) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1086 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | LY_TREE_FOR(second->child, diter) { |
| 1091 | if (diter->schema == snode) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1092 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | if (val1 != val2) { |
| 1097 | return 1; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | return 0; |
| 1102 | default: |
| 1103 | /* no additional check is needed */ |
| 1104 | return 0; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | API struct lyd_set * |
| 1109 | lyd_set_new(void) |
| 1110 | { |
| 1111 | return calloc(1, sizeof(struct lyd_set)); |
| 1112 | } |
| 1113 | |
| 1114 | API void |
| 1115 | lyd_set_free(struct lyd_set *set) |
| 1116 | { |
| 1117 | if (!set) { |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | free(set->set); |
| 1122 | free(set); |
| 1123 | } |
| 1124 | |
| 1125 | API int |
| 1126 | lyd_set_add(struct lyd_set *set, struct lyd_node *node) |
| 1127 | { |
| 1128 | struct lyd_node **new; |
| 1129 | |
| 1130 | if (!set) { |
| 1131 | ly_errno = LY_EINVAL; |
| 1132 | return EXIT_FAILURE; |
| 1133 | } |
| 1134 | |
| 1135 | if (set->size == set->number) { |
Radek Krejci | c5c4598 | 2016-01-07 12:50:44 +0100 | [diff] [blame] | 1136 | new = realloc(set->set, (set->size + 8) * sizeof *(set->set)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1137 | if (!new) { |
| 1138 | LOGMEM; |
| 1139 | return EXIT_FAILURE; |
| 1140 | } |
| 1141 | set->size += 8; |
| 1142 | set->set = new; |
| 1143 | } |
| 1144 | |
| 1145 | set->set[set->number++] = node; |
| 1146 | |
| 1147 | return EXIT_SUCCESS; |
| 1148 | } |