FILE * friom C to ruby with swig

Discussion in 'Ruby' started by Lyes Amazouz, Aug 21, 2008.

  1. Lyes Amazouz

    Lyes Amazouz Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hello list,

    I have written a function which prototype is:

    FILE * getContent (void * objetc, char * code);

    This function a FILE * to a temporary file it fill with user data, and copy
    in "code" (with strcpy) some string

    I warpedd this function with Swig, but when I call it from a Ruby script, I
    have two problems:

    1- The class of the function output in Ruby is <SWIG::TYPE_p_FILE>, what is
    this class? I looked for it in the net by without results, Hows can I
    recover the data from my temporary file with this?

    2- when I call this function from a C program, the variable code will
    contain a string value that the function copy to it from an other variable
    in internal. and with a C program, the content of the variable code is
    updated very well. But when I call the wrapped function from a Ruby script,
    the argument code stays empty (empty string "")? How have to give this
    argument in such way that it will be modified?

    thank you


    --
    ===========
    | Lyes Amazouz
    | USTHB, Algiers
    ===========
    Lyes Amazouz, Aug 21, 2008
    #1
    1. Advertising

  2. Lyes Amazouz

    Alex Fenton Guest

    Lyes Amazouz wrote:

    > I have written a function which prototype is:
    >
    > FILE * getContent (void * objetc, char * code);
    >
    > This function a FILE * to a temporary file it fill with user data, and copy
    > in "code" (with strcpy) some string
    >
    > I warpedd this function with Swig, but when I call it from a Ruby script, I
    > have two problems:
    >
    > 1- The class of the function output in Ruby is <SWIG::TYPE_p_FILE>, what is
    > this class? I looked for it in the net by without results, Hows can I
    > recover the data from my temporary file with this?


    <SWIG::TYPE_p_XXX> is what SWIG returns when it doesn't know how to deal
    with the type returned by a C function. It's an opaque and useless
    pointer. You need to deal with FILE* - typically by using a
    %typemap(out) - in some way.

    I don't know exactly how to deal with FILE* - there may be a standard
    SWIG typemap to deal with this, or you might search for examples to
    convert it to a Ruby File object.

    > 2- when I call this function from a C program, the variable code will
    > contain a string value that the function copy to it from an other variable
    > in internal. and with a C program, the content of the variable code is
    > updated very well. But when I call the wrapped function from a Ruby script,
    > the argument code stays empty (empty string "")? How have to give this
    > argument in such way that it will be modified?


    Passing in a char* buffer which is modified by a function is a very
    common idiom in C, but it's un-rubyish. Again, you'll need a
    %typemap(in) or write a wrapper function. Have a look at the *INOUT
    standard typemaps in SWIG.

    Usually the most natural ruby way to deal with this is to use
    %typemap(in,numinputs=0) and return two VALUEs to ruby, the file object
    and a new string.

    alex
    Alex Fenton, Aug 21, 2008
    #2
    1. Advertising

  3. On 21/08/2008, Lyes Amazouz <> wrote:
    > Hello list,
    >
    > I have written a function which prototype is:
    >
    > FILE * getContent (void * objetc, char * code);
    >
    > This function a FILE * to a temporary file it fill with user data, and copy
    > in "code" (with strcpy) some string
    >
    > I warpedd this function with Swig, but when I call it from a Ruby script, I
    > have two problems:
    >
    > 1- The class of the function output in Ruby is <SWIG::TYPE_p_FILE>, what is
    > this class? I looked for it in the net by without results, Hows can I
    > recover the data from my temporary file with this?


    What you get is a FILE * object which ruby cannot handle. However, you
    can use Swig to write read and write methods for this object, and you
    could then get at the data.

    Or you could rewrite your C function to return a file descriptor
    rather than a FILE * object, and update the Swig wrapper to turn the
    file descriptor into an IO object (using IO::for_fd) that you can use
    in Ruby without problems. Or you could just use the automatic Swig
    wrapper, and wrap the function once more in ruby to turn the fd into
    IO.

    >
    > 2- when I call this function from a C program, the variable code will
    > contain a string value that the function copy to it from an other variable
    > in internal. and with a C program, the content of the variable code is
    > updated very well. But when I call the wrapped function from a Ruby script,
    > the argument code stays empty (empty string "")? How have to give this
    > argument in such way that it will be modified?


    I haven't seen your Swig code (and I am not proficient with Swig
    anyway but others might figure out something when they look at it) but
    my guess is that autogenerated wrappers should convert String to char
    * just fine.

    What might confuse the generator is the first argument which has no
    apparent type, and you have to extract some C value from the argument
    passed to the wrapped function. Which means that you will probably
    have to write a manual wrapper or even multiple wrappers that convert
    the first argument in different ways, and perhaps pass a different
    second argument based on the conversion of the first argument.

    Generally I have no idea what the function is supposed to do so I
    cannot tell you how it could be wrapped.

    HTH

    Michal
    Michal Suchanek, Aug 21, 2008
    #3
  4. Lyes Amazouz

    Lyes Amazouz Guest

    [Note: parts of this message were removed to make it a legal post.]

    OK, thank you all, I will try your suggets!

    --
    ===========
    | Lyes Amazouz
    | USTHB, Algiers
    ===========
    Lyes Amazouz, Aug 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    82
  2. RubyQuestions
    Replies:
    3
    Views:
    277
    RubyQuestions
    Jan 13, 2004
  3. Robert  McGovern

    Phil Tomson's article on SWIG, Ruby and C

    Robert McGovern, Feb 9, 2004, in forum: Ruby
    Replies:
    0
    Views:
    95
    Robert McGovern
    Feb 9, 2004
  4. Charles Comstock
    Replies:
    1
    Views:
    119
    Lyle Johnson
    Jun 25, 2004
  5. Martin DeMello

    SWIG vs Ruby/DL

    Martin DeMello, Oct 7, 2004, in forum: Ruby
    Replies:
    8
    Views:
    130
    gabriele renzi
    Oct 7, 2004
Loading...

Share This Page