4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
31 #define NEW(t,y) t*y = (t*)rfx_calloc(sizeof(t));
33 /* dynamically growing mem section */
34 typedef struct _mem_t {
41 /* fifo buffered growing mem region */
42 typedef struct _ringbuffer_t
48 /* non-nul terminated string */
49 typedef struct _string_t {
54 /* key/value pairs of strings */
55 typedef struct _map_t {
59 /* type information */
60 typedef char (*equals_func)(const void*o1, const void*o2);
61 typedef unsigned int (*hash_func)(const void*o);
62 typedef void* (*dup_func)(const void*o);
63 typedef void (*free_func)(void*o);
65 typedef struct _type_t {
72 extern type_t charptr_type;
73 extern type_t stringstruct_type;
74 extern type_t ptr_type;
76 typedef struct _dictentry {
80 struct _dictentry*next;
83 /* (void*) pointers referenced by strings */
84 typedef struct _dict {
91 /* array of key/value pairs, with fast lookup */
92 typedef struct _array_entry {
97 typedef struct _array {
104 /* array of strings, string<->int mapping,
105 with O(1) for int->string lookup and
106 ~O(n/hashsize) for string->int lookup */
107 typedef struct _stringarray_t
120 int(*compare)(const void *, const void *);
123 typedef struct _trie {
124 struct _trie*row[256];
129 char* strdup_n(const char*str, int size);
131 unsigned int crc32_add_byte(unsigned int crc32, unsigned char b);
132 unsigned int crc32_add_string(unsigned int crc32, const char*s);
134 void mem_init(mem_t*mem);
135 int mem_put(mem_t*m, void*data, int length);
136 int mem_putstring(mem_t*m, string_t str);
137 int mem_get(mem_t*m, void*data, int length);
138 void mem_clear(mem_t*mem);
139 void mem_destroy(mem_t*mem);
141 void ringbuffer_init(ringbuffer_t*r);
142 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
143 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
144 void ringbuffer_clear(ringbuffer_t*r);
146 /* old style functions- should be renamed */
147 string_t string_new(const char*text, int len);
148 string_t string_new2(const char*text);
149 void string_dup(string_t*str, const char*text);
150 void string_dup2(string_t*str, const char*text, int len);
152 char* string_cstr(string_t*str);
153 char* string_escape(string_t*str);
154 string_t* string_new3(const char*text, int len);
155 string_t* string_new4(const char*text);
156 void string_free(string_t*s);
157 unsigned int string_hash(const string_t*str);
158 unsigned int string_hash2(const char*str);
159 unsigned int string_hash3(const char*str, int len);
160 void string_set(string_t*str, const char*text);
161 void string_set2(string_t*str, const char*text, int len);
162 string_t*string_dup3(string_t*s);
163 int string_equals(string_t*str, const char*text);
165 void stringarray_init(stringarray_t*sa, int hashsize);
166 void stringarray_put(stringarray_t*sa, string_t str);
168 char* stringarray_at(stringarray_t*sa, int pos);
169 string_t stringarray_at2(stringarray_t*sa, int pos);
170 int stringarray_find(stringarray_t*sa, string_t*str);
171 void stringarray_clear(stringarray_t*sa);
172 void stringarray_destroy(stringarray_t*sa);
175 dict_t*dict_new2(type_t*type);
176 void dict_init(dict_t*dict, int size);
177 dictentry_t*dict_put(dict_t*h, const void*key, void* data);
178 void dict_put2(dict_t*h, const char*s, void*data);
179 int dict_count(dict_t*h);
180 void dict_dump(dict_t*h, FILE*fi, const char*prefix);
181 dictentry_t* dict_get_slot(dict_t*h, const void*key);
182 char dict_contains(dict_t*h, const void*s);
183 void* dict_lookup(dict_t*h, const void*s);
184 char dict_del(dict_t*h, const void*s);
185 dict_t*dict_clone(dict_t*);
187 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data);
188 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
189 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*));
190 void dict_clear(dict_t*h);
191 void dict_destroy_shallow(dict_t*dict);
192 void dict_destroy(dict_t*dict);
194 void map_init(map_t*map);
195 void map_put(map_t*map, string_t t1, string_t t2);
196 const char* map_lookup(map_t*map, const char*name);
197 void map_dump(map_t*map, FILE*fi, const char*prefix);
198 void map_clear(map_t*map);
199 void map_destroy(map_t*map);
201 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *));
202 void heap_clear(heap_t*h);
203 void heap_put(heap_t*h, void*e);
204 int heap_size(heap_t*h);
205 void* heap_max(heap_t*h);
206 void* heap_chopmax(heap_t*h);
207 void heap_dump(heap_t*h, FILE*fi);
208 void** heap_flatten(heap_t*h);
210 void trie_put(trie_t**t, unsigned const char*id);
211 int trie_lookup(trie_t*t, unsigned const char*id);
213 array_t* array_new();
214 array_t* array_new2(type_t*type);
215 void array_free(array_t*array);
216 void*array_getkey(array_t*array, int nr);
217 void*array_getvalue(array_t*array, int nr);
218 int array_append(array_t*array, const void*name, void*data);
219 #define array_contains(a,b) (array_find((a),(b))>=0)
220 int array_find(array_t*array, const void*name);
221 int array_find2(array_t*array, const void*name, void*data);
222 int array_update(array_t*array, const void*name, void*data);
223 int array_append_if_new(array_t*array, const void*name, void*data);
224 #define array_length(a) ((a)->num)
226 #define DECLARE(x) struct _##x;typedef struct _##x x##_t;
227 #define DECLARE_LIST(x) \
228 struct _##x##_list { \
230 struct _##x##_list*next; \
232 typedef struct _##x##_list x##_list_t;
233 int list_length_(void*_list);
234 void*list_clone_(void*_list);
235 void list_append_(void*_list, void*entry);
236 void list_prepend_(void*_list, void*entry);
237 void list_free_(void*_list);
238 void list_deep_free_(void*_list);
239 void list_concat_(void*l1, void*l2);
240 #define list_new() ((void*)0)
241 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
242 #define list_concat(l1, l2) {sizeof((l1)->next);sizeof((l2)->next);list_concat_(&(l1),&(l2));}
243 #define list_prepend(list, e) {sizeof((list)->next);list_prepend_(&(list),(e));}
244 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
245 #define list_deep_free(list) {sizeof((list)->next);list_deep_free_(&(list));}
246 #define list_clone(list) (sizeof((list)->next),list_clone_(&(list)))
247 #define list_length(list) (sizeof((list)->next),list_length_(list))