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/enumerators.h" |
30 |
|
|
#include "gate/memalloc.h" |
31 |
|
|
|
32 |
|
2162 |
gate_bool_t gate_enumerator_valid(gate_enumerator_t const* enumerator) |
33 |
|
|
{ |
34 |
|
2162 |
return enumerator->is_valid(enumerator); |
35 |
|
|
} |
36 |
|
2148 |
gate_bool_t gate_enumerator_next(gate_enumerator_t* enumerator) |
37 |
|
|
{ |
38 |
|
2148 |
return enumerator->next(enumerator); |
39 |
|
|
} |
40 |
|
2245 |
void const* gate_enumerator_get(gate_enumerator_t const* enumerator) |
41 |
|
|
{ |
42 |
|
2245 |
return enumerator->get(enumerator); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
✗ |
void const* gate_enumerator_next_item(gate_enumerator_t* enumerator) |
46 |
|
|
{ |
47 |
|
✗ |
if (enumerator->next(enumerator)) |
48 |
|
|
{ |
49 |
|
✗ |
return enumerator->get(enumerator); |
50 |
|
|
} |
51 |
|
|
else |
52 |
|
|
{ |
53 |
|
✗ |
return NULL; |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
✗ |
static gate_bool_t gate_enumerator_c_array_is_valid(struct gate_enumerator_class const* enumerator) |
60 |
|
|
{ |
61 |
|
✗ |
return enumerator->current_position < enumerator->end_position; |
62 |
|
|
} |
63 |
|
✗ |
static gate_bool_t gate_enumerator_c_array_next(struct gate_enumerator_class* enumerator) |
64 |
|
|
{ |
65 |
|
✗ |
gate_size_t item_size = (gate_size_t)enumerator->handles[0]; |
66 |
|
✗ |
char* new_ptr = (char*)enumerator->current_position + item_size; |
67 |
|
✗ |
enumerator->current_position = new_ptr; |
68 |
|
✗ |
return enumerator->current_position < enumerator->end_position; |
69 |
|
|
} |
70 |
|
✗ |
static void const* gate_enumerator_c_array_get(struct gate_enumerator_class const* enumerator) |
71 |
|
|
{ |
72 |
|
✗ |
if (enumerator->current_position < enumerator->end_position) |
73 |
|
|
{ |
74 |
|
✗ |
return (void const*)enumerator->current_position; |
75 |
|
|
} |
76 |
|
|
else |
77 |
|
|
{ |
78 |
|
✗ |
return NULL; |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
✗ |
gate_enumerator_t* gate_enumerator_for_array(gate_enumerator_t* enumerator, |
83 |
|
|
void const* begin, |
84 |
|
|
gate_size_t item_size, |
85 |
|
|
gate_size_t item_count) |
86 |
|
|
{ |
87 |
|
✗ |
if (item_size == 0) |
88 |
|
|
{ |
89 |
|
✗ |
return NULL; |
90 |
|
|
} |
91 |
|
|
else |
92 |
|
|
{ |
93 |
|
✗ |
gate_mem_clear(enumerator, sizeof(gate_enumerator_t)); |
94 |
|
✗ |
enumerator->is_valid = &gate_enumerator_c_array_is_valid; |
95 |
|
✗ |
enumerator->next = &gate_enumerator_c_array_next; |
96 |
|
✗ |
enumerator->get = &gate_enumerator_c_array_get; |
97 |
|
|
|
98 |
|
✗ |
enumerator->ptr_origin = begin; |
99 |
|
✗ |
enumerator->current_position = (void*)begin; |
100 |
|
✗ |
enumerator->end_position = (void*)((char const*)begin + item_size * item_count); |
101 |
|
|
|
102 |
|
✗ |
enumerator->handles[0] = (void*)item_size; |
103 |
|
✗ |
enumerator->handles[1] = (void*)item_count; |
104 |
|
✗ |
return enumerator; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
} |
108 |
|
|
|