3 Part of the swftools package.
5 Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
28 // ------------------------------- malloc, alloc routines ---------------------
31 char* strdup_n(const char*str, int size)
33 char*m = (char*)malloc(size+1);
39 char*qstrdup(const char*string)
41 return strdup(string);
43 char*qstrndup(const char*string, int len)
45 return strdup_n(string, len);
48 // ------------------------------- mem_t --------------------------------------
50 void mem_init(mem_t*mem)
52 memset(mem, 0, sizeof(mem_t));
54 void mem_clear(mem_t*mem)
56 free(mem->buffer);mem->buffer = 0;
58 void mem_destroy(mem_t*mem)
63 static int mem_put_(mem_t*m,void*data, int length, int null)
66 m->pos += length + (null?1:0);
69 //m->len += 1024>length?1024:(null?length*2:length);
72 while(m->len < m->pos)
75 m->buffer = m->buffer?(char*)realloc(m->buffer,m->len):(char*)malloc(m->len);
77 memcpy(&m->buffer[n], data, length);
79 m->buffer[n + length] = 0;
82 int mem_put(mem_t*m,void*data, int length)
84 return mem_put_(m, data, length, 0);
86 int mem_putstring(mem_t*m,string_t str)
88 return mem_put_(m, str.str, str.len, 1);
91 // ------------------------------- ringbuffer_t -------------------------------
93 typedef struct _ringbuffer_internal_t
99 } ringbuffer_internal_t;
101 void ringbuffer_init(ringbuffer_t*r)
103 ringbuffer_internal_t*i = (ringbuffer_internal_t*)malloc(sizeof(ringbuffer_internal_t));
104 memset(r, 0, sizeof(ringbuffer_t));
105 memset(i, 0, sizeof(ringbuffer_internal_t));
107 i->buffer = (unsigned char*)malloc(1024);
108 i->buffersize = 1024;
110 int ringbuffer_read(ringbuffer_t*r, void*buf, int len)
112 unsigned char* data = (unsigned char*)buf;
113 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
114 if(r->available < len)
118 if(i->readpos + len > i->buffersize) {
119 int read1 = i->buffersize-i->readpos;
120 memcpy(data, &i->buffer[i->readpos], read1);
121 memcpy(&data[read1], &i->buffer[0], len - read1);
122 i->readpos = len - read1;
124 memcpy(data, &i->buffer[i->readpos], len);
126 i->readpos %= i->buffersize;
131 void ringbuffer_put(ringbuffer_t*r, void*buf, int len)
133 unsigned char* data = (unsigned char*)buf;
134 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
136 if(i->buffersize - r->available < len)
139 int newbuffersize = i->buffersize;
140 int oldavailable = r->available;
141 newbuffersize*=3;newbuffersize/=2; /*grow at least by 50% each time */
143 if(newbuffersize < r->available + len)
144 newbuffersize = r->available + len + 1024;
146 buf2 = (unsigned char*)malloc(newbuffersize);
147 ringbuffer_read(r, buf2, r->available);
150 i->buffersize = newbuffersize;
152 i->writepos = oldavailable;
153 r->available = oldavailable;
155 if(i->writepos + len > i->buffersize) {
156 int read1 = i->buffersize-i->writepos;
157 memcpy(&i->buffer[i->writepos], data, read1);
158 memcpy(&i->buffer[0], &data[read1], len - read1);
159 i->writepos = len - read1;
161 memcpy(&i->buffer[i->writepos], data, len);
163 i->writepos %= i->buffersize;
167 void ringbuffer_clear(ringbuffer_t*r)
169 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
170 free(i->buffer);i->buffer = 0;
174 // ------------------------------- string_t -----------------------------------
176 void string_set2(string_t*str, char*text, int len)
181 void string_set(string_t*str, char*text)
183 str->len = strlen(text);
186 void string_dup2(string_t*str, const char*text, int len)
189 str->str = strdup_n(text, len);
191 void string_dup(string_t*str, const char*text)
193 str->len = strlen(text);
194 str->str = strdup(text);
196 int string_equals(string_t*str, const char*text)
198 int l = strlen(text);
199 if(str->len == l && !strncmp(str->str, text, l))
203 int string_equals2(string_t*str, string_t*str2)
205 if(str->len == str2->len && !strncmp(str->str, str2->str, str->len))
209 char* string_cstr(string_t*str)
211 return strdup_n(str->str, str->len);
214 // ------------------------------- stringarray_t ------------------------------
216 typedef struct _stringarray_internal_t
221 } stringarray_internal_t;
222 void stringarray_init(stringarray_t*sa)
224 stringarray_internal_t*s;
225 sa->internal = (stringarray_internal_t*)malloc(sizeof(stringarray_internal_t));
226 memset(sa->internal, 0, sizeof(stringarray_internal_t));
227 s = (stringarray_internal_t*)sa->internal;
231 void stringarray_put(stringarray_t*sa, string_t str)
233 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
235 pos = mem_putstring(&s->data, str);
236 mem_put(&s->pos, &pos, sizeof(int));
239 char* stringarray_at(stringarray_t*sa, int pos)
241 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
243 if(pos<0 || pos>=s->num)
245 p = *(int*)&s->pos.buffer[pos*sizeof(int)];
248 return &s->data.buffer[p];
250 string_t stringarray_at2(stringarray_t*sa, int pos)
253 s.str = stringarray_at(sa, pos);
254 s.len = s.str?strlen(s.str):0;
257 void stringarray_del(stringarray_t*sa, int pos)
259 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
260 *(int*)&s->pos.buffer[pos*sizeof(int)] = -1;
262 int stringarray_find(stringarray_t*sa, string_t* str)
264 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
266 for(t=0;t<s->num;t++) {
267 string_t s = stringarray_at2(sa, t);
268 if(s.str && string_equals2(&s, str)) {
274 void stringarray_clear(stringarray_t*sa)
276 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
281 void stringarray_destroy(stringarray_t*sa)
283 stringarray_clear(sa);
288 // ------------------------------- map_t --------------------------------------
290 typedef struct _map_internal_t
293 stringarray_t values;
297 void map_init(map_t*map)
300 map->internal = (map_internal_t*)malloc(sizeof(map_internal_t));
301 memset(map->internal, 0, sizeof(map_internal_t));
302 m = (map_internal_t*)map->internal;
303 stringarray_init(&m->keys);
304 stringarray_init(&m->values);
306 void map_put(map_t*map, string_t t1, string_t t2)
308 map_internal_t*m = (map_internal_t*)map->internal;
309 stringarray_put(&m->keys, t1);
310 stringarray_put(&m->values, t2);
313 char* map_lookup(map_t*map, const char*name)
316 map_internal_t*m = (map_internal_t*)map->internal;
318 string_set(&str, (char*)name);
319 s = stringarray_find(&m->keys, &str);
321 string_t s2 = stringarray_at2(&m->values, s);
326 void map_dump(map_t*map, FILE*fi, const char*prefix)
329 map_internal_t*m = (map_internal_t*)map->internal;
330 for(t=0;t<m->num;t++) {
331 string_t s1 = stringarray_at2(&m->keys, t);
332 string_t s2 = stringarray_at2(&m->values, t);
333 fprintf(fi, "%s%s=%s\n", prefix, s1.str, s2.str);
336 void map_clear(map_t*map)
338 map_internal_t*m = (map_internal_t*)map->internal;
339 stringarray_clear(&m->keys);
340 stringarray_clear(&m->values);
343 void map_destroy(map_t*map)
349 // ------------------------------- dictionary_t -------------------------------
351 typedef struct _dictionary_internal_t
356 } dictionary_internal_t;
358 void dictionary_init(dictionary_t*dict)
360 dictionary_internal_t*d;
361 dict->internal = (dictionary_internal_t*)malloc(sizeof(dictionary_internal_t));
362 memset(dict->internal, 0, sizeof(dictionary_internal_t));
363 d = (dictionary_internal_t*)dict->internal;
364 stringarray_init(&d->keys);
365 mem_init(&d->values);
367 void dictionary_put(dictionary_t*dict, string_t t1, void* t2)
369 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
371 s = stringarray_find(&d->keys, &t1);
374 *(void**)(&d->values.buffer[s*sizeof(void*)]) = t2;
376 stringarray_put(&d->keys, t1);
377 mem_put(&d->values, &t2, sizeof(void*));
381 void dictionary_put2(dictionary_t*dict, const char*t1, void* t2)
384 string_set(&s, (char*)t1);
385 dictionary_put(dict, s, t2);
387 void* dictionary_lookup(dictionary_t*dict, const char*name)
390 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
392 string_set(&str, (char*)name);
393 s = stringarray_find(&d->keys, &str);
395 return *(void**)&d->values.buffer[sizeof(void*)*s];
399 void dictionary_dump(dictionary_t*dict, FILE*fi, const char*prefix)
401 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
403 for(t=0;t<d->num;t++) {
404 string_t s1 = stringarray_at2(&d->keys, t);
405 fprintf(fi, "%s%s=%08x\n", prefix, s1.str, *(void**)&d->values.buffer[sizeof(void*)*t]);
408 void dictionary_del(dictionary_t*dict, const char* name)
410 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
413 string_set(&str, (char*)name);
414 s = stringarray_find(&d->keys, &str);
416 *(void**)(&d->values.buffer[s*sizeof(void*)]) = 0;
417 stringarray_del(&d->keys, s);
420 void dictionary_clear(dictionary_t*dict)
422 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
423 stringarray_clear(&d->keys);
424 mem_clear(&d->values);
427 void dictionary_destroy(dictionary_t*dict)
429 dictionary_clear(dict);
433 // ------------------------------- heap_t -------------------------------
435 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *))
437 memset(h, 0, sizeof(heap_t));
440 h->elem_size = elem_size;
441 h->compare = compare;
442 h->elements = (void**)malloc(n*sizeof(void*));memset(h->elements, 0, n*sizeof(void*));
443 h->data = (char*)malloc(h->max_size*h->elem_size);memset(h->data, 0, h->max_size*h->elem_size);
445 void heap_clear(heap_t*h)
451 #define HEAP_NODE_SMALLER(h,node1,node2) ((h)->compare((node1),(node2))>0)
453 static void up(heap_t*h, int node)
455 void*node_p = h->elements[node];
461 h->elements[node] = h->elements[parent];
462 } while(HEAP_NODE_SMALLER(h,h->elements[parent], node_p));
464 h->elements[node] = node_p;
466 static void down(heap_t*h, int node)
468 void*node_p = h->elements[node];
473 /* determine new child's position */
477 if(child+1 < h->size && HEAP_NODE_SMALLER(h,h->elements[child],h->elements[child+1])) // search for bigger child
480 h->elements[node] = h->elements[child];
481 } while(HEAP_NODE_SMALLER(h,node_p, h->elements[child]));
483 h->elements[node] = node_p;
485 void heap_put(heap_t*h, void*e)
488 memcpy(&h->data[pos*h->elem_size],e,h->elem_size);
489 h->elements[pos] = &h->data[pos];
492 int heap_size(heap_t*h)
496 void* heap_max(heap_t*h)
498 return h->elements[0];
500 void* heap_chopmax(heap_t*h)
502 void*p = h->elements[0];
503 h->elements[0] = h->elements[--h->size];
507 void heap_dump(heap_t*h, FILE*fi)
510 for(t=0;t<h->size;t++) {
512 for(s=0;s<=t;s=(s+1)*2-1) {
513 if(s==t) fprintf(fi,"\n");
515 //fprintf(fi,"%d ", h->elements[t]->x); //?
518 void** heap_flatten(heap_t*h)
520 void**nodes = (void**)malloc(h->size*sizeof(void*));
524 /*printf("Heap Size: %d\n", h->size);
525 heap_print(stdout, h);
527 *p++ = heap_chopmax(h);