Re: Standard Read and Write FILE functions to refer to memory
From: Steve Summit (scs_at_eskimo.com)
Date: 12/13/04
- Next message: Lew Pitcher: "Re: Origin and History of Dot Syntax"
- Previous message: Herbert Rosenau: "Re: Reading binary file finding EOF"
- In reply to: Chris Torek: "Re: Standard Read and Write FILE functions to refer to memory"
- Next in thread: Keith Thompson: "Re: Standard Read and Write FILE functions to refer to memory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 13 Dec 2004 18:07:13 GMT
dandelion wrote:
>> Defining a couple of hooks would be sufficient:
>> - file_overflow to provide the 'write' functionality and
>> - file_underflow to provide 'read' functionality.
Those are the sort of things you want if you're trying
specifically to read/write from/to strings. The more usual
hooks are general-purpose write and read functions (though doing
it that way, it's true, does not end up easily supporting the
additional goal of writing to a malloc'ed region of memory that
grows as needed).
Chris Torek replied:
> I put this sort of thing, too, into the 4.3BSD stdio...
>
> FILE *funopen(cookie, readfn, writefn, seekfn, closefn)
>
> where "cookie" is a "void *" and the... function parameters are
> pointers to functions that implement reading, writing, seeking,
> and closing.
It turns out that the GNU folks have implemented this, also,
in at least one of the versions of glibc. The recipe is slightly
different:
cookie_io_functions_t funcs = {readfn, writefn, seekfn, closefn};
FILE *fp = fopencookie(cookie, mode, funcs);
where mode is "r" or "w", as usual.
Since this is an extension, you have to define the macro
_GNU_SOURCE to enable it. Once you've done so, you also have
access to
FILE *fmemopen(void *str, size_t len, const char *mode)
which gives just the functionality the original poster was asking
for (i.e. without having to define any of your own auxiliary read
or write functions), and also
FILE *open_memstream(char **strp, size_t *lenp)
which writes to the malloc'ed region pointed to by *strp and
grows it as needed (updating the size *lenp as it does so).
I don't know if any of this is documented anywhere; I stumbled
across it only recently while looking for something else in GNU's
copy of <stdio.h>.
Anyway, if you're using Linux or some other glibc-using system,
this functionality may well be available to you already. (But
before the topicality police come down on me, I have to reiterate
that these extensions are not standard and that code which makes
use of them is therefore not portable.)
> For whatever reason, these "stackable" streams did not get into C99.
And a pity it is, because it's insanely useful functionality,
and not difficult or expensive to implement. It seems like
practically every large program I've ever written has needed to
do this sort of thing (i.e. read or write interchangeably to a
file or a string) at some point; half the time I end up writing
my own little ad-hoc wrapper on top of stdio to let me do this,
which is doubly inefficient.
Steve Summit
scs@eskimo.com
- Next message: Lew Pitcher: "Re: Origin and History of Dot Syntax"
- Previous message: Herbert Rosenau: "Re: Reading binary file finding EOF"
- In reply to: Chris Torek: "Re: Standard Read and Write FILE functions to refer to memory"
- Next in thread: Keith Thompson: "Re: Standard Read and Write FILE functions to refer to memory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|