Debian Codegraph @

libmowgli-2/src/libmowgli/base/argstack.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * argstack.c: Argument stacks.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25 #include "core/bootstrap_internal.h"
26
27 static mowgli_object_class_t klass;
28
29 /*
30 * \brief Private destructor for the mowgli_argstack_t object.
31 *
32 * \param vptr pointer to mowgli_argstack_t to destroy.
33 */
34 static void
35 mowgli_argstack_destroy(void *vptr)
36 {
37 mowgli_argstack_t *self = (mowgli_argstack_t *) vptr;
38 mowgli_node_t *n, *tn;
39
40 MOWGLI_LIST_FOREACH_SAFE(n, tn, self->stack.head)
41 {
42 mowgli_free(n->data);
43
44 mowgli_node_delete(n, &self->stack);
45 mowgli_node_free(n);
46 }
47
48 mowgli_free(self);
49 }
50
51 /*
52 * \brief Initialization code for the mowgli.argstack library.
53 *
54 * Side Effects:
55 * - the mowgli_argstack_t object class is registered.
56 */
57 void
58 mowgli_argstack_bootstrap(void)
59 {
60 mowgli_object_class_init(&klass, "mowgli_argstack_t", mowgli_argstack_destroy, FALSE);
61 }
62
63 /*
64 * \brief Creates an argument stack from a va_list and an appropriate
65 * description schema.
66 *
67 * \param descstr a description string which describes the argument stack, where:
68 * + the character 's' means that the value for that slot is a string
69 * + the character 'd' means that the value for that slot is a numeric
70 * + the character 'p' means that the value for that slot is a generic pointer
71 * + the character 'b' means that the value for that slot is a boolean
72 * \param va a va_list containing data to populate the argument stack with.
73 *
74 * \return a mowgli_argstack_t (mowgli.argstack) object.
75 */
76 mowgli_argstack_t *
77 mowgli_argstack_create_from_va_list(const char *descstr, va_list va)
78 {
79 return_null_if_fail(descstr != NULL);
80
81 const char *cp = descstr;
82 mowgli_argstack_t *out = mowgli_alloc(sizeof *out);
83 mowgli_object_init(mowgli_object(out), descstr, &klass, NULL);
84
85 while (*cp)
86 {
87 mowgli_argstack_element_t *e = mowgli_alloc(sizeof *e);
88
89 switch (*cp)
90 {
91 case 's':
92 e->data.string = va_arg(va, char *);
93 e->type = MOWGLI_ARG_STRING;
94 break;
95 case 'd':
96 e->data.numeric = va_arg(va, int);
97 e->type = MOWGLI_ARG_NUMERIC;
98 break;
99 case 'p':
100 e->data.pointer = va_arg(va, void *);
101 e->type = MOWGLI_ARG_POINTER;
102 break;
103 case 'b':
104 e->data.boolean = va_arg(va, mowgli_boolean_t);
105 e->type = MOWGLI_ARG_BOOLEAN;
106 break;
107 default:
108 mowgli_object_unref(out);
109 mowgli_log_warning("invalid description");
110 return NULL;
111 }
112
113 mowgli_node_add(e, mowgli_node_create(), &out->stack);
114 cp++;
115 }
116
117 return out;
118 }
119
120 /*
121 * \brief Creates an argument stack.
122 *
123 * \param descstr a description string which describes the argument stack, where:
124 * + the character 's' means that the value for that slot is a string
125 * + the character 'd' means that the value for that slot is a numeric
126 * + the character 'p' means that the value for that slot is a generic pointer
127 * + the character 'b' means that the value for that slot is a boolean
128 * \param va a va_list containing data to populate the argument stack with.
129 *
130 * \return a mowgli_argstack_t (mowgli.argstack) object.
131 */
132 mowgli_argstack_t *
133 mowgli_argstack_create(const char *descstr, ...)
134 {
135 return_null_if_fail(descstr != NULL);
136
137 va_list va;
138 mowgli_argstack_t *out;
139
140 va_start(va, descstr);
141 out = mowgli_argstack_create_from_va_list(descstr, va);
142 va_end(va);
143
144 return out;
145 }
146
147 /*
148 * \brief Convenience function to pop a string value off of an argument stack.
149 *
150 * \param self A mowgli_argstack_t object to pop a string off of.
151 *
152 * \return On success, a string.
153 *
154 * Side Effects:
155 * - the argument is removed from the argstack.
156 */
157 const char *
158 mowgli_argstack_pop_string(mowgli_argstack_t *self)
159 {
160 return_null_if_fail(self != NULL);
161
162 mowgli_node_t *n;
163 mowgli_argstack_element_t *e;
164
165 n = self->stack.head;
166 mowgli_node_delete(n, &self->stack);
167 e = n->data;
168 mowgli_node_free(n);
169
170 return e->data.string;
171 }
172
173 /*
174 * \brief Convenience function to pop a numeric value off of an argument stack.
175 *
176 * \param self A mowgli_argstack_t object to pop a numeric off of.
177 *
178 * \return On success, a numeric.
179 *
180 * Side Effects:
181 * - the argument is removed from the argstack.
182 */
183 int
184 mowgli_argstack_pop_numeric(mowgli_argstack_t *self)
185 {
186 return_val_if_fail(self != NULL, 0);
187
188 mowgli_node_t *n;
189 mowgli_argstack_element_t *e;
190
191 n = self->stack.head;
192 mowgli_node_delete(n, &self->stack);
193 e = n->data;
194 mowgli_node_free(n);
195
196 return e->data.numeric;
197 }
198
199 /*
200 * Convenience function to pop a boolean value off of an argument stack.
201 *
202 * \param self A mowgli_argstack_t object to pop a boolean off of.
203 *
204 * \return On success, a boolean value.
205 *
206 * Side Effects:
207 * - the argument is removed from the argstack.
208 */
209 mowgli_boolean_t
210 mowgli_argstack_pop_boolean(mowgli_argstack_t *self)
211 {
212 return_val_if_fail(self != NULL, false);
213
214 mowgli_node_t *n;
215 mowgli_argstack_element_t *e;
216
217 n = self->stack.head;
218 mowgli_node_delete(n, &self->stack);
219 e = n->data;
220 mowgli_node_free(n);
221
222 return e->data.boolean;
223 }
224
225 /*
226 * \brief Convenience function to pop a pointer value off of an argument stack.
227 *
228 * \param self A mowgli_argstack_t object to pop a pointer off of.
229 *
230 * \return On success, a pointer.
231 *
232 * Side Effects:
233 * - the argument is removed from the argstack.
234 */
235 void *
236 mowgli_argstack_pop_pointer(mowgli_argstack_t *self)
237 {
238 return_null_if_fail(self != NULL);
239
240 mowgli_node_t *n;
241 mowgli_argstack_element_t *e;
242
243 n = self->stack.head;
244 mowgli_node_delete(n, &self->stack);
245 e = n->data;
246 mowgli_node_free(n);
247
248 return e->data.pointer;
249 }