Provides the specified interface:

#define _MAX_PROTOCOL_LEN 25 #define _MAX_MODEL_LEN 25 int tdSensor(SV* protocol_sv, SV* model_sv, SV* id_sv, SV* dataTypes_sv) CODE: { char protocol[_MAX_PROTOCOL_LEN + 1]; char model[_MAX_MODEL_LEN + 1]; int id; int dataTypes; RETVAL = tdSensor( protocol, sizeof(protocol), model, sizeof(model), &id, &dataTypes ); if (RETVAL == TELLSTICK_SUCCESS) { SV* sv; sv = sv_2mortal(newSVpv(protocol, 0))); SvSetMagicSV(protoco +l_sv, sv); sv = sv_2mortal(newSVpv(model, 0)); SvSetMagicSV(model_s +v, sv); sv = sv_2mortal(newIV(id)); SvSetMagicSV(id_sv, + sv); sv = sv_2mortal(newIV(dataTypes)); SvSetMagicSV(dataTyp +es_sv, sv); } else { SvSetMagicSV(protocol_sv, &PL_sv_undef); SvSetMagicSV(model_sv, &PL_sv_undef); SvSetMagicSV(id_sv, &PL_sv_undef); SvSetMagicSV(dataTypes_sv, &PL_sv_undef); } } OUTPUT: RETVAL

Untested.


Provides a more Perlish interface:

#define _MAX_PROTOCOL_LEN 25 #define _MAX_MODEL_LEN 25 SV* tdSensor() CODE: { char protocol[_MAX_PROTOCOL_LEN + 1]; char model[_MAX_MODEL_LEN + 1]; int id; int dataTypes; int rv = tdSensor( protocol, sizeof(protocol), model, sizeof(model), &id, &dataTypes ); if (rv == TELLSTICK_SUCCESS) { HV* hv = newHV(); SV* sv; /* In theory, hv_stores can fail. */ /* However, I suspect it can't happen for this new hash. */ /* It it were to happen here, this code would leak. */ sv = newSVpv(protocol, 0); hv_stores(hv, "protocol", sv); sv = newSVpv(model, 0); hv_stores(hv, "model", sv); sv = newIV(id); hv_stores(hv, "id", sv); sv = newIV(dataTypes); hv_stores(hv, "dataTypes", sv); RETVAL = newRV_noinc(MUTABLE_SV(hv)); } else { RETVAL = &PL_sv_undef; } } OUTPUT: RETVAL

Usage:

use feature qw( say ); while (my $rec = tdSensor()) { say join ', ', "protocol: $rec->{protocol}", "model: $rec->{model}", "sensorId: $rec->{id}", "dataTypes: $rec->{dataTypes}"; }

Untested.

Update: Fixed missing sv declaration and missing typecast for newRV_noinc's argument.


In reply to Re: Passing integer pointer in XS? by ikegami
in thread Passing integer pointer in XS? by martin67

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, details, 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, summary, 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.