]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/src/libpy.c
*** empty log message ***
[wxWidgets.git] / utils / wxPython / src / libpy.c
1 /***********************************************************************
2 * $Header$
3 * swig_lib/python/python.cfg
4 *
5 * Contains variable linking and pointer type-checking code.
6 ************************************************************************/
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 #include "Python.h"
12
13 /* Definitions for Windows/Unix exporting */
14 #if defined(_WIN32) || defined(__WIN32__)
15 # if defined(_MSC_VER)
16 # define SWIGEXPORT(a) __declspec(dllexport) a
17 # else
18 # if defined(__BORLANDC__)
19 # define SWIGEXPORT(a) a _export
20 # else
21 # define SWIGEXPORT(a) a
22 # endif
23 # endif
24 #else
25 # define SWIGEXPORT(a) a
26 #endif
27
28 #ifdef SWIG_GLOBAL
29 #define SWIGSTATICRUNTIME(a) SWIGEXPORT(a)
30 #else
31 #define SWIGSTATICRUNTIME(a) static a
32 #endif
33
34 typedef struct {
35 char *name;
36 PyObject *(*get_attr)(void);
37 int (*set_attr)(PyObject *);
38 } swig_globalvar;
39
40 typedef struct swig_varlinkobject {
41 PyObject_HEAD
42 swig_globalvar **vars;
43 int nvars;
44 int maxvars;
45 } swig_varlinkobject;
46
47 /* ----------------------------------------------------------------------
48 swig_varlink_repr()
49
50 Function for python repr method
51 ---------------------------------------------------------------------- */
52
53 static PyObject *
54 swig_varlink_repr(swig_varlinkobject *v)
55 {
56 v = v;
57 return PyString_FromString("<Global variables>");
58 }
59
60 /* ---------------------------------------------------------------------
61 swig_varlink_print()
62
63 Print out all of the global variable names
64 --------------------------------------------------------------------- */
65
66 static int
67 swig_varlink_print(swig_varlinkobject *v, FILE *fp, int flags)
68 {
69
70 int i = 0;
71 flags = flags;
72 fprintf(fp,"Global variables { ");
73 while (v->vars[i]) {
74 fprintf(fp,"%s", v->vars[i]->name);
75 i++;
76 if (v->vars[i]) fprintf(fp,", ");
77 }
78 fprintf(fp," }\n");
79 return 0;
80 }
81
82 /* --------------------------------------------------------------------
83 swig_varlink_getattr
84
85 This function gets the value of a variable and returns it as a
86 PyObject. In our case, we'll be looking at the datatype and
87 converting into a number or string
88 -------------------------------------------------------------------- */
89
90 static PyObject *
91 swig_varlink_getattr(swig_varlinkobject *v, char *n)
92 {
93 int i = 0;
94 char temp[128];
95
96 while (v->vars[i]) {
97 if (strcmp(v->vars[i]->name,n) == 0) {
98 return (*v->vars[i]->get_attr)();
99 }
100 i++;
101 }
102 sprintf(temp,"C global variable %s not found.", n);
103 PyErr_SetString(PyExc_NameError,temp);
104 return NULL;
105 }
106
107 /* -------------------------------------------------------------------
108 swig_varlink_setattr()
109
110 This function sets the value of a variable.
111 ------------------------------------------------------------------- */
112
113 static int
114 swig_varlink_setattr(swig_varlinkobject *v, char *n, PyObject *p)
115 {
116 char temp[128];
117 int i = 0;
118 while (v->vars[i]) {
119 if (strcmp(v->vars[i]->name,n) == 0) {
120 return (*v->vars[i]->set_attr)(p);
121 }
122 i++;
123 }
124 sprintf(temp,"C global variable %s not found.", n);
125 PyErr_SetString(PyExc_NameError,temp);
126 return 1;
127 }
128
129 statichere PyTypeObject varlinktype = {
130 /* PyObject_HEAD_INIT(&PyType_Type) Note : This doesn't work on some machines */
131 PyObject_HEAD_INIT(0)
132 0,
133 "varlink", /* Type name */
134 sizeof(swig_varlinkobject), /* Basic size */
135 0, /* Itemsize */
136 0, /* Deallocator */
137 (printfunc) swig_varlink_print, /* Print */
138 (getattrfunc) swig_varlink_getattr, /* get attr */
139 (setattrfunc) swig_varlink_setattr, /* Set attr */
140 0, /* tp_compare */
141 (reprfunc) swig_varlink_repr, /* tp_repr */
142 0, /* tp_as_number */
143 0, /* tp_as_mapping*/
144 0, /* tp_hash */
145 };
146
147 /* Create a variable linking object for use later */
148
149 SWIGSTATICRUNTIME(PyObject *)
150 SWIG_newvarlink(void)
151 {
152 swig_varlinkobject *result = 0;
153 result = PyMem_NEW(swig_varlinkobject,1);
154 varlinktype.ob_type = &PyType_Type; /* Patch varlinktype into a PyType */
155 result->ob_type = &varlinktype;
156 /* _Py_NewReference(result); Does not seem to be necessary */
157 result->nvars = 0;
158 result->maxvars = 64;
159 result->vars = (swig_globalvar **) malloc(64*sizeof(swig_globalvar *));
160 result->vars[0] = 0;
161 result->ob_refcnt = 0;
162 Py_XINCREF((PyObject *) result);
163 return ((PyObject*) result);
164 }
165
166 SWIGSTATICRUNTIME(void)
167 SWIG_addvarlink(PyObject *p, char *name,
168 PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p))
169 {
170 swig_varlinkobject *v;
171 v= (swig_varlinkobject *) p;
172
173 if (v->nvars >= v->maxvars -1) {
174 v->maxvars = 2*v->maxvars;
175 v->vars = (swig_globalvar **) realloc(v->vars,v->maxvars*sizeof(swig_globalvar *));
176 if (v->vars == NULL) {
177 fprintf(stderr,"SWIG : Fatal error in initializing Python module.\n");
178 exit(1);
179 }
180 }
181 v->vars[v->nvars] = (swig_globalvar *) malloc(sizeof(swig_globalvar));
182 v->vars[v->nvars]->name = (char *) malloc(strlen(name)+1);
183 strcpy(v->vars[v->nvars]->name,name);
184 v->vars[v->nvars]->get_attr = get_attr;
185 v->vars[v->nvars]->set_attr = set_attr;
186 v->nvars++;
187 v->vars[v->nvars] = 0;
188 }
189
190 /* -----------------------------------------------------------------------------
191 * Pointer type-checking
192 * ----------------------------------------------------------------------------- */
193
194 /* SWIG pointer structure */
195 typedef struct SwigPtrType {
196 char *name; /* Datatype name */
197 int len; /* Length (used for optimization) */
198 void *(*cast)(void *); /* Pointer casting function */
199 struct SwigPtrType *next; /* Linked list pointer */
200 } SwigPtrType;
201
202 /* Pointer cache structure */
203 typedef struct {
204 int stat; /* Status (valid) bit */
205 SwigPtrType *tp; /* Pointer to type structure */
206 char name[256]; /* Given datatype name */
207 char mapped[256]; /* Equivalent name */
208 } SwigCacheType;
209
210 static int SwigPtrMax = 64; /* Max entries that can be currently held */
211 static int SwigPtrN = 0; /* Current number of entries */
212 static int SwigPtrSort = 0; /* Status flag indicating sort */
213 static int SwigStart[256]; /* Starting positions of types */
214 static SwigPtrType *SwigPtrTable = 0; /* Table containing pointer equivalences */
215
216 /* Cached values */
217 #define SWIG_CACHESIZE 8
218 #define SWIG_CACHEMASK 0x7
219 static SwigCacheType SwigCache[SWIG_CACHESIZE];
220 static int SwigCacheIndex = 0;
221 static int SwigLastCache = 0;
222
223 /* Sort comparison function */
224 static int swigsort(const void *data1, const void *data2) {
225 SwigPtrType *d1 = (SwigPtrType *) data1;
226 SwigPtrType *d2 = (SwigPtrType *) data2;
227 return strcmp(d1->name,d2->name);
228 }
229
230 /* Register a new datatype with the type-checker */
231 SWIGSTATICRUNTIME(void)
232 SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *)) {
233 int i;
234 SwigPtrType *t = 0,*t1;
235
236 /* Allocate the pointer table if necessary */
237 if (!SwigPtrTable) {
238 SwigPtrTable = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
239 }
240
241 /* Grow the table */
242 if (SwigPtrN >= SwigPtrMax) {
243 SwigPtrMax = 2*SwigPtrMax;
244 SwigPtrTable = (SwigPtrType *) realloc((char *) SwigPtrTable,SwigPtrMax*sizeof(SwigPtrType));
245 }
246 for (i = 0; i < SwigPtrN; i++) {
247 if (strcmp(SwigPtrTable[i].name,origtype) == 0) {
248 t = &SwigPtrTable[i];
249 break;
250 }
251 }
252 if (!t) {
253 t = &SwigPtrTable[SwigPtrN++];
254 t->name = origtype;
255 t->len = strlen(t->name);
256 t->cast = 0;
257 t->next = 0;
258 }
259
260 /* Check for existing entries */
261 while (t->next) {
262 if ((strcmp(t->name,newtype) == 0)) {
263 if (cast) t->cast = cast;
264 return;
265 }
266 t = t->next;
267 }
268 t1 = (SwigPtrType *) malloc(sizeof(SwigPtrType));
269 t1->name = newtype;
270 t1->len = strlen(t1->name);
271 t1->cast = cast;
272 t1->next = 0;
273 t->next = t1;
274 SwigPtrSort = 0;
275 }
276
277 /* Make a pointer value string */
278 SWIGSTATICRUNTIME(void)
279 SWIG_MakePtr(char *c, const void *ptr, char *type) {
280 static char hex[17] = "0123456789abcdef";
281 unsigned long p, s;
282 char result[24], *r;
283 r = result;
284 p = (unsigned long) ptr;
285 if (p > 0) {
286 while (p > 0) {
287 s = p & 0xf;
288 *(r++) = hex[s];
289 p = p >> 4;
290 }
291 *r = '_';
292 while (r >= result)
293 *(c++) = *(r--);
294 strcpy (c, type);
295 } else {
296 strcpy (c, "NULL");
297 }
298 }
299
300 /* Function for getting a pointer value */
301 SWIGSTATICRUNTIME(char *)
302 SWIG_GetPtr(char *c, void **ptr, char *t)
303 {
304 unsigned long p;
305 char temp_type[256], *name;
306 int i, len, start, end;
307 SwigPtrType *sp,*tp;
308 SwigCacheType *cache;
309 register int d;
310
311 p = 0;
312 /* Pointer values must start with leading underscore */
313 if (*c != '_') {
314 *ptr = (void *) 0;
315 if (strcmp(c,"NULL") == 0) return (char *) 0;
316 else c;
317 }
318 c++;
319 /* Extract hex value from pointer */
320 while (d = *c) {
321 if ((d >= '0') && (d <= '9'))
322 p = (p << 4) + (d - '0');
323 else if ((d >= 'a') && (d <= 'f'))
324 p = (p << 4) + (d - ('a'-10));
325 else
326 break;
327 c++;
328 }
329 *ptr = (void *) p;
330 if ((!t) || (strcmp(t,c)==0)) return (char *) 0;
331
332 if (!SwigPtrSort) {
333 qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
334 for (i = 0; i < 256; i++) SwigStart[i] = SwigPtrN;
335 for (i = SwigPtrN-1; i >= 0; i--) SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
336 for (i = 255; i >= 1; i--) {
337 if (SwigStart[i-1] > SwigStart[i])
338 SwigStart[i-1] = SwigStart[i];
339 }
340 SwigPtrSort = 1;
341 for (i = 0; i < SWIG_CACHESIZE; i++) SwigCache[i].stat = 0;
342 }
343 /* First check cache for matches. Uses last cache value as starting point */
344 cache = &SwigCache[SwigLastCache];
345 for (i = 0; i < SWIG_CACHESIZE; i++) {
346 if (cache->stat && (strcmp(t,cache->name) == 0) && (strcmp(c,cache->mapped) == 0)) {
347 cache->stat++;
348 if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
349 return (char *) 0;
350 }
351 SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
352 if (!SwigLastCache) cache = SwigCache;
353 else cache++;
354 }
355 /* Type mismatch. Look through type-mapping table */
356 start = SwigStart[(int) t[1]];
357 end = SwigStart[(int) t[1]+1];
358 sp = &SwigPtrTable[start];
359
360 /* Try to find a match */
361 while (start <= end) {
362 if (strncmp(t,sp->name,sp->len) == 0) {
363 name = sp->name;
364 len = sp->len;
365 tp = sp->next;
366 /* Try to find entry for our given datatype */
367 while(tp) {
368 if (tp->len >= 255) {
369 return c;
370 }
371 strcpy(temp_type,tp->name);
372 strncat(temp_type,t+len,255-tp->len);
373 if (strcmp(c,temp_type) == 0) {
374 strcpy(SwigCache[SwigCacheIndex].mapped,c);
375 strcpy(SwigCache[SwigCacheIndex].name,t);
376 SwigCache[SwigCacheIndex].stat = 1;
377 SwigCache[SwigCacheIndex].tp = tp;
378 SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
379 /* Get pointer value */
380 *ptr = (void *) p;
381 if (tp->cast) *ptr = (*(tp->cast))(*ptr);
382 return (char *) 0;
383 }
384 tp = tp->next;
385 }
386 }
387 sp++;
388 start++;
389 }
390 return c;
391 }
392
393 /* New object-based GetPointer function. This uses the Python abstract
394 * object interface to automatically dereference the 'this' attribute
395 * of shadow objects. */
396
397 SWIGSTATICRUNTIME(char *)
398 SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type) {
399 PyObject *sobj = obj;
400 char *str;
401 if (!PyString_Check(obj)) {
402 if (!PyInstance_Check(obj) || !(sobj = PyObject_GetAttrString(obj,"this")))
403 return "";
404 // sobj = PyObject_GetAttrString(obj,"this");
405 // if (!sobj) return "";
406 }
407 str = PyString_AsString(sobj);
408 return SWIG_GetPtr(str,ptr,type);
409 }
410
411 #ifdef __cplusplus
412 }
413 #endif
414
415