509e2fd818fb0a01e602c0544df9c931006a2ece
[swftools.git] / lib / as3 / utils.c
1 /* utils.c
2
3    Extension module for the rfxswf library.
4    Part of the swftools package.
5
6    Copyright (c) 2008 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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <memory.h>
25 #include "utils.h"
26
27 dict_t* dict_new() {
28     dict_t*d = malloc(sizeof(dict_t));
29     memset(d, 0, sizeof(dict_t));
30     return d;
31 }
32
33 void dict_free(dict_t*dict) {
34     if(dict->d)
35     free(dict->d);dict->d = 0;
36     free(dict);
37 }
38
39 const char*dict_getstr(dict_t*dict, int nr) {
40     if(nr > dict->num || nr<0) {
41         printf("error: reference to string %d in dict\n");
42         return 0;
43     }
44     return dict->d[nr].name;
45 }
46 char*dict_getdata(dict_t*dict, int nr) {
47     if(nr > dict->num || nr<0) {
48         printf("error: reference to string %d in dict\n");
49         return 0;
50     }
51     return dict->d[nr].data;
52 }
53 int dict_append(dict_t*dict, const char*name, void*data) {
54     while(dict->size <= dict->num) {
55         dict->size += 64;
56         if(!dict->d) {
57             dict->d = malloc(sizeof(dict_entry_t)*dict->size);
58         } else {
59             dict->d = realloc(dict->d, sizeof(dict_entry_t)*dict->size);
60         }
61     }
62     if(name) {
63         dict->d[dict->num].name = strdup(name);
64     } else {
65         dict->d[dict->num].name = 0;
66     }
67     dict->d[dict->num].data = data;
68     return dict->num++;
69 }
70 int dict_find(dict_t*dict, const char*name)
71 {
72     if(!name)
73         name = "";
74     int t;
75     for(t=0;t<dict->num;t++) {
76         if(dict->d[t].name && !strcmp(dict->d[t].name,name))
77             return t;
78     }
79     return -1;
80 }
81 int dict_find2(dict_t*dict, const char*name, void*data)
82 {
83     if(!name)
84         name = "";
85     int t;
86     for(t=0;t<dict->num;t++) {
87         if(dict->d[t].name && !strcmp(dict->d[t].name,name) && dict->d[t].data == data)
88             return t;
89     }
90     return -1;
91 }
92 int dict_update(dict_t*dict, const char*name, void*data) {
93     int pos = dict_find(dict, name);
94     if(pos>=0) {
95         dict->d[pos].data = data;
96         return pos;
97     }
98     return dict_append(dict, name, data);
99 }
100 int dict_append_if_new(dict_t*dict, const char*name, void*data) {
101     int pos = dict_find(dict, name);
102     if(pos>=0)
103         return pos;
104     return dict_append(dict, name, data);
105 }
106
107 typedef struct _commonlist {
108     void*entry;
109     struct _commonlist*next;
110     struct _commonlist*last[0];
111 } commonlist_t;
112
113 int list_length(void*_list)
114 {
115     commonlist_t*l = (commonlist_t*)_list;
116     int n=0;
117     while(l) {
118         l = l->next;
119         n++;
120     }
121     return n;
122 }
123 void list_append(void*_list, void*entry)
124 {
125     commonlist_t**list = (commonlist_t**)_list;
126     commonlist_t* n = 0;
127     if(!*list) {
128         n = malloc(sizeof(commonlist_t)+sizeof(commonlist_t*));
129         *list = n;
130     } else {
131         n = malloc(sizeof(commonlist_t));
132         (*list)->last[0]->next = n;
133     }
134     n->next = 0;
135     n->entry = entry;
136     (*list)->last[0] = n;
137 }
138