File Coverage

third_party/modest/source/myhtml/stream.c
Criterion Covered Total %
statement 44 67 65.6
branch 16 34 47.0
condition n/a
subroutine n/a
pod n/a
total 60 101 59.4


line stmt bran cond sub pod time code
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 3           myhtml_stream_buffer_t * myhtml_stream_buffer_create(void)
24             {
25 3           return mycore_calloc(1, sizeof(myhtml_stream_buffer_t));
26             }
27              
28 3           mystatus_t myhtml_stream_buffer_init(myhtml_stream_buffer_t* stream_buffer, size_t entries_size)
29             {
30 3           stream_buffer->length = 0;
31 3           stream_buffer->size = entries_size;
32 3           stream_buffer->entries = mycore_calloc(entries_size, sizeof(myhtml_stream_buffer_entry_t));
33            
34 3 50         if(stream_buffer->entries == NULL)
35 0           return MyHTML_STATUS_STREAM_BUFFER_ERROR_INIT;
36            
37 3           return MyHTML_STATUS_OK;
38             }
39              
40 3           mystatus_t myhtml_stream_buffer_entry_init(myhtml_stream_buffer_entry_t* stream_buffer_entry, size_t size)
41             {
42 3 50         if(stream_buffer_entry->data) {
43 0 0         if(size <= stream_buffer_entry->size)
44 0           return MyHTML_STATUS_OK;
45             else
46 0           mycore_free(stream_buffer_entry->data);
47             }
48            
49 3           stream_buffer_entry->length = 0;
50 3           stream_buffer_entry->size = size;
51 3           stream_buffer_entry->data = mycore_malloc(size * sizeof(char));
52            
53 3 50         if(stream_buffer_entry->data == NULL)
54 0           return MyHTML_STATUS_STREAM_BUFFER_ENTRY_ERROR_INIT;
55            
56 3           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 0         if(stream_buffer_entry)
62 0           stream_buffer_entry->length = 0;
63 0           }
64              
65 3           myhtml_stream_buffer_entry_t * myhtml_stream_buffer_entry_destroy(myhtml_stream_buffer_entry_t* stream_buffer_entry, bool self_destroy)
66             {
67 3 50         if(stream_buffer_entry == NULL)
68 0           return NULL;
69            
70 3 50         if(stream_buffer_entry->data)
71 3           mycore_free(stream_buffer_entry->data);
72            
73 3 50         if(self_destroy) {
74 0           mycore_free(stream_buffer_entry);
75 0           return NULL;
76             }
77            
78 3           return stream_buffer_entry;
79             }
80              
81 145           void myhtml_stream_buffer_clean(myhtml_stream_buffer_t* stream_buffer)
82             {
83 145 50         if(stream_buffer)
84 0           stream_buffer->length = 0;
85 145           }
86              
87 144           myhtml_stream_buffer_t * myhtml_stream_buffer_destroy(myhtml_stream_buffer_t* stream_buffer, bool self_destroy)
88             {
89 144 100         if(stream_buffer == NULL)
90 141           return NULL;
91            
92 3 50         if(stream_buffer->entries) {
93 6 100         for(size_t i = 0; i < stream_buffer->length; i++)
94 3           myhtml_stream_buffer_entry_destroy(&stream_buffer->entries[i], false);
95            
96 3           mycore_free(stream_buffer->entries);
97             }
98            
99 3 50         if(self_destroy) {
100 3           mycore_free(stream_buffer);
101 3           return NULL;
102             }
103            
104 0           return stream_buffer;
105             }
106              
107 3           myhtml_stream_buffer_entry_t * myhtml_stream_buffer_add_entry(myhtml_stream_buffer_t* stream_buffer, size_t entry_data_size)
108             {
109 3 50         if(stream_buffer->length >= stream_buffer->size) {
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 0         if(entries) {
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 3           myhtml_stream_buffer_entry_t *entry = &stream_buffer->entries[ stream_buffer->length ];
125 3           mystatus_t status = myhtml_stream_buffer_entry_init(entry, entry_data_size);
126            
127 3 50         if(status != MyHTML_STATUS_OK)
128 0           return NULL;
129            
130 3           stream_buffer->length++;
131            
132 3           return entry;
133             }
134              
135 3           myhtml_stream_buffer_entry_t * myhtml_stream_buffer_current_entry(myhtml_stream_buffer_t* stream_buffer)
136             {
137 3 50         if(stream_buffer->length == 0)
138 0           return NULL;
139            
140 3           return &stream_buffer->entries[ (stream_buffer->length - 1) ];
141             }
142              
143