diff options
author | Pascal Rigaux <pixel@mandriva.com> | 1999-11-14 23:09:15 +0000 |
---|---|---|
committer | Pascal Rigaux <pixel@mandriva.com> | 1999-11-14 23:09:15 +0000 |
commit | 773013b8e45a6916a74ea64152e51fb6cb951920 (patch) | |
tree | 1fe018ccc96d5ddf11081ea8d57f263b3be6ae00 /perl-install/Newt/Av_CharPtrPtr.c | |
parent | 16caaa0fdfae2bdcec187bdffac3f12d7bb9a8a1 (diff) | |
download | drakx-773013b8e45a6916a74ea64152e51fb6cb951920.tar drakx-773013b8e45a6916a74ea64152e51fb6cb951920.tar.gz drakx-773013b8e45a6916a74ea64152e51fb6cb951920.tar.bz2 drakx-773013b8e45a6916a74ea64152e51fb6cb951920.tar.xz drakx-773013b8e45a6916a74ea64152e51fb6cb951920.zip |
no_comment
Diffstat (limited to 'perl-install/Newt/Av_CharPtrPtr.c')
-rw-r--r-- | perl-install/Newt/Av_CharPtrPtr.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/perl-install/Newt/Av_CharPtrPtr.c b/perl-install/Newt/Av_CharPtrPtr.c new file mode 100644 index 000000000..d108ef5d0 --- /dev/null +++ b/perl-install/Newt/Av_CharPtrPtr.c @@ -0,0 +1,99 @@ +#ifdef __cplusplus +extern "C" { +#endif +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "Av_CharPtrPtr.h" /* XS_*_charPtrPtr() */ +#ifdef __cplusplus +} +#endif + + +/* Used by the INPUT typemap for char**. + * Will convert a Perl AV* (containing strings) to a C char**. + */ +char ** +XS_unpack_charPtrPtr( rv ) +SV *rv; +{ + AV *av; + SV **ssv; + char **s; + int avlen; + int x; + + if( SvROK( rv ) && (SvTYPE(SvRV(rv)) == SVt_PVAV) ) + av = (AV*)SvRV(rv); + else { + warn("XS_unpack_charPtrPtr: rv was not an AV ref"); + return( (char**)NULL ); + } + + /* is it empty? */ + avlen = av_len(av); + if( avlen < 0 ){ + warn("XS_unpack_charPtrPtr: array was empty"); + return( (char**)NULL ); + } + + /* av_len+2 == number of strings, plus 1 for an end-of-array sentinel. + */ + s = (char **)safemalloc( sizeof(char*) * (avlen + 2) ); + if( s == NULL ){ + warn("XS_unpack_charPtrPtr: unable to malloc char**"); + return( (char**)NULL ); + } + for( x = 0; x <= avlen; ++x ){ + ssv = av_fetch( av, x, 0 ); + if( ssv != NULL ){ + if( SvPOK( *ssv ) ){ + s[x] = (char *)safemalloc( SvCUR(*ssv) + 1 ); + if( s[x] == NULL ) + warn("XS_unpack_charPtrPtr: unable to malloc char*"); + else + strcpy( s[x], SvPV( *ssv, na ) ); + } + else + warn("XS_unpack_charPtrPtr: array elem %d was not a string.", x ); + } + else + s[x] = (char*)NULL; + } + s[x] = (char*)NULL; /* sentinel */ + return( s ); +} + +/* Used by the OUTPUT typemap for char**. + * Will convert a C char** to a Perl AV*. + */ +void +XS_pack_charPtrPtr( st, s ) +SV *st; +char **s; +{ + AV *av = newAV(); + SV *sv; + char **c; + + for( c = s; *c != NULL; ++c ){ + sv = newSVpv( *c, 0 ); + av_push( av, sv ); + } + sv = newSVrv( st, NULL ); /* upgrade stack SV to an RV */ + SvREFCNT_dec( sv ); /* discard */ + SvRV( st ) = (SV*)av; /* make stack RV point at our AV */ +} + + +/* cleanup the temporary char** from XS_unpack_charPtrPtr */ +void +XS_release_charPtrPtr(s) +char **s; +{ + char **c; + for( c = s; *c != NULL; ++c ) + safefree( *c ); + safefree( s ); +} + |