Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It need not be too bad. Here's an Inline::C demo where the function has a FILE* as it's first argument, a string as its second argument, and a variable number of following arguments (all of which are unsigned integers). If you want to see what a valid XS file looks like just uncomment  # CLEAN_AFTER_BUILD => 0, (before you have run the script) and take a look in the _Inline/build directory at the XS file that was autogenerated.
Update: Script modified slightly from when first posted
### try.pl ### use warnings; use Inline C => Config => # CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1; use Inline C => <<'EOC'; void foo(FILE * fh, SV* sv, ...) { dXSARGS; int i, arg; printf("%s ", SvPV_nolen(ST(1))); for (i = 2; i < items; i++) { arg = SvUV(ST(i)); printf("%d ", arg); } XSRETURN(0); } EOC open($rd, '<', 'try.pl') or die $!; $fmt = "foobar"; foo($rd, $fmt, 1 .. 5); print "\n"; foo($rd, $fmt, 21 .. 32); print "\n";
But there's a problem ... what if the third and subsequent args aren't all unsigned integers ? What if they're an ad hoc mixture of integers, doubles, and strings ?

That obviously complicates things. You might be able to convert each of those arguments to the appropriate C type by examining the flags associated with them. Or, you might be able to derive that info by decoding the fmt string (2nd arg). Either way, it's not all that pretty. (I don't know of any other way - but that doesn't mean that an alternative doesn't exist.)

Cheers,
Rob

In reply to Re: C Functions with variable argument list in perlxs by syphilis
in thread C Functions with variable argument list in perlxs by Temeschwarrior

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-29 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found