3 Part of the swftools package.
5 Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
7 This program is rfx_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 rfx_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 rfx_free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
31 // ------------------------------- malloc, alloc routines ---------------------
34 char* strdup_n(const char*str, int size)
36 char*m = (char*)rfx_alloc(size+1);
42 char*qstrdup(const char*string)
44 return strdup(string);
46 char*qstrndup(const char*string, int len)
48 return strdup_n(string, len);
51 // ------------------------------- mem_t --------------------------------------
53 void mem_init(mem_t*mem)
55 memset(mem, 0, sizeof(mem_t));
57 void mem_clear(mem_t*mem)
59 rfx_free(mem->buffer);mem->buffer = 0;
61 void mem_destroy(mem_t*mem)
66 static int mem_put_(mem_t*m,const void*data, int length, int null)
69 m->pos += length + (null?1:0);
71 int v1 = (m->pos+63)&~63;
72 int v2 = m->len + m->len / 2;
74 m->buffer = m->buffer?(char*)rfx_realloc(m->buffer,m->len):(char*)rfx_alloc(m->len);
76 assert(n+length <= 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);
90 int mem_get(mem_t*m, void*data, int length)
92 if(m->read_pos + length > m->pos) {
93 length = m->pos - m->read_pos;
95 memcpy(data, m->buffer+m->read_pos, length);
96 m->read_pos += length;
100 // ------------------------------- ringbuffer_t -------------------------------
102 typedef struct _ringbuffer_internal_t
104 unsigned char*buffer;
108 } ringbuffer_internal_t;
110 void ringbuffer_init(ringbuffer_t*r)
112 ringbuffer_internal_t*i = (ringbuffer_internal_t*)rfx_calloc(sizeof(ringbuffer_internal_t));
113 memset(r, 0, sizeof(ringbuffer_t));
115 i->buffer = (unsigned char*)rfx_alloc(1024);
116 i->buffersize = 1024;
118 int ringbuffer_read(ringbuffer_t*r, void*buf, int len)
120 unsigned char* data = (unsigned char*)buf;
121 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
122 if(r->available < len)
126 if(i->readpos + len > i->buffersize) {
127 int read1 = i->buffersize-i->readpos;
128 memcpy(data, &i->buffer[i->readpos], read1);
129 memcpy(&data[read1], &i->buffer[0], len - read1);
130 i->readpos = len - read1;
132 memcpy(data, &i->buffer[i->readpos], len);
134 i->readpos %= i->buffersize;
139 void ringbuffer_put(ringbuffer_t*r, void*buf, int len)
141 unsigned char* data = (unsigned char*)buf;
142 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
144 if(i->buffersize - r->available < len)
147 int newbuffersize = i->buffersize;
148 int oldavailable = r->available;
149 newbuffersize*=3;newbuffersize/=2; /*grow at least by 50% each time */
151 if(newbuffersize < r->available + len)
152 newbuffersize = r->available + len + 1024;
154 buf2 = (unsigned char*)rfx_alloc(newbuffersize);
155 ringbuffer_read(r, buf2, r->available);
158 i->buffersize = newbuffersize;
160 i->writepos = oldavailable;
161 r->available = oldavailable;
163 if(i->writepos + len > i->buffersize) {
164 int read1 = i->buffersize-i->writepos;
165 memcpy(&i->buffer[i->writepos], data, read1);
166 memcpy(&i->buffer[0], &data[read1], len - read1);
167 i->writepos = len - read1;
169 memcpy(&i->buffer[i->writepos], data, len);
171 i->writepos %= i->buffersize;
175 void ringbuffer_clear(ringbuffer_t*r)
177 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
178 rfx_free(i->buffer);i->buffer = 0;
182 // ------------------------------- heap_t -------------------------------
184 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *))
186 memset(h, 0, sizeof(heap_t));
189 h->elem_size = elem_size;
190 h->compare = compare;
191 h->elements = (void**)rfx_calloc(n*sizeof(void*));
192 h->data = (char*)rfx_calloc(h->max_size*h->elem_size);
194 void heap_clear(heap_t*h)
196 rfx_free(h->elements);
200 #define HEAP_NODE_SMALLER(h,node1,node2) ((h)->compare((node1),(node2))>0)
202 static void up(heap_t*h, int node)
204 void*node_p = h->elements[node];
210 h->elements[node] = h->elements[parent];
211 } while(HEAP_NODE_SMALLER(h,h->elements[parent], node_p));
213 h->elements[node] = node_p;
215 static void down(heap_t*h, int node)
217 void*node_p = h->elements[node];
222 /* determine new child's position */
226 if(child+1 < h->size && HEAP_NODE_SMALLER(h,h->elements[child],h->elements[child+1])) // search for bigger child
229 h->elements[node] = h->elements[child];
230 } while(HEAP_NODE_SMALLER(h,node_p, h->elements[child]));
232 h->elements[node] = node_p;
234 void heap_put(heap_t*h, void*e)
237 memcpy(&h->data[pos*h->elem_size],e,h->elem_size);
238 h->elements[pos] = &h->data[pos];
241 int heap_size(heap_t*h)
245 void* heap_max(heap_t*h)
247 return h->elements[0];
249 void* heap_chopmax(heap_t*h)
251 void*p = h->elements[0];
252 h->elements[0] = h->elements[--h->size];
256 void heap_dump(heap_t*h, FILE*fi)
259 for(t=0;t<h->size;t++) {
261 for(s=0;s<=t;s=(s+1)*2-1) {
262 if(s==t) fprintf(fi,"\n");
264 //fprintf(fi,"%d ", h->elements[t]->x); //?
267 void** heap_flatten(heap_t*h)
269 void**nodes = (void**)rfx_alloc(h->size*sizeof(void*));
273 /*printf("Heap Size: %d\n", h->size);
274 heap_print(stdout, h);
276 *p++ = heap_chopmax(h);
281 // ------------------------------- trie --------------------------------------
285 return (trie_t*)rfx_calloc(sizeof(trie_t));
287 static char _trie_put(trielayer_t**t, unsigned const char*id, void*data)
290 (*t) = rfx_calloc(sizeof(trie_t));
291 (*t)->rest = (unsigned char*)strdup(id);
295 if((*t)->rest && (*t)->rest[0]) {
296 // make room: shift whatever's currently in here one node down
297 _trie_put(&(*t)->row[(*t)->rest[0]], (*t)->rest+1, (*t)->data);
301 return _trie_put(&(*t)->row[id[0]], id+1, data);
306 (*t)->rest = strdup("");
311 static char _trie_remove(trielayer_t*t, unsigned const char*id)
314 if(t->rest && !strcmp(t->rest, id)) {
326 static void trie_rollback_removes(trie_t*t, unsigned const char*id, void*data);
327 static void trie_rollback_adds(trie_t*t, unsigned const char*id, void*data);
329 void trie_put(trie_t*t, unsigned const char*id, void*data)
332 _trie_put(&t->start, id, data);
334 char contains = trie_contains(t, id);
335 void*olddata = contains?trie_lookup(t, id):0;
336 trie_rollback_removes(t, id, data);
337 _trie_put(&t->start, id, data);
339 trie_rollback_adds(t, id, olddata);
343 char trie_remove(trie_t*t, unsigned const char*id)
346 return _trie_remove(t->start, id);
348 void*olddata = trie_lookup(t, id);
349 char exists = _trie_remove(t->start, id);
351 trie_rollback_removes(t, id, olddata);
356 int trie_contains(trie_t*trie, unsigned const char*id)
358 trielayer_t*t = trie->start;
360 if(t->rest && !strcmp(t->rest, id))
368 void* trie_lookup(trie_t*trie, unsigned const char*id)
370 trielayer_t*t = trie->start;
372 if(t->rest && !strcmp(t->rest, id))
381 typedef struct _triememory {
382 const unsigned char*key;
384 struct _triememory*next;
387 typedef struct _trierollback {
390 struct _trierollback*prev;
393 static void trie_rollback_removes(trie_t*t, unsigned const char*id, void*data)
395 trierollback_t*rollback = (trierollback_t*)t->rollback;
396 triememory_t*m = (triememory_t*)rfx_calloc(sizeof(triememory_t));
399 m->next = rollback->add;
402 static void trie_rollback_adds(trie_t*t, unsigned const char*id, void*data)
404 trierollback_t*rollback = (trierollback_t*)t->rollback;
405 triememory_t*m = (triememory_t*)rfx_calloc(sizeof(triememory_t));
408 m->next = rollback->remove;
409 rollback->remove = m;
412 void trie_remember(trie_t*t)
414 trierollback_t*old = (trierollback_t*)t->rollback;
415 t->rollback = (trierollback_t*)rfx_calloc(sizeof(trierollback_t));
416 ((trierollback_t*)t->rollback)->prev = old;
419 void trie_rollback(trie_t*t)
421 trierollback_t*rollback = (trierollback_t*)t->rollback;
423 fprintf(stderr, "Internal error: can't roll back this trie any further\n");
426 t->rollback = ((trierollback_t*)t->rollback)->prev;
428 triememory_t*remove = rollback->remove;
430 triememory_t*next = remove->next;
431 if(!trie_remove(t, remove->key)) {
432 fprintf(stderr, "Internal error: can't delete key %s in trie during rollback\n", remove->key);
437 triememory_t*add = rollback->add;
439 triememory_t*next = add->next;
440 trie_put(t, add->key, add->data);
446 // ------------------------------- crc32 --------------------------------------
447 static unsigned int*crc32 = 0;
448 static void crc32_init(void)
453 crc32= (unsigned int*)rfx_alloc(sizeof(unsigned int)*256);
454 for(t=0; t<256; t++) {
457 for (s = 0; s < 8; s++) {
458 c = (0xedb88320L*(c&1)) ^ (c >> 1);
463 // ------------------------------- string_t -----------------------------------
465 void string_set2(string_t*str, const char*text, int len)
470 void string_set(string_t*str, const char*text)
473 str->len = strlen(text);
479 string_t string_new(const char*text, int len)
486 string_t string_new2(const char*text)
490 s.len = strlen(text);
497 string_t* string_new3(const char*text, int len)
500 string_t*s = malloc(sizeof(string_t));
505 string_t*s = malloc(sizeof(string_t)+len+1);
507 s->str = (const char*)(s+1);
508 memcpy((char*)s->str, text, len);
509 ((char*)s->str)[len]=0;
513 string_t* string_new4(const char*text)
515 int l = strlen(text);
516 return string_new3(text, l);
519 void string_free(string_t*s)
524 if((string_t*)(s->str) == s+1) {
528 rfx_free((char*)(s->str));
533 char* string_cstr(string_t*str)
535 return strdup_n(str->str, str->len);
537 char* string_escape(string_t*str)
541 for(t=0;t<str->len;t++) {
547 char*s = malloc(len+1);
549 for(t=0;t<str->len;t++) {
550 if(str->str[t]<0x20) {
552 unsigned char c = str->str[t];
553 *p++ = "0123456789abcdef"[c>>4];
554 *p++ = "0123456789abcdef"[c&0x0f];
560 assert(p == &s[len+1]);
564 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b)
568 return checksum>>8 ^ crc32[(b^checksum)&0xff];
570 unsigned int crc32_add_string(unsigned int checksum, const char*s)
577 checksum = checksum>>8 ^ crc32[(*s^checksum)&0xff];
583 unsigned int string_hash(const string_t*str)
586 unsigned int checksum = 0;
589 for(t=0;t<str->len;t++) {
590 checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
594 unsigned int string_hash2(const char*str)
596 unsigned int checksum = 0;
601 checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
606 unsigned int string_hash3(const char*str, int len)
611 return string_hash(&s);
613 void string_dup2(string_t*str, const char*text, int len)
616 str->str = strdup_n(text, len);
618 void string_dup(string_t*str, const char*text)
620 str->len = strlen(text);
621 str->str = strdup(text);
623 int string_equals(string_t*str, const char*text)
625 int l = strlen(text);
626 if(str->len == l && !memcmp(str->str, text, l))
630 int string_equals2(string_t*str, string_t*str2)
632 if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
637 // ------------------------------- stringarray_t ------------------------------
639 typedef struct _stringlist {
641 struct _stringlist*next;
644 typedef struct _stringarray_internal_t
650 } stringarray_internal_t;
652 void stringarray_init(stringarray_t*sa, int hashsize)
654 stringarray_internal_t*s;
656 sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t));
657 s = (stringarray_internal_t*)sa->internal;
659 s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
660 s->hashsize = hashsize;
662 void stringarray_put(stringarray_t*sa, string_t str)
664 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
666 int hash = string_hash(&str) % s->hashsize;
668 char*ss = string_cstr(&str);
669 mem_put(&s->pos, &ss, sizeof(char*));
671 stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
673 l->next = s->hash[hash];
678 char* stringarray_at(stringarray_t*sa, int pos)
680 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
682 if(pos<0 || pos>=s->num)
684 p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
689 string_t stringarray_at2(stringarray_t*sa, int pos)
692 s.str = stringarray_at(sa, pos);
693 s.len = s.str?strlen(s.str):0;
696 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
699 stringlist_t*old = l;
701 if(index==l->index) {
703 memset(l, 0, sizeof(stringlist_t));
713 fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
717 void stringarray_del(stringarray_t*sa, int pos)
719 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
720 string_t str = stringarray_at2(sa, pos);
721 int hash = string_hash(&str) % s->hashsize;
722 s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
723 *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
725 int stringarray_find(stringarray_t*sa, string_t* str)
727 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
728 int hash = string_hash(str) % s->hashsize;
730 stringlist_t*l = s->hash[hash];
733 string_t s = stringarray_at2(sa, l->index);
734 if(string_equals2(str, &s)) {
741 void stringarray_clear(stringarray_t*sa)
743 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
746 for(t=0;t<s->hashsize;t++) {
747 stringlist_t*l = s->hash[t];
749 stringlist_t*next = l->next;
750 memset(l, 0, sizeof(stringlist_t));
755 rfx_free(s->hash);s->hash=0;
758 void stringarray_destroy(stringarray_t*sa)
760 stringarray_clear(sa);
764 // ------------------------------- type_t -------------------------------
766 char ptr_equals(const void*o1, const void*o2)
770 unsigned int ptr_hash(const void*o)
772 return string_hash3((const char*)&o, sizeof(o));
774 void* ptr_dup(const void*o)
778 void ptr_free(void*o)
783 char charptr_equals(const void*o1, const void*o2)
787 return !strcmp(o1,o2);
789 unsigned int charptr_hash(const void*o)
793 return string_hash2(o);
795 void* charptr_dup(const void*o)
801 void charptr_free(void*o)
808 char stringstruct_equals(const void*o1, const void*o2)
812 string_t*s1 = (string_t*)o1;
813 string_t*s2 = (string_t*)o2;
814 int l = s1->len<s2->len?s1->len:s2->len;
815 int r = memcmp(s1->str, s2->str, l);
819 return s1->len==s2->len;
821 unsigned int stringstruct_hash(const void*o)
824 return string_hash(o);
826 string_t*string_dup3(string_t*o)
830 string_t*s = malloc(sizeof(string_t));
835 string_t*s = rfx_alloc(sizeof(string_t)+o->len+1);
837 s->str = (const char*)(s+1);
838 memcpy((char*)s->str, o->str, s->len);
839 ((char*)s->str)[s->len]=0;
842 void stringstruct_free(void*o)
855 type_t charptr_type = {
856 equals: charptr_equals,
862 type_t stringstruct_type = {
863 equals: stringstruct_equals,
864 hash: stringstruct_hash,
865 dup: (dup_func)string_dup3,
866 free: stringstruct_free,
869 // ------------------------------- dictionary_t -------------------------------
871 #define INITIAL_SIZE 1
873 static int max(int x, int y) {
879 dict_t*d = rfx_alloc(sizeof(dict_t));
880 dict_init(d, INITIAL_SIZE);
883 dict_t*dict_new2(type_t*t)
885 dict_t*d = rfx_alloc(sizeof(dict_t));
886 dict_init(d, INITIAL_SIZE);
890 void dict_init(dict_t*h, int size)
892 memset(h, 0, sizeof(dict_t));
894 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
896 h->key_type = &charptr_type;
898 void dict_init2(dict_t*h, type_t*t, int size)
900 memset(h, 0, sizeof(dict_t));
902 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
907 dict_t*dict_clone(dict_t*o)
909 dict_t*h = rfx_alloc(sizeof(dict_t));
910 memcpy(h, o, sizeof(dict_t));
911 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
913 for(t=0;t<o->hashsize;t++) {
914 dictentry_t*e = o->slots[t];
916 dictentry_t*n = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
917 memcpy(n, e, sizeof(dictentry_t));
918 n->key = h->key_type->dup(e->key);
920 n->next = h->slots[t];
928 static void dict_expand(dict_t*h, int newlen)
930 assert(h->hashsize < newlen);
931 dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
933 for(t=0;t<h->hashsize;t++) {
934 dictentry_t*e = h->slots[t];
936 dictentry_t*next = e->next;
937 unsigned int newhash = e->hash%newlen;
938 e->next = newslots[newhash];
939 newslots[newhash] = e;
946 h->hashsize = newlen;
949 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
951 unsigned int hash = h->key_type->hash(key);
952 dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
953 unsigned int hash2 = hash % h->hashsize;
955 e->key = h->key_type->dup(key);
956 e->hash = hash; //for resizing
957 e->next = h->slots[hash2];
963 void dict_put2(dict_t*h, const char*s, void*data)
965 assert(h->key_type == &charptr_type);
966 dict_put(h, s, data);
968 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
971 for(t=0;t<h->hashsize;t++) {
972 dictentry_t*e = h->slots[t];
974 if(h->key_type!=&charptr_type) {
975 fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data);
977 fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data);
984 int dict_count(dict_t*h)
989 static inline dictentry_t* dict_do_lookup(dict_t*h, const void*key)
995 unsigned int ohash = h->key_type->hash(key);
996 unsigned int hash = ohash % h->hashsize;
998 /* check first entry for match */
999 dictentry_t*e = h->slots[hash];
1000 if(e && h->key_type->equals(e->key, key)) {
1006 /* if dict is 2/3 filled, double the size. Do
1007 this the first time we have to actually iterate
1008 through a slot to find our data */
1009 if(e && h->num*3 >= h->hashsize*2) {
1010 int newsize = h->hashsize;
1011 while(h->num*3 >= newsize*2) {
1012 newsize = newsize<15?15:(newsize+1)*2-1;
1014 dict_expand(h, newsize);
1015 hash = ohash % h->hashsize;
1017 if(e && h->key_type->equals(e->key, key)) {
1018 // omit move to front
1025 /* check subsequent entries for a match */
1026 dictentry_t*last = h->slots[hash];
1028 if(h->key_type->equals(e->key, key)) {
1029 /* move to front- makes a difference of about 10% in most applications */
1030 last->next = e->next;
1031 e->next = h->slots[hash];
1040 void* dict_lookup(dict_t*h, const void*key)
1042 dictentry_t*e = dict_do_lookup(h, key);
1047 char dict_contains(dict_t*h, const void*key)
1049 dictentry_t*e = dict_do_lookup(h, key);
1053 char dict_del(dict_t*h, const void*key)
1057 unsigned int hash = h->key_type->hash(key) % h->hashsize;
1058 dictentry_t*head = h->slots[hash];
1059 dictentry_t*e = head, *prev=0;
1061 if(h->key_type->equals(e->key, key)) {
1062 dictentry_t*next = e->next;
1063 rfx_free((void*)e->key);
1064 memset(e, 0, sizeof(dictentry_t));
1067 h->slots[hash] = next;
1081 dictentry_t* dict_get_slot(dict_t*h, const void*key)
1085 unsigned int ohash = h->key_type->hash(key);
1086 unsigned int hash = ohash % h->hashsize;
1087 return h->slots[hash];
1090 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
1093 for(t=0;t<h->hashsize;t++) {
1094 dictentry_t*e = h->slots[t];
1096 dictentry_t*next = e->next;
1098 runFunction(data, e->key, e->data);
1104 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
1107 for(t=0;t<h->hashsize;t++) {
1108 dictentry_t*e = h->slots[t];
1110 dictentry_t*next = e->next;
1112 runFunction(e->data);
1119 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*))
1122 for(t=0;t<h->hashsize;t++) {
1123 dictentry_t*e = h->slots[t];
1125 dictentry_t*next = e->next;
1127 h->key_type->free(e->key);
1129 if(free_data_function) {
1130 free_data_function(e->data);
1132 memset(e, 0, sizeof(dictentry_t));
1139 memset(h, 0, sizeof(dict_t));
1142 void dict_clear_shallow(dict_t*h)
1144 dict_free_all(h, 0, 0);
1147 void dict_clear(dict_t*h)
1149 dict_free_all(h, 1, 0);
1152 void dict_destroy_shallow(dict_t*dict)
1154 dict_clear_shallow(dict);
1158 void dict_destroy(dict_t*dict)
1164 // ------------------------------- map_t --------------------------------------
1166 typedef struct _map_internal_t
1171 void map_init(map_t*map)
1174 map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
1175 m = (map_internal_t*)map->internal;
1176 dict_init(&m->d, INITIAL_SIZE);
1178 void map_put(map_t*map, string_t t1, string_t t2)
1180 map_internal_t*m = (map_internal_t*)map->internal;
1182 char* s1 = string_cstr(&t1);
1183 dict_put2(&m->d, s1, (void*)string_cstr(&t2));
1186 const char* map_lookup(map_t*map, const char*name)
1188 map_internal_t*m = (map_internal_t*)map->internal;
1189 const char*value = dict_lookup(&m->d, name);
1192 static void freestring(void*data)
1196 static void dumpmapentry(void*data, const void*key, void*value)
1198 FILE*fi = (FILE*)data;
1199 fprintf(fi, "%s=%s\n", key, (char*)value);
1201 void map_dump(map_t*map, FILE*fi, const char*prefix)
1204 map_internal_t*m = (map_internal_t*)map->internal;
1205 dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
1207 void map_clear(map_t*map)
1209 map_internal_t*m = (map_internal_t*)map->internal;
1210 dict_free_all(&m->d, 1, freestring);
1213 void map_destroy(map_t*map)
1219 // ------------------------------- array_t --------------------------------------
1221 array_t* array_new() {
1222 array_t*d = malloc(sizeof(array_t));
1223 memset(d, 0, sizeof(array_t));
1224 d->entry2pos = dict_new();
1227 array_t* array_new2(type_t*type) {
1228 array_t*d = malloc(sizeof(array_t));
1229 memset(d, 0, sizeof(array_t));
1230 d->entry2pos = dict_new2(type);
1233 void*array_getkey(array_t*array, int nr) {
1234 if(nr > array->num || nr<0) {
1235 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1238 return array->d[nr].name;
1240 void*array_getvalue(array_t*array, int nr) {
1241 if(nr > array->num || nr<0) {
1242 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1245 return array->d[nr].data;
1247 int array_append(array_t*array, const void*name, void*data) {
1248 while(array->size <= array->num) {
1251 array->d = malloc(sizeof(array_entry_t)*array->size);
1253 array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
1257 dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
1260 array->d[array->num].name = e->key;
1262 array->d[array->num].name = 0;
1264 array->d[array->num].data = (void*)data;
1265 return array->num++;
1267 int array_find(array_t*array, const void*name)
1269 int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
1272 int array_find2(array_t*array, const void*name, void*data)
1274 dict_t*h= array->entry2pos;
1275 dictentry_t*e = dict_get_slot(array->entry2pos, name);
1278 int index = ((int)(ptroff_t)e->data) - 1;
1279 if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
1286 int array_update(array_t*array, const void*name, void*data) {
1287 int pos = array_find(array, name);
1289 array->d[pos].data = data;
1292 return array_append(array, name, data);
1294 int array_append_if_new(array_t*array, const void*name, void*data) {
1295 int pos = array_find(array, name);
1298 return array_append(array, name, data);
1300 void array_free(array_t*array) {
1301 dict_destroy(array->entry2pos);
1303 free(array->d);array->d = 0;
1308 // ------------------------------- list_t --------------------------------------
1311 typedef struct _listinfo {
1313 struct _commonlist*last;
1316 typedef struct _commonlist {
1318 struct _commonlist*next;
1322 int list_length_(void*_list)
1324 commonlist_t*l = (commonlist_t*)_list;
1327 return l->info[0].size;
1329 void list_concat_(void*_l1, void*_l2)
1331 commonlist_t**l1 = (commonlist_t**)_l1;
1332 commonlist_t**l2 = (commonlist_t**)_l2;
1337 (*l1)->info[0].last->next = *l2;
1338 (*l1)->info[0].last = (*l2)->info[0].last;
1339 (*l1)->info[0].size += (*l2)->info[0].size;
1343 void list_append_(void*_list, void*entry)
1345 commonlist_t**list = (commonlist_t**)_list;
1346 commonlist_t* n = 0;
1348 n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1350 (*list)->info[0].size = 0;
1352 n = malloc(sizeof(commonlist_t));
1353 (*list)->info[0].last->next = n;
1357 (*list)->info[0].last = n;
1358 (*list)->info[0].size++;
1360 /* notice: prepending uses slighly more space than appending */
1361 void list_prepend_(void*_list, void*entry)
1363 commonlist_t**list = (commonlist_t**)_list;
1364 commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1366 commonlist_t* last = 0;
1368 last = (*list)->info[0].last;
1369 size = (*list)->info[0].size;
1374 (*list)->info[0].last = last;
1375 (*list)->info[0].size = size+1;
1377 void list_free_(void*_list)
1379 commonlist_t**list = (commonlist_t**)_list;
1380 commonlist_t*l = *list;
1382 commonlist_t*next = l->next;
1388 void list_deep_free_(void*_list)
1390 commonlist_t**list = (commonlist_t**)_list;
1391 commonlist_t*l = *list;
1393 commonlist_t*next = l->next;
1395 free(l->entry);l->entry=0;
1402 void*list_clone_(void*_list)
1404 commonlist_t*l = *(commonlist_t**)_list;
1408 commonlist_t*next = l->next;
1409 list_append_(&dest, l->entry);