2010-08-10 00:39:43 +02:00
|
|
|
/*
|
|
|
|
* Licensed under a two-clause BSD-style license.
|
|
|
|
* See LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "line_buffer.h"
|
2010-11-06 18:01:28 +01:00
|
|
|
#include "strbuf.h"
|
2010-08-10 00:39:43 +02:00
|
|
|
|
|
|
|
#define COPY_BUFFER_LEN 4096
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
int buffer_init(struct line_buffer *buf, const char *filename)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:39:21 +02:00
|
|
|
buf->infile = filename ? fopen(filename, "r") : stdin;
|
|
|
|
if (!buf->infile)
|
2010-08-10 00:39:43 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-03 04:09:38 +01:00
|
|
|
int buffer_fdinit(struct line_buffer *buf, int fd)
|
|
|
|
{
|
|
|
|
buf->infile = fdopen(fd, "r");
|
|
|
|
if (!buf->infile)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-03 04:10:59 +01:00
|
|
|
int buffer_tmpfile_init(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
buf->infile = tmpfile();
|
|
|
|
if (!buf->infile)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
int buffer_deinit(struct line_buffer *buf)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
|
|
|
int err;
|
2010-10-11 04:39:21 +02:00
|
|
|
if (buf->infile == stdin)
|
|
|
|
return ferror(buf->infile);
|
|
|
|
err = ferror(buf->infile);
|
|
|
|
err |= fclose(buf->infile);
|
2010-08-10 00:39:43 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-01-03 04:10:59 +01:00
|
|
|
FILE *buffer_tmpfile_rewind(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
rewind(buf->infile);
|
|
|
|
return buf->infile;
|
|
|
|
}
|
|
|
|
|
|
|
|
long buffer_tmpfile_prepare_to_read(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
long pos = ftell(buf->infile);
|
|
|
|
if (pos < 0)
|
|
|
|
return error("ftell error: %s", strerror(errno));
|
|
|
|
if (fseek(buf->infile, 0, SEEK_SET))
|
|
|
|
return error("seek error: %s", strerror(errno));
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2011-01-03 04:06:32 +01:00
|
|
|
int buffer_read_char(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
return fgetc(buf->infile);
|
|
|
|
}
|
|
|
|
|
2010-08-10 00:39:43 +02:00
|
|
|
/* Read a line without trailing newline. */
|
2010-10-11 04:41:06 +02:00
|
|
|
char *buffer_read_line(struct line_buffer *buf)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
|
|
|
char *end;
|
2010-10-11 04:39:21 +02:00
|
|
|
if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
|
2010-08-10 00:39:43 +02:00
|
|
|
/* Error or data exhausted. */
|
|
|
|
return NULL;
|
2010-10-11 04:39:21 +02:00
|
|
|
end = buf->line_buffer + strlen(buf->line_buffer);
|
2010-08-10 00:39:43 +02:00
|
|
|
if (end[-1] == '\n')
|
|
|
|
end[-1] = '\0';
|
2010-10-11 04:39:21 +02:00
|
|
|
else if (feof(buf->infile))
|
2010-08-10 00:39:43 +02:00
|
|
|
; /* No newline at end of file. That's fine. */
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Line was too long.
|
|
|
|
* There is probably a saner way to deal with this,
|
|
|
|
* but for now let's return an error.
|
|
|
|
*/
|
|
|
|
return NULL;
|
2010-10-11 04:39:21 +02:00
|
|
|
return buf->line_buffer;
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
char *buffer_read_string(struct line_buffer *buf, uint32_t len)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:39:21 +02:00
|
|
|
strbuf_reset(&buf->blob_buffer);
|
|
|
|
strbuf_fread(&buf->blob_buffer, len, buf->infile);
|
|
|
|
return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|
|
|
|
|
2011-01-03 04:05:46 +01:00
|
|
|
void buffer_read_binary(struct line_buffer *buf,
|
|
|
|
struct strbuf *sb, uint32_t size)
|
|
|
|
{
|
|
|
|
strbuf_fread(sb, size, buf->infile);
|
|
|
|
}
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
void buffer_copy_bytes(struct line_buffer *buf, uint32_t len)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:37:10 +02:00
|
|
|
char byte_buffer[COPY_BUFFER_LEN];
|
2010-08-10 00:39:43 +02:00
|
|
|
uint32_t in;
|
2010-10-11 04:39:21 +02:00
|
|
|
while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
|
2010-08-10 00:39:43 +02:00
|
|
|
in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
|
2010-10-11 04:39:21 +02:00
|
|
|
in = fread(byte_buffer, 1, in, buf->infile);
|
2010-08-10 00:39:43 +02:00
|
|
|
len -= in;
|
|
|
|
fwrite(byte_buffer, 1, in, stdout);
|
|
|
|
if (ferror(stdout)) {
|
2010-10-11 04:41:06 +02:00
|
|
|
buffer_skip_bytes(buf, len);
|
2010-08-10 00:39:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
void buffer_skip_bytes(struct line_buffer *buf, uint32_t len)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:37:10 +02:00
|
|
|
char byte_buffer[COPY_BUFFER_LEN];
|
2010-08-10 00:39:43 +02:00
|
|
|
uint32_t in;
|
2010-10-11 04:39:21 +02:00
|
|
|
while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
|
2010-08-10 00:39:43 +02:00
|
|
|
in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
|
2010-10-11 04:39:21 +02:00
|
|
|
in = fread(byte_buffer, 1, in, buf->infile);
|
2010-08-10 00:39:43 +02:00
|
|
|
len -= in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
void buffer_reset(struct line_buffer *buf)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:39:21 +02:00
|
|
|
strbuf_release(&buf->blob_buffer);
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|