gwenhywfar  4.8.0beta
inherit.c
Go to the documentation of this file.
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 
41 GWEN_LIST_FUNCTIONS(GWEN_INHERITDATA, GWEN_InheritData)
42 
43 
44 
46  uint32_t id,
47  void *data,
48  void *baseData,
49  GWEN_INHERIT_FREEDATAFN freeDataFn){
51 
52  assert(t);
55  d->typeName=strdup(t);
56  d->id=id;
57  d->data=data;
58  d->baseData=baseData;
59  d->freeDataFn=freeDataFn;
60 
62  "Created inheritance for type \"%s\" (%08x)", t, id);
63  return d;
64 }
65 
66 
67 
69  if (d) {
70  if (d->freeDataFn)
71  d->freeDataFn(d->baseData, d->data);
72  free(d->typeName);
75  }
76 }
77 
78 
79 
81  if (d) {
83  "Freeing data for type \"%s\"",
84  d->typeName);
85  if (d->freeDataFn)
86  d->freeDataFn(d->baseData, d->data);
87  d->freeDataFn=NULL;
88  d->data=NULL;
89  }
90 }
91 
92 
93 
95  assert(d);
96  d->freeDataFn=0;
97  d->data=0;
98 }
99 
100 
101 
103  assert(d);
104  return d->typeName;
105 }
106 
107 
108 
110  assert(d);
111  return d->id;
112 }
113 
114 
115 
117  assert(d);
118  return d->data;
119 }
120 
121 
122 
125  assert(d);
126  return d->freeDataFn;
127 }
128 
129 
130 
131 
132 
133 uint32_t GWEN_Inherit_MakeId(const char *typeName){
134  unsigned int i, j;
135  uint32_t result;
136 
137  result=0;
138  j=strlen(typeName);
139  for (i=0; i<j; i++) {
140  uint32_t tmpResult;
141  unsigned char c;
142 
143  tmpResult=result<<8;
144  c=((result>>24)&0xff);
145  result=tmpResult|c;
146  result^=(unsigned char)(typeName[i]);
147  }
148 
150  "Id for type \"%s\" is \"%08x\"",
151  typeName, result);
152  return result;
153 }
154 
155 
156 
157 void *GWEN_Inherit_FindData(GWEN_INHERITDATA_LIST *l,
158  uint32_t id,
159  int wantCreate){
160  GWEN_INHERITDATA *ih;
161 
162  assert(l);
163 
165  "Searching for inheritance id \"%08x\"", id);
166  ih=GWEN_InheritData_List_First(l);
167  while(ih) {
169  "Checking type \"%s\" (%08x) against %08x",
170  ih->typeName, ih->id, id);
171  if (ih->id==id)
172  return ih->data;
173  ih=GWEN_InheritData_List_Next(ih);
174  } /* while */
175  if (!wantCreate) {
177  "Type \"%08x\" not derived from this base type", id);
178  }
179  return 0;
180 }
181 
182 
183 
184 GWEN_INHERITDATA *GWEN_Inherit_FindEntry(GWEN_INHERITDATA_LIST *l,
185  uint32_t id,
186  int wantCreate){
187  GWEN_INHERITDATA *ih;
188 
189  assert(l);
190 
191  DBG_VERBOUS(GWEN_LOGDOMAIN, "Searching for inheritance id \"%08x\"", id);
192  ih=GWEN_InheritData_List_First(l);
193  while(ih) {
194  DBG_VERBOUS(GWEN_LOGDOMAIN, "Checking type \"%s\" (%08x) against %08x",
195  ih->typeName, ih->id, id);
196  if (ih->id==id)
197  return ih;
198  ih=GWEN_InheritData_List_Next(ih);
199  } /* while */
200  if (!wantCreate) {
202  "Type \"%08x\" not derived from this base type", id);
203  }
204  return 0;
205 }
206 
207 
208 
209 
210