Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Dean Roehrich's CookBookB comes with a great example (Struct3) demonstrating how to make a C struct into a perl hash, and vice versa. But the structure in the example has fixed size elements:
typedef struct ab_input { long lTrackId; char szDescription[80]; } AB_IN;
I need to do a similar task, but with several pointers to char, instead of a fixed size array of char, because the size is unknown in advance:
typedef struct { /* char fn[256]; */ char *fn; /* char *desc; */ long size; } cstype;
I also have made the necessary lines for dynamic allocation in XS_unpack_cstypePtr(). Here is the RFile.xs:
#ifdef __cplusplus extern "C" { #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #ifdef __cplusplus } #endif typedef struct { /* char fn[256]; */ char *fn; /* char *desc; */ long size; } cstype; extern char* _open_read(char *fn); extern char* _open_read_st(cstype *stru); /* Will convert a Perl HV* to a C cstype* */ cstype * XS_unpack_cstypePtr( rv ) SV *rv; { HV *hv; SV **ssv; cstype *abin; char *temp; STRLEN len; if( SvROK( rv ) && (SvTYPE(SvRV(rv)) == SVt_PVHV) ) hv = (HV*)SvRV(rv); else{ warn("XS_unpack_cstypePtr: rv was not an HV ref"); return( (cstype*)NULL ); } abin = (cstype*)safemalloc( sizeof( cstype ) ); if( abin == NULL ){ warn("XS_unpack_cstypePtr: unable to malloc cstype"); return( (cstype*)NULL); } abin->fn[0] = '\0'; /* abin->desc[0] = '\0'; */ abin->size = 0; ssv = hv_fetch( hv, "size", 4, 0 ); if( ssv != NULL ){ if( SvIOK( *ssv ) ) abin->size = (long)SvIV( *ssv ); else{ warn("XS_unpack_cstypePtr: hash elem size was not IOK"); } } else warn("XS_unpack_cstypePtr: hash elem size was null"); ssv = hv_fetch( hv, "fn", 2, 0 ); if( ssv != NULL ){ if( SvPOK( *ssv ) ) { temp = SvPV(*ssv, len); if ((abin->fn = (char*) safemalloc((len + 1) * sizeof(char))) == NULL) return (cstype*)NULL; strcpy( abin->fn, temp ); } else{ warn("XS_unpack_cstypePtr: hash elem fn was not POK"); } } else warn("XS_unpack_cstypePtr: hash elem fn was null"); /* ssv = hv_fetch( hv, "desc", 4, 0 ); if( ssv != NULL ){ if( SvPOK( *ssv ) ) { temp = SvPV(*ssv, len); if ((abin->desc = (char*) safemalloc((len + 1) * sizeof(char))) == NULL) return (cstype*)NULL; strcpy( abin->desc, temp ); } else{ warn("XS_unpack_cstypePtr: hash elem desc was not POK"); } } else warn("XS_unpack_cstypePtr: hash elem desc was null"); */ return( abin ); } MODULE = RFile PACKAGE = RFile PROTOTYPES: DISABLE SV* read_file(fn) char *fn PREINIT: char *content; CODE: content = _open_read(fn); if (content == NULL) XSRETURN_UNDEF; RETVAL = newSVpv(content, 0); OUTPUT: RETVAL SV* read_st(stru) cstype *stru CODE: printf("# Hello, string=(%s)\n", stru->fn ); RETVAL = newSVpv(stru->fn, 0); OUTPUT: RETVAL
RFile.pm:
package RFile; use strict; use Carp; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; require DynaLoader; require AutoLoader; @ISA = qw(Exporter DynaLoader); $VERSION = '0.01'; bootstrap RFile $VERSION; sub new { my $class = shift; my $self = { m => [ { fn => 'Makefile.PL', size => 100, desc => 'test', }, { fn => 'typemap', size => 100, desc => 'test', }, { fn => 'RFile.c', size => 100, desc => 'test', }, ], }; bless $self, $class; } sub dump { my ($self, $idx) = @_; return RFile::read_st($self->{m}->[$idx]); } 1;
But this gives coredump:
my $n = new RFile; for (0..2) { $c = $n->dump($_); print $c, "\n"; }
I must be missing something important in the XS_unpack function which is necessary when dealing with dynamically allocated structure element. Could someone help me on this?

In reply to XS: structure with dynamically allocated element by pope

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (3)
    As of 2020-02-18 05:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (75 votes). Check out past polls.

      Notices?