summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d280f68)
raw | patch | inline | side by side (parent: d280f68)
author | Jonathan Nieder <jrnieder@gmail.com> | |
Mon, 3 Jan 2011 03:05:46 +0000 (21:05 -0600) | ||
committer | Jonathan Nieder <jrnieder@gmail.com> | |
Sat, 26 Feb 2011 10:59:37 +0000 (04:59 -0600) |
buffer_read_string works well for non line-oriented input except for
one problem: it does not tell the caller how many bytes were actually
written. This means that unless one is very careful about checking
for errors (and eof) the calling program cannot tell the difference
between the string "foo" followed by an early end of file and the
string "foo\0bar\0baz".
So introduce a variant that reports the length, too, a thinner wrapper
around strbuf_fread. Its result is written to a strbuf so the caller
does not need to keep track of the number of bytes read.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
one problem: it does not tell the caller how many bytes were actually
written. This means that unless one is very careful about checking
for errors (and eof) the calling program cannot tell the difference
between the string "foo" followed by an early end of file and the
string "foo\0bar\0baz".
So introduce a variant that reports the length, too, a thinner wrapper
around strbuf_fread. Its result is written to a strbuf so the caller
does not need to keep track of the number of bytes read.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
t/t0081-line-buffer.sh | patch | blob | history | |
test-line-buffer.c | patch | blob | history | |
vcs-svn/line_buffer.c | patch | blob | history | |
vcs-svn/line_buffer.h | patch | blob | history |
diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh
index 33a728ed98edba5c963be666472824e9f493842e..a8eeb206457be723ac35c35e557f45967650dc89 100755 (executable)
--- a/t/t0081-line-buffer.sh
+++ b/t/t0081-line-buffer.sh
test_cmp expect actual
'
+test_expect_success 'read null byte' '
+ echo ">QhelloQ" | q_to_nul >expect &&
+ q_to_nul <<-\EOF | test-line-buffer >actual &&
+ binary 8
+ QhelloQ
+ EOF
+ test_cmp expect actual
+'
+
test_expect_success 'long reads are truncated' '
echo foo >expect &&
test-line-buffer <<-\EOF >actual &&
test_cmp expect actual
'
+test_expect_success 'long binary reads are truncated' '
+ echo ">foo" >expect &&
+ test-line-buffer <<-\EOF >actual &&
+ binary 5
+ foo
+ EOF
+ test_cmp expect actual
+'
+
test_done
diff --git a/test-line-buffer.c b/test-line-buffer.c
index ec19b13ee23d0de5a2423941655452627d3d00cc..19bf2d4403d03498b5c246367e59265535d2f90c 100644 (file)
--- a/test-line-buffer.c
+++ b/test-line-buffer.c
*/
#include "git-compat-util.h"
+#include "strbuf.h"
#include "vcs-svn/line_buffer.h"
static uint32_t strtouint32(const char *s)
static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
{
switch (*command) {
+ case 'b':
+ if (!prefixcmp(command, "binary ")) {
+ struct strbuf sb = STRBUF_INIT;
+ strbuf_addch(&sb, '>');
+ buffer_read_binary(buf, &sb, strtouint32(arg));
+ fwrite(sb.buf, 1, sb.len, stdout);
+ strbuf_release(&sb);
+ return;
+ }
case 'c':
if (!prefixcmp(command, "copy ")) {
buffer_copy_bytes(buf, strtouint32(arg));
diff --git a/vcs-svn/line_buffer.c b/vcs-svn/line_buffer.c
index 806932b32187bece9076131c69e8f80ae0c3a58a..661b007092247add3d7d5121345a999b8af53f7b 100644 (file)
--- a/vcs-svn/line_buffer.c
+++ b/vcs-svn/line_buffer.c
return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
}
+void buffer_read_binary(struct line_buffer *buf,
+ struct strbuf *sb, uint32_t size)
+{
+ strbuf_fread(sb, size, buf->infile);
+}
+
void buffer_copy_bytes(struct line_buffer *buf, uint32_t len)
{
char byte_buffer[COPY_BUFFER_LEN];
diff --git a/vcs-svn/line_buffer.h b/vcs-svn/line_buffer.h
index fb373903d20318da44ddc784842c56521b41e141..0c2d3d955a7d5ee3b6562ae44bbfededd603c478 100644 (file)
--- a/vcs-svn/line_buffer.h
+++ b/vcs-svn/line_buffer.h
int buffer_deinit(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 line_buffer *buf, struct strbuf *sb, uint32_t len);
void buffer_copy_bytes(struct line_buffer *buf, uint32_t len);
void buffer_skip_bytes(struct line_buffer *buf, uint32_t len);
void buffer_reset(struct line_buffer *buf);