bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new module 'freadptr'


From: Bruno Haible
Subject: Re: new module 'freadptr'
Date: Fri, 29 Feb 2008 02:47:10 +0100
User-agent: KMail/1.5.4

Eric Blake wrote:
> > Here's one more module in the "stdio internals" series. freadptr (stream)
> > returns a pointer to the read buffer of stream.
> 
> Nice idea - I can already envision using it in M4

Yes, me too ;-) This is what triggered me to create this API.

> >               fseek (fp, nl - buf + 1, SEEK_CUR);
> 
> Not safe on pipes.

Yes. I just noticed this while testing the M4 patch :-)

> If fp is seekable, then this is okay.  But if not, then 
> you'll have to actually do the fread(fp,1, nl-buf+1, <some buf>) to keep the 
> stdio stream in sync with what you copied into the obstack.  You could 
> consider 
> using obstack_room, and if large enough, do the fread directly into 
> obstack_next_free, then follow up with obstack_blank_fast to get the obstack 
> to 
> recognize that you grew its contents on the side; otherwise do the fread into 
> a 
> temp buffer and obstack_copy it into the obstack which will grow 
> appropriately.

This solution is quite M4 specific, for a problem that is more general:
fseek does not work on pipes. We need a forward-seek (= read and discard)
function that works all on all kinds of streams. I intend to commit tomorrow
a new module that introduces this API:

========================== lib/freadseek.h ================================
/* Skipping input from a FILE stream.
   Copyright (C) 2007-2008 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <stddef.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Assuming the stream STREAM is open for reading:

   Read and discard OFFSET bytes from STREAM.

   freadseek (STREAM, OFFSET) is the same as
   fseek (STREAM, OFFSET, SEEK_CUR), except that the latter does not work
   on non-seekable input (such as pipes).

   Upon success, return 0.
   Upon error, set the error indicator in the stream and return EOF.

   STREAM must not be wide-character oriented.  */

extern int freadseek (FILE *stream, size_t offset);

#ifdef __cplusplus
}
#endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]