3 Part of the swftools package.
5 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7 This file is distributed under the GPL, see file COPYING for details */
14 // ------------------------------- malloc, alloc routines ---------------------
17 char* strndup(const char*str, int size)
19 char*m = (char*)malloc(size+1);
25 void* qmalloc_internal(int len)
27 void*val = malloc(len);
29 printf("memory error! Couldn't reserve %d bytes\n", len);
30 fprintf(stderr, "memory error! Couldn't reserve %d bytes\n", len);
35 void* qrealloc_internal(void*old, int len)
37 void*val = realloc(old, len);
39 printf("memory error! Couldn't reserve %d bytes\n", len);
40 fprintf(stderr, "memory error! Couldn't reserve %d bytes\n", len);
45 void qfree_internal(void*old)
49 char*qstrdup(const char*string)
51 return strdup(string);
53 char*qstrndup(const char*string, int len)
55 return strndup(string, len);
58 // ------------------------------- mem_t --------------------------------------
60 void mem_init(mem_t*mem)
62 memset(mem, 0, sizeof(mem_t));
64 void mem_clear(mem_t*mem)
68 void mem_destroy(mem_t*mem)
73 static int mem_put_(mem_t*m,void*data, int length, int null)
76 m->pos += length + (null?1:0);
79 //m->len += 1024>length?1024:(null?length*2:length);
82 while(m->len < m->pos)
85 m->buffer = m->buffer?realloc(m->buffer,m->len):malloc(m->len);
87 memcpy(&m->buffer[n], data, length);
89 m->buffer[n + length] = 0;
92 int mem_put(mem_t*m,void*data, int length)
94 return mem_put_(m, data, length, 0);
96 int mem_putstring(mem_t*m,string_t str)
98 return mem_put_(m, str.str, str.len, 1);
101 // ------------------------------- string_t ------------------------------------
103 void string_set2(string_t*str, char*text, int len)
108 void string_set(string_t*str, char*text)
110 str->len = strlen(text);
113 void string_dup2(string_t*str, const char*text, int len)
116 str->str = strndup(text, len);
118 void string_dup(string_t*str, const char*text)
120 str->len = strlen(text);
121 str->str = strdup(text);
123 int string_equals(string_t*str, const char*text)
125 int l = strlen(text);
126 if(str->len == l && !strncmp(str->str, text, l))
130 int string_equals2(string_t*str, string_t*str2)
132 if(str->len == str2->len && !strncmp(str->str, str2->str, str->len))
136 char* string_cstr(string_t*str)
138 return strndup(str->str, str->len);
141 // ------------------------------- stringarray_t ------------------------------
143 typedef struct _stringarray_internal_t
148 } stringarray_internal_t;
149 void stringarray_init(stringarray_t*sa)
151 stringarray_internal_t*s;
152 sa->internal = (stringarray_internal_t*)malloc(sizeof(stringarray_internal_t));
153 memset(sa->internal, 0, sizeof(stringarray_internal_t));
154 s = (stringarray_internal_t*)sa->internal;
158 void stringarray_put(stringarray_t*sa, string_t str)
160 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
162 pos = mem_putstring(&s->data, str);
163 mem_put(&s->pos, &pos, sizeof(int));
166 char* stringarray_at(stringarray_t*sa, int pos)
168 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
170 if(pos<0 || pos>=s->num)
172 p = *(int*)&s->pos.buffer[pos*sizeof(int)];
175 return &s->data.buffer[p];
177 string_t stringarray_at2(stringarray_t*sa, int pos)
180 s.str = stringarray_at(sa, pos);
181 s.len = s.str?strlen(s.str):0;
184 void stringarray_del(stringarray_t*sa, int pos)
186 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
187 *(int*)&s->pos.buffer[pos*sizeof(int)] = -1;
189 int stringarray_find(stringarray_t*sa, string_t* str)
191 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
193 for(t=0;t<s->num;t++) {
194 string_t s = stringarray_at2(sa, t);
195 if(s.str && string_equals2(&s, str)) {
201 void stringarray_clear(stringarray_t*sa)
203 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
208 void stringarray_destroy(stringarray_t*sa)
210 stringarray_clear(sa);
215 // ------------------------------- map_t --------------------------------------
217 typedef struct _map_internal_t
220 stringarray_t values;
224 void map_init(map_t*map)
227 map->internal = (map_internal_t*)malloc(sizeof(map_internal_t));
228 memset(map->internal, 0, sizeof(map_internal_t));
229 m = (map_internal_t*)map->internal;
230 stringarray_init(&m->keys);
231 stringarray_init(&m->values);
233 void map_put(map_t*map, string_t t1, string_t t2)
235 map_internal_t*m = (map_internal_t*)map->internal;
236 stringarray_put(&m->keys, t1);
237 stringarray_put(&m->values, t2);
240 char* map_lookup(map_t*map, const char*name)
243 map_internal_t*m = (map_internal_t*)map->internal;
245 string_set(&str, (char*)name);
246 s = stringarray_find(&m->keys, &str);
248 string_t s2 = stringarray_at2(&m->values, s);
253 void map_dump(map_t*map, FILE*fi, const char*prefix)
256 map_internal_t*m = (map_internal_t*)map->internal;
257 for(t=0;t<m->num;t++) {
258 string_t s1 = stringarray_at2(&m->keys, t);
259 string_t s2 = stringarray_at2(&m->values, t);
260 fprintf(fi, "%s%s=%s\n", prefix, s1.str, s2.str);
263 void map_clear(map_t*map)
265 map_internal_t*m = (map_internal_t*)map->internal;
266 stringarray_clear(&m->keys);
267 stringarray_clear(&m->values);
270 void map_destroy(map_t*map)
276 // ------------------------------- dictionary_t --------------------------------------
278 typedef struct _dictionary_internal_t
283 } dictionary_internal_t;
285 void dictionary_init(dictionary_t*dict)
287 dictionary_internal_t*d;
288 dict->internal = (dictionary_internal_t*)malloc(sizeof(dictionary_internal_t));
289 memset(dict->internal, 0, sizeof(dictionary_internal_t));
290 d = (dictionary_internal_t*)dict->internal;
291 stringarray_init(&d->keys);
292 mem_init(&d->values);
294 void dictionary_put(dictionary_t*dict, string_t t1, void* t2)
296 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
298 s = stringarray_find(&d->keys, &t1);
301 *(void**)(&d->values.buffer[s*sizeof(void*)]) = t2;
303 stringarray_put(&d->keys, t1);
304 mem_put(&d->values, &t2, sizeof(void*));
308 void dictionary_put2(dictionary_t*dict, const char*t1, void* t2)
311 string_set(&s, (char*)t1);
312 dictionary_put(dict, s, t2);
314 void* dictionary_lookup(dictionary_t*dict, const char*name)
317 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
319 string_set(&str, (char*)name);
320 s = stringarray_find(&d->keys, &str);
322 return *(void**)&d->values.buffer[sizeof(void*)*s];
326 void dictionary_dump(dictionary_t*dict, FILE*fi, const char*prefix)
328 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
330 for(t=0;t<d->num;t++) {
331 string_t s1 = stringarray_at2(&d->keys, t);
332 fprintf(fi, "%s%s=%08x\n", prefix, s1.str, *(void**)&d->values.buffer[sizeof(void*)*t]);
335 void dictionary_del(dictionary_t*dict, const char* name)
337 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
340 string_set(&str, (char*)name);
341 s = stringarray_find(&d->keys, &str);
343 *(void**)(&d->values.buffer[s*sizeof(void*)]) = 0;
344 stringarray_del(&d->keys, s);
347 void dictionary_clear(dictionary_t*dict)
349 dictionary_internal_t*d = (dictionary_internal_t*)dict->internal;
350 stringarray_clear(&d->keys);
351 mem_clear(&d->values);
354 void dictionary_destroy(dictionary_t*dict)
356 dictionary_clear(dict);