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