1 /*****************************************************************************
 
   6  * This file contains supporting code for the SWIG run-time type checking
 
   7  * mechanism.  The following functions are available :
 
   9  * SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *));
 
  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++)
 
  16  * SWIG_MakePtr(char *buffer, void *ptr, char *typestring);
 
  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.
 
  21  * char * SWIG_GetPtr(char *buffer, void **ptr, char *type)
 
  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.
 
  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.
 
  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
 
  33  *****************************************************************************/
 
  46 #define SWIGSTATIC static
 
  51 /* SWIG pointer structure */
 
  53 typedef struct SwigPtrType {
 
  54   char               *name;               /* Datatype name                  */
 
  55   int                 len;                /* Length (used for optimization) */
 
  56   void               *(*cast)(void *);    /* Pointer casting function       */
 
  57   struct SwigPtrType *next;               /* Linked list pointer            */
 
  60 /* Pointer cache structure */
 
  63   int                 stat;               /* Status (valid) bit             */
 
  64   SwigPtrType        *tp;                 /* Pointer to type structure      */
 
  65   char                name[256];          /* Given datatype name            */
 
  66   char                mapped[256];        /* Equivalent name                */
 
  71 static int SwigPtrMax  = 64;           /* Max entries that can be currently held */
 
  72                                        /* This value may be adjusted dynamically */
 
  73 static int SwigPtrN    = 0;            /* Current number of entries              */
 
  74 static int SwigPtrSort = 0;            /* Status flag indicating sort            */
 
  75 static int SwigStart[256];             /* Starting positions of types            */
 
  78 static SwigPtrType *SwigPtrTable = 0;  /* Table containing pointer equivalences  */
 
  82 #define SWIG_CACHESIZE  8
 
  83 #define SWIG_CACHEMASK  0x7
 
  84 static SwigCacheType SwigCache[SWIG_CACHESIZE];  
 
  85 static int SwigCacheIndex = 0;
 
  86 static int SwigLastCache = 0;
 
  88 /* Sort comparison function */
 
  89 static int swigsort(const void *data1, const void *data2) {
 
  90         SwigPtrType *d1 = (SwigPtrType *) data1;
 
  91         SwigPtrType *d2 = (SwigPtrType *) data2;
 
  92         return strcmp(d1->name,d2->name);
 
  95 /* Binary Search function */
 
  96 static int swigcmp(const void *key, const void *data) {
 
  97   char *k = (char *) key;
 
  98   SwigPtrType *d = (SwigPtrType *) data;
 
  99   return strncmp(k,d->name,d->len);
 
 102 /* Register a new datatype with the type-checker */
 
 105 void SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *)) {
 
 108   SwigPtrType *t = 0,*t1;
 
 110   /* Allocate the pointer table if necessary */
 
 113     SwigPtrTable = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
 
 117   if (SwigPtrN >= SwigPtrMax) {
 
 118     SwigPtrMax = 2*SwigPtrMax;
 
 119     SwigPtrTable = (SwigPtrType *) realloc((char *) SwigPtrTable,SwigPtrMax*sizeof(SwigPtrType));
 
 121   for (i = 0; i < SwigPtrN; i++)
 
 122     if (strcmp(SwigPtrTable[i].name,origtype) == 0) {
 
 123       t = &SwigPtrTable[i];
 
 127     t = &SwigPtrTable[SwigPtrN];
 
 129     t->len = strlen(t->name);
 
 135   /* Check for existing entry */
 
 138     if ((strcmp(t->name,newtype) == 0)) {
 
 139       if (cast) t->cast = cast;
 
 145   /* Now place entry (in sorted order) */
 
 147   t1 = (SwigPtrType *) malloc(sizeof(SwigPtrType));
 
 149   t1->len = strlen(t1->name);
 
 156 /* Make a pointer value string */
 
 159 void SWIG_MakePtr(char *_c, const void *_ptr, char *type) {
 
 160   static char _hex[16] =
 
 161   {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 
 162    'a', 'b', 'c', 'd', 'e', 'f'};
 
 163   unsigned long _p, _s;
 
 164   char _result[20], *_r;    /* Note : a 64-bit hex number = 16 digits */
 
 166   _p = (unsigned long) _ptr;
 
 174     while (_r >= _result)
 
 183 /* Define for backwards compatibility */
 
 185 #define _swig_make_hex   SWIG_MakePtr 
 
 187 /* Function for getting a pointer value */
 
 190 char *SWIG_GetPtr(char *_c, void **ptr, char *_t)
 
 197   SwigCacheType *cache;
 
 201   /* Pointer values must start with leading underscore */
 
 204       /* Extract hex value from pointer */
 
 206           if ((*_c >= '0') && (*_c <= '9'))
 
 207             _p = (_p << 4) + (*_c - '0');
 
 208           else if ((*_c >= 'a') && (*_c <= 'f'))
 
 209             _p = (_p << 4) + ((*_c - 'a') + 10);
 
 218             qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort); 
 
 219             for (i = 0; i < 256; i++) {
 
 220               SwigStart[i] = SwigPtrN;
 
 222             for (i = SwigPtrN-1; i >= 0; i--) {
 
 223               SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
 
 225             for (i = 255; i >= 1; i--) {
 
 226               if (SwigStart[i-1] > SwigStart[i])
 
 227                 SwigStart[i-1] = SwigStart[i];
 
 230             for (i = 0; i < SWIG_CACHESIZE; i++)  
 
 231               SwigCache[i].stat = 0;
 
 234           /* First check cache for matches.  Uses last cache value as starting point */
 
 235           cache = &SwigCache[SwigLastCache];
 
 236           for (i = 0; i < SWIG_CACHESIZE; i++) {
 
 238               if (strcmp(_t,cache->name) == 0) {
 
 239                 if (strcmp(_c,cache->mapped) == 0) {
 
 242                   if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
 
 247             SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
 
 248             if (!SwigLastCache) cache = SwigCache;
 
 251           /* We have a type mismatch.  Will have to look through our type
 
 252              mapping table to figure out whether or not we can accept this datatype */
 
 254           start = SwigStart[(int) _t[1]];
 
 255           end = SwigStart[(int) _t[1]+1];
 
 256           sp = &SwigPtrTable[start];
 
 257           while (start < end) {
 
 258             if (swigcmp(_t,sp) == 0) break;
 
 262           if (start > end) sp = 0;
 
 263           /* Try to find a match for this */
 
 264           while (start <= end) {
 
 265             if (swigcmp(_t,sp) == 0) {
 
 269               /* Try to find entry for our given datatype */
 
 271                 if (tp->len >= 255) {
 
 274                 strcpy(temp_type,tp->name);
 
 275                 strncat(temp_type,_t+len,255-tp->len);
 
 276                 if (strcmp(_c,temp_type) == 0) {
 
 278                   strcpy(SwigCache[SwigCacheIndex].mapped,_c);
 
 279                   strcpy(SwigCache[SwigCacheIndex].name,_t);
 
 280                   SwigCache[SwigCacheIndex].stat = 1;
 
 281                   SwigCache[SwigCacheIndex].tp = tp;
 
 282                   SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
 
 284                   /* Get pointer value */
 
 286                   if (tp->cast) *ptr = (*(tp->cast))(*ptr);
 
 295           /* Didn't find any sort of match for this data.  
 
 296              Get the pointer value and return the received type */
 
 300           /* Found a match on the first try.  Return pointer value */
 
 305         /* No type specified.  Good luck */
 
 310     if (strcmp (_c, "NULL") == 0) {
 
 319 /* Compatibility mode */
 
 321 #define _swig_get_hex  SWIG_GetPtr