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