added more type-checking
[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 } mem_t;
39
40 /* fifo buffered growing mem region */
41 typedef struct _ringbuffer_t
42 {
43     void*internal;
44     int available;
45 } ringbuffer_t;
46
47 /* non-nul terminated string */
48 typedef struct _string_t {
49     const char*str;
50     int len;
51 } string_t;
52
53 /* key/value pairs of strings */
54 typedef struct _map_t {
55     void*internal;
56 } map_t;
57
58 /* type information */
59 typedef char (*equals_func)(const void*o1, const void*o2);
60 typedef unsigned int (*hash_func)(const void*o);
61 typedef void* (*dup_func)(const void*o);
62 typedef void (*free_func)(void*o);
63
64 typedef struct _type_t {
65     equals_func equals;
66     hash_func hash;
67     dup_func dup;
68     free_func free;
69 } type_t;
70
71 extern type_t charptr_type;
72 extern type_t stringstruct_type;
73
74 typedef struct _dictentry {
75     void*key;
76     unsigned int hash;
77     void*data;
78     struct _dictentry*next;
79 } dictentry_t;
80
81 /* (void*) pointers referenced by strings */
82 typedef struct _dict {
83     dictentry_t**slots;
84     type_t*key_type;
85     int hashsize;
86     int num;
87 } dict_t;
88
89 /* array of key/value pairs, with fast lookup */
90 typedef struct _array_entry {
91     void*name;
92     void*data;
93 } array_entry_t;
94
95 typedef struct _array {
96     int num;
97     int size;
98     array_entry_t*d;
99     dict_t*entry2pos;
100 } array_t;
101
102 /* array of strings, string<->int mapping,
103    with O(1) for int->string lookup and
104         ~O(n/hashsize) for string->int lookup */
105 typedef struct _stringarray_t
106 {
107     void*internal;
108 } stringarray_t;
109
110 /* heap */
111 typedef struct _heap
112 {
113     void**elements;
114     char*data;
115     int elem_size;
116     int size;
117     int max_size;
118     int(*compare)(const void *, const void *);
119 } heap_t;
120
121 char* strdup_n(const char*str, int size);
122
123 unsigned int crc32_add_byte(unsigned int crc32, unsigned char b);
124 unsigned int crc32_add_string(unsigned int crc32, const char*s);
125
126 void mem_init(mem_t*mem);
127 int mem_put(mem_t*m, void*data, int length);
128 int mem_putstring(mem_t*m, string_t str);
129 void mem_clear(mem_t*mem);
130 void mem_destroy(mem_t*mem);
131
132 void ringbuffer_init(ringbuffer_t*r);
133 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
134 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
135 void ringbuffer_clear(ringbuffer_t*r);
136
137 string_t string_new(const char*text, int len);
138 string_t string_new2(const char*text);
139 unsigned int string_hash(const string_t*str);
140 unsigned int string_hash2(const char*str);
141 unsigned int string_hash3(const char*str, int len);
142 void string_set(string_t*str, const char*text);
143 void string_set2(string_t*str, const char*text, int len);
144 void string_dup(string_t*str, const char*text);
145 void string_dup2(string_t*str, const char*text, int len);
146 int string_equals(string_t*str, const char*text);
147
148 void stringarray_init(stringarray_t*sa, int hashsize);
149 void stringarray_put(stringarray_t*sa, string_t str);
150
151 char* stringarray_at(stringarray_t*sa, int pos);
152 string_t stringarray_at2(stringarray_t*sa, int pos);
153 int stringarray_find(stringarray_t*sa, string_t*str);
154 void stringarray_clear(stringarray_t*sa);
155 void stringarray_destroy(stringarray_t*sa);
156
157 dict_t*dict_new();
158 dict_t*dict_new2(type_t*type);
159 void dict_init(dict_t*dict);
160 dictentry_t*dict_put(dict_t*h, const void*key, void* data);
161 int dict_count(dict_t*h);
162 void dict_dump(dict_t*h, FILE*fi, const char*prefix);
163 void* dict_lookup(dict_t*h, const void*s);
164 char dict_del(dict_t*h, const void*s);
165
166 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data);
167 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
168 void dict_free_all(dict_t*h, void (*freeFunction)(void*));
169 void dict_clear(dict_t*h);
170 void dict_destroy(dict_t*dict);
171
172 void map_init(map_t*map);
173 void map_put(map_t*map, string_t t1, string_t t2);
174 const char* map_lookup(map_t*map, const char*name);
175 void map_dump(map_t*map, FILE*fi, const char*prefix);
176 void map_clear(map_t*map);
177 void map_destroy(map_t*map);
178
179 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *));
180 void heap_clear(heap_t*h);
181 void heap_put(heap_t*h, void*e);
182 int heap_size(heap_t*h);
183 void* heap_max(heap_t*h);
184 void* heap_chopmax(heap_t*h);
185 void heap_dump(heap_t*h, FILE*fi);
186 void** heap_flatten(heap_t*h);
187
188 array_t* array_new();
189 array_t* array_new2(type_t*type);
190 void array_free(array_t*array);
191 void*array_getkey(array_t*array, int nr);
192 void*array_getvalue(array_t*array, int nr);
193 int array_append(array_t*array, const void*name, void*data);
194 int array_find(array_t*array, const void*name);
195 int array_find2(array_t*array, const void*name, void*data);
196 int array_update(array_t*array, const void*name, void*data);
197 int array_append_if_new(array_t*array, const void*name, void*data);
198
199 #define DECLARE(x) struct _##x;typedef struct _##x x##_t;
200 #define DECLARE_LIST(x) \
201 struct _##x##_list { \
202     struct _##x* x; \
203     struct _##x##_list*next; \
204 }; \
205 typedef struct _##x##_list x##_list_t;
206 int list_length_(void*_list);
207 void*list_clone_(void*_list);
208 void list_append_(void*_list, void*entry);
209 void list_free_(void*_list);
210 #define list_new() ((void*)0)
211 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
212 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
213 #define list_clone(list) (sizeof((list)->next),list_clone_(&(list)))
214 #define list_length(list) (sizeof((list)->next),list_length_(list))
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif //__q_h__