@matheusbsbs / matheus-bagliano / commits / d234f54b2f
Fork 0

vcs-svn: make buffer_skip_bytes return length read

Currently there is no way to detect when input ended if it ended early during buffer_skip_bytes. Tell the calling program how many bytes were actually skipped for easier debugging. Existing callers will still ignore early EOF. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: David Barr <david.barr@cordelta.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Oct 10, 2010 at 21:44 UTC d234f54b2f82067699f36593188e687fc7dc321a
3 files changed +10 -8
vcs-svn/line_buffer.c
+7 -6
@@ -120,15 +120,16 @@ void buffer_copy_bytes(struct line_buffer *buf, off_t len)
120 }
121 }
122
123 -void buffer_skip_bytes(struct line_buffer *buf, off_t len)
123 +off_t buffer_skip_bytes(struct line_buffer *buf, off_t nbytes)
124 {
125 char byte_buffer[COPY_BUFFER_LEN];
126 - uint32_t in;
127 - while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
128 - in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
129 - in = fread(byte_buffer, 1, in, buf->infile);
130 - len -= in;
126 + off_t done = 0;
127 + while (done < nbytes && !feof(buf->infile) && !ferror(buf->infile)) {
128 + off_t len = nbytes - done;
129 + size_t in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
130 + done += fread(byte_buffer, 1, in, buf->infile);
131 }
132 + return done;
133 }
134
135 void buffer_reset(struct line_buffer *buf)
vcs-svn/line_buffer.h
+1 -1
@@ -27,6 +27,6 @@ char *buffer_read_string(struct line_buffer *buf, uint32_t len);
27 int buffer_read_char(struct line_buffer *buf);
28 void buffer_read_binary(struct line_buffer *buf, struct strbuf *sb, uint32_t len);
29 void buffer_copy_bytes(struct line_buffer *buf, off_t len);
30 -void buffer_skip_bytes(struct line_buffer *buf, off_t len);
30 +off_t buffer_skip_bytes(struct line_buffer *buf, off_t len);
31
32 #endif
vcs-svn/line_buffer.txt
+2 -1
@@ -76,7 +76,8 @@ Functions
76
77 `buffer_skip_bytes`::
78 Discards `len` bytes from the input stream (stopping early
79 - if necessary because of an error or eof).
79 + if necessary because of an error or eof). Return value is
80 + the number of bytes successfully read.
81
82 `buffer_reset`::
83 Deallocates non-static buffers.