2010-08-10 02:39:43 +04: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 20:01:28 +03:00
|
|
|
#include "strbuf.h"
|
2010-08-10 02:39:43 +04:00
|
|
|
|
|
|
|
#define COPY_BUFFER_LEN 4096
|
|
|
|
|
2010-10-11 06:41:06 +04:00
|
|
|
int buffer_init(struct line_buffer *buf, const char *filename)
|
2010-08-10 02:39:43 +04:00
|
|
|
{
|
2010-10-11 06:39:21 +04:00
|
|
|
buf->infile = filename ? fopen(filename, "r") : stdin;
|
|
|
|
if (!buf->infile)
|
2010-08-10 02:39:43 +04:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-03 06:09:38 +03: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 06:10:59 +03:00
|
|
|
int buffer_tmpfile_init(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
buf->infile = tmpfile();
|
|
|
|
if (!buf->infile)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-11 06:41:06 +04:00
|
|
|
int buffer_deinit(struct line_buffer *buf)
|
2010-08-10 02:39:43 +04:00
|
|
|
{
|
|
|
|
int err;
|
2010-10-11 06:39:21 +04:00
|
|
|
if (buf->infile == stdin)
|
|
|
|
return ferror(buf->infile);
|
|
|
|
err = ferror(buf->infile);
|
|
|
|
err |= fclose(buf->infile);
|
2010-08-10 02:39:43 +04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-01-03 06:10:59 +03: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 06:51:21 +04:00
|
|
|
int buffer_ferror(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
return ferror(buf->infile);
|
|
|
|
}
|
|
|
|
|
2011-01-03 06:06:32 +03:00
|
|
|
int buffer_read_char(struct line_buffer *buf)
|
|
|
|
{
|
|
|
|
return fgetc(buf->infile);
|
|
|
|
}
|
|
|
|
|
2010-08-10 02:39:43 +04:00
|
|
|
/* Read a line without trailing newline. */
|
2010-10-11 06:41:06 +04:00
|
|
|
char *buffer_read_line(struct line_buffer *buf)
|
2010-08-10 02:39:43 +04:00
|
|
|
{
|
|
|
|
char *end;
|
2010-10-11 06:39:21 +04:00
|
|
|
if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
|
2010-08-10 02:39:43 +04:00
|
|
|
/* Error or data exhausted. */
|
|
|
|
return NULL;
|
2010-10-11 06:39:21 +04:00
|
|
|
end = buf->line_buffer + strlen(buf->line_buffer);
|
2010-08-10 02:39:43 +04:00
|
|
|
if (end[-1] == '\n')
|
|
|
|
end[-1] = '\0';
|
2010-10-11 06:39:21 +04:00
|
|
|
else if (feof(buf->infile))
|
2010-08-10 02:39:43 +04: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 06:39:21 +04:00
|
|
|
return buf->line_buffer;
|
2010-08-10 02:39:43 +04:00
|
|
|
}
|
|
|
|
|
2011-01-03 06:05:46 +03:00
|
|
|
void buffer_read_binary(struct line_buffer *buf,
|
|
|
|
struct strbuf *sb, uint32_t size)
|
|
|
|
{
|
|
|
|
strbuf_fread(sb, size, buf->infile);
|
|
|
|
}
|
|
|
|
|
2010-12-28 13:26:17 +03:00
|
|
|
off_t buffer_copy_bytes(struct line_buffer *buf, off_t nbytes)
|
2010-08-10 02:39:43 +04:00
|
|
|
{
|
2010-10-11 06:37:10 +04:00
|
|
|
char byte_buffer[COPY_BUFFER_LEN];
|
2010-12-28 13:26:17 +03: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 06:39:21 +04:00
|
|
|
in = fread(byte_buffer, 1, in, buf->infile);
|
2010-12-28 13:26:17 +03:00
|
|
|
done += in;
|
2010-08-10 02:39:43 +04:00
|
|
|
fwrite(byte_buffer, 1, in, stdout);
|
2010-12-28 13:26:17 +03:00
|
|
|
if (ferror(stdout))
|
|
|
|
return done + buffer_skip_bytes(buf, nbytes - done);
|
2010-08-10 02:39:43 +04:00
|
|
|
}
|
2010-12-28 13:26:17 +03:00
|
|
|
return done;
|
2010-08-10 02:39:43 +04:00
|
|
|
}
|
|
|
|
|
2010-10-11 06:44:21 +04:00
|
|
|
off_t buffer_skip_bytes(struct line_buffer *buf, off_t nbytes)
|
2010-08-10 02:39:43 +04:00
|
|
|
{
|
2010-10-11 06:37:10 +04:00
|
|
|
char byte_buffer[COPY_BUFFER_LEN];
|
2010-10-11 06:44:21 +04: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 02:39:43 +04:00
|
|
|
}
|
2010-10-11 06:44:21 +04:00
|
|
|
return done;
|
2010-08-10 02:39:43 +04:00
|
|
|
}
|
|
|
|
|
2010-10-11 06:41:06 +04:00
|
|
|
void buffer_reset(struct line_buffer *buf)
|
2010-08-10 02:39:43 +04:00
|
|
|
{
|
|
|
|
}
|