1/*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
5
6#include "git-compat-util.h"
7#include "line_buffer.h"
8#include "strbuf.h"
9
10#define COPY_BUFFER_LEN 4096
11
12int buffer_init(struct line_buffer *buf, const char *filename)
13{
14 buf->infile = filename ? fopen(filename, "r") : stdin;
15 if (!buf->infile)
16 return -1;
17 return 0;
18}
19
20int buffer_fdinit(struct line_buffer *buf, int fd)
21{
22 buf->infile = fdopen(fd, "r");
23 if (!buf->infile)
24 return -1;
25 return 0;
26}
27
28int buffer_tmpfile_init(struct line_buffer *buf)
29{
30 buf->infile = tmpfile();
31 if (!buf->infile)
32 return -1;
33 return 0;
34}
35
36int buffer_deinit(struct line_buffer *buf)
37{
38 int err;
39 if (buf->infile == stdin)
40 return ferror(buf->infile);
41 err = ferror(buf->infile);
42 err |= fclose(buf->infile);
43 return err;
44}
45
46FILE *buffer_tmpfile_rewind(struct line_buffer *buf)
47{
48 rewind(buf->infile);
49 return buf->infile;
50}
51
52long buffer_tmpfile_prepare_to_read(struct line_buffer *buf)
53{
54 long pos = ftell(buf->infile);
55 if (pos < 0)
56 return error("ftell error: %s", strerror(errno));
57 if (fseek(buf->infile, 0, SEEK_SET))
58 return error("seek error: %s", strerror(errno));
59 return pos;
60}
61
62int buffer_read_char(struct line_buffer *buf)
63{
64 return fgetc(buf->infile);
65}
66
67/* Read a line without trailing newline. */
68char *buffer_read_line(struct line_buffer *buf)
69{
70 char *end;
71 if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
72 /* Error or data exhausted. */
73 return NULL;
74 end = buf->line_buffer + strlen(buf->line_buffer);
75 if (end[-1] == '\n')
76 end[-1] = '\0';
77 else if (feof(buf->infile))
78 ; /* No newline at end of file. That's fine. */
79 else
80 /*
81 * Line was too long.
82 * There is probably a saner way to deal with this,
83 * but for now let's return an error.
84 */
85 return NULL;
86 return buf->line_buffer;
87}
88
89char *buffer_read_string(struct line_buffer *buf, uint32_t len)
90{
91 strbuf_reset(&buf->blob_buffer);
92 strbuf_fread(&buf->blob_buffer, len, buf->infile);
93 return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
94}
95
96void buffer_read_binary(struct line_buffer *buf,
97 struct strbuf *sb, uint32_t size)
98{
99 strbuf_fread(sb, size, buf->infile);
100}
101
102void buffer_copy_bytes(struct line_buffer *buf, uint32_t len)
103{
104 char byte_buffer[COPY_BUFFER_LEN];
105 uint32_t in;
106 while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
107 in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
108 in = fread(byte_buffer, 1, in, buf->infile);
109 len -= in;
110 fwrite(byte_buffer, 1, in, stdout);
111 if (ferror(stdout)) {
112 buffer_skip_bytes(buf, len);
113 return;
114 }
115 }
116}
117
118void buffer_skip_bytes(struct line_buffer *buf, uint32_t len)
119{
120 char byte_buffer[COPY_BUFFER_LEN];
121 uint32_t in;
122 while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
123 in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
124 in = fread(byte_buffer, 1, in, buf->infile);
125 len -= in;
126 }
127}
128
129void buffer_reset(struct line_buffer *buf)
130{
131 strbuf_release(&buf->blob_buffer);
132}