[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: closein, freadahead and ungetc
From: |
Bruno Haible |
Subject: |
Re: closein, freadahead and ungetc |
Date: |
Mon, 10 Mar 2008 02:01:57 +0100 |
User-agent: |
KMail/1.5.4 |
Eric Blake wrote:
> | - freadahead keeps its signature but returns the total buffered bytes
> count.
This implements it.
2008-03-09 Bruno Haible <address@hidden>
* lib/freadahead.h (freadahead): Document more precisely.
* lib/freadahead.c (freadahead): When an ungetc is in effect, return
the sum of both buffer sizes.
* tests/test-freadahead.c (main): Also test behaviour after ungetc.
* NEWS: Document the change.
*** lib/freadahead.h.orig 2008-03-10 01:57:39.000000000 +0100
--- lib/freadahead.h 2008-03-10 00:35:33.000000000 +0100
***************
*** 1,5 ****
/* Retrieve information about a FILE stream.
! Copyright (C) 2007 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
--- 1,5 ----
/* Retrieve information about 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
***************
*** 23,28 ****
--- 23,30 ----
/* Assuming the stream STREAM is open for reading:
Return the number of bytes waiting in the input buffer of STREAM.
+ This includes both the bytes that have been read from the underlying input
+ source and the bytes that have been pushed back through 'ungetc'.
If this number is 0 and the stream is not currently writing,
fflush (STREAM) is known to be a no-op.
*** lib/freadahead.c.orig 2008-03-10 01:57:39.000000000 +0100
--- lib/freadahead.c 2008-03-10 01:44:47.000000000 +0100
***************
*** 1,5 ****
/* Retrieve information about a FILE stream.
! Copyright (C) 2007 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
--- 1,5 ----
/* Retrieve information about 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
***************
*** 25,35 ****
#if defined _IO_ferror_unlocked /* GNU libc, BeOS */
if (fp->_IO_write_ptr > fp->_IO_write_base)
return 0;
! return fp->_IO_read_end - fp->_IO_read_ptr;
#elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X,
Cygwin */
if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
return 0;
! return fp->_r;
#elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris,
mingw */
# if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
# define fp_ ((struct { unsigned char *_ptr; \
--- 25,48 ----
#if defined _IO_ferror_unlocked /* GNU libc, BeOS */
if (fp->_IO_write_ptr > fp->_IO_write_base)
return 0;
! return (fp->_IO_read_end - fp->_IO_read_ptr)
! + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base :
! 0);
#elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X,
Cygwin */
+ # if defined __NetBSD__ || defined __OpenBSD__
+ struct __sfileext
+ {
+ struct __sbuf _ub; /* ungetc buffer */
+ /* More fields, not relevant here. */
+ };
+ # define HASUB(fp) (((struct __sfileext *) (fp)->_ext._base)->_ub._base !=
NULL)
+ # else
+ # define HASUB(fp) ((fp)->_ub._base != NULL)
+ # endif
if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
return 0;
! return fp->_r
! + (HASUB (fp) ? fp->_ur : 0);
#elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris,
mingw */
# if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
# define fp_ ((struct { unsigned char *_ptr; \
***************
*** 51,66 ****
# ifdef __STDIO_BUFFERS
if (fp->__modeflags & __FLAG_WRITING)
return 0;
! return fp->__bufread - fp->__bufpos;
# else
return 0;
# endif
#elif defined __QNX__ /* QNX */
if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
return 0;
! /* fp->_Buf <= fp->_Next <= fp->_Rend */
! return fp->_Rend - fp->_Next;
#else
! #error "Please port gnulib freadahead.c to your platform! Look at the
definition of fflush, fread on your system, then report this to bug-gnulib."
#endif
}
--- 64,84 ----
# ifdef __STDIO_BUFFERS
if (fp->__modeflags & __FLAG_WRITING)
return 0;
! return (fp->__bufread - fp->__bufpos)
! + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0);
# else
return 0;
# endif
#elif defined __QNX__ /* QNX */
if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0)
return 0;
! /* fp->_Buf <= fp->_Next <= fp->_Rend,
! and fp->_Rend may be overridden by fp->_Rsave. */
! return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next)
! + (fp->_Mode & 0x4000 /* _MBYTE */
! ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback
! : 0);
#else
! #error "Please port gnulib freadahead.c to your platform! Look at the
definition of fflush, fread, ungetc on your system, then report this to
bug-gnulib."
#endif
}
*** tests/test-freadahead.c.orig 2008-03-10 01:57:39.000000000 +0100
--- tests/test-freadahead.c 2008-03-10 00:32:47.000000000 +0100
***************
*** 1,5 ****
/* Test of freadahead() function.
! Copyright (C) 2007 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
--- 1,5 ----
/* Test of freadahead() function.
! 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
***************
*** 54,61 ****
__STDIO_BUFFERS. */
ASSERT (freadahead (stdin) == 0);
else
! /* Normal buffered stdio. */
! ASSERT (freadahead (stdin) != 0);
}
return 0;
--- 54,82 ----
__STDIO_BUFFERS. */
ASSERT (freadahead (stdin) == 0);
else
! {
! /* Normal buffered stdio. */
! size_t buffered;
! int c, c2;
!
! ASSERT (freadahead (stdin) != 0);
! buffered = freadahead (stdin);
!
! c = fgetc (stdin);
! ASSERT (freadahead (stdin) == buffered - 1);
! ungetc (c, stdin);
! ASSERT (freadahead (stdin) == buffered);
! c2 = fgetc (stdin);
! ASSERT (c2 == c);
! ASSERT (freadahead (stdin) == buffered - 1);
!
! c = '@';
! ungetc (c, stdin);
! ASSERT (freadahead (stdin) == buffered);
! c2 = fgetc (stdin);
! ASSERT (c2 == c);
! ASSERT (freadahead (stdin) == buffered - 1);
! }
}
return 0;
*** NEWS.orig 2008-03-10 01:57:39.000000000 +0100
--- NEWS 2008-03-10 01:57:29.000000000 +0100
***************
*** 6,11 ****
--- 6,14 ----
Date Modules Changes
+ 2008-03-06 freadahead The return value's computation has changed. It
+ now increases by 1 after ungetc.
+
2008-01-26 isnan-nolibm The module name is changed from isnan-nolibm to
isnand-nolibm. The include file is changed from
"isnan.h" to "isnand.h". The function that it