Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 99 lines (89 sloc) 2.319 kb
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
1 /*
2 * test-line-buffer.c: code to exercise the svn importer's input helper
3 */
4
5 #include "git-compat-util.h"
e832f43 @jrn vcs-svn: add binary-safe read function
jrn authored
6 #include "strbuf.h"
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
7 #include "vcs-svn/line_buffer.h"
8
9 static uint32_t strtouint32(const char *s)
10 {
11 char *end;
12 uintmax_t n = strtoumax(s, &end, 10);
13 if (*s == '\0' || *end != '\0')
14 die("invalid count: %s", s);
15 return (uint32_t) n;
16 }
17
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
18 static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
19 {
20 switch (*command) {
e832f43 @jrn vcs-svn: add binary-safe read function
jrn authored
21 case 'b':
22 if (!prefixcmp(command, "binary ")) {
23 struct strbuf sb = STRBUF_INIT;
24 strbuf_addch(&sb, '>');
25 buffer_read_binary(buf, &sb, strtouint32(arg));
26 fwrite(sb.buf, 1, sb.len, stdout);
27 strbuf_release(&sb);
28 return;
29 }
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
30 case 'c':
31 if (!prefixcmp(command, "copy ")) {
7b990c9 @jrn vcs-svn: tweak test-line-buffer to not assume line-oriented input
jrn authored
32 buffer_copy_bytes(buf, strtouint32(arg));
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
33 return;
34 }
35 case 'r':
36 if (!prefixcmp(command, "read ")) {
37 const char *s = buffer_read_string(buf, strtouint32(arg));
7b990c9 @jrn vcs-svn: tweak test-line-buffer to not assume line-oriented input
jrn authored
38 fputs(s, stdout);
39 return;
40 }
41 case 's':
42 if (!prefixcmp(command, "skip ")) {
43 buffer_skip_bytes(buf, strtouint32(arg));
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
44 return;
45 }
46 default:
47 die("unrecognized command: %s", command);
48 }
49 }
50
51 static void handle_line(const char *line, struct line_buffer *stdin_buf)
52 {
53 const char *arg = strchr(line, ' ');
54 if (!arg)
55 die("no argument in line: %s", line);
56 handle_command(line, arg + 1, stdin_buf);
57 }
58
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
59 int main(int argc, char *argv[])
60 {
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
61 struct line_buffer stdin_buf = LINE_BUFFER_INIT;
d280f68 @jrn t0081 (line-buffer): add buffering tests
jrn authored
62 struct line_buffer file_buf = LINE_BUFFER_INIT;
63 struct line_buffer *input = &stdin_buf;
64 const char *filename;
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
65 char *s;
66
d280f68 @jrn t0081 (line-buffer): add buffering tests
jrn authored
67 if (argc == 1)
68 filename = NULL;
69 else if (argc == 2)
70 filename = argv[1];
71 else
cb3f87c @jrn vcs-svn: allow input from file descriptor
jrn authored
72 usage("test-line-buffer [file | &fd] < script");
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
73
74 if (buffer_init(&stdin_buf, NULL))
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
75 die_errno("open error");
d280f68 @jrn t0081 (line-buffer): add buffering tests
jrn authored
76 if (filename) {
cb3f87c @jrn vcs-svn: allow input from file descriptor
jrn authored
77 if (*filename == '&') {
78 if (buffer_fdinit(&file_buf, strtouint32(filename + 1)))
79 die_errno("error opening fd %s", filename + 1);
80 } else {
81 if (buffer_init(&file_buf, filename))
82 die_errno("error opening %s", filename);
83 }
d280f68 @jrn t0081 (line-buffer): add buffering tests
jrn authored
84 input = &file_buf;
85 }
86
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
87 while ((s = buffer_read_line(&stdin_buf)))
d280f68 @jrn t0081 (line-buffer): add buffering tests
jrn authored
88 handle_line(s, input);
89
90 if (filename && buffer_deinit(&file_buf))
91 die("error reading from %s", filename);
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
92 if (buffer_deinit(&stdin_buf))
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
93 die("input error");
94 if (ferror(stdout))
95 die("output error");
850c5ea @jrn vcs-svn: make test-line-buffer input format more flexible
jrn authored
96 buffer_reset(&stdin_buf);
3bbaec0 @barrbrain Add stream helper library
barrbrain authored
97 return 0;
98 }
Something went wrong with that request. Please try again.