1 /***************************************************************************
2 begin : Sun Dec 05 2003
3 copyright : (C) 2003-2010 by Martin Preuss
4 email : martin@libchipcard.de
5
6 ***************************************************************************
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library 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 GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #define DISABLE_DEBUGLOG
31
32 #include "inherit_p.h"
33 #include <gwenhywfar/misc.h>
34 #include <gwenhywfar/debug.h>
35 #include <gwenhywfar/gwenhywfarapi.h>
36
37 #include <string.h>
38
39
40
GWEN_LIST_FUNCTIONS(GWEN_INHERITDATA,GWEN_InheritData)41 GWEN_LIST_FUNCTIONS(GWEN_INHERITDATA, GWEN_InheritData)
42
43
44
45 GWEN_INHERITDATA *GWEN_InheritData_new(const char *t,
46 uint32_t id,
47 void *data,
48 void *baseData,
49 GWEN_INHERIT_FREEDATAFN freeDataFn)
50 {
51 GWEN_INHERITDATA *d;
52
53 assert(t);
54 GWEN_NEW_OBJECT(GWEN_INHERITDATA, d);
55 GWEN_LIST_INIT(GWEN_INHERITDATA, d);
56 d->typeName=strdup(t);
57 d->id=id;
58 d->data=data;
59 d->baseData=baseData;
60 d->freeDataFn=freeDataFn;
61
62 DBG_VERBOUS(GWEN_LOGDOMAIN,
63 "Created inheritance for type \"%s\" (%08x)", t, id);
64 return d;
65 }
66
67
68
GWEN_InheritData_free(GWEN_INHERITDATA * d)69 void GWEN_InheritData_free(GWEN_INHERITDATA *d)
70 {
71 if (d) {
72 if (d->freeDataFn)
73 d->freeDataFn(d->baseData, d->data);
74 free(d->typeName);
75 GWEN_LIST_FINI(GWEN_INHERITDATA, d);
76 GWEN_FREE_OBJECT(d);
77 }
78 }
79
80
81
GWEN_InheritData_freeData(GWEN_INHERITDATA * d)82 void GWEN_InheritData_freeData(GWEN_INHERITDATA *d)
83 {
84 if (d) {
85 DBG_VERBOUS(GWEN_LOGDOMAIN,
86 "Freeing data for type \"%s\"",
87 d->typeName);
88 if (d->freeDataFn)
89 d->freeDataFn(d->baseData, d->data);
90 d->freeDataFn=NULL;
91 d->data=NULL;
92 }
93 }
94
95
96
GWEN_InheritData_clear(GWEN_INHERITDATA * d)97 void GWEN_InheritData_clear(GWEN_INHERITDATA *d)
98 {
99 assert(d);
100 d->freeDataFn=0;
101 d->data=0;
102 }
103
104
105
GWEN_InheritData_GetTypeName(const GWEN_INHERITDATA * d)106 const char *GWEN_InheritData_GetTypeName(const GWEN_INHERITDATA *d)
107 {
108 assert(d);
109 return d->typeName;
110 }
111
112
113
GWEN_InheritData_GetId(const GWEN_INHERITDATA * d)114 uint32_t GWEN_InheritData_GetId(const GWEN_INHERITDATA *d)
115 {
116 assert(d);
117 return d->id;
118 }
119
120
121
GWEN_InheritData_GetData(const GWEN_INHERITDATA * d)122 void *GWEN_InheritData_GetData(const GWEN_INHERITDATA *d)
123 {
124 assert(d);
125 return d->data;
126 }
127
128
129
GWEN_InheritData_GetFreeDataFn(const GWEN_INHERITDATA * d)130 GWEN_INHERIT_FREEDATAFN GWEN_InheritData_GetFreeDataFn(const GWEN_INHERITDATA *d)
131 {
132 assert(d);
133 return d->freeDataFn;
134 }
135
136
137
138
139
GWEN_Inherit_MakeId(const char * typeName)140 uint32_t GWEN_Inherit_MakeId(const char *typeName)
141 {
142 unsigned int i, j;
143 uint32_t result;
144
145 result=0;
146 j=strlen(typeName);
147 for (i=0; i<j; i++) {
148 uint32_t tmpResult;
149 unsigned char c;
150
151 tmpResult=result<<8;
152 c=((result>>24)&0xff);
153 result=tmpResult|c;
154 result^=(unsigned char)(typeName[i]);
155 }
156
157 DBG_VERBOUS(GWEN_LOGDOMAIN,
158 "Id for type \"%s\" is \"%08x\"",
159 typeName, result);
160 return result;
161 }
162
163
164
GWEN_Inherit_FindData(GWEN_INHERITDATA_LIST * l,uint32_t id,int wantCreate)165 void *GWEN_Inherit_FindData(GWEN_INHERITDATA_LIST *l,
166 uint32_t id,
167 int wantCreate)
168 {
169 GWEN_INHERITDATA *ih;
170
171 assert(l);
172
173 DBG_VERBOUS(GWEN_LOGDOMAIN,
174 "Searching for inheritance id \"%08x\"", id);
175 ih=GWEN_InheritData_List_First(l);
176 while (ih) {
177 DBG_VERBOUS(GWEN_LOGDOMAIN,
178 "Checking type \"%s\" (%08x) against %08x",
179 ih->typeName, ih->id, id);
180 if (ih->id==id)
181 return ih->data;
182 ih=GWEN_InheritData_List_Next(ih);
183 } /* while */
184 if (!wantCreate) {
185 DBG_WARN(GWEN_LOGDOMAIN,
186 "Type \"%08x\" not derived from this base type", id);
187 }
188 return 0;
189 }
190
191
192
GWEN_Inherit_FindEntry(GWEN_INHERITDATA_LIST * l,uint32_t id,int wantCreate)193 GWEN_INHERITDATA *GWEN_Inherit_FindEntry(GWEN_INHERITDATA_LIST *l,
194 uint32_t id,
195 int wantCreate)
196 {
197 GWEN_INHERITDATA *ih;
198
199 assert(l);
200
201 DBG_VERBOUS(GWEN_LOGDOMAIN, "Searching for inheritance id \"%08x\"", id);
202 ih=GWEN_InheritData_List_First(l);
203 while (ih) {
204 DBG_VERBOUS(GWEN_LOGDOMAIN, "Checking type \"%s\" (%08x) against %08x",
205 ih->typeName, ih->id, id);
206 if (ih->id==id)
207 return ih;
208 ih=GWEN_InheritData_List_Next(ih);
209 } /* while */
210 if (!wantCreate) {
211 DBG_WARN(GWEN_LOGDOMAIN,
212 "Type \"%08x\" not derived from this base type", id);
213 }
214 return 0;
215 }
216
217
218
219
220
221