Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

A problem converting C code to XS

by mbidwell (Novice)
on Nov 15, 2004 at 23:49 UTC ( #408005=perlquestion: print w/replies, xml ) Need Help??
mbidwell has asked for the wisdom of the Perl Monks concerning the following question:

I am working on an API Test and I have opted to use the h2xs for changing the header files in to xs code.
What I have I think will suffice but I am having the trouble of converting the C code into usable XS code for 2 functions:

I32 enr_CreateChart (const CHAR* data_path, enr_Chart** chart) { I32 app_stat = ENR_NOERR; I32 chart_stat = ENR_NOERR; if (g_app == NULL) { app_stat = AppCreate (data_path, &g_app); } *chart = ChartCreate(); (*chart)->app = g_app; g_app->chart_cnt++; chart_stat = ChartInit (*chart); return (app_stat != ENR_NOERR) ? app_stat : chart_stat; }
I32 enr_GetThemeName (const enr_Chart* chart, I32 idx, CHAR** name) { enr_App* app = chart ? chart->app : NULL; JCLArray* themes = app ? &app->themes : NULL; ChartTheme** theme = themes ? (ChartTheme**) ArrayGet (themes, id +x) : NULL; if (theme == NULL) { return ENR_ERR_BADARG; } *name = (*theme)->name; return ENR_NOERR; }
All I want to do is send a call from Perl to the C API and get back the theme name using the enr_GetThemeName call.

Any help would be appreciated.

Thanks, Mike

20041115 Edit by ysth: code and p tags

20041116 Edit by ysth: retitle from: XS newbie

Replies are listed 'Best First'.
Re: A problem converting C code to XS
by tachyon (Chancellor) on Nov 16, 2004 at 02:20 UTC

    The heart of XS is really dealing with type conversion. XS can deal with some type conversions itself via its inbuilt typemap. You really have two issues. One is how you plan to pass around your C structs. You will almost certainly need to have a custom typemap (see perlxs tutorial) The second appears to be how to return strings or multiple values from your C function. You can quite happily do this in several ways:

    use Inline 'C'; my $str = string1(); print $str; my $retval = string2($str); print $str, "And the answer is $retval\n"; __END__ __C__ /* Reset the stack and push SV* onto it */ void string1() { char * set = "Hello \0"; dXSARGS; sp = mark; XPUSHs(sv_2mortal(newSVpv(set,strlen(set)))); PUTBACK; } /* Modify passed SV* C style, also return an int */ int string2( SV * str ) { char * set = "World!\n\0"; sv_setpvn( str, set, strlen(set) ); return 42; }



Re: A problem converting C code to XS
by tall_man (Parson) on Nov 16, 2004 at 00:19 UTC
    I would suggest using perl's ability to return multiple values. Your wrapper for getting the theme name could return both the error code and a regular perl string that way.

    Update: Here is an example using Inline::C. You can examine the XS code it generates (a bit of ugly stuff, but it works).

    use strict; use Inline (C => Config => CLEAN_AFTER_BUILD => 0); use Inline C => <<'END_OF_C_CODE'; void twoout() { int first = 42; char *second = "abc"; Inline_Stack_Vars; Inline_Stack_Reset; Inline_Stack_Push(sv_2mortal(newSViv(first))); Inline_Stack_Push(sv_2mortal(newSVpvn(second,3))); Inline_Stack_Done; } END_OF_C_CODE my ($num, $str) = twoout(); print "num = $num, str = $str\n";
Re: A problem converting C code to XS
by cosmicperl (Chaplain) on Nov 16, 2004 at 00:19 UTC
    I'm getting into XS code as well. If you've found any good sites on this please share.


      I don't know of any sites, but get yourself a copy of Extending and Embedding Perl and join perlxs mailing lists. The mailing list is slow sometimes, but you will get some good help, as well as asking here.


      I should have mentioned reading the man pages, perlxs & perlxstut. Here is nice site see how perl guts look :-),

      Have a look at these articles by Steven McDougall. They did help me most of all:


      This article is about XS. It explains what it is, why it is, how it works, and how to use it. It includes a complete, working example of an XS module, and a stub module that you can use as a starting point for your own code. It is an express goal of this article to provide the background and information necessary for you to write your own XS modules.

      This article is in five parts

      November - Introduction motivation, definitions, examples
      December - Architecture the Perl interpreter, calling conventions, data representation
      January - Tools h2xs, xsubpp, DynaLoader
      February - Modules Math::Ackermann, Set::Bit
      March - Align::NW Needleman-Wunsch global optimal sequence alignment

      And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
      (Terry Pratchett, Small Gods)

        Thanks to both of you. Those are some really good resources.

        Much appreciated!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://408005]
Approved by ysth
Front-paged by Courage
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-12-11 02:33 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (286 votes). Check out past polls.