simplifications
[mala.git] / engine / stringlist.c
blob7beb63f1ce149e781f4c8867cbee6ae998a96918
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 elem)
177 mala_stringlist_elem_remove (head, elem);
178 mala_string_free (elem->string);
179 free (elem);
182 void
183 mala_stringlist_erase (MalaStringList head)
185 while (head->cqh_first != (void *) head)
187 MalaStringListNode n = head->cqh_first;
188 CIRCLEQ_REMOVE(head, n, node);
189 mala_string_free (n->string);
190 free (n);
194 void
195 mala_stringlist_free (MalaStringList head)
197 if (!head)
198 return;
200 mala_stringlist_erase (head);
201 free (head);
204 void *
205 mala_stringlist_factory (MalaStringList_ref dest, char** src, MalaStringBucket bucket)
207 if (src)
209 return *dest = mala_stringlist_new_cstrs (src, -1, bucket);
211 else
213 mala_stringlist_free (*dest);
214 return NULL;
219 mala_stringlist_char_exists (MalaStringList head, const char c)
221 MalaStringListNode itr;
222 for (itr = head->cqh_first; itr != (void *)head; itr = itr->node.cqe_next)
223 if (SIZE_MAX != mala_string_char_find (itr->string, c))
224 return 1;
225 return 0;
230 // Local Variables:
231 // mode: C
232 // c-file-style: "gnu"
233 // End:
234 // arch-tag: 75b1fbb2-0328-450d-8708-daf943e7bbf6
235 // end_of_file