GCC Code Coverage Report


Directory: src/gate/
File: src/gate/utilities.c
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 380 497 76.5%
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 4 gate_map_t* gate_util_stringmap_create(gate_map_t* m)
41 {
42
43 4 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 20 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 20 times.
✗ Branch 1 not taken.
20 gate_comparer_t comp = ignore_case ? &gate_compare_string_ic : &gate_compare_string;
51
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 gate_mem_copyctor_t cctor = duplicate_ctor ? &gate_string_duplicate_constructor : &gate_string_copy_constructor;
52 20 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 1 gate_string_t const* gate_util_stringmap_get(gate_map_t const* m, char const* key)
59 {
60 gate_string_t k;
61 1 gate_string_create_static(&k, key);
62 1 return gate_util_stringmap_get_string(m, &k);
63 }
64 3 gate_string_t const* gate_util_stringmap_get_string(gate_map_t const* m, gate_string_t const* key)
65 {
66 3 gate_string_t const* ret = NULL;
67
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if ((m->key_size == sizeof(gate_string_t)) && (m->value_size == sizeof(gate_string_t)))
68 {
69 3 ret = (gate_string_t const*)gate_map_get_value(m, key);
70 }
71 3 return ret;
72 }
73 2 gate_map_iterator_t gate_util_stringmap_add(gate_map_t* m, char const* key, char const* value)
74 {
75 2 gate_map_iterator_t ret = NULL;
76 2 gate_string_t k = GATE_STRING_INIT_EMPTY;
77 2 gate_string_t v = GATE_STRING_INIT_EMPTY;
78
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (NULL != gate_string_create(&k, key, gate_str_length(key)))
79 {
80
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (NULL != gate_string_create(&v, value, gate_str_length(value)))
81 {
82 2 ret = gate_util_stringmap_add_string(m, &k, &v);
83 2 gate_string_release(&v);
84 }
85 2 gate_string_release(&k);
86 }
87 2 return ret;
88 }
89 13 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 13 gate_map_iterator_t ret = NULL;
92 13 gate_string_t key_clone = GATE_STRING_INIT_EMPTY;
93 13 gate_string_t value_clone = GATE_STRING_INIT_EMPTY;
94
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 if (gate_string_clone(&key_clone, key))
95 {
96
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 if (gate_string_clone(&value_clone, value))
97 {
98 13 ret = gate_map_add(m, &key_clone, &value_clone);
99 13 gate_string_release(&value_clone);
100 }
101 13 gate_string_release(&key_clone);
102 }
103 13 return ret;
104 }
105
106
107 /*************************
108 * stringset utilities *
109 *************************/
110
111 17 gate_set_t* gate_util_stringset_create(gate_set_t* s)
112 {
113 17 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 2 gate_bool_t gate_util_stringset_add_string(gate_set_t* s, gate_string_t const* key)
131 {
132 2 gate_bool_t ret = false;
133 gate_string_t key_clone;
134
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (gate_string_clone(&key_clone, key))
135 {
136 2 ret = gate_set_iterator_valid(gate_set_add(s, &key_clone));
137 2 gate_string_release(&key_clone);
138 }
139 2 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 2 gate_bool_t gate_util_stringset_remove_string(gate_set_t* s, gate_string_t const* key)
153 {
154 2 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 10 gate_arraylist_t gate_util_stringarray_create()
248 {
249 10 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 3 gate_string_t const* gate_util_stringarray_add(gate_arraylist_t lst, gate_string_t const* new_entry)
258 {
259 3 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 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!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 = NULL;
331
332 3 ret = gate_arraylist_create(m->key_size, NULL, 0, m->key_constructor, m->key_destructor);
333
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (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 ret = NULL;
346 break;
347 }
348 }
349 8 iter = gate_map_iterator_next(iter);
350 }
351 }
352 3 return ret;
353 }
354
355 1 gate_arraylist_t gate_util_map_export_values(gate_map_t* m)
356 {
357 1 gate_arraylist_t ret = NULL;
358
359 1 ret = gate_arraylist_create(m->value_size, NULL, 0, m->value_constructor, m->value_destructor);
360
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret)
361 {
362 1 gate_map_iterator_t iter = gate_map_first(m);
363
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 while (gate_map_iterator_valid(iter))
364 {
365 2 void const* value = gate_map_iterator_value(iter);
366
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (value)
367 {
368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!gate_arraylist_add(ret, value))
369 {
370 /* allocation error */
371 gate_arraylist_release(ret);
372 ret = NULL;
373 break;
374 }
375 }
376 2 iter = gate_map_iterator_next(iter);
377 }
378 }
379 1 return ret;
380 }
381
382
383
384
385
386
387 3 gate_result_t gate_util_buffer_load(char const** ptrbuffer, gate_size_t* ptrbuffersize, gate_stream_t* srcstream, gate_memstream_t** membuffer)
388 {
389 3 gate_result_t ret = GATE_RESULT_OK;
390 3 gate_memstream_t* memstream = NULL;
391
392 do
393 {
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (srcstream == NULL)
395 {
396 *ptrbuffer = NULL;
397 *ptrbuffersize = 0;
398 }
399 else
400 {
401 3 char const* srcstreamtype = gate_object_get_interface_name(srcstream);
402
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (0 == gate_str_comp(GATE_INTERFACE_NAME_MEMSTREAM, srcstreamtype))
403 {
404 /* the source stream is already a memory stream, we can directly access its data */
405 1 memstream = (gate_memstream_t*)srcstream;
406 1 gate_object_retain(memstream);
407 1 *ptrbuffer = gate_memstream_get_data(memstream);
408 1 *ptrbuffersize = gate_memstream_size(memstream);
409 }
410
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 else if (0 == gate_str_comp(GATE_INTERFACE_NAME_STRINGSTREAM, srcstreamtype))
411 {
412 1 gate_stringstream_t* stringstrm = (gate_stringstream_t*)srcstream;
413 1 *ptrbuffer = gate_stringstream_get_data(stringstrm);
414 1 *ptrbuffersize = gate_stringstream_size(stringstrm);
415 }
416 else
417 {
418
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (*membuffer == NULL)
419 {
420 1 memstream = gate_memstream_create(GATE_MAX_COPYBUFFER_LENGTH);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (memstream == NULL)
422 {
423 ret = GATE_RESULT_OUTOFMEMORY;
424 break;
425 }
426 1 *membuffer = memstream;
427 }
428 else
429 {
430 memstream = *membuffer;
431 }
432 1 gate_object_retain(memstream);
433 1 ret = gate_stream_transfer(srcstream, (gate_stream_t*)memstream);
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
435 {
436 break;
437 }
438 1 *ptrbuffer = gate_memstream_get_data(memstream);
439 1 *ptrbuffersize = gate_memstream_size(memstream);
440 }
441 }
442
443 } while (0);
444
445
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (memstream != NULL)
446 {
447 2 gate_object_release(memstream);
448 }
449
450 3 return ret;
451 }
452
453
454 12 gate_size_t gate_util_print_byte_size(gate_uint64_t bytes, gate_char8_t* dest_buffer, gate_size_t dest_size)
455 {
456 gate_size_t ret;
457
458
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (bytes < 1024)
459 {
460 2 ret = gate_str_print_uint64(dest_buffer, dest_size, bytes);
461 2 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " Bytes", 6);
462 }
463
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 else if (bytes < 1048576)
464 {
465 4 ret = gate_str_print_real(dest_buffer, dest_size, (gate_real64_t)(gate_int64_t)bytes / 1024.0, 0, 1, 3);
466 4 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " KB", 3);
467 }
468
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 else if (bytes < 1073741824)
469 {
470 4 ret = gate_str_print_real(dest_buffer, dest_size, (gate_real64_t)(gate_int64_t)bytes / 1048576.0, 0, 2, 3);
471 4 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " MB", 3);
472 }
473 else
474 {
475 2 ret = gate_str_print_real(dest_buffer, dest_size, (gate_real64_t)(gate_int64_t)bytes / 1073741824.0, 0, 2, 3);
476 2 ret += gate_str_print_text(&dest_buffer[ret], dest_size - ret, " GB", 3);
477 }
478
479 12 return ret;
480 }
481
482 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)
483 {
484 6 gate_size_t ret = 0;
485 6 gate_bool_t print_days = true;
486 6 gate_bool_t print_hours = true;
487 6 gate_bool_t print_minutes = true;
488
489 6 gate_uint64_t days = 0;
490 6 gate_uint64_t hours = 0;
491 6 gate_uint64_t minutes = 0;
492 gate_size_t lenused;
493
494 6 days = seconds / 86400;
495 6 seconds %= 86400;
496
497 6 hours = seconds / 3600;
498 6 seconds %= 3600;
499
500 6 minutes = seconds / 60;
501 6 seconds %= 60;
502 do
503 {
504
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (days == 0)
505 {
506 3 print_days = false;
507 }
508 else
509 {
510 3 break;
511 }
512
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (hours == 0)
513 {
514 2 print_hours = false;
515 }
516 else
517 {
518 1 break;
519 }
520
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (minutes == 0)
521 {
522 1 print_minutes = 0;
523 }
524 } while (0);
525
526
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (print_days)
527 {
528 3 lenused = gate_str_print_uint(dest_buffer, dest_size, days, 0);
529 3 dest_buffer += lenused;
530 3 dest_size -= lenused;
531 3 ret += lenused;
532
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (shortFormat)
533 {
534 2 lenused = gate_str_print_text(dest_buffer, dest_size, "d ", 2);
535 }
536 else
537 {
538 1 lenused = gate_str_print_text(dest_buffer, dest_size, " days ", 6);
539 }
540 3 dest_buffer += lenused;
541 3 dest_size -= lenused;
542 3 ret += lenused;
543 }
544
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (print_hours)
545 {
546 4 lenused = gate_str_print_uint(dest_buffer, dest_size, hours, 0);
547 4 dest_buffer += lenused;
548 4 dest_size -= lenused;
549 4 ret += lenused;
550
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (shortFormat)
551 {
552 3 lenused = gate_str_print_text(dest_buffer, dest_size, "h ", 2);
553 }
554 else
555 {
556 1 lenused = gate_str_print_text(dest_buffer, dest_size, " hours ", 7);
557 }
558 4 dest_buffer += lenused;
559 4 dest_size -= lenused;
560 4 ret += lenused;
561 }
562
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (print_minutes)
563 {
564 5 lenused = gate_str_print_uint(dest_buffer, dest_size, minutes, 0);
565 5 dest_buffer += lenused;
566 5 dest_size -= lenused;
567 5 ret += lenused;
568
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (shortFormat)
569 {
570 4 lenused = gate_str_print_text(dest_buffer, dest_size, "m ", 2);
571 }
572 else
573 {
574 1 lenused = gate_str_print_text(dest_buffer, dest_size, " minutes ", 9);
575 }
576 5 dest_buffer += lenused;
577 5 dest_size -= lenused;
578 5 ret += lenused;
579 }
580
581 {
582 6 lenused = gate_str_print_uint(dest_buffer, dest_size, seconds, 0);
583 6 dest_buffer += lenused;
584 6 dest_size -= lenused;
585 6 ret += lenused;
586
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (shortFormat)
587 {
588 5 lenused = gate_str_print_text(dest_buffer, dest_size, "s", 1);
589 }
590 else
591 {
592 1 lenused = gate_str_print_text(dest_buffer, dest_size, "seconds", 7);
593 }
594 /*
595 dest_buffer += lenused;
596 dest_size -= lenused;
597 */
598 6 ret += lenused;
599 }
600 6 return ret;
601 }
602
603 2 gate_string_t* gate_util_int_to_string(gate_string_t* output, gate_int64_t input)
604 {
605 char buffer[256];
606 2 gate_size_t used = gate_str_print_int64(buffer, sizeof(buffer), input);
607 2 return gate_string_create(output, buffer, used);
608 }
609 1 gate_string_t* gate_util_uint_to_string(gate_string_t* output, gate_uint64_t input)
610 {
611 char buffer[256];
612 1 gate_size_t used = gate_str_print_uint64(buffer, sizeof(buffer), input);
613 1 return gate_string_create(output, buffer, used);
614 }
615 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)
616 {
617 char buffer[256];
618 1 gate_size_t used = gate_str_print_real(buffer, sizeof(buffer), input, int_len, decimal_len, group_len);
619 1 return gate_string_create(output, buffer, used);
620 }
621 1 gate_int64_t gate_util_string_to_int(gate_string_t const* input)
622 {
623 1 gate_int64_t ret = 0;
624
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (input)
625 {
626 1 gate_str_parse_int64(input->str, input->length, &ret);
627 }
628 1 return ret;
629 }
630 1 gate_uint64_t gate_util_string_to_uint(gate_string_t const* input)
631 {
632 1 gate_uint64_t ret = 0;
633
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (input)
634 {
635 1 gate_str_parse_uint64(input->str, input->length, &ret);
636 }
637 1 return ret;
638 }
639 1 gate_real64_t gate_util_string_to_real(gate_string_t const* input)
640 {
641 1 gate_real64_t ret = 0.0;
642
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (input)
643 {
644 1 gate_str_parse_real(input->str, input->length, &ret);
645 }
646 1 return ret;
647 }
648
649
650 30 gate_result_t gate_util_dump_type(gate_stream_t* target, gate_type_id_t type, void const* data)
651 {
652 30 gate_result_t ret = GATE_RESULT_FAILED;
653 30 char buffer[1024] = GATE_INIT_EMPTY;
654 30 gate_size_t buffer_size = sizeof(buffer);
655 30 gate_datetime_t dt = GATE_INIT_EMPTY;
656
657
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)
658 {
659 2 case GATE_TYPE_BOOL:
660 2 case GATE_TYPE_I8: ret = gate_stream_print_int(target, *((gate_int8_t const*)data)); break;
661 2 case GATE_TYPE_I16: ret = gate_stream_print_int(target, *((gate_int16_t const*)data)); break;
662 2 case GATE_TYPE_I32: ret = gate_stream_print_int(target, *((gate_int32_t const*)data)); break;
663 2 case GATE_TYPE_I64: ret = gate_stream_print_int64(target, *((gate_int64_t const*)data)); break;
664 case GATE_TYPE_UI8: ret = gate_stream_print_uint(target, *((gate_uint8_t const*)data)); break;
665 2 case GATE_TYPE_UI16: ret = gate_stream_print_uint(target, *((gate_uint16_t const*)data)); break;
666 2 case GATE_TYPE_UI32: ret = gate_stream_print_uint(target, *((gate_uint32_t const*)data)); break;
667 2 case GATE_TYPE_UI64: ret = gate_stream_print_uint64(target, *((gate_uint64_t const*)data)); break;
668 2 case GATE_TYPE_R32: ret = gate_stream_print_real(target, *((gate_real32_t const*)data), 0, 0, 0); break;
669 2 case GATE_TYPE_R64: ret = gate_stream_print_real(target, *((gate_real64_t const*)data), 0, 0, 0); break;
670 case GATE_TYPE_ADDRESS: ret = gate_stream_print_uint64(target, *((gate_uintptr_t const*)data)); break;
671
672 case GATE_TYPE_DATAPTR: ret = gate_stream_print_uint64(target, (gate_uintptr_t)(*((gate_dataptr_t const*)data))); break;
673 case GATE_TYPE_FUNCPTR: ret = gate_stream_print_uint64(target, (gate_uintptr_t)(*((gate_funcptr_t const*)data))); break;
674 case GATE_TYPE_CSTR: ret = gate_stream_print_cstr(target, (gate_cstr_t)data); break;
675 case GATE_TYPE_WSTR: ret = gate_stream_print_wstr(target, (gate_wstr_t)data, gate_wstr_length((gate_wstr_t)data)); break;
676 2 case GATE_TYPE_GUID:
677 2 ret = gate_guid_to_string((gate_guid_t const*)data, buffer);
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
679 2 ret = gate_stream_print_cstr(target, buffer);
680 2 break;
681 2 case GATE_TYPE_DATE:
682 2 gate_mem_copy(&dt.date, data, sizeof(gate_date_t));
683 2 ret = gate_date_to_string((gate_datetime_t const*)data, 0, "{YYYY}-{MM}-{DD}", buffer, &buffer_size);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
685 2 ret = gate_stream_print_cstr(target, buffer);
686 2 break;
687 2 case GATE_TYPE_DAYTIME:
688 2 gate_mem_copy(&dt.time, data, sizeof(gate_daytime_t));
689 2 ret = gate_date_to_string((gate_datetime_t const*)data, 0, "{hh}:{mm}:{ss}.{SSS}", buffer, &buffer_size);
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
691 2 ret = gate_stream_print_cstr(target, buffer);
692 2 break;
693 2 case GATE_TYPE_DATETIME:
694 2 ret = gate_date_to_string((gate_datetime_t const*)data, 0, NULL, buffer, &buffer_size);
695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
696 2 ret = gate_stream_print_cstr(target, buffer);
697 2 break;
698 2 case GATE_TYPE_TIME:
699 2 ret = gate_time_to_string((gate_time_t const*)data, NULL, buffer, &buffer_size);
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
701 2 ret = gate_stream_print_cstr(target, buffer);
702 2 break;
703 2 case GATE_TYPE_STRING:
704 2 ret = gate_stream_print_string(target, (gate_string_t const*)data);
705 2 break;
706 case GATE_TYPE_ARRAY:
707 ret = gate_stream_print_cstr(target, "{gate_array_t}");
708 break;
709
710 case GATE_TYPE_STRUCT:
711 ret = gate_stream_print_cstr(target, "{gate_struct_t}");
712 break;
713 case GATE_TYPE_OBJECT:
714 ret = gate_stream_print_cstr(target, "{gate_object_t}");
715 break;
716 case GATE_TYPE_PROPERTY:
717 ret = gate_stream_print_cstr(target, "{gate_property_t}");
718 break;
719 default:
720 {
721 if ((type & GATE_TYPE_ARRAYLIST) == GATE_TYPE_ARRAYLIST)
722 {
723 ret = gate_stream_print_cstr(target, "{gate_arraylist_t}");
724 }
725 else
726 {
727 ret = GATE_RESULT_NOTSUPPORTED;
728 }
729 break;
730 }
731 }
732 30 return ret;
733 }
734
735 15 static gate_result_t gate_util_print_indent(gate_stream_t* target, gate_size_t indent)
736 {
737 char buffer[4096];
738 15 gate_strbuilder_t builder = GATE_INIT_EMPTY;
739 gate_size_t lenused;
740 15 gate_size_t written = 0;
741
742 15 gate_strbuilder_create_static(&builder, buffer, sizeof(buffer), 0);
743 15 lenused = gate_strbuilder_append_chars(&builder, indent, '\t');
744 15 return gate_stream_write_block(target, buffer, lenused, &written);
745 }
746
747 1 static gate_result_t gate_util_struct_dump_to(gate_stream_t* target, void const* ptr_input_struct, gate_size_t indent)
748 {
749 1 gate_result_t ret = GATE_RESULT_OK;
750 1 gate_size_t const count = gate_struct_get_member_count(ptr_input_struct);
751 gate_size_t index;
752 gate_size_t arr_length;
753 gate_size_t arr_index;
754 gate_type_id_t arr_type;
755 void const* arr_item;
756
757
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
16 for (index = 0; index != count; ++index)
758 {
759 15 char const* name = gate_struct_get_member_name(ptr_input_struct, index);
760 15 gate_type_id_t type = gate_struct_get_member_type(ptr_input_struct, index);
761 15 void const* value = gate_struct_get_member(ptr_input_struct, index);
762
763 15 gate_util_print_indent(target, indent);
764 15 gate_stream_print_cstr(target, name);
765 15 gate_stream_print_cstr(target, " = ");
766
767
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)
768 {
769 15 case GATE_TYPE_UI8:
770 case GATE_TYPE_UI16:
771 case GATE_TYPE_UI32:
772 case GATE_TYPE_UI64:
773 case GATE_TYPE_I8:
774 case GATE_TYPE_I16:
775 case GATE_TYPE_I32:
776 case GATE_TYPE_I64:
777 case GATE_TYPE_R32:
778 case GATE_TYPE_R64:
779 case GATE_TYPE_ADDRESS:
780 case GATE_TYPE_DATAPTR:
781 case GATE_TYPE_FUNCPTR:
782 case GATE_TYPE_CSTR:
783 case GATE_TYPE_WSTR:
784 case GATE_TYPE_GUID:
785 case GATE_TYPE_DATE:
786 case GATE_TYPE_DAYTIME:
787 case GATE_TYPE_DATETIME:
788 case GATE_TYPE_TIME:
789 case GATE_TYPE_STRING:
790 {
791 15 ret = gate_util_dump_type(target, type, value);
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 GATE_BREAK_IF_FAILED(ret);
793 15 ret = gate_stream_println_cstr(target, NULL);
794 15 break;
795 }
796 case GATE_TYPE_ARRAY:
797 {
798 ret = gate_stream_println_cstr(target, "{gate_array_t}");
799 break;
800 }
801 case GATE_TYPE_STRUCT:
802 {
803 ret = gate_stream_println_cstr(target, "{gate_struct_t}");
804 GATE_BREAK_IF_FAILED(ret);
805 ret = gate_util_struct_dump_to(target, value, indent + 1);
806 break;
807 }
808 case GATE_TYPE_OBJECT:
809 {
810 ret = gate_stream_println_cstr(target, "{gate_object_t}");
811 break;
812 }
813 case GATE_TYPE_PROPERTY:
814 {
815 ret = gate_stream_println_cstr(target, "{gate_property_t}");
816 break;
817 }
818 case GATE_TYPE_ARRAYLIST_STRUCT:
819 {
820 arr_length = gate_arraylist_length(*(gate_arraylist_t const*)value);
821 ret = gate_stream_println_cstr(target, "{gate_arraylist_t[gate_struct_t]}");
822 GATE_BREAK_IF_FAILED(ret);
823 for (arr_index = 0; arr_index != arr_length; ++arr_index)
824 {
825 arr_item = gate_arraylist_get(*(gate_arraylist_t const*)value, arr_index);
826 if (arr_item)
827 {
828 ret = gate_util_struct_dump_to(target, arr_item, indent + 1);
829 GATE_BREAK_IF_FAILED(ret);
830 }
831 }
832 break;
833 }
834 default:
835 {
836 if ((type & GATE_TYPE_ARRAYLIST) == GATE_TYPE_ARRAYLIST)
837 {
838 arr_type = (type & (~GATE_TYPE_ARRAYLIST));
839 arr_length = gate_arraylist_length(*(gate_arraylist_t const*)value);
840 ret = gate_stream_print_cstr(target, "{gate_arraylist_t[");
841 GATE_BREAK_IF_FAILED(ret);
842 ret = gate_stream_print_uint(target, arr_type);
843 GATE_BREAK_IF_FAILED(ret);
844 ret = gate_stream_println_cstr(target, "]}");
845 for (arr_index = 0; arr_index != arr_length; ++arr_index)
846 {
847 arr_item = gate_arraylist_get(*(gate_arraylist_t const*)value, arr_index);
848 if (arr_item)
849 {
850 ret = gate_util_print_indent(target, indent + 1);
851 GATE_BREAK_IF_FAILED(ret);
852 ret = gate_util_dump_type(target, arr_type, arr_item);
853 GATE_BREAK_IF_FAILED(ret);
854 ret = gate_stream_println_cstr(target, NULL);
855 GATE_BREAK_IF_FAILED(ret);
856 }
857 }
858 }
859 else
860 {
861 ret = gate_stream_print_cstr(target, "{unsupported_type_");
862 GATE_BREAK_IF_FAILED(ret);
863 ret = gate_stream_print_uint(target, type);
864 GATE_BREAK_IF_FAILED(ret);
865 ret = gate_stream_println_cstr(target, "}");
866 }
867 break;
868 }
869 } /* switch */
870 } /* for */
871 1 return ret;
872 }
873
874 1 gate_result_t gate_util_dump_struct(gate_stream_t* target, void const* ptr_input_struct)
875 {
876 1 return gate_util_struct_dump_to(target, ptr_input_struct, 0);
877 }
878
879
880 static gate_atomic_int_t global_state_init = 0;
881 static gate_mutex_t global_state_mutex;
882 static gate_synccondition_t global_state_signal;
883
884 2 static void close_state_objects_atexit(void)
885 {
886 2 gate_synccondition_destroy(&global_state_signal);
887 2 gate_mutex_destroy(&global_state_mutex);
888 2 gate_atomic_int_set(&global_state_init, 0);
889 2 }
890
891 10 static gate_result_t gate_util_state_init()
892 {
893 gate_result_t result;
894 gate_int32_t v;
895
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
10 if (2 == gate_atomic_int_get(&global_state_init))
896 {
897 8 return GATE_RESULT_OK;
898 }
899
900
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)))
901 {
902
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (v == 0)
903 {
904 2 result = gate_mutex_create(&global_state_mutex);
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(result))
906 {
907 gate_atomic_int_set(&global_state_init, 0);
908 return GATE_RESULT_OUTOFRESOURCES;
909 }
910 2 result = gate_synccondition_create(&global_state_signal);
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(result))
912 {
913 gate_mutex_destroy(&global_state_mutex);
914 gate_atomic_int_set(&global_state_init, 0);
915 return GATE_RESULT_OUTOFRESOURCES;
916 }
917 /* init succeeded */
918 2 gate_atomic_int_set(&global_state_init, 2);
919 2 gate_platform_atexit(&close_state_objects_atexit);
920 2 break;
921 }
922 else
923 {
924 /* spin until succeeded or fail-reset */
925 }
926 }
927 2 return GATE_RESULT_OK;
928 }
929
930 1 gate_result_t gate_util_state_set(gate_atomic_int_t* ptr_state, gate_int32_t value)
931 {
932 1 gate_result_t ret = gate_util_state_init();
933 do
934 {
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
936 1 ret = gate_mutex_acquire(&global_state_mutex);
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
938 1 gate_atomic_int_set(ptr_state, value);
939 1 ret = gate_synccondition_signal_all(&global_state_signal);
940 1 gate_mutex_release(&global_state_mutex);
941
942 } while (0);
943 1 return ret;
944 }
945 1 gate_result_t gate_util_state_update(gate_atomic_int_t* ptr_state, gate_int32_t from_value, gate_int32_t to_value)
946 {
947 gate_int32_t prev_value;
948 1 gate_result_t ret = gate_util_state_init();
949 do
950 {
951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
952 1 ret = gate_mutex_acquire(&global_state_mutex);
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
954 1 prev_value = gate_atomic_int_xchg_if(ptr_state, from_value, to_value);
955 1 ret = gate_synccondition_signal_all(&global_state_signal);
956
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
957 {
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (prev_value != from_value)
959 {
960 ret = GATE_RESULT_INVALIDSTATE;
961 }
962 }
963 1 gate_mutex_release(&global_state_mutex);
964
965 } while (0);
966 1 return ret;
967 }
968 8 gate_result_t gate_util_state_await(gate_atomic_int_t* ptr_state, gate_int32_t await_value, gate_uint32_t timeout_ms)
969 {
970 gate_timecounter_t now;
971 gate_timecounter_t timeout_time;
972 gate_int64_t diff;
973 8 gate_result_t ret = gate_util_state_init();
974 do
975 {
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
977 8 ret = gate_timecounter_now(&now);
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
979 8 timeout_time = gate_timecounter_add(now, ((gate_int64_t)timeout_ms) * 1000);
980
981 8 ret = gate_mutex_acquire(&global_state_mutex);
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 GATE_BREAK_IF_FAILED(ret);
983
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
8 while (gate_atomic_int_get(ptr_state) != await_value)
984 {
985 2 ret = gate_timecounter_now(&now);
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
987
988 2 diff = gate_timecounter_diff(timeout_time, now);
989
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (diff <= 0)
990 {
991 2 ret = GATE_RESULT_TIMEOUT;
992 2 break;
993 }
994
995 ret = gate_synccondition_timed_wait(&global_state_signal, &global_state_mutex, timeout_ms);
996 if (ret == GATE_RESULT_TIMEOUT)
997 {
998 ret = GATE_RESULT_OK;
999 }
1000 GATE_BREAK_IF_FAILED(ret);
1001 }
1002 8 gate_mutex_release(&global_state_mutex);
1003 } while (0);
1004 8 return ret;
1005 }
1006