| 1 |
|
| /* |
| 2 |
|
| Copyright (C) 2015-2017 Alexander Borisov |
| 3 |
|
| |
| 4 |
|
| This library is free software; you can redistribute it and/or |
| 5 |
|
| modify it under the terms of the GNU Lesser General Public |
| 6 |
|
| License as published by the Free Software Foundation; either |
| 7 |
|
| version 2.1 of the License, or (at your option) any later version. |
| 8 |
|
| |
| 9 |
|
| This library is distributed in the hope that it will be useful, |
| 10 |
|
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
|
| Lesser General Public License for more details. |
| 13 |
|
| |
| 14 |
|
| You should have received a copy of the GNU Lesser General Public |
| 15 |
|
| License along with this library; if not, write to the Free Software |
| 16 |
|
| Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
|
| |
| 18 |
|
| Author: lex.borisov@gmail.com (Alexander Borisov) |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| #include "myhtml/tag.h" |
| 22 |
|
| |
| 23 |
|
| |
| 24 |
0 |
| myhtml_tag_t * myhtml_tag_create(void) |
| 25 |
|
| { |
| 26 |
0 |
| return (myhtml_tag_t*)mycore_malloc(sizeof(myhtml_tag_t)); |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
0 |
| mystatus_t myhtml_tag_init(myhtml_tree_t *tree, myhtml_tag_t *tags) |
| 30 |
|
| { |
| 31 |
|
| mystatus_t status; |
| 32 |
|
| |
| 33 |
0 |
| tags->mcsimple_context = mcsimple_create(); |
| 34 |
|
| |
| 35 |
0 |
▶ | if(tags->mcsimple_context == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tags->mcsimple_context == NULL) |
0 |
0 |
0 |
|
| 36 |
0 |
| return MyHTML_STATUS_TAGS_ERROR_MEMORY_ALLOCATION; |
| 37 |
|
| |
| 38 |
0 |
| mcsimple_init(tags->mcsimple_context, 128, 1024, sizeof(myhtml_tag_context_t)); |
| 39 |
|
| |
| 40 |
0 |
| tags->mchar_node = mchar_async_node_add(tree->mchar, &status); |
| 41 |
0 |
| tags->tree = mctree_create(2); |
| 42 |
0 |
| tags->mchar = tree->mchar; |
| 43 |
0 |
| tags->tags_count = MyHTML_TAG_LAST_ENTRY; |
| 44 |
|
| |
| 45 |
0 |
▶ | if(status) |
Branch: 1 branch
| Branch | True | False | Total |
| if(status) |
0 |
0 |
0 |
|
| 46 |
0 |
| return status; |
| 47 |
|
| |
| 48 |
0 |
▶ | if(tags->tree == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tags->tree == NULL) |
0 |
0 |
0 |
|
| 49 |
0 |
| return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION; |
| 50 |
|
| |
| 51 |
0 |
| myhtml_tag_clean(tags); |
| 52 |
|
| |
| 53 |
0 |
| return MyHTML_STATUS_OK; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
0 |
| void myhtml_tag_clean(myhtml_tag_t* tags) |
| 57 |
|
| { |
| 58 |
0 |
| tags->tags_count = MyHTML_TAG_LAST_ENTRY; |
| 59 |
|
| |
| 60 |
0 |
| mcsimple_clean(tags->mcsimple_context); |
| 61 |
0 |
| mchar_async_node_clean(tags->mchar, tags->mchar_node); |
| 62 |
0 |
| mctree_clean(tags->tree); |
| 63 |
0 |
| } |
| 64 |
|
| |
| 65 |
0 |
| myhtml_tag_t * myhtml_tag_destroy(myhtml_tag_t* tags) |
| 66 |
|
| { |
| 67 |
0 |
▶ | if(tags == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tags == NULL) |
0 |
0 |
0 |
|
| 68 |
0 |
| return NULL; |
| 69 |
|
| |
| 70 |
0 |
| tags->tree = mctree_destroy(tags->tree); |
| 71 |
0 |
| tags->mcsimple_context = mcsimple_destroy(tags->mcsimple_context, true); |
| 72 |
|
| |
| 73 |
0 |
| mchar_async_node_delete(tags->mchar, tags->mchar_node); |
| 74 |
|
| |
| 75 |
0 |
| mycore_free(tags); |
| 76 |
|
| |
| 77 |
0 |
| return NULL; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
0 |
| myhtml_tag_id_t myhtml_tag_add(myhtml_tag_t* tags, const char* key, size_t key_size, |
| 81 |
|
| enum myhtml_tokenizer_state data_parser, bool to_lcase) |
| 82 |
|
| { |
| 83 |
0 |
| char* cache = mchar_async_malloc(tags->mchar, tags->mchar_node, (key_size + 1)); |
| 84 |
|
| |
| 85 |
0 |
▶ | if(to_lcase) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(to_lcase) { |
0 |
0 |
0 |
|
| 86 |
|
| size_t i; |
| 87 |
0 |
▶ | for(i = 0; i < key_size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < key_size; i++) { |
0 |
0 |
0 |
|
| 88 |
0 |
▶ | cache[i] = key[i] > 0x40 && key[i] < 0x5b ? (key[i]|0x60) : key[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| cache[i] = key[i] > 0x40 && key[i] < 0x5b ? (key[i]|0x60) : key[i]; |
0 |
0 |
0 |
| cache[i] = key[i] > 0x40 && key[i] < 0x5b ? (key[i]|0x60) : key[i]; |
0 |
0 |
0 |
|
| 89 |
|
| } |
| 90 |
0 |
| cache[i] = '\0'; |
| 91 |
|
| } |
| 92 |
|
| else { |
| 93 |
0 |
| strncpy(cache, key, key_size); |
| 94 |
0 |
| cache[key_size] = '\0'; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| // add tags |
| 98 |
|
| |
| 99 |
0 |
| myhtml_tag_context_t *tag_ctx = mcsimple_malloc(tags->mcsimple_context); |
| 100 |
|
| |
| 101 |
0 |
| mctree_insert(tags->tree, cache, key_size, (void *)tag_ctx, NULL); |
| 102 |
|
| |
| 103 |
0 |
| tag_ctx->id = tags->tags_count; |
| 104 |
0 |
| tag_ctx->name = cache; |
| 105 |
0 |
| tag_ctx->name_length = key_size; |
| 106 |
0 |
| tag_ctx->data_parser = data_parser; |
| 107 |
|
| |
| 108 |
0 |
| tags->tags_count++; |
| 109 |
|
| |
| 110 |
0 |
| memset(tag_ctx->cats, 0, sizeof(enum myhtml_tag_categories) * MyHTML_NAMESPACE_LAST_ENTRY); |
| 111 |
|
| |
| 112 |
0 |
| return tag_ctx->id; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
0 |
| void myhtml_tag_set_category(myhtml_tag_t* tags, myhtml_tag_id_t tag_idx, |
| 116 |
|
| enum myhtml_namespace ns, enum myhtml_tag_categories cats) |
| 117 |
|
| { |
| 118 |
0 |
▶ | if(tag_idx < MyHTML_TAG_LAST_ENTRY) |
Branch: 1 branch
| Branch | True | False | Total |
| if(tag_idx < MyHTML_TAG_LAST_ENTRY) |
0 |
0 |
0 |
|
| 119 |
0 |
| return; |
| 120 |
|
| |
| 121 |
0 |
| myhtml_tag_context_t *tag_ctx = mcsimple_get_by_absolute_position(tags->mcsimple_context, (tag_idx - MyHTML_TAG_LAST_ENTRY)); |
| 122 |
0 |
| tag_ctx->cats[ns] = cats; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
0 |
| const myhtml_tag_context_t * myhtml_tag_get_by_id(myhtml_tag_t* tags, myhtml_tag_id_t tag_id) |
| 126 |
|
| { |
| 127 |
0 |
▶ | if(tag_id >= MyHTML_TAG_LAST_ENTRY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tag_id >= MyHTML_TAG_LAST_ENTRY) { |
0 |
0 |
0 |
|
| 128 |
0 |
| return mcsimple_get_by_absolute_position(tags->mcsimple_context, (tag_id - MyHTML_TAG_LAST_ENTRY)); |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
0 |
| return myhtml_tag_static_get_by_id(tag_id); |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
0 |
| const myhtml_tag_context_t * myhtml_tag_get_by_name(myhtml_tag_t* tags, const char* name, size_t length) |
| 135 |
|
| { |
| 136 |
0 |
| const myhtml_tag_context_t *ctx = myhtml_tag_static_search(name, length); |
| 137 |
|
| |
| 138 |
0 |
▶ | if(ctx) |
Branch: 1 branch
| Branch | True | False | Total |
| if(ctx) |
0 |
0 |
0 |
|
| 139 |
0 |
| return ctx; |
| 140 |
|
| |
| 141 |
0 |
| mctree_index_t idx = mctree_search_lowercase(tags->tree, name, length); |
| 142 |
|
| |
| 143 |
0 |
| return (myhtml_tag_context_t*)tags->tree->nodes[idx].value; |
| 144 |
|
| } |