Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Automatically Import XS struct

by Bpl (Scribe)
on Feb 03, 2021 at 11:35 UTC ( #11127851=perlquestion: print w/replies, xml ) Need Help??

Bpl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monkers,

Do you know how I can import automatically a header defined struct into XS code without naming it?

example:
# my struct is defined in SDR.h struct rtlsdr_dev { libusb_context *ctx; struct libusb_device_handle *devh; uint32_t xfer_buf_num; uint32_t xfer_buf_len; struct libusb_transfer **xfer; unsigned char **xfer_buf; rtlsdr_read_async_cb_t cb; void *cb_ctx; enum rtlsdr_async_status async_status; int async_cancel; int use_zerocopy; /* rtl demod context */ uint32_t rate; /* Hz */ uint32_t rtl_xtal; /* Hz */ int fir[FIR_LEN]; int direct_sampling; /* tuner context */ enum rtlsdr_tuner tuner_type; rtlsdr_tuner_iface_t *tuner; uint32_t tun_xtal; /* Hz */ uint32_t freq; /* Hz */ uint32_t bw; uint32_t offs_freq; /* Hz */ int corr; /* ppm */ int gain; /* tenth dB */ struct e4k_state e4k_s; struct r82xx_config r82xx_c; struct r82xx_priv r82xx_p; /* status */ int dev_lost; int driver_active; unsigned int xfer_errors; };
How can I import automatically it in my XS file after including the SDR header? Should I define the various struct inside this struct?
REGARDS

Replies are listed 'Best First'.
Re: Automatically Import XS struct
by syphilis (Archbishop) on Feb 03, 2021 at 14:12 UTC
    Hmmm ... I've created a header file named SDR.h, which is found by default. (That is, it's in a directory that is automatically searched.)

    That file contains (for simplicity) the following:
    struct rtlsdr_dev { int x; double d; };
    Then I create an Inline::C script:
    use strict; use warnings; use Inline C =><<'EOC'; #include <SDR.h> void baz() { struct rtlsdr_dev mine; mine.x = 1032; mine.d = sqrt(2.0); printf("%d %.16e\n", mine.x, mine.d); } EOC baz();
    When I run that script, it outputs (as expected):
    1032 1.4142135623730951e+00
    Given that Inline::C is nothing more than a module that autogenerates an XS file, then compiles and runs that XS code, I'm wondering what the difficulty is.

    Is it that you want to avoid naming the struct (to, eg, "mine", as I did) ? If so, why is such a restriction being imposed ?
    Or is there some other problem ?

    Cheers,
    Rob
Re^2: Automatically Import XS struct
by dave_the_m (Monsignor) on Feb 03, 2021 at 15:23 UTC
    XS is just a wrapper around C code to automate the generation of the C code by the automatic addition of boilerplate etc. In a standard XS distribution directory, typing 'make' will generate a foo.c file from the foo.xs file. Seeing how your code gets converted into C will give you a good idea of what's happening or where you're going wrong. If you don't understand how to handle the struct within C code then you've got a bigger problem.

    Dave.

    Reparented by Grandfather to OP

      I was asking OP to show us how he's handling the struct because I believe they aren't doing it correctly. Personally, I'm fully aware myself of how it all hangs together, having authored several XS distributions that includes their own C code, as well as external library wrappers.

        Oops - that was meant as a reply to the OP, not to you!

        Dave.

Re: Automatically Import XS struct
by dave_the_m (Monsignor) on Feb 03, 2021 at 13:56 UTC
    Your question is unclear. What exactly do you mean by "import automatically it in my XS"?

    Dave.

      it means that if I have my XS code,
      including the SDR header, how can I access the various struct elements? regards
        Are you talking about accessing the struct elements from within XS code, or from within perl code?

        Dave.

Re: Automatically Import XS struct
by stevieb (Canon) on Feb 03, 2021 at 14:40 UTC

    Please show us an example of how you'd like to use the struct. Providing us just the definition leaves your question ambiguous and very unclear.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11127851]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-06-28 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (90 votes). Check out past polls.

    Notices?