« third_party/modest/source/mycore/utils/mcsimple.c
↑ Summary
third_party/modest/source/myport/posix/mycore/memory.c »
| 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 | 145 | myhtml_tag_t * myhtml_tag_create(void) | |||||||||||||
| 25 | { | ||||||||||||||
| 26 | 145 | return (myhtml_tag_t*)mycore_malloc(sizeof(myhtml_tag_t)); | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 145 | mystatus_t myhtml_tag_init(myhtml_tree_t *tree, myhtml_tag_t *tags) | |||||||||||||
| 30 | { | ||||||||||||||
| 31 | mystatus_t status; | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 145 | tags->mcsimple_context = mcsimple_create(); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 145 | ▶ | if(tags->mcsimple_context == NULL) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 0 | return MyHTML_STATUS_TAGS_ERROR_MEMORY_ALLOCATION; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 145 | mcsimple_init(tags->mcsimple_context, 128, 1024, sizeof(myhtml_tag_context_t)); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 145 | tags->mchar_node = mchar_async_node_add(tree->mchar, &status); | |||||||||||||
| 41 | 145 | tags->tree = mctree_create(2); | |||||||||||||
| 42 | 145 | tags->mchar = tree->mchar; | |||||||||||||
| 43 | 145 | tags->tags_count = MyHTML_TAG_LAST_ENTRY; | |||||||||||||
| 44 | |||||||||||||||
| 45 | 145 | ▶ | if(status) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | return status; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 145 | ▶ | if(tags->tree == NULL) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 145 | myhtml_tag_clean(tags); | |||||||||||||
| 52 | |||||||||||||||
| 53 | 145 | return MyHTML_STATUS_OK; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 290 | void myhtml_tag_clean(myhtml_tag_t* tags) | |||||||||||||
| 57 | { | ||||||||||||||
| 58 | 290 | tags->tags_count = MyHTML_TAG_LAST_ENTRY; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 290 | mcsimple_clean(tags->mcsimple_context); | |||||||||||||
| 61 | 290 | mchar_async_node_clean(tags->mchar, tags->mchar_node); | |||||||||||||
| 62 | 290 | mctree_clean(tags->tree); | |||||||||||||
| 63 | 290 | } | |||||||||||||
| 64 | |||||||||||||||
| 65 | 144 | myhtml_tag_t * myhtml_tag_destroy(myhtml_tag_t* tags) | |||||||||||||
| 66 | { | ||||||||||||||
| 67 | 144 | ▶ | if(tags == NULL) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | return NULL; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 144 | tags->tree = mctree_destroy(tags->tree); | |||||||||||||
| 71 | 144 | tags->mcsimple_context = mcsimple_destroy(tags->mcsimple_context, true); | |||||||||||||
| 72 | |||||||||||||||
| 73 | 144 | mchar_async_node_delete(tags->mchar, tags->mchar_node); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 144 | mycore_free(tags); | |||||||||||||
| 76 | |||||||||||||||
| 77 | 144 | return NULL; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 22 | 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 | 22 | char* cache = mchar_async_malloc(tags->mchar, tags->mchar_node, (key_size + 1)); | |||||||||||||
| 84 | |||||||||||||||
| 85 | 22 | ▶ | if(to_lcase) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | size_t i; | ||||||||||||||
| 87 | 921813 | ▶ | for(i = 0; i < key_size; i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 921791 | ▶ | cache[i] = key[i] > 0x40 && key[i] < 0x5b ? (key[i]|0x60) : key[i]; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 89 | } | ||||||||||||||
| 90 | 22 | 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 | 22 | myhtml_tag_context_t *tag_ctx = mcsimple_malloc(tags->mcsimple_context); | |||||||||||||
| 100 | |||||||||||||||
| 101 | 22 | mctree_insert(tags->tree, cache, key_size, (void *)tag_ctx, NULL); | |||||||||||||
| 102 | |||||||||||||||
| 103 | 22 | tag_ctx->id = tags->tags_count; | |||||||||||||
| 104 | 22 | tag_ctx->name = cache; | |||||||||||||
| 105 | 22 | tag_ctx->name_length = key_size; | |||||||||||||
| 106 | 22 | tag_ctx->data_parser = data_parser; | |||||||||||||
| 107 | |||||||||||||||
| 108 | 22 | tags->tags_count++; | |||||||||||||
| 109 | |||||||||||||||
| 110 | 22 | memset(tag_ctx->cats, 0, sizeof(enum myhtml_tag_categories) * MyHTML_NAMESPACE_LAST_ENTRY); | |||||||||||||
| 111 | |||||||||||||||
| 112 | 22 | 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
| |||||||||||||||
| 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 | 2009 | const myhtml_tag_context_t * myhtml_tag_get_by_id(myhtml_tag_t* tags, myhtml_tag_id_t tag_id) | |||||||||||||
| 126 | { | ||||||||||||||
| 127 | 2009 | ▶ | if(tag_id >= MyHTML_TAG_LAST_ENTRY) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 9 | return mcsimple_get_by_absolute_position(tags->mcsimple_context, (tag_id - MyHTML_TAG_LAST_ENTRY)); | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 2000 | return myhtml_tag_static_get_by_id(tag_id); | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | 1302 | const myhtml_tag_context_t * myhtml_tag_get_by_name(myhtml_tag_t* tags, const char* name, size_t length) | |||||||||||||
| 135 | { | ||||||||||||||
| 136 | 1302 | const myhtml_tag_context_t *ctx = myhtml_tag_static_search(name, length); | |||||||||||||
| 137 | |||||||||||||||
| 138 | 1302 | ▶ | if(ctx) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 1206 | return ctx; | |||||||||||||
| 140 | |||||||||||||||
| 141 | 96 | mctree_index_t idx = mctree_search_lowercase(tags->tree, name, length); | |||||||||||||
| 142 | |||||||||||||||
| 143 | 96 | return (myhtml_tag_context_t*)tags->tree->nodes[idx].value; | |||||||||||||
| 144 | } | ||||||||||||||