Re: Standard Read and Write FILE functions to refer to memory

From: Steve Summit (scs_at_eskimo.com)
Date: 12/13/04


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



Relevant Pages

  • Re: Solution for sorting an array alpha-numerically
    ... strings up into groups and sorting the groups seperately, ... > so that numeric and alphabetic data sort as seperate groups. ... To the same project as the web page, add the class AlphaNumCompare() ...
    (microsoft.public.dotnet.general)
  • Re: Which of these authors is still worth reading?
    ... I hate this question, I hate it very, very much. ... I think you need to stop, and think about what the word means, before you start asking this sort of question. ... You must begin with a question, that by itself, is not subject to multiple interpretations. ... this is pretty much like the idea of people writing "evaluations" for employees. ...
    (rec.arts.sf.written)
  • Re: a question for sorting keys in Map
    ... I have a Map, actually a TreeMap, which will automatically sort the keys ... You can write your own Comparator object which implements the Compare ... strings so that X10 comes after X2 instead of before it. ... Unix ls command sort file names the way you want your strings to sort. ...
    (comp.lang.java.programmer)
  • Re: Wondering if its worth continuing
    ... Are you the sort of person who needs to ... much advance planning for her backbrain to put up with. ... What, exactly, are you doing to contrast your historians and your ... scene or twist that you're looking forward to writing, ...
    (rec.arts.sf.composition)
  • Re: mws first annual essay of the year project
    ... writing instead of just doing it and seeing how far I can go. ... Of course some will have fun pulling apart this post, ... that sort I would too. ... can look mighty blank, the possibilities mighty infinite, the chance ...
    (misc.writing)