~ubuntu-branches/ubuntu/saucy/sssd/saucy

« back to all changes in this revision

Viewing changes to common/collection/collection_stack.c

  • Committer: Stéphane Graber
  • Date: 2011-06-15 16:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: stgraber@ubuntu.com-20110615162314-rbhoppnpaxfqo5q7
Merge 1.5.8

Show diffs side-by-side

added added

removed removed

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