GCC Code Coverage Report


Directory: src/gate/
File: src/gate/utilities.c
Date: 2026-10-19 17:06:30
Exec Total Coverage
Lines: 380 495 76.8%
Functions: 39 47 83.0%
Branches: 116 225 51.6%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/utilities.h"
30 #include "gate/comparers.h"
31 #include "gate/results.h"
32 #include "gate/structs.h"
33 #include "gate/properties.h"
34 #include "gate/times.h"
35 #include "gate/guids.h"
36 #include "gate/blobs.h"
37 #include "gate/synchronization.h"
38 #include "gate/platforms.h"
39
40 9 gate_map_t* gate_util_stringmap_create(gate_map_t* m)
41 {
42
43 9 return gate_map_create(m, &gate_compare_string,
44 sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
45 sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor
46 );
47 }
48 55 gate_map_t* gate_util_stringmap_create_ex(gate_map_t* m, gate_bool_t duplicate_ctor, gate_bool_t ignore_case)
49 {
50
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 gate_comparer_t comp = ignore_case ? &gate_compare_string_ic : &gate_compare_string;
51
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 gate_mem_copyctor_t cctor = duplicate_ctor ? &gate_string_duplicate_constructor : &gate_string_copy_constructor;
52 55 return gate_map_create(m, comp,
53 sizeof(gate_string_t), cctor, &gate_string_destructor,
54 sizeof(gate_string_t), cctor, &gate_string_destructor
55 );
56 }
57
58 3 gate_string_t const* gate_util_stringmap_get(gate_map_t const* m, char const* key)
59 {
60 gate_string_t k;
61 3 gate_string_create_static(&k, key);
62 3 return gate_util_stringmap_get_string(m, &k);
63 }
64 7 gate_string_t const* gate_util_stringmap_get_string(gate_map_t const* m, gate_string_t const* key)
65 {
66 7 gate_string_t const* ret = NULL;
67
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if ((m->key_size == sizeof(gate_string_t)) && (m->value_size == sizeof(gate_string_t)))
68 {
69 7 ret = (gate_string_t const*)gate_map_get_value(m, key);
70 }
71 7 return ret;
72 }
73 5 gate_map_iterator_t gate_util_stringmap_add(gate_map_t* m, char const* key, char const* value)
74 {
75 5 gate_map_iterator_t ret = NULL;
76 5 gate_string_t k = GATE_STRING_INIT_EMPTY;
77 5 gate_string_t v = GATE_STRING_INIT_EMPTY;
78
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (NULL != gate_string_create(&k, key, gate_str_length(key)))
79 {
80
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (NULL != gate_string_create(&v, value, gate_str_length(value)))
81 {
82 5 ret = gate_util_stringmap_add_string(m, &k, &v);
83 5 gate_string_release(&v);
84 }
85 5 gate_string_release(&k);
86 }
87 5 return ret;
88 }
89 34 gate_map_iterator_t gate_util_stringmap_add_string(gate_map_t* m, gate_string_t const* key, gate_string_t const* value)
90 {
91 34 gate_map_iterator_t ret = NULL;
92 34 gate_string_t key_clone = GATE_STRING_INIT_EMPTY;
93 34 gate_string_t value_clone = GATE_STRING_INIT_EMPTY;
94
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 if (gate_string_clone(&key_clone, key))
95 {
96
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 if (gate_string_clone(&value_clone, value))
97 {
98 34 ret = gate_map_add(m, &key_clone, &value_clone);
99 34 gate_string_release(&value_clone);
100 }
101 34 gate_string_release(&key_clone);
102 }
103 34 return ret;
104 }
105
106
107 /*************************
108 * stringset utilities *
109 *************************/
110
111 43 gate_set_t* gate_util_stringset_create(gate_set_t* s)
112 {
113 43 return gate_set_create(s, &gate_compare_string, sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor);
114 }
115 2 gate_bool_t gate_util_stringset_contains_string(gate_set_t const* s, gate_string_t const* key)
116 {
117 2 return gate_set_iterator_valid(gate_set_get(s, key));
118 }
119 1 gate_bool_t gate_util_stringset_contains(gate_set_t const* s, char const* key)
120 {
121 1 gate_bool_t ret = false;
122 gate_string_t str;
123
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (gate_string_create_static(&str, key))
124 {
125 1 ret = gate_util_stringset_contains_string(s, &str);
126 1 gate_string_release(&str);
127 }
128 1 return ret;
129 }
130 4 gate_bool_t gate_util_stringset_add_string(gate_set_t* s, gate_string_t const* key)
131 {
132 4 gate_bool_t ret = false;
133 gate_string_t key_clone;
134
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (gate_string_clone(&key_clone, key))
135 {
136 4 ret = gate_set_iterator_valid(gate_set_add(s, &key_clone));
137 4 gate_string_release(&key_clone);
138 }
139 4 return ret;
140 }
141 1 gate_bool_t gate_util_stringset_add(gate_set_t* s, char const* key)
142 {
143 1 gate_bool_t ret = false;
144 gate_string_t str;
145
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (gate_string_create(&str, key, gate_str_length(key)))
146 {
147 1 ret = gate_util_stringset_add_string(s, &str);
148 1 gate_string_release(&str);
149 }
150 1 return ret;
151 }
152 4 gate_bool_t gate_util_stringset_remove_string(gate_set_t* s, gate_string_t const* key)
153 {
154 4 return gate_set_remove(s, key);
155 }
156 1 gate_bool_t gate_util_stringset_remove(gate_set_t* s, char const* key)
157 {
158 1 gate_bool_t ret = false;
159 gate_string_t str;
160
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (gate_string_create_static(&str, key))
161 {
162 1 ret = gate_util_stringset_remove_string(s, &str);
163 1 gate_string_release(&str);
164 }
165 1 return ret;
166 }
167
168
169 /********************************
170 * string-value-map utilities *
171 ********************************/
172
173 ✗ gate_map_t* gate_util_stringvaluemap_create(gate_map_t* m)
174 {
175 ✗ return gate_util_stringvaluemap_create_ex(m, false, false);
176 }
177
178 ✗ gate_map_t* gate_util_stringvaluemap_create_ex(gate_map_t* m, gate_bool_t duplicate_ctor, gate_bool_t ignore_case)
179 {
180 ✗ return gate_map_create(
181 m,
182 ignore_case ? &gate_compare_string_ic : &gate_compare_string,
183 sizeof(gate_string_t),
184 duplicate_ctor ? &gate_string_duplicate_constructor : &gate_string_copy_constructor,
185 &gate_string_destructor,
186 sizeof(gate_value_t),
187 &gate_value_copy_constructor,
188 &gate_value_destructor
189 );
190 }
191
192 ✗ gate_value_t const* gate_util_stringvaluemap_get(gate_map_t const* m, char const* key)
193 {
194 gate_string_t str;
195 ✗ gate_string_create_static(&str, key);
196 ✗ return gate_util_stringvaluemap_get_string(m, &str);
197 }
198
199 ✗ gate_value_t const* gate_util_stringvaluemap_get_string(gate_map_t const* m, gate_string_t const* key)
200 {
201 ✗ return (gate_value_t const*)gate_map_get_value(m, key);
202 }
203
204 ✗ gate_map_iterator_t gate_util_stringvaluemap_add(gate_map_t* m, char const* key, gate_type_id_t value_type, void const* value_ptr)
205 {
206 gate_string_t str_key;
207 ✗ gate_string_create_static(&str_key, key);
208 ✗ return gate_util_stringvaluemap_add_string(m, &str_key, value_type, value_ptr);
209 }
210
211 ✗ gate_map_iterator_t gate_util_stringvaluemap_add_string(gate_map_t* m, gate_string_t const* key, gate_type_id_t value_type, void const* value_ptr)
212 {
213 gate_value_t v;
214 ✗ if (NULL == gate_value_create(value_type, value_ptr, &v))
215 {
216 ✗ return gate_map_end(m);
217 }
218 else
219 {
220 ✗ gate_map_iterator_t iter = gate_map_add(m, key, &v);
221 ✗ gate_value_release(&v);
222 ✗ return iter;
223 }
224 }
225
226 ✗ gate_bool_t gate_util_stringvaluemap_remove(gate_map_t* m, char const* key)
227 {
228 gate_string_t str_key;
229 ✗ gate_string_create_static(&str_key, key);
230 ✗ return gate_map_remove(m, &str_key);
231 }
232
233
234
235
236
237 /**************************
238 * array-list utilities *
239 **************************/
240
241 ✗ gate_arraylist_t gate_util_blobarray_create()
242 {
243 ✗ return gate_arraylist_create(sizeof(gate_blob_t), NULL, 0, &gate_blob_copy_constructor, &gate_blob_destructor);
244 }
245
246
247 20 gate_arraylist_t gate_util_stringarray_create()
248 {
249 20 return gate_arraylist_create(sizeof(gate_string_t), NULL, 0, &gate_string_copy_constructor, &gate_string_destructor);
250 }
251 1 gate_arraylist_t gate_util_stringarray_create_duplicate(gate_size_t prealloc)
252 {
253 1 return gate_arraylist_create(sizeof(gate_string_t), NULL, prealloc, &gate_string_duplicate_constructor, &gate_string_destructor);
254 }
255
256
257 16 gate_string_t const* gate_util_stringarray_add(gate_arraylist_t lst, gate_string_t const* new_entry)
258 {
259 16 return (gate_string_t const*)gate_arraylist_add(lst, new_entry);
260 }
261 1 gate_string_t const* gate_util_stringarray_add_str(gate_arraylist_t lst, char const* new_entry)
262 {
263 1 gate_string_t const* ret = NULL;
264 gate_string_t str;
265
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (NULL != gate_string_create(&str, new_entry, gate_str_length(new_entry)))
266 {
267 1 ret = gate_util_stringarray_add(lst, &str);
268 1 gate_string_release(&str);
269 }
270 1 return ret;
271 }
272
273
274 3 gate_bool_t gate_util_string_eval_bool(gate_string_t const* str)
275 {
276 static gate_string_t const false_token = GATE_STRING_INIT_STATIC("false");
277 static gate_string_t const zero_token = GATE_STRING_INIT_STATIC("0");
278
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (gate_string_is_empty(str))
279 {
280 1 return false;
281 }
282
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (gate_string_equals_ic(str, &false_token)
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 || gate_string_equals(str, &zero_token))
284 {
285 1 return false;
286 }
287 1 return true;
288 }
289
290 3 gate_arraylist_t gate_util_string_split(gate_string_t const* str, gate_string_t const* separator, gate_size_t max_count)
291 {
292 3 gate_string_t txt = GATE_STRING_INIT_EMPTY;
293 3 gate_string_t head = GATE_STRING_INIT_EMPTY;
294 3 gate_string_t tail = GATE_STRING_INIT_EMPTY;
295 3 gate_arraylist_t ret = gate_util_stringarray_create();
296 gate_size_t parsed;
297 3 gate_size_t items_count = 0;
298
299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (gate_arraylist_is_null(ret))
300 {
301 ✗ return ret;
302 }
303 3 gate_string_duplicate(&txt, str);
304
305
3/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 1 times.
10 while (!gate_string_is_empty(&txt) && (items_count < max_count))
306 {
307 9 parsed = gate_string_parse(&txt, separator, 0, &head, &tail, false);
308
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (!parsed)
309 {
310 2 gate_arraylist_add(ret, &txt);
311 2 break;
312 }
313 7 gate_arraylist_add(ret, &head);
314 7 gate_string_release(&head);
315 7 gate_string_release(&txt);
316 7 gate_mem_copy(&txt, &tail, sizeof(tail));
317 7 gate_mem_clear(&tail, sizeof(tail));
318 7 ++items_count;
319 }
320
321 3 gate_string_release(&txt);
322 3 return ret;
323 }
324
325
326
327
328 3 gate_arraylist_t gate_util_map_export_keys(gate_map_t* m)
329 {
330 3 gate_arraylist_t ret = GATE_INIT_EMPTY;
331
332 3 ret = gate_arraylist_create(m->key_size, NULL, 0, m->key_constructor, m->key_destructor);
333
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (!gate_arraylist_is_null(ret))
334 {
335 3 gate_map_iterator_t iter = gate_map_first(m);
336
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 3 times.
11 while (gate_map_iterator_valid(iter))
337 {
338 8 void const* key = gate_map_iterator_key(iter);
339
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (key)
340 {
341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (!gate_arraylist_add(ret, key))
342 {
343 /* allocation error */
344 ✗ gate_arraylist_release(&ret);
345 ✗ break;
346 }
347 }
348 8 iter = gate_map_iterator_next(iter);
349 }
350 }
351 3 return ret;
352 }
353
354 1 gate_arraylist_t gate_util_map_export_values(gate_map_t* m)
355 {
356 1 gate_arraylist_t ret = GATE_INIT_EMPTY;
357
358 1 ret = gate_arraylist_create(m->value_size, NULL, 0, m->value_constructor, m->value_destructor);
359
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_arraylist_is_null(ret))
360 {
361 1 gate_map_iterator_t iter = gate_map_first(m);
362
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 while (gate_map_iterator_valid(iter))
363 {
364 2 void const* value = gate_map_iterator_value(iter);
365
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (value)
366 {
367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!gate_arraylist_add(ret, value))
368 {
369 /* allocation error */
370 ✗ gate_arraylist_release(&ret);
371 ✗ break;
372 }
373 }
374 2 iter = gate_map_iterator_next(iter);
375 }
376 }
377 1 return ret;
378 }
379
380
381
382
383
384
385 3 gate_result_t gate_util_buffer_load(char const** ptrbuffer, gate_size_t* ptrbuffersize, gate_stream_t* srcstream, gate_memstream_t** membuffer)
386 {
387 3 gate_result_t ret = GATE_RESULT_OK;
388 3 gate_memstream_t* memstream = NULL;
389
390 do
391 {
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (srcstream == NULL)
393 {
394 ✗ *ptrbuffer = NULL;
395 ✗ *ptrbuffersize = 0;
396 }
397 else
398 {
399 3 char const* srcstreamtype = gate_object_get_interface_name(srcstream);
400
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (0 == gate_str_comp(GATE_INTERFACE_NAME_MEMSTREAM, srcstreamtype))
401 {
402 /* the source stream is already a memory stream, we can directly access its data */
403 1 memstream = (gate_memstream_t*)srcstream;
404 1 gate_object_retain(memstream);
405 1 *ptrbuffer = gate_memstream_get_data(memstream);
406 1 *ptrbuffersize = gate_memstream_size(memstream);
407 }
408
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 else if (0 == gate_str_comp(GATE_INTERFACE_NAME_STRINGSTREAM, srcstreamtype))
409 {
410 1 gate_stringstream_t* stringstrm = (gate_stringstream_t*)srcstream;
411 1 *ptrbuffer = gate_stringstream_get_data(stringstrm);
412 1 *ptrbuffersize = gate_stringstream_size(stringstrm);
413 }
414 else
415 {
416
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (*membuffer == NULL)
417 {
418 1 memstream = gate_memstream_create(GATE_MAX_COPYBUFFER_LENGTH);
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (memstream == NULL)
420 {
421 ✗ ret = GATE_RESULT_OUTOFMEMORY;
422 ✗ break;
423 }
424 1 *membuffer = memstream;
425 }
426 else
427 {
428 ✗ memstream = *membuffer;
429 }
430 1 gate_object_retain(memstream);
431 1 ret = gate_stream_transfer(srcstream, (gate_stream_t*)memstream);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
433 {
434 ✗ break;
435 }
436 1 *ptrbuffer = gate_memstream_get_data(memstream);
437 1 *ptrbuffersize = gate_memstream_size(memstream);
438 }
439 }
440
441 } while (0);
442
443
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (memstream != NULL)
444 {
445 2 gate_object_release(memstream);
446 }
447
448 3 return ret;
449 }
450
451
452 12 gate_size_t gate_util_print_byte_size(gate_uint64_t bytes, gate_char8_t* dest_buffer, gate_size_t dest_size)
453 {
454 gate_size_t ret;
455
456
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (bytes < 1024)
457 {
458 2 ret = gate_str_print_uint64(dest_buffer, dest_size, bytes);
459 2 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " Bytes", 6);
460 }
461
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 else if (bytes < 1048576)
462 {
463 4 ret = gate_str_print_real(dest_buffer, dest_size, (gate_real64_t)(gate_int64_t)bytes / 1024.0, 0, 1, 3);
464 4 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " KB", 3);
465 }
466
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 else if (bytes < 1073741824)
467 {
468 4 ret = gate_str_print_real(dest_buffer, dest_size, (gate_real64_t)(gate_int64_t)bytes / 1048576.0, 0, 2, 3);
469 4 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " MB", 3);
470 }
471 else
472 {
473 2 ret = gate_str_print_real(dest_buffer, dest_size, (gate_real64_t)(gate_int64_t)bytes / 1073741824.0, 0, 2, 3);
474 2 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " GB", 3);
475 }
476
477 12 return ret;
478 }
479
480 6 gate_size_t gate_util_print_duration(gate_uint64_t seconds, gate_char8_t* dest_buffer, gate_size_t dest_size, gate_bool_t shortFormat)
481 {
482 6 gate_size_t ret = 0;
483 6 gate_bool_t print_days = true;
484 6 gate_bool_t print_hours = true;
485 6 gate_bool_t print_minutes = true;
486
487 6 gate_uint64_t days = 0;
488 6 gate_uint64_t hours = 0;
489 6 gate_uint64_t minutes = 0;
490 gate_size_t lenused;
491
492 6 days = seconds / 86400;
493 6 seconds %= 86400;
494
495 6 hours = seconds / 3600;
496 6 seconds %= 3600;
497
498 6 minutes = seconds / 60;
499 6 seconds %= 60;
500 do
501 {
502
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (days == 0)
503 {
504 3 print_days = false;
505 }
506 else
507 {
508 3 break;
509 }
510
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (hours == 0)
511 {
512 2 print_hours = false;
513 }
514 else
515 {
516 1 break;
517 }
518
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (minutes == 0)
519 {
520 1 print_minutes = 0;
521 }
522 } while (0);
523
524
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (print_days)
525 {
526 3 lenused = gate_str_print_uint(dest_buffer, dest_size, days, 0);
527 3 dest_buffer += lenused;
528 3 dest_size -= lenused;
529 3 ret += lenused;
530
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (shortFormat)
531 {
532 2 lenused = gate_str_print_text(dest_buffer, dest_size, "d ", 2);
533 }
534 else
535 {
536 1 lenused = gate_str_print_text(dest_buffer, dest_size, " days ", 6);
537 }
538 3 dest_buffer += lenused;
539 3 dest_size -= lenused;
540 3 ret += lenused;
541 }
542
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (print_hours)
543 {
544 4 lenused = gate_str_print_uint(dest_buffer, dest_size, hours, 0);
545 4 dest_buffer += lenused;
546 4 dest_size -= lenused;
547 4 ret += lenused;
548
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (shortFormat)
549 {
550 3 lenused = gate_str_print_text(dest_buffer, dest_size, "h ", 2);
551 }
552 else
553 {
554 1 lenused = gate_str_print_text(dest_buffer, dest_size, " hours ", 7);
555 }
556 4 dest_buffer += lenused;
557 4 dest_size -= lenused;
558 4 ret += lenused;
559 }
560
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (print_minutes)
561 {
562 5 lenused = gate_str_print_uint(dest_buffer, dest_size, minutes, 0);
563 5 dest_buffer += lenused;
564 5 dest_size -= lenused;
565 5 ret += lenused;
566
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (shortFormat)
567 {
568 4 lenused = gate_str_print_text(dest_buffer, dest_size, "m ", 2);
569 }
570 else
571 {
572 1 lenused = gate_str_print_text(dest_buffer, dest_size, " minutes ", 9);
573 }
574 5 dest_buffer += lenused;
575 5 dest_size -= lenused;
576 5 ret += lenused;
577 }
578
579 {
580 6 lenused = gate_str_print_uint(dest_buffer, dest_size, seconds, 0);
581 6 dest_buffer += lenused;
582 6 dest_size -= lenused;
583 6 ret += lenused;
584
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (shortFormat)
585 {
586 5 lenused = gate_str_print_text(dest_buffer, dest_size, "s", 1);
587 }
588 else
589 {
590 1 lenused = gate_str_print_text(dest_buffer, dest_size, "seconds", 7);
591 }
592 /*
593 dest_buffer += lenused;
594 dest_size -= lenused;
595 */
596 6 ret += lenused;
597 }
598 6 return ret;
599 }
600
601 2 gate_string_t* gate_util_int_to_string(gate_string_t* output, gate_int64_t input)
602 {
603 char buffer[256];
604 2 gate_size_t used = gate_str_print_int64(buffer, sizeof(buffer), input);
605 2 return gate_string_create(output, buffer, used);
606 }
607 1 gate_string_t* gate_util_uint_to_string(gate_string_t* output, gate_uint64_t input)
608 {
609 char buffer[256];
610 1 gate_size_t used = gate_str_print_uint64(buffer, sizeof(buffer), input);
611 1 return gate_string_create(output, buffer, used);
612 }
613 1 gate_string_t* gate_util_real_to_string(gate_string_t* output, gate_real64_t input, unsigned int_len, unsigned decimal_len, unsigned group_len)
614 {
615 char buffer[256];
616 1 gate_size_t used = gate_str_print_real(buffer, sizeof(buffer), input, int_len, decimal_len, group_len);
617 1 return gate_string_create(output, buffer, used);
618 }
619 1 gate_int64_t gate_util_string_to_int(gate_string_t const* input)
620 {
621 1 gate_int64_t ret = 0;
622
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (input)
623 {
624 1 gate_str_parse_int64(input->str, input->length, &ret);
625 }
626 1 return ret;
627 }
628 1 gate_uint64_t gate_util_string_to_uint(gate_string_t const* input)
629 {
630 1 gate_uint64_t ret = 0;
631
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (input)
632 {
633 1 gate_str_parse_uint64(input->str, input->length, &ret);
634 }
635 1 return ret;
636 }
637 1 gate_real64_t gate_util_string_to_real(gate_string_t const* input)
638 {
639 1 gate_real64_t ret = 0.0;
640
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (input)
641 {
642 1 gate_str_parse_real(input->str, input->length, &ret);
643 }
644 1 return ret;
645 }
646
647
648 30 gate_result_t gate_util_dump_type(gate_stream_t* target, gate_type_id_t type, void const* data)
649 {
650 30 gate_result_t ret = GATE_RESULT_FAILED;
651 30 char buffer[1024] = GATE_INIT_EMPTY;
652 30 gate_size_t buffer_size = sizeof(buffer);
653 30 gate_datetime_t dt = GATE_INIT_EMPTY;
654
655
15/26
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 2 times.
✓ Branch 20 taken 2 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
30 switch (type)
656 {
657 2 case GATE_TYPE_BOOL:
658 2 case GATE_TYPE_I8: ret = gate_stream_print_int(target, *((gate_int8_t const*)data)); break;
659 2 case GATE_TYPE_I16: ret = gate_stream_print_int(target, *((gate_int16_t const*)data)); break;
660 2 case GATE_TYPE_I32: ret = gate_stream_print_int(target, *((gate_int32_t const*)data)); break;
661 2 case GATE_TYPE_I64: ret = gate_stream_print_int64(target, *((gate_int64_t const*)data)); break;
662 ✗ case GATE_TYPE_UI8: ret = gate_stream_print_uint(target, *((gate_uint8_t const*)data)); break;
663 2 case GATE_TYPE_UI16: ret = gate_stream_print_uint(target, *((gate_uint16_t const*)data)); break;
664 2 case GATE_TYPE_UI32: ret = gate_stream_print_uint(target, *((gate_uint32_t const*)data)); break;
665 2 case GATE_TYPE_UI64: ret = gate_stream_print_uint64(target, *((gate_uint64_t const*)data)); break;
666 2 case GATE_TYPE_R32: ret = gate_stream_print_real(target, *((gate_real32_t const*)data), 0, 0, 0); break;
667 2 case GATE_TYPE_R64: ret = gate_stream_print_real(target, *((gate_real64_t const*)data), 0, 0, 0); break;
668 ✗ case GATE_TYPE_ADDRESS: ret = gate_stream_print_uint64(target, *((gate_uintptr_t const*)data)); break;
669
670 ✗ case GATE_TYPE_DATAPTR: ret = gate_stream_print_uint64(target, (gate_uintptr_t)(*((gate_dataptr_t const*)data))); break;
671 ✗ case GATE_TYPE_FUNCPTR: ret = gate_stream_print_uint64(target, (gate_uintptr_t)(*((gate_funcptr_t const*)data))); break;
672 ✗ case GATE_TYPE_CSTR: ret = gate_stream_print_cstr(target, (gate_cstr_t)data); break;
673 ✗ case GATE_TYPE_WSTR: ret = gate_stream_print_wstr(target, (gate_wstr_t)data, gate_wstr_length((gate_wstr_t)data)); break;
674 2 case GATE_TYPE_GUID:
675 2 ret = gate_guid_to_string((gate_guid_t const*)data, buffer);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
677 2 ret = gate_stream_print_cstr(target, buffer);
678 2 break;
679 2 case GATE_TYPE_DATE:
680 2 gate_mem_copy(&dt.date, data, sizeof(gate_date_t));
681 2 ret = gate_date_to_string((gate_datetime_t const*)data, 0, "{YYYY}-{MM}-{DD}", buffer, &buffer_size);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
683 2 ret = gate_stream_print_cstr(target, buffer);
684 2 break;
685 2 case GATE_TYPE_DAYTIME:
686 2 gate_mem_copy(&dt.time, data, sizeof(gate_daytime_t));
687 2 ret = gate_date_to_string((gate_datetime_t const*)data, 0, "{hh}:{mm}:{ss}.{SSS}", buffer, &buffer_size);
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
689 2 ret = gate_stream_print_cstr(target, buffer);
690 2 break;
691 2 case GATE_TYPE_DATETIME:
692 2 ret = gate_date_to_string((gate_datetime_t const*)data, 0, NULL, buffer, &buffer_size);
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
694 2 ret = gate_stream_print_cstr(target, buffer);
695 2 break;
696 2 case GATE_TYPE_TIME:
697 2 ret = gate_time_to_string((gate_time_t const*)data, NULL, buffer, &buffer_size);
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
699 2 ret = gate_stream_print_cstr(target, buffer);
700 2 break;
701 2 case GATE_TYPE_STRING:
702 2 ret = gate_stream_print_string(target, (gate_string_t const*)data);
703 2 break;
704 ✗ case GATE_TYPE_ARRAY:
705 ✗ ret = gate_stream_print_cstr(target, "{gate_array_t}");
706 ✗ break;
707
708 ✗ case GATE_TYPE_STRUCT:
709 ✗ ret = gate_stream_print_cstr(target, "{gate_struct_t}");
710 ✗ break;
711 ✗ case GATE_TYPE_OBJECT:
712 ✗ ret = gate_stream_print_cstr(target, "{gate_object_t}");
713 ✗ break;
714 ✗ case GATE_TYPE_PROPERTY:
715 ✗ ret = gate_stream_print_cstr(target, "{gate_property_t}");
716 ✗ break;
717 ✗ default:
718 {
719 ✗ if ((type & GATE_TYPE_ARRAYLIST) == GATE_TYPE_ARRAYLIST)
720 {
721 ✗ ret = gate_stream_print_cstr(target, "{gate_arraylist_t}");
722 }
723 else
724 {
725 ✗ ret = GATE_RESULT_NOTSUPPORTED;
726 }
727 ✗ break;
728 }
729 }
730 30 return ret;
731 }
732
733 15 static gate_result_t gate_util_print_indent(gate_stream_t* target, gate_size_t indent)
734 {
735 char buffer[4096];
736 15 gate_strbuilder_t builder = GATE_INIT_EMPTY;
737 gate_size_t lenused;
738 15 gate_size_t written = 0;
739
740 15 gate_strbuilder_create_static(&builder, buffer, sizeof(buffer), 0);
741 15 lenused = gate_strbuilder_append_chars(&builder, indent, '\t');
742 15 return gate_stream_write_block(target, buffer, lenused, &written);
743 }
744
745 1 static gate_result_t gate_util_struct_dump_to(gate_stream_t* target, void const* ptr_input_struct, gate_size_t indent)
746 {
747 1 gate_result_t ret = GATE_RESULT_OK;
748 1 gate_size_t const count = gate_struct_get_member_count(ptr_input_struct);
749 gate_size_t index;
750 gate_size_t arr_length;
751 gate_size_t arr_index;
752 gate_type_id_t arr_type;
753 void const* arr_item;
754
755
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
16 for (index = 0; index != count; ++index)
756 {
757 15 char const* name = gate_struct_get_member_name(ptr_input_struct, index);
758 15 gate_type_id_t type = gate_struct_get_member_type(ptr_input_struct, index);
759 15 void const* value = gate_struct_get_member(ptr_input_struct, index);
760
761 15 gate_util_print_indent(target, indent);
762 15 gate_stream_print_cstr(target, name);
763 15 gate_stream_print_cstr(target, " = ");
764
765
1/7
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
15 switch (type)
766 {
767 15 case GATE_TYPE_UI8:
768 case GATE_TYPE_UI16:
769 case GATE_TYPE_UI32:
770 case GATE_TYPE_UI64:
771 case GATE_TYPE_I8:
772 case GATE_TYPE_I16:
773 case GATE_TYPE_I32:
774 case GATE_TYPE_I64:
775 case GATE_TYPE_R32:
776 case GATE_TYPE_R64:
777 case GATE_TYPE_ADDRESS:
778 case GATE_TYPE_DATAPTR:
779 case GATE_TYPE_FUNCPTR:
780 case GATE_TYPE_CSTR:
781 case GATE_TYPE_WSTR:
782 case GATE_TYPE_GUID:
783 case GATE_TYPE_DATE:
784 case GATE_TYPE_DAYTIME:
785 case GATE_TYPE_DATETIME:
786 case GATE_TYPE_TIME:
787 case GATE_TYPE_STRING:
788 {
789 15 ret = gate_util_dump_type(target, type, value);
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 GATE_BREAK_IF_FAILED(ret);
791 15 ret = gate_stream_println_cstr(target, NULL);
792 15 break;
793 }
794 ✗ case GATE_TYPE_ARRAY:
795 {
796 ✗ ret = gate_stream_println_cstr(target, "{gate_array_t}");
797 ✗ break;
798 }
799 ✗ case GATE_TYPE_STRUCT:
800 {
801 ✗ ret = gate_stream_println_cstr(target, "{gate_struct_t}");
802 ✗ GATE_BREAK_IF_FAILED(ret);
803 ✗ ret = gate_util_struct_dump_to(target, value, indent + 1);
804 ✗ break;
805 }
806 ✗ case GATE_TYPE_OBJECT:
807 {
808 ✗ ret = gate_stream_println_cstr(target, "{gate_object_t}");
809 ✗ break;
810 }
811 ✗ case GATE_TYPE_PROPERTY:
812 {
813 ✗ ret = gate_stream_println_cstr(target, "{gate_property_t}");
814 ✗ break;
815 }
816 ✗ case GATE_TYPE_ARRAYLIST_STRUCT:
817 {
818 ✗ arr_length = gate_arraylist_length(*(gate_arraylist_t const*)value);
819 ✗ ret = gate_stream_println_cstr(target, "{gate_arraylist_t[gate_struct_t]}");
820 ✗ GATE_BREAK_IF_FAILED(ret);
821 ✗ for (arr_index = 0; arr_index != arr_length; ++arr_index)
822 {
823 ✗ arr_item = gate_arraylist_get(*(gate_arraylist_t const*)value, arr_index);
824 ✗ if (arr_item)
825 {
826 ✗ ret = gate_util_struct_dump_to(target, arr_item, indent + 1);
827 ✗ GATE_BREAK_IF_FAILED(ret);
828 }
829 }
830 ✗ break;
831 }
832 ✗ default:
833 {
834 ✗ if ((type & GATE_TYPE_ARRAYLIST) == GATE_TYPE_ARRAYLIST)
835 {
836 ✗ arr_type = (type & (~GATE_TYPE_ARRAYLIST));
837 ✗ arr_length = gate_arraylist_length(*(gate_arraylist_t const*)value);
838 ✗ ret = gate_stream_print_cstr(target, "{gate_arraylist_t[");
839 ✗ GATE_BREAK_IF_FAILED(ret);
840 ✗ ret = gate_stream_print_uint(target, arr_type);
841 ✗ GATE_BREAK_IF_FAILED(ret);
842 ✗ ret = gate_stream_println_cstr(target, "]}");
843 ✗ for (arr_index = 0; arr_index != arr_length; ++arr_index)
844 {
845 ✗ arr_item = gate_arraylist_get(*(gate_arraylist_t const*)value, arr_index);
846 ✗ if (arr_item)
847 {
848 ✗ ret = gate_util_print_indent(target, indent + 1);
849 ✗ GATE_BREAK_IF_FAILED(ret);
850 ✗ ret = gate_util_dump_type(target, arr_type, arr_item);
851 ✗ GATE_BREAK_IF_FAILED(ret);
852 ✗ ret = gate_stream_println_cstr(target, NULL);
853 ✗ GATE_BREAK_IF_FAILED(ret);
854 }
855 }
856 }
857 else
858 {
859 ✗ ret = gate_stream_print_cstr(target, "{unsupported_type_");
860 ✗ GATE_BREAK_IF_FAILED(ret);
861 ✗ ret = gate_stream_print_uint(target, type);
862 ✗ GATE_BREAK_IF_FAILED(ret);
863 ✗ ret = gate_stream_println_cstr(target, "}");
864 }
865 ✗ break;
866 }
867 } /* switch */
868 } /* for */
869 1 return ret;
870 }
871
872 1 gate_result_t gate_util_dump_struct(gate_stream_t* target, void const* ptr_input_struct)
873 {
874 1 return gate_util_struct_dump_to(target, ptr_input_struct, 0);
875 }
876
877
878 static gate_atomic_int_t global_state_init = 0;
879 static gate_mutex_t global_state_mutex;
880 static gate_synccondition_t global_state_signal;
881
882 2 static void close_state_objects_atexit(void)
883 {
884 2 gate_synccondition_destroy(&global_state_signal);
885 2 gate_mutex_destroy(&global_state_mutex);
886 2 gate_atomic_int_set(&global_state_init, 0);
887 2 }
888
889 10 static gate_result_t gate_util_state_init()
890 {
891 gate_result_t result;
892 gate_int32_t v;
893
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
10 if (2 == gate_atomic_int_get(&global_state_init))
894 {
895 8 return GATE_RESULT_OK;
896 }
897
898
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 while (2 != (v = gate_atomic_int_xchg_if(&global_state_init, 0, 1)))
899 {
900
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (v == 0)
901 {
902 2 result = gate_mutex_create(&global_state_mutex);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(result))
904 {
905 ✗ gate_atomic_int_set(&global_state_init, 0);
906 ✗ return GATE_RESULT_OUTOFRESOURCES;
907 }
908 2 result = gate_synccondition_create(&global_state_signal);
909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(result))
910 {
911 ✗ gate_mutex_destroy(&global_state_mutex);
912 ✗ gate_atomic_int_set(&global_state_init, 0);
913 ✗ return GATE_RESULT_OUTOFRESOURCES;
914 }
915 /* init succeeded */
916 2 gate_atomic_int_set(&global_state_init, 2);
917 2 gate_platform_atexit(&close_state_objects_atexit);
918 2 break;
919 }
920 else
921 {
922 /* spin until succeeded or fail-reset */
923 }
924 }
925 2 return GATE_RESULT_OK;
926 }
927
928 1 gate_result_t gate_util_state_set(gate_atomic_int_t* ptr_state, gate_int32_t value)
929 {
930 1 gate_result_t ret = gate_util_state_init();
931 do
932 {
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
934 1 ret = gate_mutex_acquire(&global_state_mutex);
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
936 1 gate_atomic_int_set(ptr_state, value);
937 1 ret = gate_synccondition_signal_all(&global_state_signal);
938 1 gate_mutex_release(&global_state_mutex);
939
940 } while (0);
941 1 return ret;
942 }
943 1 gate_result_t gate_util_state_update(gate_atomic_int_t* ptr_state, gate_int32_t from_value, gate_int32_t to_value)
944 {
945 gate_int32_t prev_value;
946 1 gate_result_t ret = gate_util_state_init();
947 do
948 {
949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
950 1 ret = gate_mutex_acquire(&global_state_mutex);
951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
952 1 prev_value = gate_atomic_int_xchg_if(ptr_state, from_value, to_value);
953 1 ret = gate_synccondition_signal_all(&global_state_signal);
954
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
955 {
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (prev_value != from_value)
957 {
958 ✗ ret = GATE_RESULT_INVALIDSTATE;
959 }
960 }
961 1 gate_mutex_release(&global_state_mutex);
962
963 } while (0);
964 1 return ret;
965 }
966 8 gate_result_t gate_util_state_await(gate_atomic_int_t* ptr_state, gate_int32_t await_value, gate_uint32_t timeout_ms)
967 {
968 gate_timecounter_t now;
969 gate_timecounter_t timeout_time;
970 gate_int64_t diff;
971 8 gate_result_t ret = gate_util_state_init();
972 do
973 {
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
975 8 ret = gate_timecounter_now(&now);
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
977 8 timeout_time = gate_timecounter_add(now, ((gate_int64_t)timeout_ms) * 1000);
978
979 8 ret = gate_mutex_acquire(&global_state_mutex);
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
981
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
8 while (gate_atomic_int_get(ptr_state) != await_value)
982 {
983 2 ret = gate_timecounter_now(&now);
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
985
986 2 diff = gate_timecounter_diff(timeout_time, now);
987
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (diff <= 0)
988 {
989 2 ret = GATE_RESULT_TIMEOUT;
990 2 break;
991 }
992
993 ✗ ret = gate_synccondition_timed_wait(&global_state_signal, &global_state_mutex, timeout_ms);
994 ✗ if (ret == GATE_RESULT_TIMEOUT)
995 {
996 ✗ ret = GATE_RESULT_OK;
997 }
998 ✗ GATE_BREAK_IF_FAILED(ret);
999 }
1000 8 gate_mutex_release(&global_state_mutex);
1001 } while (0);
1002 8 return ret;
1003 }
1004