2 * test-line-buffer.c: code to exercise the svn importer's input helper
5 #include "git-compat-util.h"
7 #include "vcs-svn/line_buffer.h"
9 static uint32_t strtouint32(const char *s)
12 uintmax_t n = strtoumax(s, &end, 10);
13 if (*s == '\0' || *end != '\0')
14 die("invalid count: %s", s);
18 static void handle_command(const char *command, const char *arg, struct line_buffer *buf)
20 if (starts_with(command, "binary ")) {
21 struct strbuf sb = STRBUF_INIT;
22 strbuf_addch(&sb, '>');
23 buffer_read_binary(buf, &sb, strtouint32(arg));
24 fwrite(sb.buf, 1, sb.len, stdout);
26 } else if (starts_with(command, "copy ")) {
27 buffer_copy_bytes(buf, strtouint32(arg));
28 } else if (starts_with(command, "skip ")) {
29 buffer_skip_bytes(buf, strtouint32(arg));
31 die("unrecognized command: %s", command);
35 static void handle_line(const char *line, struct line_buffer *stdin_buf)
37 const char *arg = strchr(line, ' ');
39 die("no argument in line: %s", line);
40 handle_command(line, arg + 1, stdin_buf);
43 int cmd_main(int argc, const char **argv)
45 struct line_buffer stdin_buf = LINE_BUFFER_INIT;
46 struct line_buffer file_buf = LINE_BUFFER_INIT;
47 struct line_buffer *input = &stdin_buf;
56 usage("test-line-buffer [file | &fd] < script");
58 if (buffer_init(&stdin_buf, NULL))
59 die_errno("open error");
61 if (*filename == '&') {
62 if (buffer_fdinit(&file_buf, strtouint32(filename + 1)))
63 die_errno("error opening fd %s", filename + 1);
65 if (buffer_init(&file_buf, filename))
66 die_errno("error opening %s", filename);
71 while ((s = buffer_read_line(&stdin_buf)))
72 handle_line(s, input);
74 if (filename && buffer_deinit(&file_buf))
75 die("error reading from %s", filename);
76 if (buffer_deinit(&stdin_buf))