bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
dxml.h@26:c14d034d7459, 2020-07-10 (annotated)
- Committer:
- bosko001
- Date:
- Fri Jul 10 11:33:05 2020 +0200
- Revision:
- 26:c14d034d7459
- Parent:
- 4:7abcf4543282
ispituje se na terenu
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 | 4:7abcf4543282 | 3 | * softverski modul korigovan 11.04.2020 |
bosko001 | 2:45b351b4fc2a | 4 | * |
bosko001 | 2:45b351b4fc2a | 5 | */ |
bosko001 | 2:45b351b4fc2a | 6 | |
bosko001 | 2:45b351b4fc2a | 7 | #ifndef _dxml_H |
bosko001 | 2:45b351b4fc2a | 8 | #define _dxml_H |
bosko001 | 2:45b351b4fc2a | 9 | |
bosko001 | 2:45b351b4fc2a | 10 | #include <stdlib.h> |
bosko001 | 2:45b351b4fc2a | 11 | #include <stdio.h> |
bosko001 | 2:45b351b4fc2a | 12 | #include <stdarg.h> |
bosko001 | 2:45b351b4fc2a | 13 | //#include <fcntl.h> |
bosko001 | 2:45b351b4fc2a | 14 | |
bosko001 | 2:45b351b4fc2a | 15 | #ifdef __cplusplus |
bosko001 | 2:45b351b4fc2a | 16 | extern "C" { |
bosko001 | 2:45b351b4fc2a | 17 | #endif |
bosko001 | 2:45b351b4fc2a | 18 | |
bosko001 | 2:45b351b4fc2a | 19 | #define dxml_BUFSIZE 1024 // size of internal memory buffers |
bosko001 | 2:45b351b4fc2a | 20 | #define dxml_NAMEM 0x80 // name is malloced |
bosko001 | 2:45b351b4fc2a | 21 | #define dxml_TXTM 0x40 // txt is malloced |
bosko001 | 2:45b351b4fc2a | 22 | #define dxml_DUP 0x20 // attribute name and value are strduped |
bosko001 | 2:45b351b4fc2a | 23 | |
bosko001 | 2:45b351b4fc2a | 24 | typedef struct dxml *dxml_t; |
bosko001 | 2:45b351b4fc2a | 25 | struct dxml { |
bosko001 | 2:45b351b4fc2a | 26 | char *name; // tag name |
bosko001 | 2:45b351b4fc2a | 27 | char **attr; // tag attributes { name, value, name, value, ... NULL } |
bosko001 | 2:45b351b4fc2a | 28 | char *txt; // tag character content, empty string if none |
bosko001 | 2:45b351b4fc2a | 29 | size_t off; // tag offset from start of parent tag character content |
bosko001 | 2:45b351b4fc2a | 30 | dxml_t next; // next tag with same name in this section at this depth |
bosko001 | 2:45b351b4fc2a | 31 | dxml_t sibling; // next tag with different name in same section and depth |
bosko001 | 2:45b351b4fc2a | 32 | dxml_t ordered; // next tag, same section and depth, in original order |
bosko001 | 2:45b351b4fc2a | 33 | dxml_t child; // head of sub tag list, NULL if none |
bosko001 | 2:45b351b4fc2a | 34 | dxml_t parent; // parent tag, NULL if current tag is root tag |
bosko001 | 2:45b351b4fc2a | 35 | short flags; // additional information |
bosko001 | 2:45b351b4fc2a | 36 | }; |
bosko001 | 2:45b351b4fc2a | 37 | |
bosko001 | 2:45b351b4fc2a | 38 | // Given a string of xml data and its length, parses it and creates an dxml |
bosko001 | 2:45b351b4fc2a | 39 | // structure. For efficiency, modifies the data by adding null terminators |
bosko001 | 2:45b351b4fc2a | 40 | // and decoding ampersand sequences. If you don't want this, copy the data and |
bosko001 | 2:45b351b4fc2a | 41 | // pass in the copy. Returns NULL on failure. |
bosko001 | 2:45b351b4fc2a | 42 | dxml_t dxml_parse_str(char *s, size_t len); |
bosko001 | 2:45b351b4fc2a | 43 | |
bosko001 | 2:45b351b4fc2a | 44 | // A wrapper for dxml_parse_str() that accepts a file descriptor. First |
bosko001 | 2:45b351b4fc2a | 45 | // attempts to mem map the file. Failing that, reads the file into memory. |
bosko001 | 2:45b351b4fc2a | 46 | // Returns NULL on failure. |
bosko001 | 2:45b351b4fc2a | 47 | //dxml_t dxml_parse_fd(int fd); |
bosko001 | 2:45b351b4fc2a | 48 | |
bosko001 | 2:45b351b4fc2a | 49 | // a wrapper for dxml_parse_fd() that accepts a file name |
bosko001 | 2:45b351b4fc2a | 50 | dxml_t dxml_parse_file(const char *file); |
bosko001 | 4:7abcf4543282 | 51 | |
bosko001 | 2:45b351b4fc2a | 52 | // Wrapper for dxml_parse_str() that accepts a file stream. Reads the entire |
bosko001 | 2:45b351b4fc2a | 53 | // stream into memory and then parses it. For xml files, use dxml_parse_file() |
bosko001 | 2:45b351b4fc2a | 54 | // or dxml_parse_fd() |
bosko001 | 2:45b351b4fc2a | 55 | dxml_t dxml_parse_fp(FILE *fp); |
bosko001 | 2:45b351b4fc2a | 56 | |
bosko001 | 2:45b351b4fc2a | 57 | // returns the first child tag (one level deeper) with the given name or NULL |
bosko001 | 2:45b351b4fc2a | 58 | // if not found |
bosko001 | 2:45b351b4fc2a | 59 | dxml_t dxml_child(dxml_t xml, const char *name); |
bosko001 | 2:45b351b4fc2a | 60 | |
bosko001 | 2:45b351b4fc2a | 61 | // returns the next tag of the same name in the same section and depth or NULL |
bosko001 | 2:45b351b4fc2a | 62 | // if not found |
bosko001 | 2:45b351b4fc2a | 63 | #define dxml_next(xml) ((xml) ? xml->next : NULL) |
bosko001 | 2:45b351b4fc2a | 64 | |
bosko001 | 2:45b351b4fc2a | 65 | // Returns the Nth tag with the same name in the same section at the same depth |
bosko001 | 2:45b351b4fc2a | 66 | // or NULL if not found. An index of 0 returns the tag given. |
bosko001 | 2:45b351b4fc2a | 67 | dxml_t dxml_idx(dxml_t xml, int idx); |
bosko001 | 2:45b351b4fc2a | 68 | |
bosko001 | 2:45b351b4fc2a | 69 | // returns the name of the given tag |
bosko001 | 2:45b351b4fc2a | 70 | #define dxml_name(xml) ((xml) ? xml->name : NULL) |
bosko001 | 2:45b351b4fc2a | 71 | |
bosko001 | 2:45b351b4fc2a | 72 | // returns the given tag's character content or empty string if none |
bosko001 | 2:45b351b4fc2a | 73 | #define dxml_txt(xml) ((xml) ? xml->txt : "") |
bosko001 | 2:45b351b4fc2a | 74 | |
bosko001 | 2:45b351b4fc2a | 75 | // returns the value of the requested tag attribute, or NULL if not found |
bosko001 | 2:45b351b4fc2a | 76 | const char *dxml_attr(dxml_t xml, const char *attr); |
bosko001 | 2:45b351b4fc2a | 77 | |
bosko001 | 2:45b351b4fc2a | 78 | // Traverses the dxml sturcture to retrieve a specific subtag. Takes a |
bosko001 | 2:45b351b4fc2a | 79 | // variable length list of tag names and indexes. The argument list must be |
bosko001 | 4:7abcf4543282 | 80 | // terminated by either an index of -1 or an empty string tag name. Example: |
bosko001 | 2:45b351b4fc2a | 81 | // title = dxml_get(library, "shelf", 0, "book", 2, "title", -1); |
bosko001 | 2:45b351b4fc2a | 82 | // This retrieves the title of the 3rd book on the 1st shelf of library. |
bosko001 | 2:45b351b4fc2a | 83 | // Returns NULL if not found. |
bosko001 | 2:45b351b4fc2a | 84 | dxml_t dxml_get(dxml_t xml, ...); |
bosko001 | 2:45b351b4fc2a | 85 | |
bosko001 | 2:45b351b4fc2a | 86 | // Converts an dxml structure back to xml. Returns a string of xml data that |
bosko001 | 2:45b351b4fc2a | 87 | // must be freed. |
bosko001 | 2:45b351b4fc2a | 88 | char *dxml_toxml(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 89 | |
bosko001 | 2:45b351b4fc2a | 90 | // returns a NULL terminated array of processing instructions for the given |
bosko001 | 2:45b351b4fc2a | 91 | // target |
bosko001 | 2:45b351b4fc2a | 92 | const char **dxml_pi(dxml_t xml, const char *target); |
bosko001 | 2:45b351b4fc2a | 93 | |
bosko001 | 2:45b351b4fc2a | 94 | // frees the memory allocated for an dxml structure |
bosko001 | 2:45b351b4fc2a | 95 | void dxml_free(dxml_t xml); |
bosko001 | 4:7abcf4543282 | 96 | |
bosko001 | 2:45b351b4fc2a | 97 | // returns parser error message or empty string if none |
bosko001 | 2:45b351b4fc2a | 98 | const char *dxml_error(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 99 | |
bosko001 | 2:45b351b4fc2a | 100 | // returns a new empty dxml structure with the given root tag name |
bosko001 | 2:45b351b4fc2a | 101 | dxml_t dxml_new(const char *name); |
bosko001 | 2:45b351b4fc2a | 102 | |
bosko001 | 2:45b351b4fc2a | 103 | // wrapper for dxml_new() that strdup()s name |
bosko001 | 2:45b351b4fc2a | 104 | #define dxml_new_d(name) dxml_set_flag(dxml_new(strdup(name)), dxml_NAMEM) |
bosko001 | 2:45b351b4fc2a | 105 | |
bosko001 | 2:45b351b4fc2a | 106 | // Adds a child tag. off is the offset of the child tag relative to the start |
bosko001 | 2:45b351b4fc2a | 107 | // of the parent tag's character content. Returns the child tag. |
bosko001 | 2:45b351b4fc2a | 108 | dxml_t dxml_add_child(dxml_t xml, const char *name, size_t off); |
bosko001 | 2:45b351b4fc2a | 109 | |
bosko001 | 2:45b351b4fc2a | 110 | // wrapper for dxml_add_child() that strdup()s name |
bosko001 | 2:45b351b4fc2a | 111 | #define dxml_add_child_d(xml, name, off) \ |
bosko001 | 2:45b351b4fc2a | 112 | dxml_set_flag(dxml_add_child(xml, strdup(name), off), dxml_NAMEM) |
bosko001 | 2:45b351b4fc2a | 113 | |
bosko001 | 2:45b351b4fc2a | 114 | // sets the character content for the given tag and returns the tag |
bosko001 | 2:45b351b4fc2a | 115 | dxml_t dxml_set_txt(dxml_t xml, const char *txt); |
bosko001 | 2:45b351b4fc2a | 116 | |
bosko001 | 2:45b351b4fc2a | 117 | // wrapper for dxml_set_txt() that strdup()s txt |
bosko001 | 2:45b351b4fc2a | 118 | #define dxml_set_txt_d(xml, txt) \ |
bosko001 | 2:45b351b4fc2a | 119 | dxml_set_flag(dxml_set_txt(xml, strdup(txt)), dxml_TXTM) |
bosko001 | 2:45b351b4fc2a | 120 | |
bosko001 | 2:45b351b4fc2a | 121 | // Sets the given tag attribute or adds a new attribute if not found. A value |
bosko001 | 2:45b351b4fc2a | 122 | // of NULL will remove the specified attribute. Returns the tag given. |
bosko001 | 2:45b351b4fc2a | 123 | dxml_t dxml_set_attr(dxml_t xml, const char *name, const char *value); |
bosko001 | 2:45b351b4fc2a | 124 | |
bosko001 | 2:45b351b4fc2a | 125 | // Wrapper for dxml_set_attr() that strdup()s name/value. Value cannot be NULL |
bosko001 | 2:45b351b4fc2a | 126 | #define dxml_set_attr_d(xml, name, value) \ |
bosko001 | 2:45b351b4fc2a | 127 | dxml_set_attr(dxml_set_flag(xml, dxml_DUP), strdup(name), strdup(value)) |
bosko001 | 2:45b351b4fc2a | 128 | |
bosko001 | 2:45b351b4fc2a | 129 | // sets a flag for the given tag and returns the tag |
bosko001 | 2:45b351b4fc2a | 130 | dxml_t dxml_set_flag(dxml_t xml, short flag); |
bosko001 | 2:45b351b4fc2a | 131 | |
bosko001 | 2:45b351b4fc2a | 132 | // removes a tag along with its subtags without freeing its memory |
bosko001 | 2:45b351b4fc2a | 133 | dxml_t dxml_cut(dxml_t xml); |
bosko001 | 2:45b351b4fc2a | 134 | |
bosko001 | 2:45b351b4fc2a | 135 | // inserts an existing tag into an dxml structure |
bosko001 | 2:45b351b4fc2a | 136 | dxml_t dxml_insert(dxml_t xml, dxml_t dest, size_t off); |
bosko001 | 2:45b351b4fc2a | 137 | |
bosko001 | 2:45b351b4fc2a | 138 | // Moves an existing tag to become a subtag of dest at the given offset from |
bosko001 | 2:45b351b4fc2a | 139 | // the start of dest's character content. Returns the moved tag. |
bosko001 | 2:45b351b4fc2a | 140 | #define dxml_move(xml, dest, off) dxml_insert(dxml_cut(xml), dest, off) |
bosko001 | 2:45b351b4fc2a | 141 | |
bosko001 | 2:45b351b4fc2a | 142 | // removes a tag along with all its subtags |
bosko001 | 2:45b351b4fc2a | 143 | #define dxml_remove(xml) dxml_free(dxml_cut(xml)) |
bosko001 | 2:45b351b4fc2a | 144 | |
bosko001 | 2:45b351b4fc2a | 145 | #ifdef __cplusplus |
bosko001 | 2:45b351b4fc2a | 146 | } |
bosko001 | 2:45b351b4fc2a | 147 | #endif |
bosko001 | 2:45b351b4fc2a | 148 | |
bosko001 | 2:45b351b4fc2a | 149 | #endif // _dxml_H |
bosko001 | 2:45b351b4fc2a | 150 | |
bosko001 | 4:7abcf4543282 | 151 |