small fixes
[swftools.git] / lib / q.h
1 /* q.h
2    Header file for q.c.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #ifndef __q_h__
23 #define __q_h__
24
25 #include <stdio.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #define NEW(t,y) t*y = (t*)rfx_calloc(sizeof(t));
32
33 /* dynamically growing mem section */
34 typedef struct _mem_t {
35     char*buffer;
36     int len;
37     int pos;
38     int read_pos;
39 } mem_t;
40
41 /* fifo buffered growing mem region */
42 typedef struct _ringbuffer_t
43 {
44     void*internal;
45     int available;
46 } ringbuffer_t;
47
48 /* non-nul terminated string */
49 typedef struct _string_t {
50     const char*str;
51     int len;
52 } string_t;
53
54 /* key/value pairs of strings */
55 typedef struct _map_t {
56     void*internal;
57 } map_t;
58
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);
64
65 typedef struct _type_t {
66     equals_func equals;
67     hash_func hash;
68     dup_func dup;
69     free_func free;
70 } type_t;
71
72 extern type_t charptr_type;
73 extern type_t stringstruct_type;
74 extern type_t ptr_type;
75
76 typedef struct _dictentry {
77     void*key;
78     unsigned int hash;
79     void*data;
80     struct _dictentry*next;
81 } dictentry_t;
82
83 /* (void*) pointers referenced by strings */
84 typedef struct _dict {
85     dictentry_t**slots;
86     type_t*key_type;
87     int hashsize;
88     int num;
89 } dict_t;
90
91 /* array of key/value pairs, with fast lookup */
92 typedef struct _array_entry {
93     void*name;
94     void*data;
95 } array_entry_t;
96
97 typedef struct _array {
98     int num;
99     int size;
100     array_entry_t*d;
101     dict_t*entry2pos;
102 } array_t;
103
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
108 {
109     void*internal;
110 } stringarray_t;
111
112 /* heap */
113 typedef struct _heap {
114     void**elements;
115     char*data;
116     int elem_size;
117     int size;
118     int max_size;
119     int(*compare)(const void *, const void *);
120 } heap_t;
121
122 /* trie (with rollback) */
123 typedef struct _trielayer {
124     struct _trielayer*row[256];
125     unsigned char*rest;
126     void*data;
127 } trielayer_t;
128
129 typedef struct _trie {
130     trielayer_t* start;
131     void*rollback;
132 } trie_t;
133
134 char* strdup_n(const char*str, int size);
135
136 unsigned int crc32_add_byte(unsigned int crc32, unsigned char b);
137 unsigned int crc32_add_string(unsigned int crc32, const char*s);
138
139 void mem_init(mem_t*mem);
140 int mem_put(mem_t*m, void*data, int length);
141 int mem_putstring(mem_t*m, string_t str);
142 int mem_get(mem_t*m, void*data, int length);
143 void mem_clear(mem_t*mem);
144 void mem_destroy(mem_t*mem);
145
146 void ringbuffer_init(ringbuffer_t*r);
147 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
148 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
149 void ringbuffer_clear(ringbuffer_t*r);
150
151 /* old style functions- should be renamed */
152 string_t string_new(const char*text, int len);
153 string_t string_new2(const char*text);
154 void string_dup(string_t*str, const char*text);
155 void string_dup2(string_t*str, const char*text, int len);
156
157 char* string_cstr(string_t*str);
158 char* string_escape(string_t*str);
159 string_t* string_new3(const char*text, int len);
160 string_t* string_new4(const char*text);
161 void string_free(string_t*s);
162 unsigned int string_hash(const string_t*str);
163 unsigned int string_hash2(const char*str);
164 unsigned int string_hash3(const char*str, int len);
165 void string_set(string_t*str, const char*text);
166 void string_set2(string_t*str, const char*text, int len);
167 string_t*string_dup3(string_t*s);
168 int string_equals(string_t*str, const char*text);
169
170 void stringarray_init(stringarray_t*sa, int hashsize);
171 void stringarray_put(stringarray_t*sa, string_t str);
172
173 char* stringarray_at(stringarray_t*sa, int pos);
174 string_t stringarray_at2(stringarray_t*sa, int pos);
175 int stringarray_find(stringarray_t*sa, string_t*str);
176 void stringarray_clear(stringarray_t*sa);
177 void stringarray_destroy(stringarray_t*sa);
178
179 dict_t*dict_new();
180 dict_t*dict_new2(type_t*type);
181 void dict_init(dict_t*dict, int size);
182 void dict_init2(dict_t*dict, type_t*type, int size);
183 dictentry_t*dict_put(dict_t*h, const void*key, void* data);
184 void dict_put2(dict_t*h, const char*s, void*data);
185 int dict_count(dict_t*h);
186 void dict_dump(dict_t*h, FILE*fi, const char*prefix);
187 dictentry_t* dict_get_slot(dict_t*h, const void*key);
188 char dict_contains(dict_t*h, const void*s);
189 void* dict_lookup(dict_t*h, const void*s);
190 char dict_del(dict_t*h, const void*s);
191 dict_t*dict_clone(dict_t*);
192
193 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data);
194 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
195 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*));
196 void dict_clear(dict_t*h);
197 void dict_destroy_shallow(dict_t*dict);
198 void dict_destroy(dict_t*dict);
199 #define DICT_ITERATE_DATA(d,t,v) int v##_i;dictentry_t*v##_e;t v;for(v##_i=0;v##_i<(d)->hashsize;v##_i++) for(v##_e=(d)->slots[v##_i]; v##_e && ((v=(t)v##_e->data)||1); v##_e=v##_e->next)
200 #define DICT_ITERATE_KEY(d,t,v)  int v##_i;dictentry_t*v##_e;t v;for(v##_i=0;v##_i<(d)->hashsize;v##_i++) for(v##_e=(d)->slots[v##_i];v##_e && ((v=(t)v##_e->key)||1);v##_e=v##_e->next)
201 #define DICT_ITERATE_ITEMS(d,t1,v1,t2,v2) int v1##_i;dictentry_t*v1##_e;t1 v1;t2 v2;for(v1##_i=0;v1##_i<(d)->hashsize;v1##_i++) for(v1##_e=(d)->slots[v1##_i]; v1##_e && (((v1=(t1)v1##_e->key)&&(v2=(t2)v1##_e->data))||1); v1##_e=v1##_e->next)
202
203 void map_init(map_t*map);
204 void map_put(map_t*map, string_t t1, string_t t2);
205 const char* map_lookup(map_t*map, const char*name);
206 void map_dump(map_t*map, FILE*fi, const char*prefix);
207 void map_clear(map_t*map);
208 void map_destroy(map_t*map);
209
210 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *));
211 void heap_clear(heap_t*h);
212 void heap_put(heap_t*h, void*e);
213 int heap_size(heap_t*h);
214 void* heap_max(heap_t*h);
215 void* heap_chopmax(heap_t*h);
216 void heap_dump(heap_t*h, FILE*fi);
217 void** heap_flatten(heap_t*h);
218
219 trie_t*trie_new();
220 void trie_put(trie_t*t, unsigned const char*id, void*data);
221 char trie_remove(trie_t*t, unsigned const char*id);
222 void*trie_lookup(trie_t*t, unsigned const char*id);
223 int trie_contains(trie_t*t, unsigned const char*id);
224 void trie_remember(trie_t*t);
225 void trie_rollback(trie_t*t);
226 void trie_dump(trie_t*t);
227
228 array_t* array_new();
229 array_t* array_new2(type_t*type);
230 void array_free(array_t*array);
231 void*array_getkey(array_t*array, int nr);
232 void*array_getvalue(array_t*array, int nr);
233 int array_append(array_t*array, const void*name, void*data);
234 #define array_contains(a,b) (array_find((a),(b))>=0)
235 int array_find(array_t*array, const void*name);
236 int array_find2(array_t*array, const void*name, void*data);
237 int array_update(array_t*array, const void*name, void*data);
238 int array_append_if_new(array_t*array, const void*name, void*data);
239 #define array_length(a) ((a)->num)
240
241 #define DECLARE(x) struct _##x;typedef struct _##x x##_t;
242 #define DECLARE_LIST(x) \
243 struct _##x##_list { \
244     struct _##x* x; \
245     struct _##x##_list*next; \
246 }; \
247 typedef struct _##x##_list x##_list_t;
248 int list_length_(void*_list);
249 void*list_clone_(void*_list);
250 void list_append_(void*_list, void*entry);
251 void list_prepend_(void*_list, void*entry);
252 void list_free_(void*_list);
253 void list_deep_free_(void*_list);
254 void list_concat_(void*l1, void*l2);
255 #define list_new() ((void*)0)
256 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
257 #define list_concat(l1, l2) {sizeof((l1)->next);sizeof((l2)->next);list_concat_(&(l1),&(l2));}
258 #define list_prepend(list, e) {sizeof((list)->next);list_prepend_(&(list),(e));}
259 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
260 #define list_deep_free(list) {sizeof((list)->next);list_deep_free_(&(list));}
261 #define list_clone(list) (sizeof((list)->next),(list?list_clone_(&(list)):0))
262 #define list_length(list) (sizeof((list)->next),list_length_(list))
263
264 #ifdef __cplusplus
265 }
266 #endif
267
268 #endif //__q_h__