[phobos] escaping FILE's buffering tyranny

Andrei Alexandrescu andrei at erdani.com
Fri Sep 10 09:11:48 PDT 2010


I looked a bit yesterday into figuring out to better control FILE's 
buffering behavior. The problem is unpleasant enough that Lars had to 
define a whole separate type UnbufferedFile.

Background: FILE has only setbuf() (uninteresting) and setvbuf() to 
control buffering. There's no getvbuf() or generally any way to query 
what buffering mode and what buffer size has been set etc.

I've been looking at the actual definition of the FILE structure and it 
turns out it is possible (and quite easily) to define query functions 
that have OS-independent signatures and OS-dependent implementation.

This doesn't add much to the system-specific implementation because the 
FILE structure itself (which stores information regarding buffering 
etc.) is already defined in a system-specific manner in core.stdc.stdio.

I think we can define any or all of the following APIs for the File type:

/**
    Returns _IONBF, _IOLBF, or _IOFBF
*/
int getBufferingMode() const;
/**
    Returns the buffer size of the file (will be 1 if file is unbuffered).
*/
size_t getBufferSize() const;

We could even get into more esoteric things like getting how many bytes 
of lookahead are available or how many bytes could be pushed back into 
the stream with ungetc().


Thoughts?

Andrei



More information about the phobos mailing list