Advertisement
vipulraheja

ideal ossimInit.i

Jul 6th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. /*-----------------------------------------------------------------------------
  2. Filename        : ossimInit.i
  3. Author          : Vipul Raheja
  4. License         : See top level LICENSE.txt file.
  5. Description     : Contains SWIG-Python of class ossimInit which handles
  6.                   all aspects of initialization for OSSIM applications.
  7. -----------------------------------------------------------------------------*/
  8.  
  9. %module pyossim
  10.  
  11. %{
  12.  
  13. #include <ossim/init/ossimInit.h>
  14. #include <ossim/base/ossimFilename.h>        
  15. #include <ossim/base/ossimConstants.h>
  16.  
  17. %}
  18.  
  19. %include "typemaps.i"
  20.  
  21. /* Handling the std::exception */
  22. %include "exception.i"
  23. %exception
  24. {
  25.         try
  26.         {
  27.                 $action
  28.         }
  29.         catch(const std::exception& e)
  30.         {
  31.                 SWIG_exception(SWIG_RuntimeError, e.what());
  32.         }
  33. }
  34.  
  35. /* This tells SWIG to treat char ** as a special case */
  36. %typemap(in) char **
  37. {
  38.         /* Check if input is a list */
  39.         if (PyList_Check($input))
  40.         {
  41.                 int size = PyList_Size($input);
  42.                 int i = 0;
  43.  
  44.                 /* Allocate memory */
  45.                 $1 = (char **) malloc((size+1)*sizeof(char *));
  46.  
  47.                 for (i = 0; i < size; i++)
  48.                 {
  49.                         PyObject *o = PyList_GetItem($input,i);
  50.  
  51.                         if (PyString_Check(o))
  52.                         {
  53.                                 $1[i] = PyString_AsString(PyList_GetItem($input,i));
  54.                         }
  55.  
  56.                         else
  57.                         {
  58.                                 PyErr_SetString(PyExc_TypeError,"List must contain strings");
  59.                                 free($1);
  60.                                 return NULL;
  61.                         }
  62.                 }
  63.                 $1[i] = 0;
  64.         }
  65.  
  66.         else
  67.         {
  68.                 PyErr_SetString(PyExc_TypeError,"not a list");
  69.                 return NULL;
  70.         }
  71. }
  72.  
  73. /* To return char** from a C function as Python List */
  74. %typemap(out) char**
  75. {
  76.         int i;
  77.         int len = 0;
  78.  
  79.         while ($1[len])
  80.         {
  81.                 len++;
  82.         }
  83.  
  84.         $result = PyList_New(len);
  85.  
  86.         for (i = 0; i < len; i++)
  87.         {
  88.                 PyList_SetItem($result, i, PyString_FromString($1[i]));
  89.         }
  90. }
  91.  
  92.  
  93. /* Handling Init Assignment operator */
  94. %rename(__setattr__) ossimInit::operator=;
  95.  
  96.  
  97. /* Include the header file containing the declarations to be wrapped */
  98. %include "ossim/init/ossimInit.h"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement