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

I've written a C library that exposes certain functionality via FILE * streams:

void foo_read_stuff(foo_t *ctx, FILE *stream); /* reads text from stre +am, updates internals of ctx with the data */ void foo_write_stuff(foo_t *ctx, FILE *stream); /* writes text to stre +am based on contents of ctx */

The intention was that the C applications using these functions either use stdin/stdout or open a file with fopen().

Now I want to write a perl wrapper around the library where the equivalents of these functions read from, or write to Perl strings instead of stdin or stdout or a file.

What's the best way to accomplish this?

Obviously, I could open a temporary file, let the library functions do their job, then reopen the file from the XS code and dump its contents into an SV (or write the contents of the SV to the file then reopen and call the function), but this seems clumsy and unnecessary.

There are also the GNU libc functions fmemopen() and open_memstream(), which allow me to treat a memory buffer as a FILE * stream, but these are not portable and I don't know whether they can be used safely in an XS function.

Since I wrote the C library myself, I could modify it that the functions in question could operate on strings as well as streams, but I'd rather not do that as I'd like to keep the library free of bloat.

Is there a more Perlish solution to this?

Replies are listed 'Best First'.
Re: Use a string as a FILE * stream in XS code
by dcmertens (Scribe) on Aug 18, 2016 at 16:33 UTC

      (OP here)

      I went with the fmemopen/open_memstream route.

      They seem to do what I want without too much trouble, and I can live with the fact that they aren't portable - the library itself is pretty much Linux-only at this point.