Pratyush Mallick
/
nano_dac
this is testing
noos_mbed/util/xml.c@0:e8a1ba50c46b, 2021-01-14 (annotated)
- Committer:
- pmallick
- Date:
- Thu Jan 14 19:12:57 2021 +0530
- Revision:
- 0:e8a1ba50c46b
this is testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmallick | 0:e8a1ba50c46b | 1 | /***************************************************************************//** |
pmallick | 0:e8a1ba50c46b | 2 | * @file xml.c |
pmallick | 0:e8a1ba50c46b | 3 | * @brief Implementation of xml |
pmallick | 0:e8a1ba50c46b | 4 | * @author Cristian Pop (cristian.pop@analog.com) |
pmallick | 0:e8a1ba50c46b | 5 | ******************************************************************************** |
pmallick | 0:e8a1ba50c46b | 6 | * Copyright 2019(c) Analog Devices, Inc. |
pmallick | 0:e8a1ba50c46b | 7 | * |
pmallick | 0:e8a1ba50c46b | 8 | * All rights reserved. |
pmallick | 0:e8a1ba50c46b | 9 | * |
pmallick | 0:e8a1ba50c46b | 10 | * Redistribution and use in source and binary forms, with or without |
pmallick | 0:e8a1ba50c46b | 11 | * modification, are permitted provided that the following conditions are met: |
pmallick | 0:e8a1ba50c46b | 12 | * - Redistributions of source code must retain the above copyright |
pmallick | 0:e8a1ba50c46b | 13 | * notice, this list of conditions and the following disclaimer. |
pmallick | 0:e8a1ba50c46b | 14 | * - Redistributions in binary form must reproduce the above copyright |
pmallick | 0:e8a1ba50c46b | 15 | * notice, this list of conditions and the following disclaimer in |
pmallick | 0:e8a1ba50c46b | 16 | * the documentation and/or other materials provided with the |
pmallick | 0:e8a1ba50c46b | 17 | * distribution. |
pmallick | 0:e8a1ba50c46b | 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
pmallick | 0:e8a1ba50c46b | 19 | * contributors may be used to endorse or promote products derived |
pmallick | 0:e8a1ba50c46b | 20 | * from this software without specific prior written permission. |
pmallick | 0:e8a1ba50c46b | 21 | * - The use of this software may or may not infringe the patent rights |
pmallick | 0:e8a1ba50c46b | 22 | * of one or more patent holders. This license does not release you |
pmallick | 0:e8a1ba50c46b | 23 | * from the requirement that you obtain separate licenses from these |
pmallick | 0:e8a1ba50c46b | 24 | * patent holders to use this software. |
pmallick | 0:e8a1ba50c46b | 25 | * - Use of the software either in source or binary form, must be run |
pmallick | 0:e8a1ba50c46b | 26 | * on or directly connected to an Analog Devices Inc. component. |
pmallick | 0:e8a1ba50c46b | 27 | * |
pmallick | 0:e8a1ba50c46b | 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
pmallick | 0:e8a1ba50c46b | 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
pmallick | 0:e8a1ba50c46b | 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
pmallick | 0:e8a1ba50c46b | 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
pmallick | 0:e8a1ba50c46b | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
pmallick | 0:e8a1ba50c46b | 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
pmallick | 0:e8a1ba50c46b | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
pmallick | 0:e8a1ba50c46b | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
pmallick | 0:e8a1ba50c46b | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
pmallick | 0:e8a1ba50c46b | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
pmallick | 0:e8a1ba50c46b | 38 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 39 | |
pmallick | 0:e8a1ba50c46b | 40 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 41 | /***************************** Include Files **********************************/ |
pmallick | 0:e8a1ba50c46b | 42 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 43 | |
pmallick | 0:e8a1ba50c46b | 44 | #include <string.h> |
pmallick | 0:e8a1ba50c46b | 45 | #include <stdlib.h> |
pmallick | 0:e8a1ba50c46b | 46 | #include "xml.h" |
pmallick | 0:e8a1ba50c46b | 47 | #include "error.h" |
pmallick | 0:e8a1ba50c46b | 48 | |
pmallick | 0:e8a1ba50c46b | 49 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 50 | /************************ Functions Definitions *******************************/ |
pmallick | 0:e8a1ba50c46b | 51 | /******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 52 | |
pmallick | 0:e8a1ba50c46b | 53 | /** |
pmallick | 0:e8a1ba50c46b | 54 | * create attribute |
pmallick | 0:e8a1ba50c46b | 55 | * @param **attribute pointer to new attribute |
pmallick | 0:e8a1ba50c46b | 56 | * @param *name attribute name |
pmallick | 0:e8a1ba50c46b | 57 | * @param *value attribute value |
pmallick | 0:e8a1ba50c46b | 58 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 59 | */ |
pmallick | 0:e8a1ba50c46b | 60 | ssize_t xml_create_attribute(struct xml_attribute **attribute, char *name, |
pmallick | 0:e8a1ba50c46b | 61 | const char *value) |
pmallick | 0:e8a1ba50c46b | 62 | { |
pmallick | 0:e8a1ba50c46b | 63 | if(!attribute) |
pmallick | 0:e8a1ba50c46b | 64 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 65 | if(!name) |
pmallick | 0:e8a1ba50c46b | 66 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 67 | if(!value) |
pmallick | 0:e8a1ba50c46b | 68 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 69 | |
pmallick | 0:e8a1ba50c46b | 70 | *attribute = calloc(1, sizeof(struct xml_attribute)); |
pmallick | 0:e8a1ba50c46b | 71 | if (!(*attribute)) |
pmallick | 0:e8a1ba50c46b | 72 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 73 | |
pmallick | 0:e8a1ba50c46b | 74 | (*attribute)->name = calloc(1, strlen(name) + 1); |
pmallick | 0:e8a1ba50c46b | 75 | if (!(*attribute)->name) { |
pmallick | 0:e8a1ba50c46b | 76 | free(*attribute); |
pmallick | 0:e8a1ba50c46b | 77 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 78 | } |
pmallick | 0:e8a1ba50c46b | 79 | strcpy((*attribute)->name, name); |
pmallick | 0:e8a1ba50c46b | 80 | |
pmallick | 0:e8a1ba50c46b | 81 | (*attribute)->value = calloc(1, strlen(value) + 1); |
pmallick | 0:e8a1ba50c46b | 82 | if (!(*attribute)->value) { |
pmallick | 0:e8a1ba50c46b | 83 | free((*attribute)->name); |
pmallick | 0:e8a1ba50c46b | 84 | free(*attribute); |
pmallick | 0:e8a1ba50c46b | 85 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 86 | } |
pmallick | 0:e8a1ba50c46b | 87 | strcpy((*attribute)->value, value); |
pmallick | 0:e8a1ba50c46b | 88 | |
pmallick | 0:e8a1ba50c46b | 89 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 90 | } |
pmallick | 0:e8a1ba50c46b | 91 | |
pmallick | 0:e8a1ba50c46b | 92 | /** |
pmallick | 0:e8a1ba50c46b | 93 | * add attribute to a xml node |
pmallick | 0:e8a1ba50c46b | 94 | * @param *node pointer to the node, where the attribute is inserted |
pmallick | 0:e8a1ba50c46b | 95 | * @param *attribute attribute |
pmallick | 0:e8a1ba50c46b | 96 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 97 | */ |
pmallick | 0:e8a1ba50c46b | 98 | ssize_t xml_add_attribute(struct xml_node *node, |
pmallick | 0:e8a1ba50c46b | 99 | struct xml_attribute *attribute) |
pmallick | 0:e8a1ba50c46b | 100 | { |
pmallick | 0:e8a1ba50c46b | 101 | if(!node) |
pmallick | 0:e8a1ba50c46b | 102 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 103 | if(!attribute) |
pmallick | 0:e8a1ba50c46b | 104 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 105 | |
pmallick | 0:e8a1ba50c46b | 106 | if (!node->attributes) { |
pmallick | 0:e8a1ba50c46b | 107 | node->attributes = calloc(1, sizeof(struct xml_attribute*)); |
pmallick | 0:e8a1ba50c46b | 108 | if (!node->attributes) |
pmallick | 0:e8a1ba50c46b | 109 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 110 | } else { |
pmallick | 0:e8a1ba50c46b | 111 | struct xml_attribute **buff = realloc(node->attributes, |
pmallick | 0:e8a1ba50c46b | 112 | (node->attr_cnt + 1) * sizeof(struct xml_attribute*)); |
pmallick | 0:e8a1ba50c46b | 113 | if (!buff) |
pmallick | 0:e8a1ba50c46b | 114 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 115 | node->attributes = buff; |
pmallick | 0:e8a1ba50c46b | 116 | } |
pmallick | 0:e8a1ba50c46b | 117 | node->attributes[node->attr_cnt] = attribute; |
pmallick | 0:e8a1ba50c46b | 118 | node->attr_cnt++; |
pmallick | 0:e8a1ba50c46b | 119 | |
pmallick | 0:e8a1ba50c46b | 120 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 121 | } |
pmallick | 0:e8a1ba50c46b | 122 | |
pmallick | 0:e8a1ba50c46b | 123 | /** |
pmallick | 0:e8a1ba50c46b | 124 | * create new xml node |
pmallick | 0:e8a1ba50c46b | 125 | * @param **node pointer to the new node |
pmallick | 0:e8a1ba50c46b | 126 | * @param *name |
pmallick | 0:e8a1ba50c46b | 127 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 128 | */ |
pmallick | 0:e8a1ba50c46b | 129 | ssize_t xml_create_node(struct xml_node **node, char *name) |
pmallick | 0:e8a1ba50c46b | 130 | { |
pmallick | 0:e8a1ba50c46b | 131 | if(!node) |
pmallick | 0:e8a1ba50c46b | 132 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 133 | if(!name) |
pmallick | 0:e8a1ba50c46b | 134 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 135 | |
pmallick | 0:e8a1ba50c46b | 136 | *node = calloc(1, sizeof(struct xml_node)); |
pmallick | 0:e8a1ba50c46b | 137 | if (!(*node)) |
pmallick | 0:e8a1ba50c46b | 138 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 139 | (*node)->name = calloc(1, strlen(name) + 1); |
pmallick | 0:e8a1ba50c46b | 140 | if (!(*node)->name) { |
pmallick | 0:e8a1ba50c46b | 141 | free(*node); |
pmallick | 0:e8a1ba50c46b | 142 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 143 | } |
pmallick | 0:e8a1ba50c46b | 144 | strcpy((*node)->name, name); |
pmallick | 0:e8a1ba50c46b | 145 | |
pmallick | 0:e8a1ba50c46b | 146 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 147 | } |
pmallick | 0:e8a1ba50c46b | 148 | |
pmallick | 0:e8a1ba50c46b | 149 | /** |
pmallick | 0:e8a1ba50c46b | 150 | * add child node to a parent node |
pmallick | 0:e8a1ba50c46b | 151 | * @param *node_parent |
pmallick | 0:e8a1ba50c46b | 152 | * @param *node_child |
pmallick | 0:e8a1ba50c46b | 153 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 154 | */ |
pmallick | 0:e8a1ba50c46b | 155 | ssize_t xml_add_node(struct xml_node *node_parent, struct xml_node *node_child) |
pmallick | 0:e8a1ba50c46b | 156 | { |
pmallick | 0:e8a1ba50c46b | 157 | if(!node_parent) |
pmallick | 0:e8a1ba50c46b | 158 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 159 | if(!node_child) |
pmallick | 0:e8a1ba50c46b | 160 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 161 | |
pmallick | 0:e8a1ba50c46b | 162 | if (!node_parent->children) { |
pmallick | 0:e8a1ba50c46b | 163 | node_parent->children = calloc(1, sizeof(struct xml_node*)); |
pmallick | 0:e8a1ba50c46b | 164 | if (!node_parent->children) |
pmallick | 0:e8a1ba50c46b | 165 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 166 | } else { |
pmallick | 0:e8a1ba50c46b | 167 | struct xml_node **buff = realloc(node_parent->children, |
pmallick | 0:e8a1ba50c46b | 168 | (node_parent->children_cnt + 1) * sizeof(struct xml_node*)); |
pmallick | 0:e8a1ba50c46b | 169 | if (!buff) |
pmallick | 0:e8a1ba50c46b | 170 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 171 | node_parent->children = buff; |
pmallick | 0:e8a1ba50c46b | 172 | } |
pmallick | 0:e8a1ba50c46b | 173 | node_parent->children[node_parent->children_cnt] = node_child; |
pmallick | 0:e8a1ba50c46b | 174 | node_parent->children_cnt++; |
pmallick | 0:e8a1ba50c46b | 175 | |
pmallick | 0:e8a1ba50c46b | 176 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 177 | } |
pmallick | 0:e8a1ba50c46b | 178 | |
pmallick | 0:e8a1ba50c46b | 179 | /** |
pmallick | 0:e8a1ba50c46b | 180 | * delete attribute |
pmallick | 0:e8a1ba50c46b | 181 | * @param *attribute |
pmallick | 0:e8a1ba50c46b | 182 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 183 | */ |
pmallick | 0:e8a1ba50c46b | 184 | ssize_t xml_delete_attribute(struct xml_attribute *attribute) |
pmallick | 0:e8a1ba50c46b | 185 | { |
pmallick | 0:e8a1ba50c46b | 186 | free(attribute->name); |
pmallick | 0:e8a1ba50c46b | 187 | free(attribute->value); |
pmallick | 0:e8a1ba50c46b | 188 | free(attribute); |
pmallick | 0:e8a1ba50c46b | 189 | |
pmallick | 0:e8a1ba50c46b | 190 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 191 | } |
pmallick | 0:e8a1ba50c46b | 192 | |
pmallick | 0:e8a1ba50c46b | 193 | /** |
pmallick | 0:e8a1ba50c46b | 194 | * delete xml node |
pmallick | 0:e8a1ba50c46b | 195 | * @param *node |
pmallick | 0:e8a1ba50c46b | 196 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 197 | */ |
pmallick | 0:e8a1ba50c46b | 198 | ssize_t xml_delete_node(struct xml_node *node) |
pmallick | 0:e8a1ba50c46b | 199 | { |
pmallick | 0:e8a1ba50c46b | 200 | uint16_t i; |
pmallick | 0:e8a1ba50c46b | 201 | for (i = 0; i < node->attr_cnt; i++) { |
pmallick | 0:e8a1ba50c46b | 202 | xml_delete_attribute(node->attributes[i]); |
pmallick | 0:e8a1ba50c46b | 203 | } |
pmallick | 0:e8a1ba50c46b | 204 | for (i = 0; i < node->children_cnt; i++) { |
pmallick | 0:e8a1ba50c46b | 205 | xml_delete_node(node->children[i]); |
pmallick | 0:e8a1ba50c46b | 206 | } |
pmallick | 0:e8a1ba50c46b | 207 | free(node->name); |
pmallick | 0:e8a1ba50c46b | 208 | free(node->attributes); |
pmallick | 0:e8a1ba50c46b | 209 | free(node->children); |
pmallick | 0:e8a1ba50c46b | 210 | free(node); |
pmallick | 0:e8a1ba50c46b | 211 | |
pmallick | 0:e8a1ba50c46b | 212 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 213 | } |
pmallick | 0:e8a1ba50c46b | 214 | |
pmallick | 0:e8a1ba50c46b | 215 | /** |
pmallick | 0:e8a1ba50c46b | 216 | * print string to xml_document. |
pmallick | 0:e8a1ba50c46b | 217 | * @param *doc |
pmallick | 0:e8a1ba50c46b | 218 | * @param *data to be written. |
pmallick | 0:e8a1ba50c46b | 219 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 220 | */ |
pmallick | 0:e8a1ba50c46b | 221 | static ssize_t xml_print_to_doc(struct xml_document *doc, char *data) |
pmallick | 0:e8a1ba50c46b | 222 | { |
pmallick | 0:e8a1ba50c46b | 223 | uint32_t calc_len, print_len; |
pmallick | 0:e8a1ba50c46b | 224 | |
pmallick | 0:e8a1ba50c46b | 225 | calc_len = strlen(data) + 1; |
pmallick | 0:e8a1ba50c46b | 226 | char *buff = realloc(doc->buff, doc->index + calc_len); |
pmallick | 0:e8a1ba50c46b | 227 | if (!buff) |
pmallick | 0:e8a1ba50c46b | 228 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 229 | doc->buff = buff; |
pmallick | 0:e8a1ba50c46b | 230 | print_len = sprintf(&(doc->buff[doc->index]), "%s", data); |
pmallick | 0:e8a1ba50c46b | 231 | if(print_len != calc_len - 1) |
pmallick | 0:e8a1ba50c46b | 232 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 233 | doc->index +=print_len; |
pmallick | 0:e8a1ba50c46b | 234 | |
pmallick | 0:e8a1ba50c46b | 235 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 236 | } |
pmallick | 0:e8a1ba50c46b | 237 | |
pmallick | 0:e8a1ba50c46b | 238 | /** |
pmallick | 0:e8a1ba50c46b | 239 | * print xml tree into a xml document |
pmallick | 0:e8a1ba50c46b | 240 | * @param **document |
pmallick | 0:e8a1ba50c46b | 241 | * @param *node pointer to parent node, that contains the xml tree |
pmallick | 0:e8a1ba50c46b | 242 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 243 | */ |
pmallick | 0:e8a1ba50c46b | 244 | ssize_t xml_create_document(struct xml_document **document, |
pmallick | 0:e8a1ba50c46b | 245 | struct xml_node *node) |
pmallick | 0:e8a1ba50c46b | 246 | { |
pmallick | 0:e8a1ba50c46b | 247 | uint16_t i; |
pmallick | 0:e8a1ba50c46b | 248 | ssize_t ret; |
pmallick | 0:e8a1ba50c46b | 249 | struct xml_document *doc; |
pmallick | 0:e8a1ba50c46b | 250 | |
pmallick | 0:e8a1ba50c46b | 251 | if(!document) |
pmallick | 0:e8a1ba50c46b | 252 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 253 | if(!node) |
pmallick | 0:e8a1ba50c46b | 254 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 255 | |
pmallick | 0:e8a1ba50c46b | 256 | if (!(*document)) { |
pmallick | 0:e8a1ba50c46b | 257 | *document = calloc(1, sizeof(struct xml_document)); |
pmallick | 0:e8a1ba50c46b | 258 | if (!(*document)) |
pmallick | 0:e8a1ba50c46b | 259 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 260 | } |
pmallick | 0:e8a1ba50c46b | 261 | doc = *document; |
pmallick | 0:e8a1ba50c46b | 262 | |
pmallick | 0:e8a1ba50c46b | 263 | ret = xml_print_to_doc(doc, "<"); |
pmallick | 0:e8a1ba50c46b | 264 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 265 | goto error; |
pmallick | 0:e8a1ba50c46b | 266 | ret = xml_print_to_doc(doc, node->name); |
pmallick | 0:e8a1ba50c46b | 267 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 268 | goto error; |
pmallick | 0:e8a1ba50c46b | 269 | ret = xml_print_to_doc(doc, " "); |
pmallick | 0:e8a1ba50c46b | 270 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 271 | goto error; |
pmallick | 0:e8a1ba50c46b | 272 | |
pmallick | 0:e8a1ba50c46b | 273 | for (i = 0; i < node->attr_cnt; i++) { |
pmallick | 0:e8a1ba50c46b | 274 | ret = xml_print_to_doc(doc, node->attributes[i]->name); |
pmallick | 0:e8a1ba50c46b | 275 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 276 | goto error; |
pmallick | 0:e8a1ba50c46b | 277 | ret = xml_print_to_doc(doc, "=\""); |
pmallick | 0:e8a1ba50c46b | 278 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 279 | goto error; |
pmallick | 0:e8a1ba50c46b | 280 | ret = xml_print_to_doc(doc, node->attributes[i]->value); |
pmallick | 0:e8a1ba50c46b | 281 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 282 | goto error; |
pmallick | 0:e8a1ba50c46b | 283 | ret = xml_print_to_doc(doc, "\" "); |
pmallick | 0:e8a1ba50c46b | 284 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 285 | goto error; |
pmallick | 0:e8a1ba50c46b | 286 | } |
pmallick | 0:e8a1ba50c46b | 287 | |
pmallick | 0:e8a1ba50c46b | 288 | if (node->children_cnt == 0) { |
pmallick | 0:e8a1ba50c46b | 289 | ret = xml_print_to_doc(doc, "/>\n"); |
pmallick | 0:e8a1ba50c46b | 290 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 291 | goto error; |
pmallick | 0:e8a1ba50c46b | 292 | |
pmallick | 0:e8a1ba50c46b | 293 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 294 | } else { |
pmallick | 0:e8a1ba50c46b | 295 | ret = xml_print_to_doc(doc, ">\n"); |
pmallick | 0:e8a1ba50c46b | 296 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 297 | goto error; |
pmallick | 0:e8a1ba50c46b | 298 | } |
pmallick | 0:e8a1ba50c46b | 299 | |
pmallick | 0:e8a1ba50c46b | 300 | for (i = 0; i < node->children_cnt; i++) { |
pmallick | 0:e8a1ba50c46b | 301 | ret = xml_create_document(document, node->children[i]); |
pmallick | 0:e8a1ba50c46b | 302 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 303 | return ret; |
pmallick | 0:e8a1ba50c46b | 304 | } |
pmallick | 0:e8a1ba50c46b | 305 | |
pmallick | 0:e8a1ba50c46b | 306 | ret = xml_print_to_doc(doc, "</"); |
pmallick | 0:e8a1ba50c46b | 307 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 308 | goto error; |
pmallick | 0:e8a1ba50c46b | 309 | ret = xml_print_to_doc(doc, node->name); |
pmallick | 0:e8a1ba50c46b | 310 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 311 | goto error; |
pmallick | 0:e8a1ba50c46b | 312 | ret = xml_print_to_doc(doc, ">\n"); |
pmallick | 0:e8a1ba50c46b | 313 | if (ret < 0) |
pmallick | 0:e8a1ba50c46b | 314 | goto error; |
pmallick | 0:e8a1ba50c46b | 315 | |
pmallick | 0:e8a1ba50c46b | 316 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 317 | |
pmallick | 0:e8a1ba50c46b | 318 | error: |
pmallick | 0:e8a1ba50c46b | 319 | free(doc->buff); |
pmallick | 0:e8a1ba50c46b | 320 | free(doc); |
pmallick | 0:e8a1ba50c46b | 321 | |
pmallick | 0:e8a1ba50c46b | 322 | return FAILURE; |
pmallick | 0:e8a1ba50c46b | 323 | } |
pmallick | 0:e8a1ba50c46b | 324 | |
pmallick | 0:e8a1ba50c46b | 325 | /** |
pmallick | 0:e8a1ba50c46b | 326 | * delete xml document |
pmallick | 0:e8a1ba50c46b | 327 | * @param **document pointer to document |
pmallick | 0:e8a1ba50c46b | 328 | * @return SUCCESS in case of success or negative value otherwise |
pmallick | 0:e8a1ba50c46b | 329 | */ |
pmallick | 0:e8a1ba50c46b | 330 | ssize_t xml_delete_document(struct xml_document *document) |
pmallick | 0:e8a1ba50c46b | 331 | { |
pmallick | 0:e8a1ba50c46b | 332 | free(document->buff); |
pmallick | 0:e8a1ba50c46b | 333 | free(document); |
pmallick | 0:e8a1ba50c46b | 334 | |
pmallick | 0:e8a1ba50c46b | 335 | return SUCCESS; |
pmallick | 0:e8a1ba50c46b | 336 | } |