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(trielayer_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_put(&t->start, id, data);
338 trie_rollback_adds(t, id, olddata);
340 trie_rollback_removes(t, id, data);
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_adds(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;
385 struct _triememory*next;
388 typedef struct _trierollback {
390 struct _trierollback*prev;
393 static void trie_rollback_adds(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));
400 m->next = rollback->ops;
403 static void trie_rollback_removes(trie_t*t, unsigned const char*id, void*data)
405 trierollback_t*rollback = (trierollback_t*)t->rollback;
406 triememory_t*m = (triememory_t*)rfx_calloc(sizeof(triememory_t));
410 m->next = rollback->ops;
414 void _trie_dump(trielayer_t*t, char*buffer, int pos)
420 _trie_dump(t->row[i], buffer, pos+1);
425 printf("%s%s %08x\n", buffer, t->rest, t->data);
429 void trie_dump(trie_t*t)
432 _trie_dump(t->start, buffer, 0);
436 void trie_remember(trie_t*t)
438 trierollback_t*old = (trierollback_t*)t->rollback;
439 t->rollback = (trierollback_t*)rfx_calloc(sizeof(trierollback_t));
440 ((trierollback_t*)t->rollback)->prev = old;
443 void trie_rollback(trie_t*t)
445 trierollback_t*rollback = (trierollback_t*)t->rollback;
447 fprintf(stderr, "Internal error: can't roll back this trie any further\n");
450 t->rollback = ((trierollback_t*)t->rollback)->prev;
452 triememory_t*op = rollback->ops;
454 triememory_t*next = op->next;
456 if(!_trie_remove(t->start, op->key)) {
457 fprintf(stderr, "Internal error: can't delete key %s in trie during rollback\n", op->key);
460 if(_trie_put(&t->start, op->key, op->data)) {
461 fprintf(stderr, "Internal error: overwrote key %s in trie during rollback\n", op->key);
470 // ------------------------------- crc32 --------------------------------------
471 static unsigned int*crc32 = 0;
472 static void crc32_init(void)
477 crc32= (unsigned int*)rfx_alloc(sizeof(unsigned int)*256);
478 for(t=0; t<256; t++) {
481 for (s = 0; s < 8; s++) {
482 c = (0xedb88320L*(c&1)) ^ (c >> 1);
487 // ------------------------------- string_t -----------------------------------
489 void string_set2(string_t*str, const char*text, int len)
494 void string_set(string_t*str, const char*text)
497 str->len = strlen(text);
503 string_t string_new(const char*text, int len)
510 string_t string_new2(const char*text)
514 s.len = strlen(text);
521 string_t* string_new3(const char*text, int len)
524 string_t*s = malloc(sizeof(string_t));
529 string_t*s = malloc(sizeof(string_t)+len+1);
531 s->str = (const char*)(s+1);
532 memcpy((char*)s->str, text, len);
533 ((char*)s->str)[len]=0;
537 string_t* string_new4(const char*text)
539 int l = strlen(text);
540 return string_new3(text, l);
543 void string_free(string_t*s)
548 if((string_t*)(s->str) == s+1) {
552 rfx_free((char*)(s->str));
557 char* string_cstr(string_t*str)
559 return strdup_n(str->str, str->len);
561 char* string_escape(string_t*str)
565 for(t=0;t<str->len;t++) {
571 char*s = malloc(len+1);
573 for(t=0;t<str->len;t++) {
574 if(str->str[t]<0x20) {
576 unsigned char c = str->str[t];
577 *p++ = "0123456789abcdef"[c>>4];
578 *p++ = "0123456789abcdef"[c&0x0f];
584 assert(p == &s[len+1]);
588 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b)
592 return checksum>>8 ^ crc32[(b^checksum)&0xff];
594 unsigned int crc32_add_string(unsigned int checksum, const char*s)
601 checksum = checksum>>8 ^ crc32[(*s^checksum)&0xff];
607 unsigned int string_hash(const string_t*str)
610 unsigned int checksum = 0;
613 for(t=0;t<str->len;t++) {
614 checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
618 unsigned int string_hash2(const char*str)
620 unsigned int checksum = 0;
625 checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
630 unsigned int string_hash3(const char*str, int len)
635 return string_hash(&s);
637 void string_dup2(string_t*str, const char*text, int len)
640 str->str = strdup_n(text, len);
642 void string_dup(string_t*str, const char*text)
644 str->len = strlen(text);
645 str->str = strdup(text);
647 int string_equals(string_t*str, const char*text)
649 int l = strlen(text);
650 if(str->len == l && !memcmp(str->str, text, l))
654 int string_equals2(string_t*str, string_t*str2)
656 if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
661 // ------------------------------- stringarray_t ------------------------------
663 typedef struct _stringlist {
665 struct _stringlist*next;
668 typedef struct _stringarray_internal_t
674 } stringarray_internal_t;
676 void stringarray_init(stringarray_t*sa, int hashsize)
678 stringarray_internal_t*s;
680 sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t));
681 s = (stringarray_internal_t*)sa->internal;
683 s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
684 s->hashsize = hashsize;
686 void stringarray_put(stringarray_t*sa, string_t str)
688 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
690 int hash = string_hash(&str) % s->hashsize;
692 char*ss = string_cstr(&str);
693 mem_put(&s->pos, &ss, sizeof(char*));
695 stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
697 l->next = s->hash[hash];
702 char* stringarray_at(stringarray_t*sa, int pos)
704 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
706 if(pos<0 || pos>=s->num)
708 p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
713 string_t stringarray_at2(stringarray_t*sa, int pos)
716 s.str = stringarray_at(sa, pos);
717 s.len = s.str?strlen(s.str):0;
720 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
723 stringlist_t*old = l;
725 if(index==l->index) {
727 memset(l, 0, sizeof(stringlist_t));
737 fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
741 void stringarray_del(stringarray_t*sa, int pos)
743 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
744 string_t str = stringarray_at2(sa, pos);
745 int hash = string_hash(&str) % s->hashsize;
746 s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
747 *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
749 int stringarray_find(stringarray_t*sa, string_t* str)
751 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
752 int hash = string_hash(str) % s->hashsize;
754 stringlist_t*l = s->hash[hash];
757 string_t s = stringarray_at2(sa, l->index);
758 if(string_equals2(str, &s)) {
765 void stringarray_clear(stringarray_t*sa)
767 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
770 for(t=0;t<s->hashsize;t++) {
771 stringlist_t*l = s->hash[t];
773 stringlist_t*next = l->next;
774 memset(l, 0, sizeof(stringlist_t));
779 rfx_free(s->hash);s->hash=0;
782 void stringarray_destroy(stringarray_t*sa)
784 stringarray_clear(sa);
788 // ------------------------------- type_t -------------------------------
790 char ptr_equals(const void*o1, const void*o2)
794 unsigned int ptr_hash(const void*o)
796 return string_hash3((const char*)&o, sizeof(o));
798 void* ptr_dup(const void*o)
802 void ptr_free(void*o)
807 char charptr_equals(const void*o1, const void*o2)
811 return !strcmp(o1,o2);
813 unsigned int charptr_hash(const void*o)
817 return string_hash2(o);
819 void* charptr_dup(const void*o)
825 void charptr_free(void*o)
832 char stringstruct_equals(const void*o1, const void*o2)
836 string_t*s1 = (string_t*)o1;
837 string_t*s2 = (string_t*)o2;
838 int l = s1->len<s2->len?s1->len:s2->len;
839 int r = memcmp(s1->str, s2->str, l);
843 return s1->len==s2->len;
845 unsigned int stringstruct_hash(const void*o)
848 return string_hash(o);
850 string_t*string_dup3(string_t*o)
854 string_t*s = malloc(sizeof(string_t));
859 string_t*s = rfx_alloc(sizeof(string_t)+o->len+1);
861 s->str = (const char*)(s+1);
862 memcpy((char*)s->str, o->str, s->len);
863 ((char*)s->str)[s->len]=0;
866 void stringstruct_free(void*o)
879 type_t charptr_type = {
880 equals: charptr_equals,
886 type_t stringstruct_type = {
887 equals: stringstruct_equals,
888 hash: stringstruct_hash,
889 dup: (dup_func)string_dup3,
890 free: stringstruct_free,
893 // ------------------------------- dictionary_t -------------------------------
895 #define INITIAL_SIZE 1
897 static int max(int x, int y) {
903 dict_t*d = rfx_alloc(sizeof(dict_t));
904 dict_init(d, INITIAL_SIZE);
907 dict_t*dict_new2(type_t*t)
909 dict_t*d = rfx_alloc(sizeof(dict_t));
910 dict_init(d, INITIAL_SIZE);
914 void dict_init(dict_t*h, int size)
916 memset(h, 0, sizeof(dict_t));
918 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
920 h->key_type = &charptr_type;
922 void dict_init2(dict_t*h, type_t*t, int size)
924 memset(h, 0, sizeof(dict_t));
926 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
931 dict_t*dict_clone(dict_t*o)
933 dict_t*h = rfx_alloc(sizeof(dict_t));
934 memcpy(h, o, sizeof(dict_t));
935 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
937 for(t=0;t<o->hashsize;t++) {
938 dictentry_t*e = o->slots[t];
940 dictentry_t*n = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
941 memcpy(n, e, sizeof(dictentry_t));
942 n->key = h->key_type->dup(e->key);
944 n->next = h->slots[t];
952 static void dict_expand(dict_t*h, int newlen)
954 assert(h->hashsize < newlen);
955 dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
957 for(t=0;t<h->hashsize;t++) {
958 dictentry_t*e = h->slots[t];
960 dictentry_t*next = e->next;
961 unsigned int newhash = e->hash%newlen;
962 e->next = newslots[newhash];
963 newslots[newhash] = e;
970 h->hashsize = newlen;
973 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
975 unsigned int hash = h->key_type->hash(key);
976 dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
977 unsigned int hash2 = hash % h->hashsize;
979 e->key = h->key_type->dup(key);
980 e->hash = hash; //for resizing
981 e->next = h->slots[hash2];
987 void dict_put2(dict_t*h, const char*s, void*data)
989 assert(h->key_type == &charptr_type);
990 dict_put(h, s, data);
992 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
995 for(t=0;t<h->hashsize;t++) {
996 dictentry_t*e = h->slots[t];
998 if(h->key_type!=&charptr_type) {
999 fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data);
1001 fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data);
1008 int dict_count(dict_t*h)
1013 static inline dictentry_t* dict_do_lookup(dict_t*h, const void*key)
1019 unsigned int ohash = h->key_type->hash(key);
1020 unsigned int hash = ohash % h->hashsize;
1022 /* check first entry for match */
1023 dictentry_t*e = h->slots[hash];
1024 if(e && h->key_type->equals(e->key, key)) {
1030 /* if dict is 2/3 filled, double the size. Do
1031 this the first time we have to actually iterate
1032 through a slot to find our data */
1033 if(e && h->num*3 >= h->hashsize*2) {
1034 int newsize = h->hashsize;
1035 while(h->num*3 >= newsize*2) {
1036 newsize = newsize<15?15:(newsize+1)*2-1;
1038 dict_expand(h, newsize);
1039 hash = ohash % h->hashsize;
1041 if(e && h->key_type->equals(e->key, key)) {
1042 // omit move to front
1049 /* check subsequent entries for a match */
1050 dictentry_t*last = h->slots[hash];
1052 if(h->key_type->equals(e->key, key)) {
1053 /* move to front- makes a difference of about 10% in most applications */
1054 last->next = e->next;
1055 e->next = h->slots[hash];
1064 void* dict_lookup(dict_t*h, const void*key)
1066 dictentry_t*e = dict_do_lookup(h, key);
1071 char dict_contains(dict_t*h, const void*key)
1073 dictentry_t*e = dict_do_lookup(h, key);
1077 char dict_del(dict_t*h, const void*key)
1081 unsigned int hash = h->key_type->hash(key) % h->hashsize;
1082 dictentry_t*head = h->slots[hash];
1083 dictentry_t*e = head, *prev=0;
1085 if(h->key_type->equals(e->key, key)) {
1086 dictentry_t*next = e->next;
1087 rfx_free((void*)e->key);
1088 memset(e, 0, sizeof(dictentry_t));
1091 h->slots[hash] = next;
1105 dictentry_t* dict_get_slot(dict_t*h, const void*key)
1109 unsigned int ohash = h->key_type->hash(key);
1110 unsigned int hash = ohash % h->hashsize;
1111 return h->slots[hash];
1114 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
1117 for(t=0;t<h->hashsize;t++) {
1118 dictentry_t*e = h->slots[t];
1120 dictentry_t*next = e->next;
1122 runFunction(data, e->key, e->data);
1128 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
1131 for(t=0;t<h->hashsize;t++) {
1132 dictentry_t*e = h->slots[t];
1134 dictentry_t*next = e->next;
1136 runFunction(e->data);
1143 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*))
1146 for(t=0;t<h->hashsize;t++) {
1147 dictentry_t*e = h->slots[t];
1149 dictentry_t*next = e->next;
1151 h->key_type->free(e->key);
1153 if(free_data_function) {
1154 free_data_function(e->data);
1156 memset(e, 0, sizeof(dictentry_t));
1163 memset(h, 0, sizeof(dict_t));
1166 void dict_clear_shallow(dict_t*h)
1168 dict_free_all(h, 0, 0);
1171 void dict_clear(dict_t*h)
1173 dict_free_all(h, 1, 0);
1176 void dict_destroy_shallow(dict_t*dict)
1178 dict_clear_shallow(dict);
1182 void dict_destroy(dict_t*dict)
1188 // ------------------------------- map_t --------------------------------------
1190 typedef struct _map_internal_t
1195 void map_init(map_t*map)
1198 map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
1199 m = (map_internal_t*)map->internal;
1200 dict_init(&m->d, INITIAL_SIZE);
1202 void map_put(map_t*map, string_t t1, string_t t2)
1204 map_internal_t*m = (map_internal_t*)map->internal;
1206 char* s1 = string_cstr(&t1);
1207 dict_put2(&m->d, s1, (void*)string_cstr(&t2));
1210 const char* map_lookup(map_t*map, const char*name)
1212 map_internal_t*m = (map_internal_t*)map->internal;
1213 const char*value = dict_lookup(&m->d, name);
1216 static void freestring(void*data)
1220 static void dumpmapentry(void*data, const void*key, void*value)
1222 FILE*fi = (FILE*)data;
1223 fprintf(fi, "%s=%s\n", key, (char*)value);
1225 void map_dump(map_t*map, FILE*fi, const char*prefix)
1228 map_internal_t*m = (map_internal_t*)map->internal;
1229 dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
1231 void map_clear(map_t*map)
1233 map_internal_t*m = (map_internal_t*)map->internal;
1234 dict_free_all(&m->d, 1, freestring);
1237 void map_destroy(map_t*map)
1243 // ------------------------------- array_t --------------------------------------
1245 array_t* array_new() {
1246 array_t*d = malloc(sizeof(array_t));
1247 memset(d, 0, sizeof(array_t));
1248 d->entry2pos = dict_new();
1251 array_t* array_new2(type_t*type) {
1252 array_t*d = malloc(sizeof(array_t));
1253 memset(d, 0, sizeof(array_t));
1254 d->entry2pos = dict_new2(type);
1257 void*array_getkey(array_t*array, int nr) {
1258 if(nr > array->num || nr<0) {
1259 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1262 return array->d[nr].name;
1264 void*array_getvalue(array_t*array, int nr) {
1265 if(nr > array->num || nr<0) {
1266 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1269 return array->d[nr].data;
1271 int array_append(array_t*array, const void*name, void*data) {
1272 while(array->size <= array->num) {
1275 array->d = malloc(sizeof(array_entry_t)*array->size);
1277 array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
1281 dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
1284 array->d[array->num].name = e->key;
1286 array->d[array->num].name = 0;
1288 array->d[array->num].data = (void*)data;
1289 return array->num++;
1291 int array_find(array_t*array, const void*name)
1293 int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
1296 int array_find2(array_t*array, const void*name, void*data)
1298 dict_t*h= array->entry2pos;
1299 dictentry_t*e = dict_get_slot(array->entry2pos, name);
1302 int index = ((int)(ptroff_t)e->data) - 1;
1303 if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
1310 int array_update(array_t*array, const void*name, void*data) {
1311 int pos = array_find(array, name);
1313 array->d[pos].data = data;
1316 return array_append(array, name, data);
1318 int array_append_if_new(array_t*array, const void*name, void*data) {
1319 int pos = array_find(array, name);
1322 return array_append(array, name, data);
1324 void array_free(array_t*array) {
1325 dict_destroy(array->entry2pos);
1327 free(array->d);array->d = 0;
1332 // ------------------------------- list_t --------------------------------------
1335 typedef struct _listinfo {
1337 struct _commonlist*last;
1340 typedef struct _commonlist {
1342 struct _commonlist*next;
1346 int list_length_(void*_list)
1348 commonlist_t*l = (commonlist_t*)_list;
1351 return l->info[0].size;
1353 void list_concat_(void*_l1, void*_l2)
1355 commonlist_t**l1 = (commonlist_t**)_l1;
1356 commonlist_t**l2 = (commonlist_t**)_l2;
1361 (*l1)->info[0].last->next = *l2;
1362 (*l1)->info[0].last = (*l2)->info[0].last;
1363 (*l1)->info[0].size += (*l2)->info[0].size;
1367 void list_append_(void*_list, void*entry)
1369 commonlist_t**list = (commonlist_t**)_list;
1370 commonlist_t* n = 0;
1372 n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1374 (*list)->info[0].size = 0;
1376 n = malloc(sizeof(commonlist_t));
1377 (*list)->info[0].last->next = n;
1381 (*list)->info[0].last = n;
1382 (*list)->info[0].size++;
1384 /* notice: prepending uses slighly more space than appending */
1385 void list_prepend_(void*_list, void*entry)
1387 commonlist_t**list = (commonlist_t**)_list;
1388 commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1390 commonlist_t* last = 0;
1392 last = (*list)->info[0].last;
1393 size = (*list)->info[0].size;
1398 (*list)->info[0].last = last;
1399 (*list)->info[0].size = size+1;
1401 void list_free_(void*_list)
1403 commonlist_t**list = (commonlist_t**)_list;
1404 commonlist_t*l = *list;
1406 commonlist_t*next = l->next;
1412 void list_deep_free_(void*_list)
1414 commonlist_t**list = (commonlist_t**)_list;
1415 commonlist_t*l = *list;
1417 commonlist_t*next = l->next;
1419 free(l->entry);l->entry=0;
1426 void*list_clone_(void*_list)
1428 commonlist_t*l = *(commonlist_t**)_list;
1432 commonlist_t*next = l->next;
1433 list_append_(&dest, l->entry);