fast-import: Use strbuf API, and simplify cmd_data()

This patch features the use of strbuf_detach, and prevent the programmer
to mess with allocation directly. The code is as efficent as before, just
more concise and more straightforward.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Pierre Habouzit 2007-09-06 13:20:07 +02:00 committed by Junio C Hamano
parent 7a604f16b7
commit 4a241d79c9

View file

@ -340,7 +340,7 @@ static struct tag *last_tag;
/* Input stream parsing */ /* Input stream parsing */
static whenspec_type whenspec = WHENSPEC_RAW; static whenspec_type whenspec = WHENSPEC_RAW;
static struct strbuf command_buf; static struct strbuf command_buf = STRBUF_INIT;
static int unread_command_buf; static int unread_command_buf;
static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL}; static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
static struct recent_command *cmd_tail = &cmd_hist; static struct recent_command *cmd_tail = &cmd_hist;
@ -1638,17 +1638,16 @@ static void cmd_mark(void)
static void *cmd_data (size_t *size) static void *cmd_data (size_t *size)
{ {
size_t length; struct strbuf buffer;
char *buffer;
strbuf_init(&buffer);
if (prefixcmp(command_buf.buf, "data ")) if (prefixcmp(command_buf.buf, "data "))
die("Expected 'data n' command, found: %s", command_buf.buf); die("Expected 'data n' command, found: %s", command_buf.buf);
if (!prefixcmp(command_buf.buf + 5, "<<")) { if (!prefixcmp(command_buf.buf + 5, "<<")) {
char *term = xstrdup(command_buf.buf + 5 + 2); char *term = xstrdup(command_buf.buf + 5 + 2);
size_t sz = 8192, term_len = command_buf.len - 5 - 2; size_t term_len = command_buf.len - 5 - 2;
length = 0;
buffer = xmalloc(sz);
for (;;) { for (;;) {
read_line(&command_buf, stdin, '\n'); read_line(&command_buf, stdin, '\n');
if (command_buf.eof) if (command_buf.eof)
@ -1656,21 +1655,18 @@ static void *cmd_data (size_t *size)
if (term_len == command_buf.len if (term_len == command_buf.len
&& !strcmp(term, command_buf.buf)) && !strcmp(term, command_buf.buf))
break; break;
ALLOC_GROW(buffer, length + command_buf.len + 1, sz); strbuf_addbuf(&buffer, &command_buf);
memcpy(buffer + length, strbuf_addch(&buffer, '\n');
command_buf.buf,
command_buf.len);
length += command_buf.len;
buffer[length++] = '\n';
} }
free(term); free(term);
} }
else { else {
size_t n = 0; size_t n = 0, length;
length = strtoul(command_buf.buf + 5, NULL, 10); length = strtoul(command_buf.buf + 5, NULL, 10);
buffer = xmalloc(length);
while (n < length) { while (n < length) {
size_t s = fread(buffer + n, 1, length - n, stdin); size_t s = strbuf_fread(&buffer, length - n, stdin);
if (!s && feof(stdin)) if (!s && feof(stdin))
die("EOF in data (%lu bytes remaining)", die("EOF in data (%lu bytes remaining)",
(unsigned long)(length - n)); (unsigned long)(length - n));
@ -1679,8 +1675,8 @@ static void *cmd_data (size_t *size)
} }
skip_optional_lf(); skip_optional_lf();
*size = length; *size = buffer.len;
return buffer; return strbuf_detach(&buffer);
} }
static int validate_raw_date(const char *src, char *result, int maxlen) static int validate_raw_date(const char *src, char *result, int maxlen)