2 // SWIG pointer conversion and utility library
7 // Perl5 specific implementation. This file is included
8 // by the file ../pointer.i
14 #define isspace(c) (c == ' ')
19 /*------------------------------------------------------------------
20 ptr2array(ptr,index,len,type = 0)
22 Attempts to dereference a pointer value, just like ptrvalue(), but
23 returns a reference to a list containing len values. If type is given,
24 it will try to use that type. Otherwise, this function will attempt to
25 "guess" the proper datatype by checking against all of the builtin C
27 ------------------------------------------------------------------ */
30 static AV *_ptr2array(CPerlObj *pPerl,SV *_PTRVALUE, int index, int len, char *type) {
31 #define ptr2array(a,b,c,d) _ptr2array(pPerl,a,b,c,d)
33 static AV *_ptr2array(SV *_PTRVALUE, int index, int len, char *type) {
34 #define ptr2array(a,b,c,d) _ptr2array(a,b,c,d)
44 svs = (SV **) malloc(len*sizeof(SV *));
46 svs[i] = sv_newmortal();
48 if (SWIG_ConvertPtr(_PTRVALUE,&ptr,0,0)) {
49 croak("Type error in ptr2array. Argument is not a valid pointer value.");
51 /* If no datatype was passed, try a few common datatypes first */
54 /* No datatype was passed. Type to figure out if it's a common one */
56 if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_int_p,0) >= 0) {
58 } else if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_double_p,0) >= 0) {
60 } else if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_short_p,0) >= 0) {
62 } else if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_long_p,0) >= 0) {
64 } else if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_float_p,0) >= 0) {
66 } else if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_p,0) >= 0) {
68 } else if (SWIG_ConvertPtr(_PTRVALUE,&ptr,SWIG_POINTER_char_pp,0) >= 0) {
76 croak("Unable to dereference NULL pointer.");
80 /* Now we have a datatype. Try to figure out what to do about it */
81 if (strcmp(type,"int") == 0) {
82 for (i = 0; i < len; ++i)
83 sv_setiv(svs[i], (IV) *(((int *) ptr) + index+i));
84 } else if (strcmp(type,"double") == 0) {
85 for (i = 0; i < len; ++i)
86 sv_setnv(svs[i], (double) *(((double *) ptr) + index+i));
87 } else if (strcmp(type,"short") == 0) {
88 for (i = 0; i < len; ++i)
89 sv_setiv(svs[i],(IV) *(((short *) ptr) + index+i));
90 } else if (strcmp(type,"long") == 0) {
91 for (i = 0; i < len; ++i)
92 sv_setiv(svs[i],(IV) *(((long *) ptr) + index+i));
93 } else if (strcmp(type,"float") == 0) {
94 for (i = 0; i < len; ++i)
95 sv_setnv(svs[i],(double) *(((float *) ptr)+index+i));
96 } else if (strcmp(type,"char") == 0) {
97 for (i = 0; i < len; ++i)
98 sv_setpv(svs[i],((char *) ptr)+index+i);
99 } else if (strcmp(type,"char *") == 0) {
100 for (i = 0; i < len; ++i) {
101 char *c = *(((char **) ptr)+index+i);
105 sv_setpv(svs[i],"NULL");
108 croak("Unable to dereference unsupported datatype.");
113 av = av_make(len,svs);
120 %typemap(perl5, out) AV *ptr2array
122 $result = newRV_noinc((SV*) $1);
127 AV *ptr2array(SV *ptr, int index = 0, int len = 1, char *type = 0);