📄 typemaps_python.i
字号:
{ /* %typemap(freearg) (int *nLen, char **pBuf ) */ if( *$1 ) { free( *$2 ); }}%typemap(in,numinputs=1) (int nLen, char *pBuf ){ /* %typemap(in,numinputs=1) (int nLen, char *pBuf ) */ PyString_AsStringAndSize($input, &$2, &$1 );}%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (int nLen, char *pBuf){ /* %typecheck(SWIG_TYPECHECK_POINTER) (int nLen, char *pBuf) */ $1 = (PyString_Check($input)) ? 1 : 0;}/* * Typemap argout of GDAL_GCP* used in Dataset::GetGCPs( ) */%typemap(in,numinputs=0) (int *nGCPs, GDAL_GCP const **pGCPs ) (int nGCPs=0, GDAL_GCP *pGCPs=0 ){ /* %typemap(in,numinputs=0) (int *nGCPs, GDAL_GCP const **pGCPs ) */ $1 = &nGCPs; $2 = &pGCPs;}%typemap(argout) (int *nGCPs, GDAL_GCP const **pGCPs ){ /* %typemap(argout) (int *nGCPs, GDAL_GCP const **pGCPs ) */ PyObject *dict = PyTuple_New( *$1 ); for( int i = 0; i < *$1; i++ ) { GDAL_GCP *o = new_GDAL_GCP( (*$2)[i].dfGCPX, (*$2)[i].dfGCPY, (*$2)[i].dfGCPZ, (*$2)[i].dfGCPPixel, (*$2)[i].dfGCPLine, (*$2)[i].pszInfo, (*$2)[i].pszId ); PyTuple_SetItem(dict, i, SWIG_NewPointerObj((void*)o,SWIGTYPE_p_GDAL_GCP,1) ); } Py_DECREF($result); $result = dict;}%typemap(in,numinputs=1) (int nGCPs, GDAL_GCP const *pGCPs ) ( GDAL_GCP *tmpGCPList ){ /* %typemap(in,numinputs=1) (int nGCPs, GDAL_GCP const *pGCPs ) */ /* check if is List */ if ( !PySequence_Check($input) ) { PyErr_SetString(PyExc_TypeError, "not a sequence"); SWIG_fail; } $1 = PySequence_Size($input); tmpGCPList = (GDAL_GCP*) malloc($1*sizeof(GDAL_GCP)); $2 = tmpGCPList; for( int i = 0; i<$1; i++ ) { PyObject *o = PySequence_GetItem($input,i); GDAL_GCP *item = 0; SWIG_ConvertPtr( o, (void**)&item, SWIGTYPE_p_GDAL_GCP, SWIG_POINTER_EXCEPTION | 0 ); if ( ! item ) { SWIG_fail; } memcpy( (void*) tmpGCPList, (void*) item, sizeof( GDAL_GCP ) ); ++tmpGCPList; }}%typemap(freearg) (int nGCPs, GDAL_GCP const *pGCPs ){ /* %typemap(freearg) (int nGCPs, GDAL_GCP const *pGCPs ) */ if ($2) { free( (void*) $2 ); }}/* * Typemap for GDALColorEntry* <-> tuple */%typemap(out) GDALColorEntry*{ /* %typemap(out) GDALColorEntry* */ $result = Py_BuildValue( "(hhhh)", (*$1).c1,(*$1).c2,(*$1).c3,(*$1).c4);}%typemap(in) GDALColorEntry* (GDALColorEntry ce){ /* %typemap(in) GDALColorEntry* */ ce.c4 = 255; int size = PySequence_Size($input); if ( size > 4 ) { PyErr_SetString(PyExc_TypeError, "ColorEntry sequence too long"); SWIG_fail; } if ( size < 3 ) { PyErr_SetString(PyExc_TypeError, "ColorEntry sequence too short"); SWIG_fail; } PyArg_ParseTuple( $input,"hhh|h", &ce.c1, &ce.c2, &ce.c3, &ce.c4 ); $1 = &ce;}/* * Typemap char ** -> dict */%typemap(out) char **dict{ /* %typemap(out) char **dict */ char **stringarray = $1; $result = PyDict_New(); if ( stringarray != NULL ) { while (*stringarray != NULL ) { char const *valptr; char *keyptr; valptr = CPLParseNameValue( *stringarray, &keyptr ); if ( valptr != 0 ) { PyObject *nm = PyString_FromString( keyptr ); PyObject *val = PyString_FromString( valptr ); PyDict_SetItem($result, nm, val ); CPLFree( keyptr ); } stringarray++; } }}/* * Typemap char **<- dict. This typemap actually supports lists as well, * Then each entry in the list must be a string and have the form: * "name=value" so gdal can handle it. */%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (char **dict){ /* %typecheck(SWIG_TYPECHECK_POINTER) (char **dict) */ $1 = (PyMapping_Check($input) || PySequence_Check($input) ) ? 1 : 0;}%typemap(in) char **dict{ /* %typemap(in) char **dict */ $1 = NULL; if ( PySequence_Check( $input ) ) { int size = PySequence_Size($input); for (int i = 0; i < size; i++) { char *pszItem = NULL; if ( ! PyArg_Parse( PySequence_GetItem($input,i), "s", &pszItem ) ) { PyErr_SetString(PyExc_TypeError,"sequence must contain strings"); SWIG_fail; } $1 = CSLAddString( $1, pszItem ); } } else if ( PyMapping_Check( $input ) ) { /* We need to use the dictionary form. */ int size = PyMapping_Length( $input ); if ( size > 0 ) { PyObject *item_list = PyMapping_Items( $input ); for( int i=0; i<size; i++ ) { PyObject *it = PySequence_GetItem( item_list, i ); char *nm; char *val; PyArg_ParseTuple( it, "ss", &nm, &val ); $1 = CSLAddNameValue( $1, nm, val ); } } } else { PyErr_SetString(PyExc_TypeError,"Argument must be dictionary or sequence of strings"); SWIG_fail; }}%typemap(freearg) char **dict{ /* %typemap(freearg) char **dict */ CSLDestroy( $1 );}/* * Typemap maps char** arguments from Python Sequence Object */%typemap(in) char **options{ /* %typemap(in) char **options */ /* Check if is a list */ if ( ! PySequence_Check($input)) { PyErr_SetString(PyExc_TypeError,"not a sequence"); SWIG_fail; } int size = PySequence_Size($input); for (int i = 0; i < size; i++) { char *pszItem = NULL; if ( ! PyArg_Parse( PySequence_GetItem($input,i), "s", &pszItem ) ) { PyErr_SetString(PyExc_TypeError,"sequence must contain strings"); SWIG_fail; } $1 = CSLAddString( $1, pszItem ); }}%typemap(freearg) char **options{ /* %typemap(freearg) char **options */ CSLDestroy( $1 );}/* * Typemap converts an array of strings into a list of strings * with the assumption that the called object maintains ownership of the * array of strings. */%typemap(out) char **options{ /* %typemap(out) char ** -> ( string ) */ char **stringarray = $1; if ( stringarray == NULL ) { $result = Py_None; Py_INCREF( $result ); } else { int len = CSLCount( stringarray ); $result = PyList_New( len ); for ( int i = 0; i < len; ++i, ++stringarray ) { PyObject *o = PyString_FromString( *stringarray ); PyList_SetItem($result, i, o ); } }}/* * Typemaps map mutable char ** arguments from PyStrings. Does not * return the modified argument */%typemap(in) (char **ignorechange) ( char *val ){ /* %typemap(in) (char **ignorechange) */ PyArg_Parse( $input, "s", &val ); $1 = &val;}/* * Typemap for char **argout. */%typemap(in,numinputs=0) (char **argout) ( char *argout=0 ){ /* %typemap(in,numinputs=0) (char **argout) */ $1 = &argout;}%typemap(argout,fragment="t_output_helper") (char **argout){ /* %typemap(argout) (char **argout) */ PyObject *o; if ( $1 ) { o = PyString_FromString( *$1 ); } else { o = Py_None; Py_INCREF( o ); } $result = t_output_helper($result, o);}%typemap(freearg) (char **argout){ /* %typemap(freearg) (char **argout) */ if ( *$1 ) CPLFree( *$1 );}/* * Typemap for an optional POD argument. * Declare function to take POD *. If the parameter * is NULL then the function needs to define a default * value. */%define OPTIONAL_POD(type,argstring)%typemap(in) (type *optional_##type) ( type val ){ /* %typemap(in) (type *optional_##type) */ if ( $input == Py_None ) { $1 = 0; } else if ( PyArg_Parse( $input, #argstring ,&val ) ) { $1 = ($1_type) &val; } else { PyErr_SetString( PyExc_TypeError, "Invalid Parameter" ); SWIG_fail; }}%typemap(typecheck,precedence=0) (type *optional_##type){ /* %typemap(typecheck,precedence=0) (type *optionalInt) */ $1 = (($input==Py_None) || my_PyCheck_##type($input)) ? 1 : 0;}%enddefOPTIONAL_POD(int,i);/* * Typedef const char * <- Any object. * * Formats the object using str and returns the string representation */%typemap(in) (tostring argin) (PyObject *str){ /* %typemap(in) (tostring argin) */ str = PyObject_Str( $input ); if ( str == 0 ) { PyErr_SetString( PyExc_RuntimeError, "Unable to format argument as string"); SWIG_fail; } $1 = PyString_AsString(str); }%typemap(freearg)(tostring argin){ /* %typemap(freearg) (tostring argin) */ Py_DECREF(str$argnum);}%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (tostring argin){ /* %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (tostring argin) */ $1 = 1;}/* * Typemap for CPLErr. * This typemap will use the wrapper C-variable * int UseExceptions to determine proper behavour for * CPLErr return codes. * If UseExceptions ==0, then return the rc. * If UseExceptions ==1, then if rc >= CE_Failure, raise an exception. */%typemap(ret) CPLErr{ /* %typemap(ret) CPLErr */ if ( bUseExceptions == 0 ) { /* We're not using exceptions. And no error has occurred */ if ( $result == 0 ) { /* No other return values set so return ErrorCode */ $result = PyInt_FromLong($1); } }}/* * Typemaps for minixml: CPLXMLNode* input, CPLXMLNode *ret */%fragment("PyListToXMLTree","header") %{/************************************************************************//* PyListToXMLTree() *//************************************************************************/static CPLXMLNode *PyListToXMLTree( PyObject *pyList ){ int nChildCount = 0, iChild, nType; CPLXMLNode *psThisNode; CPLXMLNode *psChild; char *pszText = NULL; nChildCount = PyList_Size(pyList) - 2; if( nChildCount < 0 ) { PyErr_SetString(PyExc_TypeError,"Error in input XMLTree." ); return NULL; } PyArg_Parse( PyList_GET_ITEM(pyList,0), "i", &nType ); PyArg_Parse( PyList_GET_ITEM(pyList,1), "s", &pszText ); psThisNode = CPLCreateXMLNode( NULL, (CPLXMLNodeType) nType, pszText ); for( iChild = 0; iChild < nChildCount; iChild++ ) { psChild = PyListToXMLTree( PyList_GET_ITEM(pyList,iChild+2) ); CPLAddXMLChild( psThisNode, psChild ); } return psThisNode;}%}%typemap(in,fragment="PyListToXMLTree") (CPLXMLNode* xmlnode ){ /* %typemap(python,in) (CPLXMLNode* xmlnode ) */ $1 = PyListToXMLTree( $input ); if ( !$1 ) SWIG_fail;}%typemap(freearg) (CPLXMLNode *xmlnode){ /* %typemap(freearg) (CPLXMLNode *xmlnode) */ if ( $1 ) CPLDestroyXMLNode( $1 );}%fragment("XMLTreeToPyList","header") %{/************************************************************************//* XMLTreeToPyList() *//************************************************************************/static PyObject *XMLTreeToPyList( CPLXMLNode *psTree ){ PyObject *pyList; int nChildCount = 0, iChild; CPLXMLNode *psChild; for( psChild = psTree->psChild; psChild != NULL; psChild = psChild->psNext ) nChildCount++; pyList = PyList_New(nChildCount+2); PyList_SetItem( pyList, 0, Py_BuildValue( "i", (int) psTree->eType ) ); PyList_SetItem( pyList, 1, Py_BuildValue( "s", psTree->pszValue ) ); for( psChild = psTree->psChild, iChild = 2; psChild != NULL; psChild = psChild->psNext, iChild++ ) { PyList_SetItem( pyList, iChild, XMLTreeToPyList( psChild ) ); } return pyList; }%}%typemap(out,fragment="XMLTreeToPyList") (CPLXMLNode*){ /* %typemap(out) (CPLXMLNode*) */ $result = XMLTreeToPyList( $1 );}%typemap(ret) (CPLXMLNode*){ /* %typemap(ret) (CPLXMLNode*) */ if ( $1 ) CPLDestroyXMLNode( $1 );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -