Subject: [PATCH 08/16] vcs-svn: Let callers peek ahead to find stream end

From: Jonathan Nieder <[email-masked]>
Date: Sun, 10 Oct 2010 21:47:54 -0500

The buffer_at_eof() function returns 1 if and only if all input from
the input stream has been exhausted (because of EOF or error).  The
implementation calls fgetc() followed by ungetc() to force an EOF
condition when there is no more input remaining.

Like many functions in the line_buffer API, this function is not
thread-safe.  It could be made to be so with a mutex if needed.

Signed-off-by: Jonathan Nieder <[email-masked]>
---
 vcs-svn/line_buffer.c |   10 ++++++++++
 vcs-svn/line_buffer.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/vcs-svn/line_buffer.c b/vcs-svn/line_buffer.c
index 6dd0189..19caa21 100644
--- a/vcs-svn/line_buffer.c
+++ b/vcs-svn/line_buffer.c
@@ -27,6 +27,16 @@ int buffer_deinit(struct line_buffer *buf)
 	return err;
 }
 
+int buffer_at_eof(struct line_buffer *buf)
+{
+	int ch;
+	if ((ch = fgetc(buf->infile)) == EOF)
+		return 1;
+	if (ungetc(ch, buf->infile) == EOF)
+		return error("cannot unget %c: %s\n", ch, strerror(errno));
+	return 0;
+}
+
 /* Read a line without trailing newline. */
 char *buffer_read_line(struct line_buffer *buf)
 {
diff --git a/vcs-svn/line_buffer.h b/vcs-svn/line_buffer.h
index 873b0e4..0269aed 100644
--- a/vcs-svn/line_buffer.h
+++ b/vcs-svn/line_buffer.h
@@ -14,6 +14,7 @@ struct line_buffer {
 
 int buffer_init(struct line_buffer *buf, const char *filename);
 int buffer_deinit(struct line_buffer *buf);
+int buffer_at_eof(struct line_buffer *buf);
 char *buffer_read_line(struct line_buffer *buf);
 char *buffer_read_string(struct line_buffer *buf, uint32_t len);
 void buffer_read_binary(struct strbuf *sb, uint32_t len, struct line_buffer *f);
-- 
1.7.2.3