merged with lib/as3/utils.{c,h}
[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*)malloc(sizeof(t));memset(y, 0, 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 typedef struct _dictentry {
59     const char*s;
60     int len;
61     unsigned int hash;
62     void*data;
63     struct _dictentry*next;
64 } dictentry_t;
65
66 /* (void*) pointers referenced by strings */
67 typedef struct _dict {
68     dictentry_t**slots;
69     int hashsize;
70     int num;
71 } dict_t;
72
73 /* array of key/value pairs, with fast lookup */
74 typedef struct _array_entry {
75     const char*name;
76     void*data;
77 } array_entry_t;
78
79 typedef struct _array {
80     int num;
81     int size;
82     array_entry_t*d;
83     dict_t*entry2pos;
84 } array_t;
85
86 /* array of strings, string<->int mapping,
87    with O(1) for int->string lookup and
88         ~O(n/hashsize) for string->int lookup */
89 typedef struct _stringarray_t
90 {
91     void*internal;
92 } stringarray_t;
93
94 /* heap */
95 typedef struct _heap
96 {
97     void**elements;
98     char*data;
99     int elem_size;
100     int size;
101     int max_size;
102     int(*compare)(const void *, const void *);
103 } heap_t;
104
105 char* strdup_n(const char*str, int size);
106
107 void mem_init(mem_t*mem);
108 int mem_put(mem_t*m, void*data, int length);
109 int mem_putstring(mem_t*m, string_t str);
110 void mem_clear(mem_t*mem);
111 void mem_destroy(mem_t*mem);
112
113 void ringbuffer_init(ringbuffer_t*r);
114 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
115 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
116 void ringbuffer_clear(ringbuffer_t*r);
117
118 string_t string_new(const char*text, int len);
119 string_t string_new2(const char*text);
120 unsigned int string_hash(string_t*str);
121 unsigned int string_hash2(const char*str);
122 unsigned int string_hash3(const char*str, int len);
123 void string_set(string_t*str, const char*text);
124 void string_set2(string_t*str, const char*text, int len);
125 void string_dup(string_t*str, const char*text);
126 void string_dup2(string_t*str, const char*text, int len);
127 int string_equals(string_t*str, const char*text);
128
129 void stringarray_init(stringarray_t*sa, int hashsize);
130 void stringarray_put(stringarray_t*sa, string_t str);
131
132 char* stringarray_at(stringarray_t*sa, int pos);
133 string_t stringarray_at2(stringarray_t*sa, int pos);
134 int stringarray_find(stringarray_t*sa, string_t*str);
135 void stringarray_clear(stringarray_t*sa);
136 void stringarray_destroy(stringarray_t*sa);
137
138 dict_t*dict_new();
139 void dict_init(dict_t*dict);
140 void dict_put(dict_t*dict, string_t t1, void* t2);
141 void dict_put2(dict_t*dict, const char* t1, void* t2);
142 void dict_put3(dict_t*dict, const char* t1, int len, void* t2);
143 int dict_count(dict_t*h);
144 void dict_dump(dict_t*h, FILE*fi, const char*prefix);
145 void* dict_lookup3(dict_t*h, const char*s, const void*data);
146 void* dict_lookup2(dict_t*h, const char*s, int len);
147 void* dict_lookup(dict_t*h, const char*s);
148 char dict_del(dict_t*h, const char*s);
149 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const char*key, void*val), void*data);
150 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
151 void dict_free_all(dict_t*h, void (*freeFunction)(void*));
152 void dict_clear(dict_t*h);
153 void dict_destroy(dict_t*dict);
154
155 void map_init(map_t*map);
156 void map_put(map_t*map, string_t t1, string_t t2);
157 const char* map_lookup(map_t*map, const char*name);
158 void map_dump(map_t*map, FILE*fi, const char*prefix);
159 void map_clear(map_t*map);
160 void map_destroy(map_t*map);
161
162 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *));
163 void heap_clear(heap_t*h);
164 void heap_put(heap_t*h, void*e);
165 int heap_size(heap_t*h);
166 void* heap_max(heap_t*h);
167 void* heap_chopmax(heap_t*h);
168 void heap_dump(heap_t*h, FILE*fi);
169 void** heap_flatten(heap_t*h);
170
171 array_t* array_new();
172 void array_free(array_t*array);
173 const char*array_getkey(array_t*array, int nr);
174 char*array_getvalue(array_t*array, int nr);
175 int array_append(array_t*array, const char*name, const void*data);
176 int array_find(array_t*array, const char*name);
177 int array_find2(array_t*array, const char*name, void*data);
178 int array_update(array_t*array, const char*name, void*data);
179 int array_append_if_new(array_t*array, const char*name, void*data);
180
181 #define DECLARE(x) struct _##x;typedef struct _##x x##_t;
182 #define DECLARE_LIST(x) \
183 struct _##x##_list { \
184     struct _##x* x; \
185     struct _##x##_list*next; \
186 }; \
187 typedef struct _##x##_list x##_list_t;
188 int list_length(void*_list);
189 void list_append_(void*_list, void*entry);
190 void list_free_(void*_list);
191 #define list_new() ((void*)0)
192 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
193 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
194
195 #ifdef __cplusplus
196 }
197 #endif
198
199 #endif //__q_h__