macrodelete_parser fix
[mala.git] / engine / stringbucket.c
blob749f0ebfd10b72aaf7f80a946858a8bbb117f75f
1 /*
2 stringbucket.c - MaLa stringbucket handling
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.
20 #define _GNU_SOURCE
22 #include <stdlib.h>
23 #include <search.h>
24 #include "stringbucket.h"
25 #include "strings.h"
28 void mala_stringbucket_init (MalaStringBucket bucket,
29 mala_string_cmp_fn cmpfn,
30 void (*freefn)(void*))
32 bucket->bucket = NULL;
33 bucket->cmpfn = cmpfn;
34 bucket->freefn = freefn;
35 bucket->destroying = 0;
39 static void
40 twalk_stringdata_destroy (const void *nodep,
41 const VISIT which,
42 const int depth)
44 (void) depth;
45 (void) which;
46 mala_string_data_free (*(MalaString_ref)nodep);
50 void
51 mala_stringbucket_erase (MalaStringBucket bucket)
53 bucket->destroying = 1;
55 /* it makes the string destructor a bit easier when we destroy data on a single pass */
56 twalk (bucket->bucket, twalk_stringdata_destroy);
58 tdestroy (bucket->bucket, (void(*)(void*)) mala_string_free);
59 bucket->bucket = NULL;
60 bucket->destroying = 0;
64 int
65 mala_stringbucket_exists_cstr (MalaStringBucket bucket, const char * cstr)
67 mala_string tmp = MALA_STRING_AUTO (cstr);
68 return !!tfind (&tmp, &bucket->bucket, bucket->cmpfn);
72 * remove string from bucket
74 MalaString
75 mala_stringbucket_string_remove (MalaString_ref self)
77 MALA_ASSERT(self);
79 if (!(*self)->bucket)
80 return *self;
82 /* self is the last reference in the bucket */
83 if ((*self)->refcnt <= 8)
85 if ((*self)->user && (*self)->bucket->freefn)
86 (*self)->bucket->freefn ((*self)->user);
87 (*self)->user = NULL;
89 tdelete (*self, &(*self)->bucket->bucket, (*self)->bucket->cmpfn);
90 (*self)->bucket = NULL;
92 /* buckets counted as reference too */
93 (*self)->refcnt -= 4;
95 return *self;
97 else
99 /* make a new string */
100 (*self)->refcnt -= 4;
101 return *self = mala_string_new ((*self)->str, NULL);
106 MalaString
107 mala_stringbucket_insert (MalaStringBucket bucket, MalaString_ref s)
109 MalaString_ref loc;
111 /* no bucket */
112 if (!bucket)
113 return *s;
115 /* already in this bucket */
116 if (bucket && (*s)->bucket == bucket)
117 return *s;
119 /* in another bucket, remove it */
120 if ((*s)->bucket)
122 mala_stringbucket_string_remove (s);
125 /*create in bucket*/
126 mala_string tmp = MALA_STRING_AUTO ((*s)->str);
128 /*create in bucket*/
129 loc = (MalaString_ref) tsearch (&tmp, &bucket->bucket, bucket->cmpfn);
130 if (!loc)
131 return NULL;
133 if (*loc == &tmp)
135 /*not in bucket*/
136 (*s)->bucket = bucket;
137 *loc = *s;
139 else
141 /*string already in bucket*/
142 mala_string_free (*s);
143 *s = *loc;
145 (*s)->refcnt += 4;
146 return *s;
152 // Local Variables:
153 // mode: C
154 // c-file-style: "gnu"
155 // End:
156 // arch-tag: 84fbd8ab-4e90-4d04-ab70-d9b955dde41c
157 // end_of_file