block_parser
[mala.git] / engine / stringlist.c
blobbc2fc0bec1e736a80b2a969331deab3d2c5b2f9b
1 /*
2 stringlist.c - MaLa stringlists
4 Copyright (C) 2004, 2005, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
21 #include "stringlist.h"
23 MalaStringList
24 mala_stringlist_new ()
26 MalaStringList self;
27 self = malloc (sizeof(mala_stringlist));
28 if (!self)
29 return NULL;
31 CIRCLEQ_INIT(self);
32 return self;
35 MalaStringList
36 mala_stringlist_new_cstrs (char **cstrs,
37 int nelem,
38 MalaStringBucket bucket)
40 MalaStringList self;
41 MalaStringListNode node;
43 self = mala_stringlist_new ();
44 if (!self)
45 return NULL;
47 for (; nelem != 0 && *cstrs; --nelem , ++cstrs)
49 node = malloc (sizeof(mala_stringlistnode));
50 if (!node)
51 goto ealloc_node;
53 node->string = mala_string_new (*cstrs, bucket);
54 if (!node->string)
55 goto ealloc_str;
57 CIRCLEQ_INSERT_TAIL(self, node, node);
60 return self;
62 ealloc_str:
63 free (node);
64 ealloc_node:
65 mala_stringlist_free (self);
66 return NULL;
70 MalaStringListNode
71 mala_stringlist_head_new (MalaStringList head, MalaString str)
73 MalaStringListNode n;
75 if (!str)
76 return NULL;
78 n = malloc(sizeof(mala_stringlistnode));
79 if (!n)
80 return NULL;
82 n->string = mala_string_copy (str);
84 CIRCLEQ_INSERT_HEAD (head, n, node);
85 return n;
88 MalaStringListNode
89 mala_stringlist_tail_new (MalaStringList head, MalaString str)
91 MalaStringListNode n;
93 if (!str)
94 return NULL;
96 n = malloc(sizeof(mala_stringlistnode));
97 if (!n)
98 return NULL;
100 n->string = mala_string_copy (str);
102 CIRCLEQ_INSERT_TAIL (head, n, node);
103 return n;
106 MalaStringListNode
107 mala_stringlist_head_new_cstr (MalaStringList head, const char * cstr, MalaStringBucket bucket)
109 MalaString str;
110 MalaStringListNode n;
112 str = mala_string_new (cstr, bucket);
113 if (!str)
114 return NULL;
116 n = mala_stringlist_head_new (head, str);
118 mala_string_free (str);
119 return n;
122 MalaStringListNode
123 mala_stringlist_tail_new_cstr (MalaStringList head, const char * cstr, MalaStringBucket bucket)
125 MalaString str;
126 MalaStringListNode n;
128 str = mala_string_new (cstr, bucket);
129 if (!str)
130 return NULL;
132 n = mala_stringlist_tail_new (head, str);
134 mala_string_free (str);
135 return n;
139 MalaStringListNode
140 mala_stringlist_after_new (MalaStringList head, MalaStringListNode pred, MalaString str)
142 MalaStringListNode n = malloc(sizeof(mala_stringlistnode));
143 if (!n)
144 return NULL;
146 n->string = mala_string_copy (str);
148 CIRCLEQ_INSERT_AFTER (head, pred, n, node);
149 return n;
153 MalaStringListNode
154 mala_stringlist_before_new (MalaStringList head, MalaStringListNode succ, MalaString str)
156 MalaStringListNode n = malloc(sizeof(mala_stringlistnode));
157 if (!n)
158 return NULL;
160 n->string = mala_string_copy (str);
162 CIRCLEQ_INSERT_BEFORE (head, succ, n, node);
163 return n;
167 MalaStringListNode
168 mala_stringlist_elem_remove (MalaStringList head, MalaStringListNode elem)
170 CIRCLEQ_REMOVE (head, elem, node);
171 return elem;
174 void
175 mala_stringlist_elem_delete (MalaStringList head, MalaStringListNode_ref elem)
177 mala_stringlist_elem_remove (head, *elem);
178 mala_string_free ((*elem)->string);
179 free (*elem);
180 *elem=NULL;
183 void
184 mala_stringlist_erase (MalaStringList head)
186 while (head->cqh_first != (void *) head)
188 MalaStringListNode n = head->cqh_first;
189 CIRCLEQ_REMOVE(head, n, node);
190 mala_string_free (n->string);
191 free (n);
195 void
196 mala_stringlist_free (MalaStringList head)
198 if (!head)
199 return;
201 mala_stringlist_erase (head);
202 free (head);
205 void *
206 mala_stringlist_factory (MalaStringList_ref dest, char** src, MalaStringBucket bucket)
208 if (src)
210 return *dest = mala_stringlist_new_cstrs (src, -1, bucket);
212 else
214 mala_stringlist_free (*dest);
215 return NULL;
220 // Local Variables:
221 // mode: C
222 // c-file-style: "gnu"
223 // End:
224 // arch-tag: 75b1fbb2-0328-450d-8708-daf943e7bbf6
225 // end_of_file