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?
In reply to Use a string as a FILE * stream in XS code by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |