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