« third_party/modest/source/myhtml/serialization.c
↑ Summary
third_party/modest/source/myhtml/tag.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/stream.h" | ||||||||||
| 22 | |||||||||||
| 23 | 0 | myhtml_stream_buffer_t * myhtml_stream_buffer_create(void) | |||||||||
| 24 | { | ||||||||||
| 25 | 0 | return mycore_calloc(1, sizeof(myhtml_stream_buffer_t)); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 0 | mystatus_t myhtml_stream_buffer_init(myhtml_stream_buffer_t* stream_buffer, size_t entries_size) | |||||||||
| 29 | { | ||||||||||
| 30 | 0 | stream_buffer->length = 0; | |||||||||
| 31 | 0 | stream_buffer->size = entries_size; | |||||||||
| 32 | 0 | stream_buffer->entries = mycore_calloc(entries_size, sizeof(myhtml_stream_buffer_entry_t)); | |||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | if(stream_buffer->entries == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | return MyHTML_STATUS_STREAM_BUFFER_ERROR_INIT; | |||||||||
| 36 | |||||||||||
| 37 | 0 | return MyHTML_STATUS_OK; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | mystatus_t myhtml_stream_buffer_entry_init(myhtml_stream_buffer_entry_t* stream_buffer_entry, size_t size) | |||||||||
| 41 | { | ||||||||||
| 42 | 0 | ▶ | if(stream_buffer_entry->data) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | ▶ | if(size <= stream_buffer_entry->size) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return MyHTML_STATUS_OK; | |||||||||
| 45 | else | ||||||||||
| 46 | 0 | mycore_free(stream_buffer_entry->data); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | stream_buffer_entry->length = 0; | |||||||||
| 50 | 0 | stream_buffer_entry->size = size; | |||||||||
| 51 | 0 | stream_buffer_entry->data = mycore_malloc(size * sizeof(char)); | |||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | if(stream_buffer_entry->data == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | return MyHTML_STATUS_STREAM_BUFFER_ENTRY_ERROR_INIT; | |||||||||
| 55 | |||||||||||
| 56 | 0 | return MyHTML_STATUS_OK; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | void myhtml_stream_buffer_entry_clean(myhtml_stream_buffer_entry_t* stream_buffer_entry) | |||||||||
| 60 | { | ||||||||||
| 61 | 0 | ▶ | if(stream_buffer_entry) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | stream_buffer_entry->length = 0; | |||||||||
| 63 | 0 | } | |||||||||
| 64 | |||||||||||
| 65 | 0 | myhtml_stream_buffer_entry_t * myhtml_stream_buffer_entry_destroy(myhtml_stream_buffer_entry_t* stream_buffer_entry, bool self_destroy) | |||||||||
| 66 | { | ||||||||||
| 67 | 0 | ▶ | if(stream_buffer_entry == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | return NULL; | |||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | if(stream_buffer_entry->data) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | mycore_free(stream_buffer_entry->data); | |||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | if(self_destroy) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | mycore_free(stream_buffer_entry); | |||||||||
| 75 | 0 | return NULL; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | return stream_buffer_entry; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | void myhtml_stream_buffer_clean(myhtml_stream_buffer_t* stream_buffer) | |||||||||
| 82 | { | ||||||||||
| 83 | 0 | ▶ | if(stream_buffer) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | stream_buffer->length = 0; | |||||||||
| 85 | 0 | } | |||||||||
| 86 | |||||||||||
| 87 | 0 | myhtml_stream_buffer_t * myhtml_stream_buffer_destroy(myhtml_stream_buffer_t* stream_buffer, bool self_destroy) | |||||||||
| 88 | { | ||||||||||
| 89 | 0 | ▶ | if(stream_buffer == NULL) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | return NULL; | |||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | if(stream_buffer->entries) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | ▶ | for(size_t i = 0; i < stream_buffer->length; i++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | myhtml_stream_buffer_entry_destroy(&stream_buffer->entries[i], false); | |||||||||
| 95 | |||||||||||
| 96 | 0 | mycore_free(stream_buffer->entries); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | if(self_destroy) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | mycore_free(stream_buffer); | |||||||||
| 101 | 0 | return NULL; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 0 | return stream_buffer; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | myhtml_stream_buffer_entry_t * myhtml_stream_buffer_add_entry(myhtml_stream_buffer_t* stream_buffer, size_t entry_data_size) | |||||||||
| 108 | { | ||||||||||
| 109 | 0 | ▶ | if(stream_buffer->length >= stream_buffer->size) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | size_t new_size = stream_buffer->size << 1; | |||||||||
| 111 | |||||||||||
| 112 | 0 | myhtml_stream_buffer_entry_t *entries = mycore_realloc(stream_buffer, sizeof(myhtml_stream_buffer_entry_t) * new_size); | |||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | if(entries) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | memset(&entries[stream_buffer->size], 0, (new_size - stream_buffer->size)); | |||||||||
| 116 | |||||||||||
| 117 | 0 | stream_buffer->entries = entries; | |||||||||
| 118 | 0 | stream_buffer->size = new_size; | |||||||||
| 119 | } | ||||||||||
| 120 | else | ||||||||||
| 121 | 0 | return NULL; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | myhtml_stream_buffer_entry_t *entry = &stream_buffer->entries[ stream_buffer->length ]; | |||||||||
| 125 | 0 | mystatus_t status = myhtml_stream_buffer_entry_init(entry, entry_data_size); | |||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | if(status != MyHTML_STATUS_OK) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | return NULL; | |||||||||
| 129 | |||||||||||
| 130 | 0 | stream_buffer->length++; | |||||||||
| 131 | |||||||||||
| 132 | 0 | return entry; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | myhtml_stream_buffer_entry_t * myhtml_stream_buffer_current_entry(myhtml_stream_buffer_t* stream_buffer) | |||||||||
| 136 | { | ||||||||||
| 137 | 0 | ▶ | if(stream_buffer->length == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 0 | return NULL; | |||||||||
| 139 | |||||||||||
| 140 | 0 | return &stream_buffer->entries[ (stream_buffer->length - 1) ]; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||