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
)
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
);
31 if (!prefixcmp(command
, "copy ")) {
32 buffer_copy_bytes(buf
, strtouint32(arg
));
36 if (!prefixcmp(command
, "read ")) {
37 const char *s
= buffer_read_string(buf
, strtouint32(arg
));
42 if (!prefixcmp(command
, "skip ")) {
43 buffer_skip_bytes(buf
, strtouint32(arg
));
47 die("unrecognized command: %s", command
);
51 static void handle_line(const char *line
, struct line_buffer
*stdin_buf
)
53 const char *arg
= strchr(line
, ' ');
55 die("no argument in line: %s", line
);
56 handle_command(line
, arg
+ 1, stdin_buf
);
59 int main(int argc
, char *argv
[])
61 struct line_buffer stdin_buf
= LINE_BUFFER_INIT
;
62 struct line_buffer file_buf
= LINE_BUFFER_INIT
;
63 struct line_buffer
*input
= &stdin_buf
;
72 usage("test-line-buffer [file | &fd] < script");
74 if (buffer_init(&stdin_buf
, NULL
))
75 die_errno("open error");
77 if (*filename
== '&') {
78 if (buffer_fdinit(&file_buf
, strtouint32(filename
+ 1)))
79 die_errno("error opening fd %s", filename
+ 1);
81 if (buffer_init(&file_buf
, filename
))
82 die_errno("error opening %s", filename
);
87 while ((s
= buffer_read_line(&stdin_buf
)))
88 handle_line(s
, input
);
90 if (filename
&& buffer_deinit(&file_buf
))
91 die("error reading from %s", filename
);
92 if (buffer_deinit(&stdin_buf
))
96 buffer_reset(&stdin_buf
);