File Coverage

third_party/modest/source/myurl/path.c
Criterion Covered Total %
statement 0 139 0.0
branch 0 84 0.0
condition n/a
subroutine n/a
pod n/a
total 0 223 0.0


line stmt bran cond sub pod time code
1             /*
2             Copyright (C) 2016-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 "myurl/path.h"
22             #include "myurl/url.h"
23              
24 0           myurl_path_t * myurl_path_create(myurl_t* url)
25             {
26 0           myurl_path_t *path = url->callback_malloc(sizeof(myurl_path_t), url->callback_ctx);
27            
28 0 0         if(path)
29 0           memset(path, 0, sizeof(myurl_path_t));
30            
31 0           return path;
32             }
33              
34 0           mystatus_t myurl_path_init(myurl_t* url, myurl_path_t* path, size_t begin_size)
35             {
36 0 0         if(begin_size == 0)
37 0           return MyURL_STATUS_ERROR;
38            
39 0           path->length = 0;
40 0           path->size = begin_size;
41 0           path->list = url->callback_malloc(sizeof(myurl_path_entry_t) * path->size, url->callback_ctx);
42            
43 0 0         if(path->list == NULL)
44 0           return MyURL_STATUS_ERROR_MEMORY_ALLOCATION;
45            
46 0           memset(path->list, 0, sizeof(myurl_path_entry_t) * path->size);
47            
48 0           return MyURL_STATUS_OK;
49             }
50              
51 0           void myurl_path_clean(myurl_t* url, myurl_path_t* path)
52             {
53 0 0         for(size_t i = 0; i < path->length; i++) {
54 0 0         if(path->list[i].data) {
55 0           url->callback_free(path->list[i].data, url->callback_ctx);
56             }
57             }
58            
59 0           path->length = 0;
60 0           }
61              
62 0           myurl_path_entry_t * myurl_path_entry_destroy(myurl_t* url, myurl_path_entry_t* path, bool destroy_self)
63             {
64 0 0         if(path == NULL)
65 0           return NULL;
66            
67 0 0         if(path->data)
68 0           url->callback_free(path->data, url->callback_ctx);
69            
70 0 0         if(destroy_self) {
71 0           url->callback_free(path, url->callback_ctx);
72 0           return NULL;
73             }
74            
75 0           return path;
76             }
77              
78 0           myurl_path_t * myurl_path_destroy(myurl_t* url, myurl_path_t* path, bool destroy_self)
79             {
80 0 0         if(path == NULL)
81 0           return NULL;
82            
83 0 0         if(path->list) {
84 0           myurl_path_clean(url, path);
85 0           url->callback_free(path->list, url->callback_ctx);
86             }
87            
88 0 0         if(destroy_self && path) {
    0          
89 0           return url->callback_free(path, url->callback_ctx);
90             }
91            
92 0           return path;
93             }
94              
95 0           myurl_path_entry_t * myurl_path_append(myurl_t* url, myurl_path_t* path, const char* data, size_t length)
96             {
97 0 0         if(path->length >= path->size) {
98 0           size_t new_size = path->length + 1024;
99            
100 0           myurl_path_entry_t *tmp = url->callback_realloc(path->list, sizeof(myurl_path_entry_t) * new_size, url->callback_ctx);
101            
102 0 0         if(tmp) {
103 0           memset(&tmp[path->length], 0, sizeof(myurl_path_entry_t) * (new_size - path->length));
104            
105 0           path->list = tmp;
106 0           path->size = new_size;
107             }
108             else
109 0           return NULL;
110             }
111            
112 0           myurl_path_entry_t *entry = &path->list[ path->length ];
113 0           path->length++;
114            
115 0 0         if(length) {
116 0           entry->data = url->callback_malloc(sizeof(char) * length, url->callback_ctx);
117            
118 0 0         if(entry->data == NULL)
119 0           return NULL;
120            
121 0           memcpy(entry->data, data, sizeof(char) * length);
122             }
123             else
124 0           entry->data = NULL;
125            
126 0           entry->length = length;
127 0           return entry;
128             }
129              
130 0           myurl_path_entry_t * myurl_path_push(myurl_t* url, myurl_path_t* path, char* data, size_t length)
131             {
132 0 0         if(path->length >= path->size) {
133 0           size_t new_size = path->length + 32;
134            
135 0           myurl_path_entry_t *tmp = url->callback_realloc(path->list, sizeof(myurl_path_entry_t) * new_size, url->callback_ctx);
136            
137 0 0         if(tmp) {
138 0           memset(&tmp[path->length], 0, sizeof(myurl_path_entry_t) * (new_size - path->length));
139            
140 0           path->list = tmp;
141 0           path->size = new_size;
142             }
143             else
144 0           return NULL;
145             }
146            
147 0           myurl_path_entry_t *entry = &path->list[ path->length ];
148 0           path->length++;
149              
150 0           entry->data = data;
151 0           entry->length = length;
152            
153 0           return entry;
154             }
155              
156 0           myurl_path_entry_t * myurl_path_push_to_index(myurl_t* url, myurl_path_t* path, size_t index, char* data, size_t length)
157             {
158 0 0         if(index >= path->size) {
159 0           myurl_path_entry_t *tmp = url->callback_realloc(path->list, sizeof(myurl_path_entry_t) * index, url->callback_ctx);
160            
161 0 0         if(tmp) {
162 0           memset(&tmp[path->length], 0, sizeof(myurl_path_entry_t) * (index - path->length));
163            
164 0           path->list = tmp;
165 0           path->size = index;
166             }
167             else
168 0           return NULL;
169             }
170            
171 0 0         if(index > path->length)
172 0           path->length = index;
173            
174 0 0         if(path->list[ index ].data)
175 0           url->callback_free(path->list[ index ].data, url->callback_ctx);
176            
177 0           path->list[ index ].data = data;
178 0           path->list[ index ].length = length;
179            
180 0           return &path->list[ index ];
181             }
182              
183 0           myurl_path_entry_t * myurl_path_current(myurl_path_t* path)
184             {
185 0 0         if(path->length == 0)
186 0           return NULL;
187            
188 0           return &path->list[ (path->length - 1) ];
189             }
190              
191 0           myurl_path_entry_t * myurl_path_pop(myurl_path_t* path)
192             {
193 0 0         if(path->length == 0)
194 0           return NULL;
195            
196 0           path->length--;
197            
198 0           return &path->list[path->length];
199             }
200              
201 0           void myurl_path_remove_by_index(myurl_t* url, myurl_path_t* path, size_t index)
202             {
203 0 0         if(path->length == 0 || index >= path->length || path->list == NULL)
    0          
    0          
204 0           return;
205            
206 0           myurl_path_entry_destroy(url, &path->list[index], false);
207            
208 0 0         if((path->length - 1) > index) {
209 0           memmove(&path->list[index], &path->list[index + 1], sizeof(myurl_path_entry_t) * (path->length - (path->length - index)));
210             }
211            
212 0           path->length--;
213             }
214              
215 0           mystatus_t myurl_path_copy(myurl_t* url, myurl_path_t* path_from, myurl_path_t* path_to)
216             {
217 0 0         if(path_from->length >= path_to->size) {
218 0           size_t new_size = path_from->length + 2;
219            
220 0           myurl_path_entry_t* tmp = url->callback_realloc(path_to->list, sizeof(myurl_path_entry_t) * new_size, url->callback_ctx);
221            
222 0 0         if(tmp) {
223 0           memset(&tmp[path_to->length], 0, sizeof(myurl_path_entry_t) * (new_size - path_to->length));
224            
225 0           path_to->list = tmp;
226 0           path_to->size = new_size;
227             }
228             else
229 0           return MyURL_STATUS_ERROR_MEMORY_ALLOCATION;
230             }
231            
232 0           myurl_path_entry_t *list_to = path_to->list;
233 0           myurl_path_entry_t *list_from = path_from->list;
234            
235 0 0         if(path_to->length > path_from->length)
236             {
237 0 0         while(path_to->length > path_from->length) {
238 0           path_to->length--;
239            
240 0 0         if(list_to[path_to->length].data)
241 0           url->callback_free( list_to[path_to->length].data, url->callback_ctx );
242             }
243             }
244            
245 0           path_to->length = path_from->length;
246            
247 0 0         for(size_t i = 0; i < path_from->length; i++)
248             {
249 0 0         if(list_to[i].data)
250             {
251 0 0         if(list_to[i].length < list_from[i].length)
252 0           list_to[i].data = url->callback_realloc(list_to[i].data, (list_from[i].length + 1), url->callback_ctx);
253             }
254             else {
255 0           list_to[i].data = url->callback_malloc((list_from[i].length + 1), url->callback_ctx);
256             }
257            
258 0 0         if(list_to[i].data == NULL)
259 0           return MyURL_STATUS_ERROR_MEMORY_ALLOCATION;
260            
261 0           list_to[i].length = list_from[i].length;
262 0           list_to[i].data[ list_to[i].length ] = '\0';
263            
264 0 0         if(list_from[i].length) {
265 0           memcpy(list_to[i].data, list_from[i].data, sizeof(char) * list_from[i].length);
266             }
267             }
268            
269 0           return MyURL_STATUS_OK;
270             }
271              
272 0           void myurl_path_shorten(myurl_path_t* path, myurl_scheme_id_t scheme_id)
273             {
274 0 0         if(path->length == 0)
275 0           return;
276            
277 0 0         if(scheme_id == MyURL_SCHEME_ID_FILE) {
278 0 0         if(path->length == 1 && myurl_utils_is_windows_drive_letter(path->list[0].data, 0, path->list[0].length))
    0          
279 0           return;
280             }
281            
282 0           myurl_path_pop(path);
283             }
284              
285              
286