Original Post
As I'm sure many of you know, the C standard says that at least one character can be pushed back on to the stream with the ungetc() function. However, as for when more successive ungetc() calls are allowed, all bets are off as to how it works. I've finally gotten around to dealing with the frustration of adding an unget function to my I/O stream library, and it supports multiple ungets (up to 16 currently, but can be changed by modifying a constant). I've read documentation from all sorts of C libraries, and how they handle multiple calls to ungetc() is wildly different. I couldn't even get a grasp of what was the most popular method. What follows is a description of my current implementation:
Maximum of 16 ungotten characters; can be changed through modifying a constant.
Characters that are put back are read in LIFO order.
The put-back buffer is discarded after any seeking operation (fseek(), fsetpos(), rewind()), and the file position is unchanged from where ungetc() left it. I'm considering adding fflush() to this list.
A write operation starts writing at where the modified file position is; for streams that need to be flushed or seek()'ed between mode changes, this is irrelevant. It discards an amount of put-back characters equal to the amount written.
If anyone could share their thoughts on what makes the most sense, I'd appreciate it.
Maximum of 16 ungotten characters; can be changed through modifying a constant.
Characters that are put back are read in LIFO order.
The put-back buffer is discarded after any seeking operation (fseek(), fsetpos(), rewind()), and the file position is unchanged from where ungetc() left it. I'm considering adding fflush() to this list.
A write operation starts writing at where the modified file position is; for streams that need to be flushed or seek()'ed between mode changes, this is irrelevant. It discards an amount of put-back characters equal to the amount written.
If anyone could share their thoughts on what makes the most sense, I'd appreciate it.