summaryrefslogtreecommitdiff
path: root/src/libmowgli/mowgli_argstack.c
blob: 0ad5c914ca6922766e67d2fe62abe8ed9dda5e13 (plain)
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
/*
 * libmowgli: A collection of useful routines for programming.
 * mowgli_argstack.c: Argument stacks.
 *
 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 "mowgli.h"

static mowgli_object_class_t klass;

/*
 * \brief Private destructor for the mowgli_argstack_t object.
 *
 * \param vptr pointer to mowgli_argstack_t to destroy.
 */
static void mowgli_argstack_destroy(void *vptr)
{
	mowgli_argstack_t *self = (mowgli_argstack_t *) vptr;
	mowgli_node_t *n, *tn;

	MOWGLI_LIST_FOREACH_SAFE(n, tn, self->stack.head)
	{
		mowgli_free(n->data);

		mowgli_node_delete(n, &self->stack);
		mowgli_node_free(n);
	}

	mowgli_free(self);
}

/*
 * \brief Initialization code for the mowgli.argstack library.
 * 
 * Side Effects:
 *       - the mowgli_argstack_t object class is registered.
 */
void mowgli_argstack_init(void)
{
	mowgli_object_class_init(&klass, "mowgli_argstack_t", mowgli_argstack_destroy, FALSE);
}

/*
 * \brief Creates an argument stack from a va_list and an appropriate 
 * description schema.
 *
 * \param descstr a description string which describes the argument stack, where:
 *   + the character 's' means that the value for that slot is a string
 *   + the character 'd' means that the value for that slot is a numeric
 *   + the character 'p' means that the value for that slot is a generic pointer
 *   + the character 'b' means that the value for that slot is a boolean
 * \param va a va_list containing data to populate the argument stack with.
 *
 * \return a mowgli_argstack_t (mowgli.argstack) object.
 */
mowgli_argstack_t *mowgli_argstack_create_from_va_list(const char *descstr, va_list va)
{
	const char *cp = descstr;
	mowgli_argstack_t *out = mowgli_alloc(sizeof(mowgli_argstack_t));
	mowgli_object_init(mowgli_object(out), descstr, &klass, NULL);

	if (descstr == NULL)
		mowgli_throw_exception_val(mowgli.argstack.invalid_description, NULL);

	while (*cp)
	{
		mowgli_argstack_element_t *e = mowgli_alloc(sizeof(mowgli_argstack_element_t));

		switch(*cp)
		{
		case 's':
			e->data.string = va_arg(va, char *);
			e->type = MOWGLI_ARG_STRING;
			break;
		case 'd':
			e->data.numeric = va_arg(va, int);
			e->type = MOWGLI_ARG_NUMERIC;
			break;
		case 'p':
			e->data.pointer = va_arg(va, void *);
			e->type = MOWGLI_ARG_POINTER;
			break;
		case 'b':
			e->data.boolean = va_arg(va, mowgli_boolean_t);
			e->type = MOWGLI_ARG_BOOLEAN;
			break;
		default:
			va_end(va);
			mowgli_object_unref(out);
			mowgli_throw_exception_val(mowgli.argstack.invalid_description, NULL);
			break;
		}

		mowgli_node_add(e, mowgli_node_create(), &out->stack);
		cp++;
	}

	return out;
}

/*
 * \brief Creates an argument stack.
 *
 * \param descstr a description string which describes the argument stack, where:
 *   + the character 's' means that the value for that slot is a string
 *   + the character 'd' means that the value for that slot is a numeric
 *   + the character 'p' means that the value for that slot is a generic pointer
 *   + the character 'b' means that the value for that slot is a boolean
 * \param va a va_list containing data to populate the argument stack with.
 *
 * \return a mowgli_argstack_t (mowgli.argstack) object.
 */
mowgli_argstack_t *mowgli_argstack_create(const char *descstr, ...)
{
	va_list va;
	mowgli_argstack_t *out;

	if (descstr == NULL)
		mowgli_throw_exception_val(mowgli.argstack.invalid_description, NULL);

	va_start(va, descstr);
	out = mowgli_argstack_create_from_va_list(descstr, va);
	va_end(va);

	return out;
}

/*
 * \brief Convenience function to pop a string value off of an argument stack.
 *
 * \param self A mowgli_argstack_t object to pop a string off of.
 *
 * \return On success, a string.
 *
 * Side Effects:
 *       - the argument is removed from the argstack.
 */
const char *mowgli_argstack_pop_string(mowgli_argstack_t *self)
{
	mowgli_node_t *n;
	mowgli_argstack_element_t *e;

	if (self == NULL)
		mowgli_throw_exception_val(mowgli.null_pointer_exception, NULL);

	n = self->stack.head;
	mowgli_node_delete(n, &self->stack);
	e = n->data;
	mowgli_node_free(n);

	return e->data.string;
}

/*
 * \brief Convenience function to pop a numeric value off of an argument stack.
 *
 * \param self A mowgli_argstack_t object to pop a numeric off of.
 *
 * \return On success, a numeric.
 *
 * Side Effects:
 *       - the argument is removed from the argstack.
 */
int mowgli_argstack_pop_numeric(mowgli_argstack_t *self)
{
	mowgli_node_t *n;
	mowgli_argstack_element_t *e;

	if (self == NULL)
		mowgli_throw_exception_val(mowgli.null_pointer_exception, 0);

	n = self->stack.head;
	mowgli_node_delete(n, &self->stack);
	e = n->data;
	mowgli_node_free(n);

	return e->data.numeric;
}

/*
 * Convenience function to pop a boolean value off of an argument stack.
 *
 * \param self A mowgli_argstack_t object to pop a boolean off of.
 *
 * \return On success, a boolean value.
 *
 * Side Effects:
 *       - the argument is removed from the argstack.
 */
mowgli_boolean_t mowgli_argstack_pop_boolean(mowgli_argstack_t *self)
{
	mowgli_node_t *n;
	mowgli_argstack_element_t *e;

	if (self == NULL)
		mowgli_throw_exception_val(mowgli.null_pointer_exception, FALSE);

	n = self->stack.head;
	mowgli_node_delete(n, &self->stack);
	e = n->data;
	mowgli_node_free(n);

	return e->data.boolean;
}

/*
 * \brief Convenience function to pop a pointer value off of an argument stack.
 *
 * \param self A mowgli_argstack_t object to pop a pointer off of.
 *
 * \return On success, a pointer.
 *
 * Side Effects:
 *       - the argument is removed from the argstack.
 */
void *mowgli_argstack_pop_pointer(mowgli_argstack_t *self)
{
	mowgli_node_t *n;
	mowgli_argstack_element_t *e;

	if (self == NULL)
		mowgli_throw_exception_val(mowgli.null_pointer_exception, NULL);

	n = self->stack.head;
	mowgli_node_delete(n, &self->stack);
	e = n->data;
	mowgli_node_free(n);

	return e->data.pointer;
}