bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
dxml.h@2:45b351b4fc2a, 2020-04-05 (annotated)
- Committer:
- bosko001
- Date:
- Sun Apr 05 12:56:41 2020 +0000
- Revision:
- 2:45b351b4fc2a
- Child:
- 4:7abcf4543282
n0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 2:45b351b4fc2a | 1 | /* dxml.h |
bosko001 | 2:45b351b4fc2a | 2 | * |
bosko001 | 2:45b351b4fc2a | 3 | * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org> |
bosko001 | 2:45b351b4fc2a | 4 | * |
bosko001 | 2:45b351b4fc2a | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
bosko001 | 2:45b351b4fc2a | 6 | * a copy of this software and associated documentation files (the |
bosko001 | 2:45b351b4fc2a | 7 | * "Software"), to deal in the Software without restriction, including |
bosko001 | 2:45b351b4fc2a | 8 | * without limitation the rights to use, copy, modify, merge, publish, |
bosko001 | 2:45b351b4fc2a | 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
bosko001 | 2:45b351b4fc2a | 10 | * permit persons to whom the Software is furnished to do so, subject to |
bosko001 | 2:45b351b4fc2a | 11 | * the following conditions: |
bosko001 | 2:45b351b4fc2a | 12 | * |
bosko001 | 2:45b351b4fc2a | 13 | * The above copyright notice and this permission notice shall be included |
bosko001 | 2:45b351b4fc2a | 14 | * in all copies or substantial portions of the Software. |
bosko001 | 2:45b351b4fc2a | 15 | * |
bosko001 | 2:45b351b4fc2a | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
bosko001 | 2:45b351b4fc2a | 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
bosko001 | 2:45b351b4fc2a | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
bosko001 | 2:45b351b4fc2a | 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
bosko001 | 2:45b351b4fc2a | 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
bosko001 | 2:45b351b4fc2a | 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
bosko001 | 2:45b351b4fc2a | 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bosko001 | 2:45b351b4fc2a | 23 | */ |
bosko001 | 2:45b351b4fc2a | 24 | |
bosko001 | 2:45b351b4fc2a | 25 | #ifndef _dxml_H |
bosko001 | 2:45b351b4fc2a | 26 | #define _dxml_H |
bosko001 | 2:45b351b4fc2a | 27 | |
bosko001 | 2:45b351b4fc2a | 28 | #include <stdlib.h> |
bosko001 | 2:45b351b4fc2a | 29 | #include <stdio.h> |
bosko001 | 2:45b351b4fc2a | 30 | #include <stdarg.h> |
bosko001 | 2:45b351b4fc2a | 31 | //#include <fcntl.h> |
bosko001 | 2:45b351b4fc2a | 32 | |
bosko001 | 2:45b351b4fc2a | 33 | #ifdef __cplusplus |
bosko001 | 2:45b351b4fc2a | 34 | extern "C" { |
bosko001 | 2:45b351b4fc2a | 35 | #endif |
bosko001 | 2:45b351b4fc2a | 36 | |
bosko001 | 2:45b351b4fc2a | 37 | #define dxml_BUFSIZE 1024 // size of internal memory buffers |
bosko001 | 2:45b351b4fc2a | 38 | #define dxml_NAMEM 0x80 // name is malloced |
bosko001 | 2:45b351b4fc2a | 39 | #define dxml_TXTM 0x40 // txt is malloced |
bosko001 | 2:45b351b4fc2a | 40 | #define dxml_DUP 0x20 // attribute name and value are strduped |
bosko001 | 2:45b351b4fc2a | 41 | |
bosko001 | 2:45b351b4fc2a | 42 | typedef struct dxml *dxml_t; |
bosko001 | 2:45b351b4fc2a | 43 | struct dxml { |
bosko001 | 2:45b351b4fc2a | 44 | char *name; // tag name |
bosko001 | 2:45b351b4fc2a | 45 | char **attr; // tag attributes { name, value, name, value, ... NULL } |
bosko001 | 2:45b351b4fc2a | 46 | char *txt; // tag character content, empty string if none |
bosko001 | 2:45b351b4fc2a | 47 | size_t off; // tag offset from start of parent tag character content |
bosko001 | 2:45b351b4fc2a | 48 | dxml_t next; // next tag with same name in this section at this depth |
bosko001 | 2:45b351b4fc2a | 49 | dxml_t sibling; // next tag with different name in same section and depth |
bosko001 | 2:45b351b4fc2a | 50 | dxml_t ordered; // next tag, same section and depth, in original order |
bosko001 | 2:45b351b4fc2a | 51 | dxml_t child; // head of sub tag list, NULL if none |
bosko001 | 2:45b351b4fc2a | 52 | dxml_t parent; // parent tag, NULL if current tag is root tag |
bosko001 | 2:45b351b4fc2a | 53 | short flags; // additional information |
bosko001 | 2:45b351b4fc2a | 54 | }; |
bosko001 | 2:45b351b4fc2a | 55 | |
bosko001 | 2:45b351b4fc2a | 56 | // Given a string of xml data and its length, parses it and creates an dxml |
bosko001 | 2:45b351b4fc2a | 57 | // structure. For efficiency, modifies the data by adding null terminators |
bosko001 | 2:45b351b4fc2a | 58 | // and decoding ampersand sequences. If you don't want this, copy the data and |
bosko001 | 2:45b351b4fc2a | 59 | // pass in the copy. Returns NULL on failure. |
bosko001 | 2:45b351b4fc2a | 60 | dxml_t dxml_parse_str(char *s, size_t len); |
bosko001 | 2:45b351b4fc2a | 61 | |
bosko001 | 2:45b351b4fc2a | 62 | // A wrapper for dxml_parse_str() that accepts a file descriptor. First |
bosko001 | 2:45b351b4fc2a | 63 | // attempts to mem map the file. Failing that, reads the file into memory. |
bosko001 | 2:45b351b4fc2a | 64 | // Returns NULL on failure. |
bosko001 | 2:45b351b4fc2a | 65 | //dxml_t dxml_parse_fd(int fd); |
bosko001 | 2:45b351b4fc2a | 66 | |
bosko001 | 2:45b351b4fc2a | 67 | // a wrapper for dxml_parse_fd() that accepts a file name |
bosko001 | 2:45b351b4fc2a | 68 | dxml_t dxml_parse_file(const char *file); |
bosko001 | 2:45b351b4fc2a | 69 | |
bosko001 | 2:45b351b4fc2a | 70 | // Wrapper for dxml_parse_str() that accepts a file stream. Reads the entire |
bosko001 | 2:45b351b4fc2a | 71 | // stream into memory and then parses it. For xml files, use dxml_parse_file() |
bosko001 | 2:45b351b4fc2a | 72 | // or dxml_parse_fd() |
bosko001 | 2:45b351b4fc2a | 73 | dxml_t dxml_parse_fp(FILE *fp); |
bosko001 | 2:45b351b4fc2a | 74 | |
bosko001 | 2:45b351b4fc2a | 75 | // returns the first child tag (one level deeper) with the given name or NULL |
bosko001 | 2:45b351b4fc2a | 76 | // if not found |
bosko001 | 2:45b351b4fc2a | 77 | dxml_t dxml_child(dxml_t xml, const char *name); |
bosko001 | 2:45b351b4fc2a | 78 | |
bosko001 | 2:45b351b4fc2a | 79 | // returns the next tag of the same name in the same section and depth or NULL |
bosko001 | 2:45b351b4fc2a | 80 | // if not found |
bosko001 | 2:45b351b4fc2a | 81 | #define dxml_next(xml) ((xml) ? xml->next : NULL) |
bosko001 | 2:45b351b4fc2a | 82 | |
bosko001 | 2:45b351b4fc2a | 83 | // Returns the Nth tag with the same name in the same section at the same depth |
bosko001 | 2:45b351b4fc2a | 84 | // or NULL if not found. An index of 0 returns the tag given. |
bosko001 | 2:45b351b4fc2a | 85 | dxml_t dxml_idx(dxml_t xml, int idx); |
bosko001 | 2:45b351b4fc2a | 86 | |
bosko001 | 2:45b351b4fc2a | 87 | // returns the name of the given tag |
bosko001 | 2:45b351b4fc2a | 88 | #define dxml_name(xml) ((xml) ? xml->name : NULL) |
bosko001 | 2:45b351b4fc2a | 89 | |
bosko001 | 2:45b351b4fc2a | 90 | // returns the given tag's character content or empty string if none |
bosko001 | 2:45b351b4fc2a | 91 | #define dxml_txt(xml) ((xml) ? xml->txt : "") |
bosko001 | 2:45b351b4fc2a | 92 | |
bosko001 | 2:45b351b4fc2a | 93 | // returns the value of the requested tag attribute, or NULL if not found |
bosko001 | 2:45b351b4fc2a | 94 | const char *dxml_attr(dxml_t xml, const char *attr); |
bosko001 | 2:45b351b4fc2a | 95 | |
bosko001 | 2:45b351b4fc2a | 96 | // Traverses the dxml sturcture to retrieve a specific subtag. Takes a |
bosko001 | 2:45b351b4fc2a | 97 | // variable length list of tag names and indexes. The argument list must be |
bosko001 | 2:45b351b4fc2a | 98 | // terminated by either an index of -1 or an empty string tag name. Example: |
bosko001 | 2:45b351b4fc2a | 99 | // title = dxml_get(library, "shelf", 0, "book", 2, "title", -1); |
bosko001 | 2:45b351b4fc2a | 100 | // This retrieves the title of the 3rd book on the 1st shelf of library. |
bosko001 | 2:45b351b4fc2a | 101 | // Returns NULL if not found. |
bosko001 | 2:45b351b4fc2a | 102 | dxml_t dxml_get(dxml_t xml, ...); |
bosko001 | 2:45b351b4fc2a | 103 | |
bosko001 | 2:45b351b4fc2a | 104 | // Converts an dxml structure back to xml. Returns a string of xml data that |
bosko001 | 2:45b351b4fc2a | 105 | // must be freed. |
bosko001 | 2:45b351b4fc2a | 106 | char *dxml_toxml(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 107 | |
bosko001 | 2:45b351b4fc2a | 108 | // returns a NULL terminated array of processing instructions for the given |
bosko001 | 2:45b351b4fc2a | 109 | // target |
bosko001 | 2:45b351b4fc2a | 110 | const char **dxml_pi(dxml_t xml, const char *target); |
bosko001 | 2:45b351b4fc2a | 111 | |
bosko001 | 2:45b351b4fc2a | 112 | // frees the memory allocated for an dxml structure |
bosko001 | 2:45b351b4fc2a | 113 | void dxml_free(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 114 | |
bosko001 | 2:45b351b4fc2a | 115 | // returns parser error message or empty string if none |
bosko001 | 2:45b351b4fc2a | 116 | const char *dxml_error(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 117 | |
bosko001 | 2:45b351b4fc2a | 118 | // returns a new empty dxml structure with the given root tag name |
bosko001 | 2:45b351b4fc2a | 119 | dxml_t dxml_new(const char *name); |
bosko001 | 2:45b351b4fc2a | 120 | |
bosko001 | 2:45b351b4fc2a | 121 | // wrapper for dxml_new() that strdup()s name |
bosko001 | 2:45b351b4fc2a | 122 | #define dxml_new_d(name) dxml_set_flag(dxml_new(strdup(name)), dxml_NAMEM) |
bosko001 | 2:45b351b4fc2a | 123 | |
bosko001 | 2:45b351b4fc2a | 124 | // Adds a child tag. off is the offset of the child tag relative to the start |
bosko001 | 2:45b351b4fc2a | 125 | // of the parent tag's character content. Returns the child tag. |
bosko001 | 2:45b351b4fc2a | 126 | dxml_t dxml_add_child(dxml_t xml, const char *name, size_t off); |
bosko001 | 2:45b351b4fc2a | 127 | |
bosko001 | 2:45b351b4fc2a | 128 | // wrapper for dxml_add_child() that strdup()s name |
bosko001 | 2:45b351b4fc2a | 129 | #define dxml_add_child_d(xml, name, off) \ |
bosko001 | 2:45b351b4fc2a | 130 | dxml_set_flag(dxml_add_child(xml, strdup(name), off), dxml_NAMEM) |
bosko001 | 2:45b351b4fc2a | 131 | |
bosko001 | 2:45b351b4fc2a | 132 | // sets the character content for the given tag and returns the tag |
bosko001 | 2:45b351b4fc2a | 133 | dxml_t dxml_set_txt(dxml_t xml, const char *txt); |
bosko001 | 2:45b351b4fc2a | 134 | |
bosko001 | 2:45b351b4fc2a | 135 | // wrapper for dxml_set_txt() that strdup()s txt |
bosko001 | 2:45b351b4fc2a | 136 | #define dxml_set_txt_d(xml, txt) \ |
bosko001 | 2:45b351b4fc2a | 137 | dxml_set_flag(dxml_set_txt(xml, strdup(txt)), dxml_TXTM) |
bosko001 | 2:45b351b4fc2a | 138 | |
bosko001 | 2:45b351b4fc2a | 139 | // Sets the given tag attribute or adds a new attribute if not found. A value |
bosko001 | 2:45b351b4fc2a | 140 | // of NULL will remove the specified attribute. Returns the tag given. |
bosko001 | 2:45b351b4fc2a | 141 | dxml_t dxml_set_attr(dxml_t xml, const char *name, const char *value); |
bosko001 | 2:45b351b4fc2a | 142 | |
bosko001 | 2:45b351b4fc2a | 143 | // Wrapper for dxml_set_attr() that strdup()s name/value. Value cannot be NULL |
bosko001 | 2:45b351b4fc2a | 144 | #define dxml_set_attr_d(xml, name, value) \ |
bosko001 | 2:45b351b4fc2a | 145 | dxml_set_attr(dxml_set_flag(xml, dxml_DUP), strdup(name), strdup(value)) |
bosko001 | 2:45b351b4fc2a | 146 | |
bosko001 | 2:45b351b4fc2a | 147 | // sets a flag for the given tag and returns the tag |
bosko001 | 2:45b351b4fc2a | 148 | dxml_t dxml_set_flag(dxml_t xml, short flag); |
bosko001 | 2:45b351b4fc2a | 149 | |
bosko001 | 2:45b351b4fc2a | 150 | // removes a tag along with its subtags without freeing its memory |
bosko001 | 2:45b351b4fc2a | 151 | dxml_t dxml_cut(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 152 | |
bosko001 | 2:45b351b4fc2a | 153 | // inserts an existing tag into an dxml structure |
bosko001 | 2:45b351b4fc2a | 154 | dxml_t dxml_insert(dxml_t xml, dxml_t dest, size_t off); |
bosko001 | 2:45b351b4fc2a | 155 | |
bosko001 | 2:45b351b4fc2a | 156 | // Moves an existing tag to become a subtag of dest at the given offset from |
bosko001 | 2:45b351b4fc2a | 157 | // the start of dest's character content. Returns the moved tag. |
bosko001 | 2:45b351b4fc2a | 158 | #define dxml_move(xml, dest, off) dxml_insert(dxml_cut(xml), dest, off) |
bosko001 | 2:45b351b4fc2a | 159 | |
bosko001 | 2:45b351b4fc2a | 160 | // removes a tag along with all its subtags |
bosko001 | 2:45b351b4fc2a | 161 | #define dxml_remove(xml) dxml_free(dxml_cut(xml)) |
bosko001 | 2:45b351b4fc2a | 162 | |
bosko001 | 2:45b351b4fc2a | 163 | #ifdef __cplusplus |
bosko001 | 2:45b351b4fc2a | 164 | } |
bosko001 | 2:45b351b4fc2a | 165 | #endif |
bosko001 | 2:45b351b4fc2a | 166 | |
bosko001 | 2:45b351b4fc2a | 167 | #endif // _dxml_H |
bosko001 | 2:45b351b4fc2a | 168 |