arrayret.i.old

来自「perl learn perl by examples」· OLD 代码 · 共 36 行

OLD
36
字号
%module ArrayRet

%{
char **
test (){
    static char * argv[] = {"abc", "def", "ghi", NULL};
    return argv;
}
%}

%typemap(perl5,out) char ** {   /* All functions returning char **  */
                                /* get this typemap                 */
    /* $source is of type char **
     * $target is of type RV (referring to an AV)
     */
    AV *ret_av = newAV();
    int i      = 0;
    char **p   = $source;
    /* First allocate a new AV, of the right size */
    while (*p++)
        ;            /* Incr. p while *p is non-null */ 

    av_extend(ret_av, p - $source);

    /* For each element in the array of strings, create a new
     * mortalscalar, and stuff it into the above array */
    for (i = 0, p = $source; *p; p++, i++) {
        av_store(ret_av, i, sv_2mortal( newSVpv(*p, 0)));
    }
    /* Finally, create a reference to the array; the "target"
       of this typemap */
    $target = newRV((SV*)ret_av);
    argvi++;
}

char **test();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?