| 1 |
|
| /* Modified from API Cookbook A Example 8 */ |
| 2 |
|
| |
| 3 |
|
| #ifdef __cplusplus |
| 4 |
|
| extern "C" { |
| 5 |
|
| #endif |
| 6 |
|
| #include "EXTERN.h" |
| 7 |
|
| #include "perl.h" |
| 8 |
|
| #include "XSUB.h" |
| 9 |
|
| #include "ppport.h" |
| 10 |
|
| #include "Av_CharPtrPtr.h" /* XS_*_charPtrPtr() */ |
| 11 |
|
| #ifdef __cplusplus |
| 12 |
|
| } |
| 13 |
|
| #endif |
| 14 |
|
| |
| 15 |
|
| #if defined(_MSC_VER) |
| 16 |
|
| #define _CRT_SECURE_NO_DEPRECATE 1 |
| 17 |
|
| #define _CRT_NONSTDC_NO_DEPRECATE 1 |
| 18 |
|
| #endif |
| 19 |
|
| |
| 20 |
|
| |
| 21 |
|
| /* Used by the INPUT typemap for char**. |
| 22 |
|
| * Will convert a Perl AV* (containing strings) to a C char**. |
| 23 |
|
| */ |
| 24 |
24 |
| char ** XS_unpack_charPtrPtr(SV* rv ) |
| 25 |
|
| { |
| 26 |
|
| AV *av; |
| 27 |
|
| SV **ssv; |
| 28 |
|
| char **s; |
| 29 |
|
| int avlen; |
| 30 |
|
| int x; |
| 31 |
|
| |
| 32 |
24 |
▶ | if( SvROK( rv ) && (SvTYPE(SvRV(rv)) == SVt_PVAV) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if( SvROK( rv ) && (SvTYPE(SvRV(rv)) == SVt_PVAV) ) |
7 |
17 |
24 |
| if( SvROK( rv ) && (SvTYPE(SvRV(rv)) == SVt_PVAV) ) |
7 |
0 |
7 |
|
| 33 |
7 |
| av = (AV*)SvRV(rv); |
| 34 |
|
| else { |
| 35 |
17 |
| return( (char**)NULL ); |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| /* is it empty? */ |
| 39 |
7 |
| avlen = av_len(av); |
| 40 |
7 |
▶ | if( avlen < 0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( avlen < 0 ){ |
2 |
5 |
7 |
|
| 41 |
2 |
| return( (char**)NULL ); |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| /* av_len+2 == number of strings, plus 1 for an end-of-array sentinel. |
| 45 |
|
| */ |
| 46 |
5 |
| s = (char **)safemalloc( sizeof(char*) * (avlen + 2) ); |
| 47 |
5 |
▶ | if( s == NULL ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( s == NULL ){ |
0 |
5 |
5 |
|
| 48 |
0 |
| warn("XS_unpack_charPtrPtr: unable to malloc char**"); |
| 49 |
0 |
| return( (char**)NULL ); |
| 50 |
|
| } |
| 51 |
12 |
▶ | for( x = 0; x <= avlen; ++x ){ |
Branch: 1 branch
| Branch | True | False | Total |
| for( x = 0; x <= avlen; ++x ){ |
7 |
5 |
12 |
|
| 52 |
7 |
| ssv = av_fetch( av, x, 0 ); |
| 53 |
7 |
▶ | if( ssv != NULL ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( ssv != NULL ){ |
7 |
0 |
7 |
|
| 54 |
7 |
▶ | if( SvPOK( *ssv ) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( SvPOK( *ssv ) ){ |
7 |
0 |
7 |
|
| 55 |
7 |
| s[x] = (char *)safemalloc( SvCUR(*ssv) + 1 ); |
| 56 |
7 |
▶ | if( s[x] == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( s[x] == NULL ) |
0 |
7 |
7 |
|
| 57 |
0 |
| warn("XS_unpack_charPtrPtr: unable to malloc char*"); |
| 58 |
|
| else |
| 59 |
14 |
| strcpy( s[x], SvPV( *ssv, PL_na ) ); |
| 60 |
|
| } |
| 61 |
|
| else |
| 62 |
0 |
| warn("XS_unpack_charPtrPtr: array elem %d was not a string.", x ); |
| 63 |
|
| } |
| 64 |
|
| else |
| 65 |
0 |
| s[x] = (char*)NULL; |
| 66 |
|
| } |
| 67 |
5 |
| s[x] = (char*)NULL; /* sentinel */ |
| 68 |
5 |
| return( s ); |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| /* Used by the OUTPUT typemap for char**. |
| 72 |
|
| * Will convert a C char** to a Perl AV*. |
| 73 |
|
| */ |
| 74 |
0 |
| void XS_pack_charPtrPtr(SV* st, char **s) |
| 75 |
|
| { |
| 76 |
0 |
| AV *av = newAV(); |
| 77 |
|
| SV *sv; |
| 78 |
|
| char **c; |
| 79 |
|
| |
| 80 |
0 |
▶ | for( c = s; *c != NULL; ++c ){ |
Branch: 1 branch
| Branch | True | False | Total |
| for( c = s; *c != NULL; ++c ){ |
0 |
0 |
0 |
|
| 81 |
0 |
| sv = newSVpv( *c, 0 ); |
| 82 |
0 |
| av_push( av, sv ); |
| 83 |
|
| } |
| 84 |
0 |
| sv = newSVrv( st, NULL ); /* upgrade stack SV to an RV */ |
| 85 |
0 |
| SvREFCNT_dec( sv ); /* discard */ |
| 86 |
0 |
| SvRV( st ) = (SV*)av; /* make stack RV point at our AV */ |
| 87 |
0 |
| } |
| 88 |
|
| |
| 89 |
|
| |
| 90 |
|
| /* cleanup the temporary char** from XS_unpack_charPtrPtr */ |
| 91 |
0 |
| void XS_release_charPtrPtr(char **s) |
| 92 |
|
| { |
| 93 |
|
| char **c; |
| 94 |
0 |
▶ | for( c = s; *c != NULL; ++c ) |
Branch: 1 branch
| Branch | True | False | Total |
| for( c = s; *c != NULL; ++c ) |
0 |
0 |
0 |
|
| 95 |
0 |
| safefree( *c ); |
| 96 |
0 |
| safefree( s ); |
| 97 |
0 |
| } |
| 98 |
|
| |