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;
|
|
|
|
}
|
|
|
|
|
2010-10-11 04:51:21 +02:00
|
|
|
int buffer_ferror(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
return ferror(buf->infile);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-01-03 04:37:36 +01:00
|
|
|
size_t buffer_read_binary(struct line_buffer *buf,
|
|
|
|
struct strbuf *sb, size_t size)
|
2011-01-03 04:05:46 +01:00
|
|
|
{
|
2011-01-03 04:37:36 +01:00
|
|
|
return strbuf_fread(sb, size, buf->infile);
|
2011-01-03 04:05:46 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 11:26:17 +01:00
|
|
|
off_t buffer_copy_bytes(struct line_buffer *buf, off_t nbytes)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:37:10 +02:00
|
|
|
char byte_buffer[COPY_BUFFER_LEN];
|
2010-12-28 11:26:17 +01:00
|
|
|
off_t done = 0;
|
|
|
|
while (done < nbytes && !feof(buf->infile) && !ferror(buf->infile)) {
|
|
|
|
off_t len = nbytes - done;
|
|
|
|
size_t 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-12-28 11:26:17 +01:00
|
|
|
done += in;
|
2010-08-10 00:39:43 +02:00
|
|
|
fwrite(byte_buffer, 1, in, stdout);
|
2010-12-28 11:26:17 +01:00
|
|
|
if (ferror(stdout))
|
|
|
|
return done + buffer_skip_bytes(buf, nbytes - done);
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|
2010-12-28 11:26:17 +01:00
|
|
|
return done;
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|
|
|
|
|
2010-10-11 04:44:21 +02:00
|
|
|
off_t buffer_skip_bytes(struct line_buffer *buf, off_t nbytes)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
2010-10-11 04:37:10 +02:00
|
|
|
char byte_buffer[COPY_BUFFER_LEN];
|
2010-10-11 04:44:21 +02:00
|
|
|
off_t done = 0;
|
|
|
|
while (done < nbytes && !feof(buf->infile) && !ferror(buf->infile)) {
|
|
|
|
off_t len = nbytes - done;
|
|
|
|
size_t in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
|
|
|
|
done += fread(byte_buffer, 1, in, buf->infile);
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|
2010-10-11 04:44:21 +02:00
|
|
|
return done;
|
2010-08-10 00:39:43 +02:00
|
|
|
}
|
|
|
|
|
2010-10-11 04:41:06 +02:00
|
|
|
void buffer_reset(struct line_buffer *buf)
|
2010-08-10 00:39:43 +02:00
|
|
|
{
|
|
|
|
}
|