PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/gdal-1.9.1-fedora/swig/include/perl/typemaps_perl.i

#
Swig | 1139 lines | 841 code | 62 blank | 236 comment | 0 complexity | 42d313394e04669cadd3b0516adfb160 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0
  1. /*
  2. * Perl typemaps for GDAL SWIG bindings
  3. * Copyright Ari Jolma 2005. Based on typemaps_python.i
  4. * You may distribute this file under the same terms as GDAL itself.
  5. */
  6. /*
  7. * Include the typemaps from swig library for returning of
  8. * standard types through arguments.
  9. */
  10. %include "typemaps.i"
  11. %apply (long *OUTPUT) { long *argout };
  12. %apply (double *OUTPUT) { double *argout };
  13. %apply (double *OUTPUT) { double *defaultval };
  14. /*
  15. * double *val, int*hasval, is a special contrived typemap used for
  16. * the RasterBand GetNoDataValue, GetMinimum, GetMaximum, GetOffset, GetScale methods.
  17. * the variable hasval is tested. If it is false (is, the value
  18. * is not set in the raster band) then undef is returned. If is is != 0, then
  19. * the value is coerced into a long and returned.
  20. */
  21. %typemap(in,numinputs=0) (double *val, int *hasval) ( double tmpval, int tmphasval ) {
  22. /* %typemap(in,numinputs=0) (double *val, int *hasval) */
  23. $1 = &tmpval;
  24. $2 = &tmphasval;
  25. }
  26. %typemap(argout) (double *val, int *hasval) {
  27. /* %typemap(argout) (double *val, int *hasval) */
  28. $result = sv_newmortal();
  29. if ( *$2 )
  30. sv_setnv($result, *$1);
  31. argvi++;
  32. }
  33. %typemap(out) GIntBig
  34. {
  35. /* %typemap(out) GIntBig */
  36. $result = sv_newmortal();
  37. sv_setiv($result, (IV) $1);
  38. argvi++;
  39. }
  40. %typemap(out) const char *
  41. {
  42. /* %typemap(out) const char * */
  43. $result = newSVpv(result, 0);
  44. SvUTF8_on($result); /* expecting GDAL to give us UTF-8 */
  45. sv_2mortal($result);
  46. argvi++;
  47. }
  48. %typemap(out) (char **CSL)
  49. {
  50. /* %typemap(out) char **CSL */
  51. if (GIMME_V == G_ARRAY) {
  52. if ($1) {
  53. int i;
  54. for (i = 0; $1[i]; i++) {
  55. if (argvi > items-1) EXTEND(SP, 1);
  56. SV *sv = newSVpv($1[i], 0);
  57. SvUTF8_on(sv); /* expecting GDAL to give us UTF-8 */
  58. ST(argvi++) = sv_2mortal(sv);
  59. }
  60. CSLDestroy($1);
  61. }
  62. } else {
  63. AV *av = (AV*)sv_2mortal((SV*)newAV());
  64. if ($1) {
  65. int i;
  66. for (i = 0; $1[i]; i++) {
  67. SV *sv = newSVpv($1[i], 0);
  68. SvUTF8_on(sv); /* expecting GDAL to give us UTF-8 */
  69. if (!av_store(av, i, sv))
  70. SvREFCNT_dec(sv);
  71. }
  72. CSLDestroy($1);
  73. }
  74. $result = newRV_noinc((SV*)av);
  75. argvi++;
  76. }
  77. }
  78. %typemap(out) (char **CSL_REF)
  79. {
  80. /* %typemap(out) char **CSL_REF */
  81. AV *av = (AV*)sv_2mortal((SV*)newAV());
  82. if ($1) {
  83. int i;
  84. for (i = 0; $1[i]; i++) {
  85. SV *sv = newSVpv($1[i], 0);
  86. SvUTF8_on(sv); /* expecting GDAL to give us UTF-8 */
  87. if (!av_store(av, i, sv))
  88. SvREFCNT_dec(sv);
  89. }
  90. CSLDestroy($1);
  91. }
  92. $result = newRV_noinc((SV*)av);
  93. argvi++;
  94. }
  95. %typemap(out) (char **free)
  96. {
  97. /* %typemap(out) char **free */
  98. AV *av = (AV*)sv_2mortal((SV*)newAV());
  99. if ($1) {
  100. int i;
  101. for (i = 0; $1[i]; i++) {
  102. av_store(av, i, newSVpv($1[0], 0));
  103. }
  104. CPLFree($1);
  105. }
  106. $result = newRV_noinc((SV*)av);
  107. argvi++;
  108. }
  109. /* drop GDAL return value */
  110. %typemap(out) IF_FALSE_RETURN_NONE
  111. {
  112. /* %typemap(out) IF_FALSE_RETURN_NONE */
  113. }
  114. /* croak if GDAL return FALSE */
  115. %typemap(ret) IF_FALSE_RETURN_NONE
  116. {
  117. /* %typemap(ret) IF_FALSE_RETURN_NONE */
  118. if ($1 == 0 ) {
  119. SWIG_croak("unexpected error in $symname");
  120. }
  121. }
  122. /* drop GDAL return value */
  123. %typemap(out) RETURN_NONE_TRUE_IS_ERROR
  124. {
  125. /* %typemap(out) RETURN_NONE_TRUE_IS_ERROR */
  126. }
  127. /* croak if GDAL return TRUE */
  128. %typemap(ret) RETURN_NONE_TRUE_IS_ERROR
  129. {
  130. /* %typemap(ret) RETURN_NONE_TRUE_IS_ERROR */
  131. if ($1 != 0 ) {
  132. SWIG_croak("unexpected error in $symname");
  133. }
  134. }
  135. /* drop GDAL return value */
  136. %typemap(out) IF_ERROR_RETURN_NONE
  137. {
  138. /* %typemap(out) IF_ERROR_RETURN_NONE */
  139. }
  140. /*
  141. * SWIG macro to define fixed length array typemaps
  142. * defines three different typemaps.
  143. *
  144. * 1) For argument in. The wrapped function's prototype is:
  145. *
  146. * FunctionOfDouble3( double *vector );
  147. *
  148. * The function assumes that vector points to three consecutive doubles.
  149. * This can be wrapped using:
  150. *
  151. * %apply (double_3 argin) { (double *vector) };
  152. * FunctionOfDouble3( double *vector );
  153. * %clear (double *vector);
  154. *
  155. * Example: Dataset.SetGeoTransform().
  156. *
  157. * 2) Functions which modify a fixed length array passed as
  158. * an argument or return data in an array allocated by the
  159. * caller.
  160. *
  161. * %apply (double_6 argout ) { (double *vector) };
  162. * GetVector6( double *vector );
  163. * %clear ( double *vector );
  164. *
  165. * Example: Dataset.GetGeoTransform().
  166. *
  167. * 3) Functions which take a double **. Through this argument it
  168. * returns a pointer to a fixed size array allocated with CPLMalloc.
  169. *
  170. * %apply (double_17 *argoug) { (double **vector) };
  171. * ReturnVector17( double **vector );
  172. * %clear ( double **vector );
  173. *
  174. * Example: SpatialReference.ExportToPCI().
  175. *
  176. */
  177. %fragment("CreateArrayFromIntArray","header") %{
  178. static SV *
  179. CreateArrayFromIntArray( int *first, unsigned int size ) {
  180. AV *av = (AV*)sv_2mortal((SV*)newAV());
  181. for( unsigned int i=0; i<size; i++ ) {
  182. av_store(av,i,newSViv(*first));
  183. ++first;
  184. }
  185. return newRV_noinc((SV*)av);
  186. }
  187. %}
  188. %fragment("CreateArrayFromDoubleArray","header") %{
  189. static SV *
  190. CreateArrayFromDoubleArray( double *first, unsigned int size ) {
  191. AV *av = (AV*)sv_2mortal((SV*)newAV());
  192. for( unsigned int i=0; i<size; i++ ) {
  193. av_store(av,i,newSVnv(*first));
  194. ++first;
  195. }
  196. return newRV_noinc((SV*)av);
  197. }
  198. %}
  199. %fragment("CreateArrayFromStringArray","header") %{
  200. static SV *
  201. CreateArrayFromStringArray( char **first ) {
  202. AV *av = (AV*)sv_2mortal((SV*)newAV());
  203. for( unsigned int i = 0; *first != NULL; i++ ) {
  204. SV *sv = newSVpv(*first, strlen(*first));
  205. SvUTF8_on(sv); /* expecting UTF-8 from GDAL */
  206. av_store(av,i,sv);
  207. ++first;
  208. }
  209. return newRV_noinc((SV*)av);
  210. }
  211. %}
  212. %typemap(in,numinputs=0) (int *nLen, const int **pList) (int nLen, int *pList)
  213. {
  214. /* %typemap(in,numinputs=0) (int *nLen, const int **pList) */
  215. $1 = &nLen;
  216. $2 = &pList;
  217. }
  218. %typemap(argout,fragment="CreateArrayFromIntArray") (int *nLen, const int **pList)
  219. {
  220. /* %typemap(argout) (int *nLen, const int **pList) */
  221. $result = CreateArrayFromIntArray( *($2), *($1) );
  222. argvi++;
  223. }
  224. %typemap(in,numinputs=1) (int len, int *output)
  225. {
  226. /* %typemap(in,numinputs=1) (int len, int *output) */
  227. $1 = SvIV($input);
  228. }
  229. %typemap(check) (int len, int *output)
  230. {
  231. /* %typemap(check) (int len, int *output) */
  232. if ($1 < 1) $1 = 1; /* stop idiocy */
  233. $2 = (int *)CPLMalloc( $1 * sizeof(int) );
  234. }
  235. %typemap(argout,fragment="CreateArrayFromIntArray") (int len, int *output)
  236. {
  237. /* %typemap(argout) (int len, int *output) */
  238. if (GIMME_V == G_ARRAY) {
  239. /* return a list */
  240. int i;
  241. EXTEND(SP, argvi+$1-items+1);
  242. for (i = 0; i < $1; i++)
  243. ST(argvi++) = sv_2mortal(newSVnv($2[i]));
  244. } else {
  245. $result = CreateArrayFromIntArray( $2, $1 );
  246. argvi++;
  247. }
  248. }
  249. %typemap(freearg) (int len, int *output)
  250. {
  251. /* %typemap(freearg) (int len, int *output) */
  252. CPLFree($2);
  253. }
  254. %typemap(in,numinputs=0) (int *nLen, const double **pList) (int nLen, double *pList)
  255. {
  256. /* %typemap(in,numinputs=0) (int *nLen, const double **pList) */
  257. $1 = &nLen;
  258. $2 = &pList;
  259. }
  260. %typemap(argout,fragment="CreateArrayFromDoubleArray") (int *nLen, const double **pList)
  261. {
  262. /* %typemap(argout) (int *nLen, const double **pList) */
  263. $result = CreateArrayFromDoubleArray( *($2), *($1) );
  264. argvi++;
  265. }
  266. %typemap(in,numinputs=0) (char ***pList) (char **pList)
  267. {
  268. /* %typemap(in,numinputs=0) (char ***pList) */
  269. $1 = &pList;
  270. }
  271. %typemap(argout,fragment="CreateArrayFromStringArray") (char ***pList)
  272. {
  273. /* %typemap(argout) (char ***pList) */
  274. $result = CreateArrayFromStringArray( *($1) );
  275. argvi++;
  276. }
  277. %typemap(in,numinputs=0) ( double argout[ANY]) (double argout[$dim0])
  278. {
  279. /* %typemap(in,numinputs=0) (double argout[ANY]) */
  280. $1 = argout;
  281. }
  282. %typemap(argout,fragment="CreateArrayFromDoubleArray") ( double argout[ANY])
  283. {
  284. /* %typemap(argout) (double argout[ANY]) */
  285. if (GIMME_V == G_ARRAY) {
  286. /* return a list */
  287. int i;
  288. EXTEND(SP, argvi+$dim0-items+1);
  289. for (i = 0; i < $dim0; i++)
  290. ST(argvi++) = sv_2mortal(newSVnv($1[i]));
  291. } else {
  292. $result = CreateArrayFromDoubleArray( $1, $dim0 );
  293. argvi++;
  294. }
  295. }
  296. %typemap(in,numinputs=0) ( double *argout[ANY]) (double *argout)
  297. {
  298. /* %typemap(in,numinputs=0) (double *argout[ANY]) */
  299. $1 = &argout;
  300. }
  301. %typemap(argout,fragment="CreateArrayFromDoubleArray") ( double *argout[ANY])
  302. {
  303. /* %typemap(argout) (double *argout[ANY]) */
  304. $result = CreateArrayFromDoubleArray( *$1, $dim0 );
  305. argvi++;
  306. }
  307. %typemap(freearg) (double *argout[ANY])
  308. {
  309. /* %typemap(freearg) (double *argout[ANY]) */
  310. CPLFree(*$1);
  311. }
  312. %typemap(in) (double argin[ANY]) (double argin[$dim0])
  313. {
  314. /* %typemap(in) (double argin[ANY]) */
  315. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  316. SWIG_croak("expected a reference to an array");
  317. $1 = argin;
  318. AV *av = (AV*)(SvRV($input));
  319. for (unsigned int i=0; i<$dim0; i++) {
  320. SV **sv = av_fetch(av, i, 0);
  321. $1[i] = SvNV(*sv);
  322. }
  323. }
  324. /*
  325. * Typemap for counted arrays of ints <- Perl list
  326. */
  327. %typemap(in,numinputs=1) (int nList, int* pList)
  328. {
  329. /* %typemap(in,numinputs=1) (int nList, int* pList) */
  330. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  331. SWIG_croak("expected a reference to an array");
  332. AV *av = (AV*)(SvRV($input));
  333. $1 = av_len(av)+1;
  334. $2 = (int*) malloc($1*sizeof(int));
  335. for( int i = 0; i<$1; i++ ) {
  336. SV **sv = av_fetch(av, i, 0);
  337. $2[i] = SvIV(*sv);
  338. }
  339. }
  340. %typemap(freearg) (int nList, int* pList)
  341. {
  342. /* %typemap(freearg) (int nList, int* pList) */
  343. if ($2)
  344. free((void*) $2);
  345. }
  346. %typemap(in,numinputs=1) (int nList, double* pList)
  347. {
  348. /* %typemap(in,numinputs=1) (int nList, double* pList) */
  349. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  350. SWIG_croak("expected a reference to an array");
  351. AV *av = (AV*)(SvRV($input));
  352. $1 = av_len(av)+1;
  353. $2 = (double*) malloc($1*sizeof(double));
  354. for( int i = 0; i<$1; i++ ) {
  355. SV **sv = av_fetch(av, i, 0);
  356. $2[i] = SvNV(*sv);
  357. }
  358. }
  359. %typemap(freearg) (int nList, double* pList)
  360. {
  361. /* %typemap(freearg) (int nList, double* pList) */
  362. if ($2)
  363. free((void*) $2);
  364. }
  365. %typemap(in) (char **pList)
  366. {
  367. /* %typemap(in) (char **pList) */
  368. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  369. SWIG_croak("expected a reference to an array");
  370. AV *av = (AV*)(SvRV($input));
  371. for (int i = 0; i < av_len(av)+1; i++) {
  372. SV *sv = *(av_fetch(av, i, 0));
  373. sv_utf8_upgrade(sv); /* GDAL expects UTF-8 */
  374. char *pszItem = SvPV_nolen(sv);
  375. $1 = CSLAddString( $1, pszItem );
  376. }
  377. }
  378. %typemap(freearg) (char **pList)
  379. {
  380. /* %typemap(freearg) (char **pList) */
  381. if ($1)
  382. CSLDestroy( $1 );
  383. }
  384. %typemap(in,numinputs=1) (int defined, double value)
  385. {
  386. /* %typemap(in,numinputs=1) (int defined, double value) */
  387. $1 = SvOK($input);
  388. $2 = SvNV($input);
  389. }
  390. /*
  391. * Typemap for buffers with length <-> AV
  392. * Used in Band::ReadRaster() and Band::WriteRaster()
  393. *
  394. * This typemap has a typecheck also since the WriteRaster()
  395. * methods are overloaded.
  396. */
  397. %typemap(in,numinputs=0) (int *nLen, char **pBuf ) ( int nLen = 0, char *pBuf = 0 )
  398. {
  399. /* %typemap(in,numinputs=0) (int *nLen, char **pBuf ) */
  400. $1 = &nLen;
  401. $2 = &pBuf;
  402. }
  403. %typemap(argout) (int *nLen, char **pBuf )
  404. {
  405. /* %typemap(argout) (int *nLen, char **pBuf ) */
  406. $result = sv_2mortal(newSVpv( *$2, *$1 ));
  407. argvi++;
  408. }
  409. %typemap(freearg) (int *nLen, char **pBuf )
  410. {
  411. /* %typemap(freearg) (int *nLen, char **pBuf ) */
  412. if( *$1 ) {
  413. free( *$2 );
  414. }
  415. }
  416. %typemap(in,numinputs=1) (int nLen, char *pBuf )
  417. {
  418. /* %typemap(in,numinputs=1) (int nLen, char *pBuf ) */
  419. if (SvOK($input)) {
  420. if (!SvPOK($input))
  421. SWIG_croak("expected binary data as input");
  422. STRLEN len = SvCUR($input);
  423. $2 = SvPV_nolen($input);
  424. $1 = len;
  425. } else {
  426. $2 = NULL;
  427. $1 = 0;
  428. }
  429. }
  430. %typemap(in,numinputs=1) (int nLen, unsigned char *pBuf )
  431. {
  432. /* %typemap(in,numinputs=1) (int nLen, unsigned char *pBuf ) */
  433. if (SvOK($input)) {
  434. if (!SvPOK($input))
  435. SWIG_croak("expected binary data as input");
  436. STRLEN len = SvCUR($input);
  437. $2 = (unsigned char *)SvPV_nolen($input);
  438. $1 = len;
  439. } else {
  440. $2 = NULL;
  441. $1 = 0;
  442. }
  443. }
  444. /***************************************************
  445. * Typemaps for (retStringAndCPLFree*)
  446. ***************************************************/
  447. %typemap(out) (retStringAndCPLFree*)
  448. %{
  449. /* %typemap(out) (retStringAndCPLFree*) */
  450. if($1)
  451. {
  452. $result = SWIG_FromCharPtr((const char *)result);
  453. CPLFree($1);
  454. }
  455. else
  456. {
  457. $result = sv_newmortal();
  458. }
  459. argvi++ ;
  460. %}
  461. /* slightly different version(?) for GDALAsyncReader */
  462. %typemap(in,numinputs=0) (int *nLength, char **pBuffer ) ( int nLength = 0, char *pBuffer = 0 )
  463. {
  464. /* %typemap(in,numinputs=0) (int *nLength, char **pBuffer ) */
  465. $1 = &nLength;
  466. $2 = &pBuffer;
  467. }
  468. %typemap(freearg) (int *nLength, char **pBuffer )
  469. {
  470. /* %typemap(freearg) (int *nLength, char **pBuffer ) */
  471. if( *$1 ) {
  472. free( *$2 );
  473. }
  474. }
  475. /*
  476. * Typemap argout of GDAL_GCP* used in Dataset::GetGCPs( )
  477. */
  478. %typemap(in,numinputs=0) (int *nGCPs, GDAL_GCP const **pGCPs ) (int nGCPs=0, GDAL_GCP *pGCPs=0 )
  479. {
  480. /* %typemap(in,numinputs=0) (int *nGCPs, GDAL_GCP const **pGCPs ) */
  481. $1 = &nGCPs;
  482. $2 = &pGCPs;
  483. }
  484. %typemap(argout) (int *nGCPs, GDAL_GCP const **pGCPs )
  485. {
  486. /* %typemap(argout) (int *nGCPs, GDAL_GCP const **pGCPs ) */
  487. AV *dict = (AV*)sv_2mortal((SV*)newAV());
  488. for( int i = 0; i < *$1; i++ ) {
  489. GDAL_GCP *o = new_GDAL_GCP( (*$2)[i].dfGCPX,
  490. (*$2)[i].dfGCPY,
  491. (*$2)[i].dfGCPZ,
  492. (*$2)[i].dfGCPPixel,
  493. (*$2)[i].dfGCPLine,
  494. (*$2)[i].pszInfo,
  495. (*$2)[i].pszId );
  496. SV *sv = newSV(0);
  497. SWIG_MakePtr( sv, (void*)o, $*2_descriptor, SWIG_SHADOW|SWIG_OWNER);
  498. av_store(dict, i, sv);
  499. }
  500. $result = newRV_noinc((SV*)dict);
  501. argvi++;
  502. }
  503. %typemap(in,numinputs=1) (int nGCPs, GDAL_GCP const *pGCPs ) ( GDAL_GCP *tmpGCPList )
  504. {
  505. /* %typemap(in,numinputs=1) (int nGCPs, GDAL_GCP const *pGCPs ) */
  506. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  507. SWIG_croak("expected a reference to an array");
  508. AV *av = (AV*)(SvRV($input));
  509. $1 = av_len(av)+1;
  510. tmpGCPList = (GDAL_GCP*) malloc($1*sizeof(GDAL_GCP));
  511. $2 = tmpGCPList;
  512. for( int i = 0; i<$1; i++ ) {
  513. SV **sv = av_fetch(av, i, 0);
  514. GDAL_GCP *item = 0;
  515. SWIG_ConvertPtr( *sv, (void**)&item, SWIGTYPE_p_GDAL_GCP, 0 );
  516. if (!item )
  517. SWIG_fail;
  518. memcpy( (void*) tmpGCPList, (void*) item, sizeof( GDAL_GCP ) );
  519. ++tmpGCPList;
  520. }
  521. }
  522. %typemap(freearg) (int nGCPs, GDAL_GCP const *pGCPs )
  523. {
  524. /* %typemap(freearg) (int nGCPs, GDAL_GCP const *pGCPs ) */
  525. if ($2)
  526. free($2);
  527. }
  528. /*
  529. * Typemap for GDALColorEntry* <-> AV
  530. * GDALColorEntry* may be a return value and both input and output param
  531. */
  532. %typemap(out) GDALColorEntry*
  533. {
  534. /* %typemap(out) GDALColorEntry* */
  535. if (!result)
  536. SWIG_croak("GetColorEntry failed");
  537. $result = sv_newmortal();
  538. sv_setiv(ST(argvi++), (IV) result->c1);
  539. $result = sv_newmortal();
  540. sv_setiv(ST(argvi++), (IV) result->c2);
  541. $result = sv_newmortal();
  542. sv_setiv(ST(argvi++), (IV) result->c3);
  543. $result = sv_newmortal();
  544. sv_setiv(ST(argvi++), (IV) result->c4);
  545. }
  546. %typemap(in,numinputs=0) GDALColorEntry*(GDALColorEntry e)
  547. {
  548. /* %typemap(in,numinputs=0) GDALColorEntry*(GDALColorEntry e) */
  549. $1 = &e;
  550. }
  551. %typemap(argout) GDALColorEntry*
  552. {
  553. /* %typemap(argout) GDALColorEntry* */
  554. if (!result)
  555. SWIG_croak("GetColorEntryAsRGB failed");
  556. argvi--;
  557. $result = sv_newmortal();
  558. sv_setiv(ST(argvi++), (IV) e3.c1);
  559. $result = sv_newmortal();
  560. sv_setiv(ST(argvi++), (IV) e3.c2);
  561. $result = sv_newmortal();
  562. sv_setiv(ST(argvi++), (IV) e3.c3);
  563. $result = sv_newmortal();
  564. sv_setiv(ST(argvi++), (IV) e3.c4);
  565. }
  566. %typemap(argout) const GDALColorEntry*
  567. {
  568. /* %typemap(argout) const GDALColorEntry* */
  569. }
  570. %typemap(in,numinputs=1) const GDALColorEntry*(GDALColorEntry e)
  571. {
  572. /* %typemap(in,numinputs=1) const GDALColorEntry*(GDALColorEntry e) */
  573. $1 = &e3;
  574. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  575. SWIG_croak("expected a reference to an array");
  576. AV *av = (AV*)(SvRV($input));
  577. SV **sv = av_fetch(av, 0, 0);
  578. $1->c1 = SvIV(*sv);
  579. sv = av_fetch(av, 1, 0);
  580. $1->c2 = SvIV(*sv);
  581. sv = av_fetch(av, 2, 0);
  582. $1->c3 = SvIV(*sv);
  583. sv = av_fetch(av, 3, 0);
  584. $1->c4 = SvIV(*sv);
  585. }
  586. /*
  587. * Typemap char ** <-> HV *
  588. */
  589. %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (char **dict)
  590. {
  591. /* %typecheck(SWIG_TYPECHECK_POINTER) (char **dict) */
  592. $1 = (SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVHV)) ? 1 : 0;
  593. }
  594. %typemap(in) char **dict
  595. {
  596. /* %typemap(in) char **dict */
  597. HV *hv = (HV*)SvRV($input);
  598. SV *sv;
  599. char *key;
  600. I32 klen;
  601. $1 = NULL;
  602. hv_iterinit(hv);
  603. while(sv = hv_iternextsv(hv,&key,&klen)) {
  604. $1 = CSLAddNameValue( $1, key, SvPV_nolen(sv) );
  605. }
  606. }
  607. %typemap(out) char **dict
  608. {
  609. /* %typemap(out) char **dict */
  610. char **stringarray = $1;
  611. HV *hv = (HV*)sv_2mortal((SV*)newHV());
  612. if ( stringarray != NULL ) {
  613. while (*stringarray != NULL ) {
  614. char const *valptr;
  615. char *keyptr;
  616. valptr = CPLParseNameValue( *stringarray, &keyptr );
  617. if ( valptr != 0 ) {
  618. hv_store(hv, keyptr, strlen(keyptr), newSVpv(valptr, strlen(valptr)), 0);
  619. CPLFree( keyptr );
  620. }
  621. stringarray++;
  622. }
  623. }
  624. $result = newRV_noinc((SV*)hv);
  625. argvi++;
  626. }
  627. %typemap(freearg) char **dict
  628. {
  629. /* %typemap(freearg) char **dict */
  630. CSLDestroy( $1 );
  631. }
  632. /*
  633. * Typemap char **options <-> AV
  634. */
  635. %typemap(in) char **options
  636. {
  637. /* %typemap(in) char **options */
  638. if (SvOK($input)) {
  639. if (SvROK($input)) {
  640. if (SvTYPE(SvRV($input))==SVt_PVAV) {
  641. AV *av = (AV*)(SvRV($input));
  642. for (int i = 0; i < av_len(av)+1; i++) {
  643. SV *sv = *(av_fetch(av, i, 0));
  644. sv_utf8_upgrade(sv); /* GDAL expects UTF-8 */
  645. char *pszItem = SvPV_nolen(sv);
  646. $1 = CSLAddString( $1, pszItem );
  647. }
  648. } else if (SvTYPE(SvRV($input))==SVt_PVHV) {
  649. HV *hv = (HV*)SvRV($input);
  650. SV *sv;
  651. char *key;
  652. I32 klen;
  653. $1 = NULL;
  654. hv_iterinit(hv);
  655. while(sv = hv_iternextsv(hv,&key,&klen)) {
  656. sv_utf8_upgrade(sv); /* GDAL expects UTF-8 */
  657. $1 = CSLAddNameValue( $1, key, SvPV_nolen(sv) );
  658. }
  659. } else
  660. SWIG_croak("'options' is not a reference to an array or hash");
  661. } else
  662. SWIG_croak("'options' is not a reference");
  663. }
  664. }
  665. %typemap(freearg) char **options
  666. {
  667. /* %typemap(freearg) char **options */
  668. if ($1) CSLDestroy( $1 );
  669. }
  670. %typemap(out) char **options
  671. {
  672. /* %typemap(out) char **options -> ( string ) */
  673. AV* av = (AV*)sv_2mortal((SV*)newAV());
  674. char **stringarray = $1;
  675. if ( stringarray != NULL ) {
  676. int n = CSLCount( stringarray );
  677. for ( int i = 0; i < n; i++ ) {
  678. SV *sv = newSVpv(stringarray[i], 0);
  679. SvUTF8_on(sv); /* expecting UTF-8 from GDAL */
  680. if (!av_store(av, i, sv))
  681. SvREFCNT_dec(sv);
  682. }
  683. }
  684. $result = newRV_noinc((SV*)av);
  685. argvi++;
  686. }
  687. /*
  688. * Typemaps map mutable char ** arguments from AV. Does not
  689. * return the modified argument
  690. */
  691. %typemap(in) (char **ignorechange) ( char *val )
  692. {
  693. /* %typemap(in) (char **ignorechange) */
  694. sv_utf8_upgrade($input); /* GDAL expects UTF-8 */
  695. val = SvPV_nolen($input);
  696. $1 = &val;
  697. }
  698. /*
  699. * Typemap for char **argout.
  700. */
  701. %typemap(in,numinputs=0) (char **argout) (char *argout=0), (char **username) (char *argout=0), (char **usrname) (char *argout=0), (char **type) (char *argout=0)
  702. {
  703. /* %typemap(in,numinputs=0) (char **argout) */
  704. $1 = &argout;
  705. }
  706. %typemap(argout) (char **argout), (char **username), (char **usrname), (char **type)
  707. {
  708. /* %typemap(argout) (char **argout) */
  709. $result = sv_newmortal();
  710. if ( $1 ) {
  711. sv_setpv($result, *$1);
  712. SvUTF8_on($result); /* expecting UTF-8 from GDAL */
  713. }
  714. argvi++;
  715. }
  716. %typemap(freearg) (char **argout)
  717. {
  718. /* %typemap(freearg) (char **argout) */
  719. if ( *$1 )
  720. CPLFree( *$1 );
  721. }
  722. /*
  723. * Typemap for an optional POD argument.
  724. * Declare function to take POD *. If the parameter
  725. * is NULL then the function needs to define a default
  726. * value.
  727. */
  728. %typemap(in) (int *optional_int) ( int val )
  729. {
  730. /* %typemap(in) (int *optional_int) */
  731. if ( !SvOK($input) ) {
  732. $1 = 0;
  733. }
  734. else {
  735. val = SvIV($input);
  736. $1 = ($1_type)&val;
  737. }
  738. }
  739. /*
  740. * Typedef const char * <- Any object.
  741. *
  742. * Formats the object using str and returns the string representation
  743. */
  744. %typemap(in) (tostring argin)
  745. {
  746. /* %typemap(in) (tostring argin) */
  747. sv_utf8_upgrade($input); /* GDAL expects UTF-8 */
  748. $1 = SvPV_nolen( $input );
  749. }
  750. %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (tostring argin)
  751. {
  752. /* %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (tostring argin) */
  753. $1 = 1;
  754. }
  755. /*
  756. * Typemap for CPLErr.
  757. * The assumption is that all errors have been reported through CPLError
  758. * and are thus caught by call to CPLGetLastErrorType() in %exception
  759. */
  760. %typemap(out) CPLErr
  761. {
  762. /* %typemap(out) CPLErr */
  763. }
  764. /*
  765. * Typemap for OGRErr.
  766. * _Some_ errors in OGR are not reported through CPLError and the return
  767. * value of the function must be examined and the message obtained from
  768. * OGRErrMessages, which is a function within these wrappers.
  769. */
  770. %import "ogr_error_map.i"
  771. %typemap(out,fragment="OGRErrMessages") OGRErr
  772. {
  773. /* %typemap(out) OGRErr */
  774. if ( result != 0 ) {
  775. const char *err = CPLGetLastErrorMsg();
  776. if (err and *err) SWIG_croak(err); /* this is usually better */
  777. SWIG_croak( OGRErrMessages(result) );
  778. }
  779. }
  780. /*
  781. * Typemaps for minixml: CPLXMLNode* input, CPLXMLNode *ret
  782. */
  783. %fragment("AVToXMLTree","header") %{
  784. /************************************************************************/
  785. /* AVToXMLTree() */
  786. /************************************************************************/
  787. static CPLXMLNode *AVToXMLTree( AV *av )
  788. {
  789. int nChildCount = 0, iChild, nType;
  790. CPLXMLNode *psThisNode;
  791. char *pszText = NULL;
  792. nChildCount = av_len(av) - 1; /* there are two non-childs in the array */
  793. if (nChildCount < 0)
  794. /* the input XML is empty */
  795. return NULL;
  796. nType = SvIV(*(av_fetch(av,0,0)));
  797. SV *sv = *(av_fetch(av,1,0));
  798. sv_utf8_upgrade(sv); /* GDAL expects UTF-8 */
  799. pszText = SvPV_nolen(sv);
  800. psThisNode = CPLCreateXMLNode( NULL, (CPLXMLNodeType) nType, pszText );
  801. for( iChild = 0; iChild < nChildCount; iChild++ )
  802. {
  803. SV **s = av_fetch(av, iChild+2, 0);
  804. CPLXMLNode *psChild;
  805. if (!(SvROK(*s) && (SvTYPE(SvRV(*s))==SVt_PVAV)))
  806. /* expected a reference to an array */
  807. psChild = NULL;
  808. else
  809. psChild = AVToXMLTree((AV*)SvRV(*s));
  810. if (psChild)
  811. CPLAddXMLChild( psThisNode, psChild );
  812. else {
  813. CPLDestroyXMLNode(psThisNode);
  814. return NULL;
  815. }
  816. }
  817. return psThisNode;
  818. }
  819. %}
  820. %typemap(in,fragment="AVToXMLTree") (CPLXMLNode* xmlnode )
  821. {
  822. /* %typemap(in) (CPLXMLNode* xmlnode ) */
  823. if (!(SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  824. SWIG_croak("expected a reference to an array");
  825. AV *av = (AV*)(SvRV($input));
  826. $1 = AVToXMLTree( av );
  827. if ( !$1 ) SWIG_croak("Conversion Perl array to XMLTree failed");
  828. }
  829. %typemap(freearg) (CPLXMLNode *xmlnode)
  830. {
  831. /* %typemap(freearg) (CPLXMLNode *xmlnode) */
  832. if ( $1 ) CPLDestroyXMLNode( $1 );
  833. }
  834. %fragment("XMLTreeToAV","header") %{
  835. /************************************************************************/
  836. /* XMLTreeToAV() */
  837. /************************************************************************/
  838. static AV *XMLTreeToAV( CPLXMLNode *psTree )
  839. {
  840. AV *av;
  841. int nChildCount = 0, iChild;
  842. CPLXMLNode *psChild;
  843. for( psChild = psTree->psChild;
  844. psChild != NULL;
  845. psChild = psChild->psNext )
  846. nChildCount++;
  847. av = (AV*)sv_2mortal((SV*)newAV());
  848. av_store(av,0,newSViv((int) psTree->eType));
  849. SV *sv = newSVpv(psTree->pszValue, strlen(psTree->pszValue));
  850. SvUTF8_on(sv); /* expecting UTF-8 from GDAL */
  851. av_store(av,1,sv);
  852. for( psChild = psTree->psChild, iChild = 2;
  853. psChild != NULL;
  854. psChild = psChild->psNext, iChild++ )
  855. {
  856. SV *s = newRV_inc((SV*)XMLTreeToAV(psChild));
  857. if (!av_store(av, iChild, s))
  858. SvREFCNT_dec(s);
  859. }
  860. return av;
  861. }
  862. %}
  863. %typemap(out,fragment="XMLTreeToAV") (CPLXMLNode*)
  864. {
  865. /* %typemap(out) (CPLXMLNode*) */
  866. $result = newRV_noinc((SV*)XMLTreeToAV( $1 ));
  867. argvi++;
  868. }
  869. %typemap(ret) (CPLXMLNode*)
  870. {
  871. /* %typemap(ret) (CPLXMLNode*) */
  872. if ( $1 ) CPLDestroyXMLNode( $1 );
  873. }
  874. /* non NULL input pointer checks */
  875. %define CHECK_NOT_UNDEF(type, param, msg)
  876. %typemap(check) (type *param)
  877. {
  878. /* %typemap(check) (type *param) */
  879. if (!$1)
  880. SWIG_croak("The msg must not be undefined");
  881. }
  882. %enddef
  883. CHECK_NOT_UNDEF(char, method, method)
  884. CHECK_NOT_UNDEF(const char, name, name)
  885. CHECK_NOT_UNDEF(const char, utf8_path, path)
  886. CHECK_NOT_UNDEF(const char, request, request)
  887. CHECK_NOT_UNDEF(const char, cap, capability)
  888. CHECK_NOT_UNDEF(const char, statement, statement)
  889. CHECK_NOT_UNDEF(const char, pszNewDesc, description)
  890. CHECK_NOT_UNDEF(OSRCoordinateTransformationShadow, , coordinate transformation)
  891. CHECK_NOT_UNDEF(OGRGeometryShadow, other, other geometry)
  892. CHECK_NOT_UNDEF(OGRGeometryShadow, other_disown, other geometry)
  893. CHECK_NOT_UNDEF(OGRGeometryShadow, geom, geometry)
  894. CHECK_NOT_UNDEF(OGRFieldDefnShadow, defn, field definition)
  895. CHECK_NOT_UNDEF(OGRFieldDefnShadow, field_defn, field definition)
  896. CHECK_NOT_UNDEF(OGRFeatureShadow, feature, feature)
  897. %typemap(in, numinputs=1) (int nCount, double *x, double *y, double *z)
  898. {
  899. /* %typemap(in) (int nCount, double *x, double *y, double *z) */
  900. /* $input is a ref to a list of refs to point lists */
  901. if (! (SvROK($input) && (SvTYPE(SvRV($input))==SVt_PVAV)))
  902. SWIG_croak("expected a reference to an array");
  903. AV *av = (AV*)(SvRV($input));
  904. $1 = av_len(av)+1;
  905. $2 = (double*) malloc($1*sizeof(double));
  906. $3 = (double*) malloc($1*sizeof(double));
  907. $4 = (double*) malloc($1*sizeof(double));
  908. if (!$2 or !$3 or !$4)
  909. SWIG_croak("out of memory");
  910. for (int i = 0; i < $1; i++) {
  911. SV **sv = av_fetch(av, i, 0); /* ref to one point list */
  912. if (!(SvROK(*sv) && (SvTYPE(SvRV(*sv))==SVt_PVAV)))
  913. SWIG_croak("expected a reference to a list of coordinates");
  914. AV *ac = (AV*)(SvRV(*sv));
  915. int n = av_len(ac)+1;
  916. SV **c = av_fetch(ac, 0, 0);
  917. $2[i] = SvNV(*c);
  918. c = av_fetch(ac, 1, 0);
  919. $3[i] = SvNV(*c);
  920. if (n < 3) {
  921. $4[i] = 0;
  922. } else {
  923. c = av_fetch(ac, 2, 0);
  924. $4[i] = SvNV(*c);
  925. }
  926. }
  927. }
  928. %typemap(argout) (int nCount, double *x, double *y, double *z)
  929. {
  930. /* %typemap(argout) (int nCount, double *x, double *y, double *z) */
  931. AV *av = (AV*)(SvRV($input));
  932. for (int i = 0; i < $1; i++) {
  933. SV **sv = av_fetch(av, i, 0);
  934. AV *ac = (AV*)(SvRV(*sv));
  935. int n = av_len(ac)+1;
  936. SV *c = newSVnv($2[i]);
  937. if (!av_store(ac, 0, c))
  938. SvREFCNT_dec(c);
  939. c = newSVnv($3[i]);
  940. if (!av_store(ac, 1, c))
  941. SvREFCNT_dec(c);
  942. c = newSVnv($4[i]);
  943. if (!av_store(ac, 2, c))
  944. SvREFCNT_dec(c);
  945. }
  946. }
  947. %typemap(freearg) (int nCount, double *x, double *y, double *z)
  948. {
  949. /* %typemap(freearg) (int nCount, double *x, double *y, double *z) */
  950. if ($2) free($2);
  951. if ($3) free($3);
  952. if ($4) free($4);
  953. }
  954. %typemap(arginit, noblock=1) ( void* callback_data = NULL)
  955. {
  956. SavedEnv saved_env;
  957. saved_env.fct = NULL;
  958. saved_env.data = &PL_sv_undef;
  959. $1 = (void *)(&saved_env);
  960. }
  961. %typemap(in) (GDALProgressFunc callback = NULL)
  962. {
  963. /* %typemap(in) (GDALProgressFunc callback = NULL) */
  964. if (SvOK($input)) {
  965. if (SvROK($input)) {
  966. if (SvTYPE(SvRV($input)) != SVt_PVCV) {
  967. SWIG_croak("the callback arg must be a reference to a subroutine\n");
  968. } else {
  969. saved_env.fct = (SV *)$input;
  970. $1 = &callback_d_cp_vp;
  971. }
  972. } else {
  973. SWIG_croak("the callback arg must be a reference to a subroutine\n");
  974. }
  975. }
  976. }
  977. %typemap(in) (void* callback_data = NULL)
  978. {
  979. /* %typemap(in) (void* callback_data=NULL) */
  980. if (SvOK($input))
  981. saved_env.data = (SV *)$input;
  982. }
  983. /*
  984. * Typemaps for VSIStatL
  985. */
  986. %typemap(in,numinputs=0) (VSIStatBufL *) (VSIStatBufL sStatBuf)
  987. {
  988. /* %typemap(in,numinputs=0) (VSIStatBufL *) (VSIStatBufL sStatBuf) */
  989. $1 = &sStatBuf;
  990. }
  991. %typemap(argout) (VSIStatBufL *)
  992. {
  993. /* %typemap(argout) (VSIStatBufL *) */
  994. SP -= 1; /* should be somewhere else, remove the filename arg */
  995. EXTEND(SP, 1);
  996. char mode[2];
  997. mode[0] = ' ';
  998. mode[1] = '\0';
  999. if (S_ISREG(sStatBuf2.st_mode)) mode[0] = 'f';
  1000. else if (S_ISDIR(sStatBuf2.st_mode)) mode[0] = 'd';
  1001. else if (S_ISLNK(sStatBuf2.st_mode)) mode[0] = 'l';
  1002. else if (S_ISFIFO(sStatBuf2.st_mode)) mode[0] = 'p';
  1003. else if (S_ISSOCK(sStatBuf2.st_mode)) mode[0] = 'S';
  1004. else if (S_ISBLK(sStatBuf2.st_mode)) mode[0] = 'b';
  1005. else if (S_ISCHR(sStatBuf2.st_mode)) mode[0] = 'c';
  1006. PUSHs(sv_2mortal(newSVpv(mode, 0)));
  1007. argvi++;
  1008. EXTEND(SP, 1);
  1009. PUSHs(sv_2mortal(newSVuv(sStatBuf2.st_size)));
  1010. argvi++;
  1011. }
  1012. /*
  1013. * Typemaps for VSIFReadL
  1014. */
  1015. %typemap(in,numinputs=1) (void *pBuffer, size_t nSize, size_t nCount)
  1016. {
  1017. /* %typemap(in,numinputs=1) (void *pBuffer, size_t nSize, size_t nCount) */
  1018. size_t len = SvIV($input);
  1019. $1 = malloc(len);
  1020. $2 = 1;
  1021. $3 = len;
  1022. }
  1023. %typemap(argout) (void *pBuffer, size_t nSize, size_t nCount)
  1024. {
  1025. /* %typemap(argout) (void *pBuffer, size_t nSize, size_t nCount) */
  1026. if (result) {
  1027. $result = newSVpvn((char*)$1, result);
  1028. sv_2mortal($result);
  1029. } else {
  1030. $result = &PL_sv_undef;
  1031. }
  1032. argvi++;
  1033. }
  1034. %typemap(out) (size_t VSIFReadL)
  1035. {
  1036. /* %typemap(out) (size_t VSIFReadL) */
  1037. }
  1038. /*
  1039. * Typemaps for VSIFWriteL
  1040. */
  1041. %typemap(in,numinputs=1) (const void *pBuffer, size_t nSize, size_t nCount)
  1042. {
  1043. /* %typemap(in,numinputs=1) (const void *pBuffer, size_t nSize, size_t nCount) */
  1044. size_t len;
  1045. $1 = SvPV($input, len);
  1046. $2 = 1;
  1047. $3 = len;
  1048. }
  1049. /*
  1050. * Typemaps for ensuring UTF-8 is given to GDAL if requested impacts:
  1051. * (ogr:) Driver_CreateDataSource, Driver_CopyDataSource, Driver_Open,
  1052. * Driver_DeleteDataSource Open, OpenShared,
  1053. * DataSource__GetLayerByName, DataSource__CreateLayer,
  1054. * Feature_GetFieldDefnRef__SWIG_1, Feature_IsFieldSet__SWIG_1,
  1055. * Feature_GetFieldIndex (gdal:) PushFinderLocation, FindFile,
  1056. * ReadDir, FileFromMemBuffer, Unlink, MkDir, RmDir, Stat VSIFOpenL,
  1057. * Driver__Create, Driver_CreateCopy, Driver_Delete, Open__SWIG_1,
  1058. * OpenShared__SWIG_1, IdentifyDriver
  1059. */
  1060. %typemap(in,numinputs=1) (const char* utf8_path)
  1061. {
  1062. /* %typemap(in,numinputs=1) (const char* utf8_path) */
  1063. sv_utf8_upgrade($input);
  1064. $1 = SvPV_nolen($input);
  1065. }
  1066. %typemap(in,numinputs=1) (const char* layer_name)
  1067. {
  1068. /* %typemap(in,numinputs=1) (const char* layer_name) */
  1069. sv_utf8_upgrade($input);
  1070. $1 = SvPV_nolen($input);
  1071. }
  1072. %typemap(in,numinputs=1) (const char* name)
  1073. {
  1074. /* %typemap(in,numinputs=1) (const char* name) */
  1075. sv_utf8_upgrade($input);
  1076. $1 = SvPV_nolen($input);
  1077. }