source/myhtml/tag.c

filter: Statementstmt 32.4% 48 / 148 Branchbran 11.5% 6 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 27.0% 54 / 200 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« source/myhtml/tokenizer.c ↑ Summary source/myhtml/myosi.c »
1 /*
2 Copyright 2015-2016 Alexander Borisov
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Author: lex.borisov@gmail.com (Alexander Borisov)
17 */
18  
19 #include "myhtml/tag.h"
20  
21  
22 1 myhtml_tag_t * myhtml_tag_create(void)
23 {
24 1 return (myhtml_tag_t*)mymalloc(sizeof(myhtml_tag_t));
25 }
26  
27 1 myhtml_status_t myhtml_tag_init(myhtml_tree_t *tree, myhtml_tag_t *tags)
28 {
29 1 tags->mcsimple_context = mcsimple_create();
30
31 1 ▶ if(tags->mcsimple_context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(tags->mcsimple_context == NULL) 0 1 1
32 0 return MyHTML_STATUS_TAGS_ERROR_MEMORY_ALLOCATION;
33
34 1 mcsimple_init(tags->mcsimple_context, 128, 1024, sizeof(myhtml_tag_context_t));
35
36 1 tags->mchar_node = mchar_async_node_add(tree->myhtml->mchar);
37 1 tags->tree = mctree_create(32);
38
39 1 tags->mchar = tree->myhtml->mchar;
40 1 tags->tag_index = tree->myhtml->tag_index;
41
42 1 tags->tags_count = MyHTML_TAG_LAST_ENTRY;
43
44 1 myhtml_tag_clean(tags);
45
46 1 return MyHTML_STATUS_OK;
47 }
48  
49 2 void myhtml_tag_clean(myhtml_tag_t* tags)
50 {
51 2 tags->tags_count = MyHTML_TAG_LAST_ENTRY;
52
53 2 mcsimple_clean(tags->mcsimple_context);
54 2 mchar_async_node_clean(tags->mchar, tags->mchar_node);
55 2 mctree_clean(tags->tree);
56 2 }
57  
58 1 myhtml_tag_t * myhtml_tag_destroy(myhtml_tag_t* tags)
59 {
60 1 ▶ if(tags == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(tags == NULL) 0 1 1
61 0 return NULL;
62
63 1 tags->tree = mctree_destroy(tags->tree);
64 1 tags->mcsimple_context = mcsimple_destroy(tags->mcsimple_context, true);
65
66 1 mchar_async_node_delete(tags->mchar, tags->mchar_node);
67
68 1 free(tags);
69
70 1 return NULL;
71 }
72  
73 1 myhtml_tag_index_t * myhtml_tag_index_create(void)
74 {
75 1 return (myhtml_tag_index_t*)calloc(1, sizeof(myhtml_tag_index_t));
76 }
77  
78 1 myhtml_status_t myhtml_tag_index_init(myhtml_tag_t* tags, myhtml_tag_index_t* idx_tags)
79 {
80 mcobject_async_status_t mcstatus;
81 1 tags->mcobject_node = mcobject_async_node_add(tags->tag_index, &mcstatus);
82
83 1 ▶ if(mcstatus != MCOBJECT_ASYNC_STATUS_OK)
Branch: 1 branch
BranchTrueFalseTotal
if(mcstatus != MCOBJECT_ASYNC_STATUS_OK) 0 1 1
84 0 return MyHTML_STATUS_TAGS_ERROR_MCOBJECT_CREATE;
85
86 1 idx_tags->tags_size = tags->tags_count + 128;
87 1 idx_tags->tags_length = 0;
88 1 idx_tags->tags = (myhtml_tag_index_entry_t*)calloc(idx_tags->tags_size, sizeof(myhtml_tag_index_entry_t));
89
90 1 ▶ if(idx_tags->tags == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(idx_tags->tags == NULL) 0 1 1
91 0 return MyHTML_STATUS_TAGS_ERROR_INDEX_MEMORY_ALLOCATION;
92
93 1 return MyHTML_STATUS_OK;
94 }
95  
96 1 void myhtml_tag_index_clean(myhtml_tag_t* tags, myhtml_tag_index_t* index_tags)
97 {
98 1 mcobject_async_node_clean(tags->tag_index, tags->mcobject_node);
99 1 memset(index_tags->tags, 0, sizeof(myhtml_tag_index_entry_t) * index_tags->tags_size);
100 1 }
101  
102 1 myhtml_tag_index_t * myhtml_tag_index_destroy(myhtml_tag_t* tags, myhtml_tag_index_t* index_tags)
103 {
104 1 mcobject_async_node_delete(tags->tag_index, tags->mcobject_node);
105
106 1 ▶ if(index_tags == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(index_tags == NULL) 0 1 1
107 0 return NULL;
108
109 1 ▶ if(index_tags->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if(index_tags->tags) { 1 0 1
110 1 free(index_tags->tags);
111 1 index_tags->tags = NULL;
112 }
113
114 1 free(index_tags);
115
116 1 return NULL;
117 }
118  
119 0 void myhtml_tag_index_check_size(myhtml_tag_t* tags, myhtml_tag_index_t* index_tags, myhtml_tag_id_t tag_id)
120 {
121 0 ▶ if(tag_id >= index_tags->tags_size) {
Branch: 1 branch
BranchTrueFalseTotal
if(tag_id >= index_tags->tags_size) { 0 0 0
122 0 size_t new_size = tag_id + 128;
123
124 0 myhtml_tag_index_entry_t *index_entries = (myhtml_tag_index_entry_t*)myrealloc(index_tags->tags,
125 sizeof(myhtml_tag_index_entry_t) *
126 new_size);
127
128 0 ▶ if(index_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if(index_entries) { 0 0 0
129 0 index_tags->tags = index_entries;
130
131 0 memset(&index_tags->tags[index_tags->tags_size], 0, sizeof(myhtml_tag_index_entry_t)
132 0 * (new_size - index_tags->tags_size));
133
134 0 index_tags->tags_size = new_size;
135 }
136 else {
137 // TODO: error
138 }
139 }
140 0 }
141  
142 0 myhtml_status_t myhtml_tag_index_add(myhtml_tag_t* tags, myhtml_tag_index_t* idx_tags, myhtml_tree_node_t* node)
143 {
144 0 myhtml_tag_index_check_size(tags, idx_tags, node->tag_idx);
145
146 0 myhtml_tag_index_entry_t* tag = &idx_tags->tags[node->tag_idx];
147
148 mcobject_async_status_t mcstatus;
149 0 myhtml_tag_index_node_t* new_node = mcobject_async_malloc(tags->tag_index, tags->mcobject_node, &mcstatus);
150
151 0 ▶ if(mcstatus != MCOBJECT_ASYNC_STATUS_OK)
Branch: 1 branch
BranchTrueFalseTotal
if(mcstatus != MCOBJECT_ASYNC_STATUS_OK) 0 0 0
152 0 return MyHTML_STATUS_TAGS_ERROR_MCOBJECT_MALLOC;
153
154 0 myhtml_tag_index_clean_node(new_node);
155
156 0 ▶ if(tag->first == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(tag->first == NULL) { 0 0 0
157 0 tag->first = new_node;
158 0 new_node->prev = NULL;
159 }
160 else {
161 0 tag->last->next = new_node;
162 0 new_node->prev = tag->last;
163 }
164
165 0 new_node->next = NULL;
166 0 new_node->node = node;
167
168 0 tag->last = new_node;
169
170 0 tag->count++;
171
172 0 return MyHTML_STATUS_OK;
173 }
174  
175 0 myhtml_tag_index_entry_t * myhtml_tag_index_entry(myhtml_tag_index_t* tag_index, myhtml_tag_id_t tag_id)
176 {
177 0 ▶ if(tag_index->tags_size > tag_id)
Branch: 1 branch
BranchTrueFalseTotal
if(tag_index->tags_size > tag_id) 0 0 0
178 0 return &tag_index->tags[tag_id];
179
180 0 return NULL;
181 }
182  
183 0 size_t myhtml_tag_index_entry_count(myhtml_tag_index_t* tag_index, myhtml_tag_id_t tag_id)
184 {
185 0 ▶ if(tag_index->tags_size > tag_id)
Branch: 1 branch
BranchTrueFalseTotal
if(tag_index->tags_size > tag_id) 0 0 0
186 0 return tag_index->tags[tag_id].count;
187
188 0 return 0;
189 }
190  
191 0 myhtml_tag_index_node_t * myhtml_tag_index_first(myhtml_tag_index_t* tag_index, myhtml_tag_id_t tag_id)
192 {
193 0 ▶ if(tag_index->tags_size > tag_id)
Branch: 1 branch
BranchTrueFalseTotal
if(tag_index->tags_size > tag_id) 0 0 0
194 0 return tag_index->tags[tag_id].first;
195
196 0 return NULL;
197 }
198  
199 0 myhtml_tag_index_node_t * myhtml_tag_index_last(myhtml_tag_index_t* tag_index, myhtml_tag_id_t tag_id)
200 {
201 0 ▶ if(tag_index->tags_size > tag_id)
Branch: 1 branch
BranchTrueFalseTotal
if(tag_index->tags_size > tag_id) 0 0 0
202 0 return tag_index->tags[tag_id].last;
203
204 0 return NULL;
205 }
206  
207 0 myhtml_tag_index_node_t * myhtml_tag_index_next(myhtml_tag_index_node_t *index_node)
208 {
209 0 ▶ if(index_node)
Branch: 1 branch
BranchTrueFalseTotal
if(index_node) 0 0 0
210 0 return index_node->next;
211
212 0 return NULL;
213 }
214  
215 0 myhtml_tag_index_node_t * myhtml_tag_index_prev(myhtml_tag_index_node_t *index_node)
216 {
217 0 ▶ if(index_node)
Branch: 1 branch
BranchTrueFalseTotal
if(index_node) 0 0 0
218 0 return index_node->prev;
219
220 0 return NULL;
221 }
222  
223  
224 0 myhtml_tree_node_t * myhtml_tag_index_tree_node(myhtml_tag_index_node_t *index_node)
225 {
226 0 ▶ if(index_node)
Branch: 1 branch
BranchTrueFalseTotal
if(index_node) 0 0 0
227 0 return index_node->node;
228
229 0 return NULL;
230 }
231  
232 0 myhtml_tag_id_t myhtml_tag_add(myhtml_tag_t* tags, const char* key, size_t key_size,
233 enum myhtml_tokenizer_state data_parser, bool to_lcase)
234 {
235 0 char* cache = mchar_async_malloc(tags->mchar, tags->mchar_node, (key_size + 1));
236
237 0 ▶ if(to_lcase) {
Branch: 1 branch
BranchTrueFalseTotal
if(to_lcase) { 0 0 0
238 size_t i;
239 0 ▶ for(i = 0; i < key_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < key_size; i++) { 0 0 0
240 0 ▶ cache[i] = key[i] > 0x40 && key[i] < 0x5b ? (key[i]|0x60) : key[i];
Branch: 2 branches
BranchTrueFalseTotal
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
241 }
242 0 cache[i] = '\0';
243 }
244 else {
245 0 strncpy(cache, key, key_size);
246 0 cache[key_size] = '\0';
247 }
248
249 // add tags
250
251 0 myhtml_tag_context_t *tag_ctx = mcsimple_malloc(tags->mcsimple_context);
252
253 0 mctree_insert(tags->tree, cache, key_size, (void *)tag_ctx, NULL);
254
255 0 tag_ctx->id = tags->tags_count;
256 0 tag_ctx->name = cache;
257 0 tag_ctx->name_length = key_size;
258 0 tag_ctx->data_parser = data_parser;
259
260 0 tags->tags_count++;
261
262 0 memset(tag_ctx->cats, 0, sizeof(enum myhtml_tag_categories) * MyHTML_NAMESPACE_LAST_ENTRY);
263
264 0 return tag_ctx->id;
265 }
266  
267 0 void myhtml_tag_set_category(myhtml_tag_t* tags, myhtml_tag_id_t tag_idx,
268 enum myhtml_namespace my_namespace, enum myhtml_tag_categories cats)
269 {
270 0 ▶ if(tag_idx < MyHTML_TAG_LAST_ENTRY)
Branch: 1 branch
BranchTrueFalseTotal
if(tag_idx < MyHTML_TAG_LAST_ENTRY) 0 0 0
271 0 return;
272
273 0 myhtml_tag_context_t *tag_ctx = mcsimple_get_by_absolute_position(tags->mcsimple_context, (tag_idx - MyHTML_TAG_LAST_ENTRY));
274 0 tag_ctx->cats[my_namespace] = cats;
275 }
276  
277 0 const myhtml_tag_context_t * myhtml_tag_get_by_id(myhtml_tag_t* tags, myhtml_tag_id_t tag_id)
278 {
279 0 ▶ if(tag_id >= MyHTML_TAG_LAST_ENTRY) {
Branch: 1 branch
BranchTrueFalseTotal
if(tag_id >= MyHTML_TAG_LAST_ENTRY) { 0 0 0
280 0 return mcsimple_get_by_absolute_position(tags->mcsimple_context, (tag_id - MyHTML_TAG_LAST_ENTRY));
281 }
282
283 0 return myhtml_tag_static_get_by_id(tag_id);
284 }
285  
286 0 const myhtml_tag_context_t * myhtml_tag_get_by_name(myhtml_tag_t* tags, const char* name, size_t length)
287 {
288 0 const myhtml_tag_context_t *ctx = myhtml_tag_static_search(name, length);
289
290 0 ▶ if(ctx)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx) 0 0 0
291 0 return ctx;
292
293 0 mctree_index_t idx = mctree_search_lowercase(tags->tree, name, length);
294
295 0 return (myhtml_tag_context_t*)tags->tree->nodes[idx].value;
296 }
297  
298 0 void myhtml_tag_print(myhtml_tag_t* tags, FILE* fh)
299 {
300 size_t i;
301 0 ▶ for(i = MyHTML_TAG_FIRST_ENTRY; i < MyHTML_TAG_LAST_ENTRY; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = MyHTML_TAG_FIRST_ENTRY; i < MyHTML_TAG_LAST_ENTRY; i++) 0 0 0
302 {
303 0 const myhtml_tag_context_t *ctx = myhtml_tag_get_by_id(tags, i);
304
305 0 fprintf(fh, "<%s id=\"%zu\">\n", ctx->name, i);
306 }
307
308 0 ▶ for(i = (MyHTML_TAG_LAST_ENTRY + 1); i < tags->tags_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = (MyHTML_TAG_LAST_ENTRY + 1); i < tags->tags_count; i++) 0 0 0
309 {
310 0 const myhtml_tag_context_t *ctx = myhtml_tag_get_by_id(tags, i);
311
312 0 fprintf(fh, "<%s id=\"%zu\">\n", ctx->name, i);
313 }
314 0 }
315  
316  
317  
« source/myhtml/tokenizer.c ↑ Summary source/myhtml/myosi.c »