Debian Codegraph @

collection/collection_stack.c

raw symbols SCIP
1 /*
2 STACK
3
4 Implementation of the stack on top of collection library interface.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2009
7
8 Collection Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Collection Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with Collection Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <errno.h>
25 #include "collection_stack.h"
26 #include "trace.h"
27
28 /* Function that creates a stack object */
29 int col_create_stack(struct collection_item **stack)
30 {
31 int error = EOK;
32
33 TRACE_FLOW_STRING("col_create_stack", "Entry point.");
34
35 error = col_create_collection(stack, COL_NAME_STACK, COL_CLASS_STACK);
36
37 TRACE_FLOW_STRING("col_create_stack", "Exit.");
38 return error;
39 }
40
41 /* Function that destroys a stack object */
42 void col_destroy_stack(struct collection_item *stack)
43 {
44 TRACE_FLOW_STRING("col_destroy_stack", "Entry point.");
45
46 col_destroy_collection(stack);
47
48 TRACE_FLOW_STRING("col_destroy_stack", "Exit");
49 }
50
51
52
53 int col_push_str_property(struct collection_item *stack,
54 const char *property, const char *string, int length)
55 {
56 int error = EOK;
57
58 TRACE_FLOW_STRING("col_push_str_property", "Entry point.");
59
60 /* Check that stack is not empty */
61 if (stack == NULL) {
62 TRACE_ERROR_STRING("Stack can't be NULL", "");
63 return EINVAL;
64 }
65
66 /* Make sure it is a stack */
67 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
68 TRACE_ERROR_STRING("Wrong class", "");
69 return EINVAL;
70 }
71
72 error = col_add_str_property(stack, NULL, property, string, length);
73
74 TRACE_FLOW_STRING("col_push_str_property", "Exit.");
75 return error;
76 }
77
78 /* Push a binary property to stack. */
79 int col_push_binary_property(struct collection_item *stack,
80 const char *property,
81 void *binary_data,
82 int length)
83 {
84 int error = EOK;
85
86 TRACE_FLOW_STRING("col_push_binary_property", "Entry point.");
87
88 /* Check that stack is not empty */
89 if (stack == NULL) {
90 TRACE_ERROR_STRING("Stack can't be NULL", "");
91 return EINVAL;
92 }
93
94 /* Make sure it is a stack */
95 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
96 TRACE_ERROR_STRING("Wrong class", "");
97 return EINVAL;
98 }
99
100 error = col_add_binary_property(stack, NULL, property, binary_data, length);
101
102 TRACE_FLOW_STRING("col_push_binary_property", "Exit.");
103 return error;
104 }
105
106
107 /* Push an int property to stack. */
108 int col_push_int_property(struct collection_item *stack,
109 const char *property,
110 int32_t number)
111 {
112 int error = EOK;
113
114 TRACE_FLOW_STRING("col_push_int_property", "Entry point.");
115
116 /* Check that stack is not empty */
117 if (stack == NULL) {
118 TRACE_ERROR_STRING("Stack can't be NULL", "");
119 return EINVAL;
120 }
121
122 /* Make sure it is a stack */
123 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
124 TRACE_ERROR_STRING("Wrong class", "");
125 return EINVAL;
126 }
127
128 error = col_add_int_property(stack, NULL, property, number);
129
130 TRACE_FLOW_STRING("col_push_int_property", "Exit.");
131 return error;
132 }
133
134 /* Push an unsigned int property to stack. */
135 int col_push_unsigned_property(struct collection_item *stack,
136 const char *property,
137 uint32_t number)
138 {
139 int error = EOK;
140
141 TRACE_FLOW_STRING("col_push_unsigned_property", "Entry point.");
142
143 /* Check that stack is not empty */
144 if (stack == NULL) {
145 TRACE_ERROR_STRING("Stack can't be NULL", "");
146 return EINVAL;
147 }
148
149 /* Make sure it is a stack */
150 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
151 TRACE_ERROR_STRING("Wrong class", "");
152 return EINVAL;
153 }
154
155 error = col_add_unsigned_property(stack, NULL, property, number);
156
157 TRACE_FLOW_STRING("col_push_unsigned_property", "Exit.");
158 return error;
159 }
160
161
162 /* Push a long property. */
163 int col_push_long_property(struct collection_item *stack,
164 const char *property,
165 int64_t number)
166 {
167 int error = EOK;
168
169 TRACE_FLOW_STRING("col_push_long_property", "Entry point.");
170
171 /* Check that stack is not empty */
172 if (stack == NULL) {
173 TRACE_ERROR_STRING("Stack can't be NULL", "");
174 return EINVAL;
175 }
176
177 /* Make sure it is a stack */
178 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
179 TRACE_ERROR_STRING("Wrong class", "");
180 return EINVAL;
181 }
182
183 error = col_add_long_property(stack, NULL, property, number);
184
185 TRACE_FLOW_STRING("col_push_long_property", "Exit.");
186 return error;
187 }
188
189 /* Push an unsigned long property. */
190 int col_push_ulong_property(struct collection_item *stack,
191 const char *property,
192 uint64_t number)
193 {
194 int error = EOK;
195
196 TRACE_FLOW_STRING("col_push_ulong_property", "Entry point.");
197
198 /* Check that stack is not empty */
199 if (stack == NULL) {
200 TRACE_ERROR_STRING("Stack can't be NULL", "");
201 return EINVAL;
202 }
203
204 /* Make sure it is a stack */
205 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
206 TRACE_ERROR_STRING("Wrong class", "");
207 return EINVAL;
208 }
209
210 error = col_add_ulong_property(stack, NULL, property, number);
211
212 TRACE_FLOW_STRING("col_push_ulong_property", "Exit.");
213 return error;
214 }
215
216 /* Push a double property. */
217 int col_push_double_property(struct collection_item *stack,
218 const char *property,
219 double number)
220 {
221 int error = EOK;
222
223 TRACE_FLOW_STRING("col_push_double_property", "Entry point.");
224
225 /* Check that stack is not empty */
226 if (stack == NULL) {
227 TRACE_ERROR_STRING("Stack can't be NULL", "");
228 return EINVAL;
229 }
230
231 /* Make sure it is a stack */
232 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
233 TRACE_ERROR_STRING("Wrong class", "");
234 return EINVAL;
235 }
236
237 error = col_add_double_property(stack, NULL, property, number);
238
239 TRACE_FLOW_STRING("col_push_double_property", "Exit.");
240 return error;
241 }
242
243 /* Push a bool property. */
244 int col_push_bool_property(struct collection_item *stack,
245 const char *property,
246 unsigned char logical)
247 {
248 int error = EOK;
249
250 TRACE_FLOW_STRING("col_push_bool_property", "Entry point.");
251
252 /* Check that stack is not empty */
253 if (stack == NULL) {
254 TRACE_ERROR_STRING("Stack can't be NULL", "");
255 return EINVAL;
256 }
257
258 /* Make sure it is a stack */
259 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
260 TRACE_ERROR_STRING("Wrong class", "");
261 return EINVAL;
262 }
263
264 error = col_add_bool_property(stack, NULL, property, logical);
265
266 TRACE_FLOW_STRING("push_double_property", "Exit.");
267 return error;
268 }
269
270 /* Push any property */
271 int col_push_any_property(struct collection_item *stack,
272 const char *property,
273 int type,
274 void *data,
275 int length)
276 {
277 int error = EOK;
278
279 TRACE_FLOW_STRING("col_push_any_property", "Entry point.");
280
281 /* Check that stack is not empty */
282 if (stack == NULL) {
283 TRACE_ERROR_STRING("Stack can't be NULL", "");
284 return EINVAL;
285 }
286
287 /* Make sure it is a stack */
288 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
289 TRACE_ERROR_STRING("Wrong class", "");
290 return EINVAL;
291 }
292
293 error = col_add_any_property(stack, NULL, property, type, data, length);
294
295 TRACE_FLOW_STRING("col_push_any_property", "Exit.");
296 return error;
297 }
298
299 /* Push item */
300 int col_push_item(struct collection_item *stack,
301 struct collection_item *item)
302 {
303 int error = EOK;
304
305 TRACE_FLOW_STRING("col_push_item", "Entry point.");
306
307 /* Check that stack is not empty */
308 if (stack == NULL) {
309 TRACE_ERROR_STRING("Stack can't be NULL", "");
310 return EINVAL;
311 }
312
313 /* Make sure it is a stack */
314 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
315 TRACE_ERROR_STRING("Wrong class", "");
316 return EINVAL;
317 }
318
319 error = col_insert_item_into_current(stack,
320 item,
321 COL_DSP_END,
322 NULL,
323 0,
324 COL_INSERT_NOCHECK);
325
326 TRACE_FLOW_STRING("col_push_item", "Exit.");
327 return error;
328 }
329
330 /* Pop_item */
331 int col_pop_item(struct collection_item *stack,
332 struct collection_item **item)
333 {
334 int error = EOK;
335
336 TRACE_FLOW_STRING("col_pop_item", "Entry point.");
337
338 /* Check that stack is not empty */
339 if (stack == NULL) {
340 TRACE_ERROR_STRING("Stack can't be NULL", "");
341 return EINVAL;
342 }
343
344 /* Make sure it is a stack */
345 if (!col_is_of_class(stack, COL_CLASS_STACK)) {
346 TRACE_ERROR_STRING("Wrong class", "");
347 return EINVAL;
348 }
349
350 error = col_extract_item_from_current(stack,
351 COL_DSP_END,
352 NULL,
353 0,
354 0,
355 item);
356
357 TRACE_FLOW_STRING("col_pop_item", "Exit.");
358 return error;
359 }