]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/src/libptr.c
5fad186f890e6295aa63b322836e38f473941aa7
[wxWidgets.git] / utils / wxPython / src / libptr.c
1 /*****************************************************************************
2 * $Header$
3 *
4 * swigptr.swg
5 *
6 * This file contains supporting code for the SWIG run-time type checking
7 * mechanism. The following functions are available :
8 *
9 * SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *));
10 *
11 * Registers a new type-mapping with the type-checker. origtype is the
12 * original datatype and newtype is an equivalent type. cast is optional
13 * pointer to a function to cast pointer values between types (this
14 * is typically used to cast pointers from derived classes to base classes in C++)
15 *
16 * SWIG_MakePtr(char *buffer, void *ptr, char *typestring);
17 *
18 * Makes a pointer string from a pointer and typestring. The result is returned
19 * in buffer which is assumed to hold enough space for the result.
20 *
21 * char * SWIG_GetPtr(char *buffer, void **ptr, char *type)
22 *
23 * Gets a pointer value from a string. If there is a type-mismatch, returns
24 * a character string to the received type. On success, returns NULL.
25 *
26 *
27 * You can remap these functions by making a file called "swigptr.swg" in
28 * your the same directory as the interface file you are wrapping.
29 *
30 * These functions are normally declared static, but this file can be
31 * can be used in a multi-module environment by redefining the symbol
32 * SWIGSTATIC.
33 *****************************************************************************/
34
35 #include <stdlib.h>
36
37 #ifdef SWIG_GLOBAL
38 #ifdef __cplusplus
39 #define SWIGSTATIC extern "C"
40 #else
41 #define SWIGSTATIC
42 #endif
43 #endif
44
45 #ifndef SWIGSTATIC
46 #define SWIGSTATIC static
47 #endif
48
49
50 /* SWIG pointer structure */
51
52 typedef struct SwigPtrType {
53 char *name; /* Datatype name */
54 int len; /* Length (used for optimization) */
55 void *(*cast)(void *); /* Pointer casting function */
56 struct SwigPtrType *next; /* Linked list pointer */
57 } SwigPtrType;
58
59 /* Pointer cache structure */
60
61 typedef struct {
62 int stat; /* Status (valid) bit */
63 SwigPtrType *tp; /* Pointer to type structure */
64 char name[256]; /* Given datatype name */
65 char mapped[256]; /* Equivalent name */
66 } SwigCacheType;
67
68 /* Some variables */
69
70 static int SwigPtrMax = 64; /* Max entries that can be currently held */
71 /* This value may be adjusted dynamically */
72 static int SwigPtrN = 0; /* Current number of entries */
73 static int SwigPtrSort = 0; /* Status flag indicating sort */
74 static int SwigStart[256]; /* Starting positions of types */
75
76 /* Pointer table */
77 static SwigPtrType *SwigPtrTable = 0; /* Table containing pointer equivalences */
78
79 /* Cached values */
80
81 #define SWIG_CACHESIZE 8
82 #define SWIG_CACHEMASK 0x7
83 static SwigCacheType SwigCache[SWIG_CACHESIZE];
84 static int SwigCacheIndex = 0;
85 static int SwigLastCache = 0;
86
87 /* Sort comparison function */
88 static int swigsort(const void *data1, const void *data2) {
89 SwigPtrType *d1 = (SwigPtrType *) data1;
90 SwigPtrType *d2 = (SwigPtrType *) data2;
91 return strcmp(d1->name,d2->name);
92 }
93
94 /* Binary Search function */
95 static int swigcmp(const void *key, const void *data) {
96 char *k = (char *) key;
97 SwigPtrType *d = (SwigPtrType *) data;
98 return strncmp(k,d->name,d->len);
99 }
100
101 /* Register a new datatype with the type-checker */
102
103 SWIGSTATIC
104 void SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *)) {
105
106 int i;
107 SwigPtrType *t = 0,*t1;
108
109 /* Allocate the pointer table if necessary */
110
111 if (!SwigPtrTable) {
112 SwigPtrTable = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
113 SwigPtrN = 0;
114 }
115 /* Grow the table */
116 if (SwigPtrN >= SwigPtrMax) {
117 SwigPtrMax = 2*SwigPtrMax;
118 SwigPtrTable = (SwigPtrType *) realloc((char *) SwigPtrTable,SwigPtrMax*sizeof(SwigPtrType));
119 }
120 for (i = 0; i < SwigPtrN; i++)
121 if (strcmp(SwigPtrTable[i].name,origtype) == 0) {
122 t = &SwigPtrTable[i];
123 break;
124 }
125 if (!t) {
126 t = &SwigPtrTable[SwigPtrN];
127 t->name = origtype;
128 t->len = strlen(t->name);
129 t->cast = 0;
130 t->next = 0;
131 SwigPtrN++;
132 }
133
134 /* Check for existing entry */
135
136 while (t->next) {
137 if ((strcmp(t->name,newtype) == 0)) {
138 if (cast) t->cast = cast;
139 return;
140 }
141 t = t->next;
142 }
143
144 /* Now place entry (in sorted order) */
145
146 t1 = (SwigPtrType *) malloc(sizeof(SwigPtrType));
147 t1->name = newtype;
148 t1->len = strlen(t1->name);
149 t1->cast = cast;
150 t1->next = 0;
151 t->next = t1;
152 SwigPtrSort = 0;
153 }
154
155 /* Make a pointer value string */
156
157 SWIGSTATIC
158 void SWIG_MakePtr(char *_c, const void *_ptr, char *type) {
159 static char _hex[16] =
160 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
161 'a', 'b', 'c', 'd', 'e', 'f'};
162 unsigned long _p, _s;
163 char _result[20], *_r; /* Note : a 64-bit hex number = 16 digits */
164 _r = _result;
165 _p = (unsigned long) _ptr;
166 if (_p > 0) {
167 while (_p > 0) {
168 _s = _p & 0xf;
169 *(_r++) = _hex[_s];
170 _p = _p >> 4;
171 }
172 *_r = '_';
173 while (_r >= _result)
174 *(_c++) = *(_r--);
175 } else {
176 strcpy (_c, "NULL");
177 }
178 if (_ptr)
179 strcpy (_c, type);
180 }
181
182 /* Define for backwards compatibility */
183
184 #define _swig_make_hex SWIG_MakePtr
185
186 /* Function for getting a pointer value */
187
188 SWIGSTATIC
189 char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
190 {
191 unsigned long _p;
192 char temp_type[256];
193 char *name;
194 int i, len;
195 SwigPtrType *sp,*tp;
196 SwigCacheType *cache;
197 int start, end;
198 _p = 0;
199
200 /* Pointer values must start with leading underscore */
201 if (*_c == '_') {
202 _c++;
203 /* Extract hex value from pointer */
204 while (*_c) {
205 if ((*_c >= '0') && (*_c <= '9'))
206 _p = (_p << 4) + (*_c - '0');
207 else if ((*_c >= 'a') && (*_c <= 'f'))
208 _p = (_p << 4) + ((*_c - 'a') + 10);
209 else
210 break;
211 _c++;
212 }
213
214 if (_t) {
215 if (strcmp(_t,_c)) {
216 if (!SwigPtrSort) {
217 qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
218 for (i = 0; i < 256; i++) {
219 SwigStart[i] = SwigPtrN;
220 }
221 for (i = SwigPtrN-1; i >= 0; i--) {
222 SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
223 }
224 for (i = 255; i >= 1; i--) {
225 if (SwigStart[i-1] > SwigStart[i])
226 SwigStart[i-1] = SwigStart[i];
227 }
228 SwigPtrSort = 1;
229 for (i = 0; i < SWIG_CACHESIZE; i++)
230 SwigCache[i].stat = 0;
231 }
232
233 /* First check cache for matches. Uses last cache value as starting point */
234 cache = &SwigCache[SwigLastCache];
235 for (i = 0; i < SWIG_CACHESIZE; i++) {
236 if (cache->stat) {
237 if (strcmp(_t,cache->name) == 0) {
238 if (strcmp(_c,cache->mapped) == 0) {
239 cache->stat++;
240 *ptr = (void *) _p;
241 if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
242 return (char *) 0;
243 }
244 }
245 }
246 SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
247 if (!SwigLastCache) cache = SwigCache;
248 else cache++;
249 }
250 /* We have a type mismatch. Will have to look through our type
251 mapping table to figure out whether or not we can accept this datatype */
252
253 start = SwigStart[(int) _t[1]];
254 end = SwigStart[(int) _t[1]+1];
255 sp = &SwigPtrTable[start];
256 while (start < end) {
257 if (swigcmp(_t,sp) == 0) break;
258 sp++;
259 start++;
260 }
261 if (start >= end) sp = 0;
262 /* Try to find a match for this */
263 if (sp) {
264 while (swigcmp(_t,sp) == 0) {
265 name = sp->name;
266 len = sp->len;
267 tp = sp->next;
268 /* Try to find entry for our given datatype */
269 while(tp) {
270 if (tp->len >= 255) {
271 return _c;
272 }
273 strcpy(temp_type,tp->name);
274 strncat(temp_type,_t+len,255-tp->len);
275 if (strcmp(_c,temp_type) == 0) {
276
277 strcpy(SwigCache[SwigCacheIndex].mapped,_c);
278 strcpy(SwigCache[SwigCacheIndex].name,_t);
279 SwigCache[SwigCacheIndex].stat = 1;
280 SwigCache[SwigCacheIndex].tp = tp;
281 SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
282
283 /* Get pointer value */
284 *ptr = (void *) _p;
285 if (tp->cast) *ptr = (*(tp->cast))(*ptr);
286 return (char *) 0;
287 }
288 tp = tp->next;
289 }
290 sp++;
291 /* Hmmm. Didn't find it this time */
292 }
293 }
294 /* Didn't find any sort of match for this data.
295 Get the pointer value and return the received type */
296 *ptr = (void *) _p;
297 return _c;
298 } else {
299 /* Found a match on the first try. Return pointer value */
300 *ptr = (void *) _p;
301 return (char *) 0;
302 }
303 } else {
304 /* No type specified. Good luck */
305 *ptr = (void *) _p;
306 return (char *) 0;
307 }
308 } else {
309 if (strcmp (_c, "NULL") == 0) {
310 *ptr = (void *) 0;
311 return (char *) 0;
312 }
313 *ptr = (void *) 0;
314 return _c;
315 }
316 }
317
318 /* Compatibility mode */
319
320 #define _swig_get_hex SWIG_GetPtr
321