]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/src/libpy.c
Fixed a misplaced } that was causing the wrong encoding to be enumerated
[wxWidgets.git] / utils / 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
8#ifdef __cplusplus
9extern "C" {
10#endif
11#include "Python.h"
6bddd8c5
RD
12
13/* Definitions for Windows/Unix exporting */
1d99702e 14#if defined(_WIN32) || defined(__WIN32__)
6bddd8c5 15# if defined(_MSC_VER)
1d99702e 16# define SWIGEXPORT(a) __declspec(dllexport) a
6bddd8c5
RD
17# else
18# if defined(__BORLANDC__)
1d99702e 19# define SWIGEXPORT(a) a _export
6bddd8c5 20# else
1d99702e 21# define SWIGEXPORT(a) a
6bddd8c5
RD
22# endif
23# endif
24#else
1d99702e 25# define SWIGEXPORT(a) a
6bddd8c5
RD
26#endif
27
28#ifdef SWIG_GLOBAL
1d99702e 29#define SWIGSTATICRUNTIME(a) SWIGEXPORT(a)
6bddd8c5 30#else
1d99702e 31#define SWIGSTATICRUNTIME(a) static a
6bddd8c5
RD
32#endif
33
34typedef struct {
35 char *name;
36 PyObject *(*get_attr)(void);
37 int (*set_attr)(PyObject *);
38} swig_globalvar;
39
40typedef 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
53static PyObject *
54swig_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
66static int
67swig_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
1afc06c2 84
6bddd8c5
RD
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
90static PyObject *
91swig_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
113static int
114swig_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
129statichere PyTypeObject varlinktype = {
130/* PyObject_HEAD_INIT(&PyType_Type) Note : This doesn't work on some machines */
1afc06c2 131 PyObject_HEAD_INIT(0)
6bddd8c5
RD
132 0,
133 "varlink", /* Type name */
134 sizeof(swig_varlinkobject), /* Basic size */
135 0, /* Itemsize */
1afc06c2 136 0, /* Deallocator */
6bddd8c5
RD
137 (printfunc) swig_varlink_print, /* Print */
138 (getattrfunc) swig_varlink_getattr, /* get attr */
139 (setattrfunc) swig_varlink_setattr, /* Set attr */
140 0, /* tp_compare */
1afc06c2 141 (reprfunc) swig_varlink_repr, /* tp_repr */
6bddd8c5
RD
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
1d99702e 149SWIGSTATICRUNTIME(PyObject *)
6bddd8c5
RD
150SWIG_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
1d99702e 166SWIGSTATICRUNTIME(void)
6bddd8c5
RD
167SWIG_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;
1afc06c2 172
6bddd8c5
RD
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
1d99702e
RD
190/* -----------------------------------------------------------------------------
191 * Pointer type-checking
192 * ----------------------------------------------------------------------------- */
193
194/* SWIG pointer structure */
195typedef 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 */
203typedef 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
210static int SwigPtrMax = 64; /* Max entries that can be currently held */
211static int SwigPtrN = 0; /* Current number of entries */
212static int SwigPtrSort = 0; /* Status flag indicating sort */
213static int SwigStart[256]; /* Starting positions of types */
214static SwigPtrType *SwigPtrTable = 0; /* Table containing pointer equivalences */
215
216/* Cached values */
217#define SWIG_CACHESIZE 8
218#define SWIG_CACHEMASK 0x7
1afc06c2 219static SwigCacheType SwigCache[SWIG_CACHESIZE];
1d99702e
RD
220static int SwigCacheIndex = 0;
221static int SwigLastCache = 0;
222
223/* Sort comparison function */
224static 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 */
1afc06c2 231SWIGSTATICRUNTIME(void)
1d99702e
RD
232SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *)) {
233 int i;
234 SwigPtrType *t = 0,*t1;
235
236 /* Allocate the pointer table if necessary */
1afc06c2 237 if (!SwigPtrTable) {
1d99702e
RD
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;
1afc06c2
RD
272 t1->next = 0;
273 t->next = t1;
1d99702e
RD
274 SwigPtrSort = 0;
275}
276
277/* Make a pointer value string */
1afc06c2 278SWIGSTATICRUNTIME(void)
1d99702e
RD
279SWIG_MakePtr(char *c, const void *ptr, char *type) {
280 static char hex[17] = "0123456789abcdef";
281 unsigned long p, s;
1afc06c2 282 char result[24], *r;
1d99702e
RD
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 */
1afc06c2 301SWIGSTATICRUNTIME(char *)
1d99702e
RD
302SWIG_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
1afc06c2 326 break;
1d99702e
RD
327 c++;
328 }
329 *ptr = (void *) p;
330 if ((!t) || (strcmp(t,c)==0)) return (char *) 0;
331
332 if (!SwigPtrSort) {
1afc06c2 333 qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
1d99702e
RD
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;
1afc06c2 391}
1d99702e
RD
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
397SWIGSTATICRUNTIME(char *)
398SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type) {
399 PyObject *sobj = obj;
400 char *str;
401 if (!PyString_Check(obj)) {
1afc06c2
RD
402 if (!PyInstance_Check(obj) || !(sobj = PyObject_GetAttrString(obj,"this")))
403 return "";
404// sobj = PyObject_GetAttrString(obj,"this");
405// if (!sobj) return "";
1d99702e
RD
406 }
407 str = PyString_AsString(sobj);
408 return SWIG_GetPtr(str,ptr,type);
409}
410
411#ifdef __cplusplus
412}
413#endif
6bddd8c5
RD
414
415