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