Code

747de07e6b4608d9d1ec68c9dc41d89aec552330
[git.git] / vcs-svn / line_buffer.c
1 /*
2  * Licensed under a two-clause BSD-style license.
3  * See LICENSE for details.
4  */
6 #include "git-compat-util.h"
7 #include "line_buffer.h"
8 #include "strbuf.h"
10 #define COPY_BUFFER_LEN 4096
12 int 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 }
20 int 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 }
28 int buffer_tmpfile_init(struct line_buffer *buf)
29 {
30         buf->infile = tmpfile();
31         if (!buf->infile)
32                 return -1;
33         return 0;
34 }
36 int 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 }
46 FILE *buffer_tmpfile_rewind(struct line_buffer *buf)
47 {
48         rewind(buf->infile);
49         return buf->infile;
50 }
52 long 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 }
62 int buffer_ferror(struct line_buffer *buf)
63 {
64         return ferror(buf->infile);
65 }
67 int buffer_read_char(struct line_buffer *buf)
68 {
69         return fgetc(buf->infile);
70 }
72 /* Read a line without trailing newline. */
73 char *buffer_read_line(struct line_buffer *buf)
74 {
75         char *end;
76         if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
77                 /* Error or data exhausted. */
78                 return NULL;
79         end = buf->line_buffer + strlen(buf->line_buffer);
80         if (end[-1] == '\n')
81                 end[-1] = '\0';
82         else if (feof(buf->infile))
83                 ; /* No newline at end of file.  That's fine. */
84         else
85                 /*
86                  * Line was too long.
87                  * There is probably a saner way to deal with this,
88                  * but for now let's return an error.
89                  */
90                 return NULL;
91         return buf->line_buffer;
92 }
94 char *buffer_read_string(struct line_buffer *buf, uint32_t len)
95 {
96         strbuf_reset(&buf->blob_buffer);
97         strbuf_fread(&buf->blob_buffer, len, buf->infile);
98         return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
99 }
101 void buffer_read_binary(struct line_buffer *buf,
102                                 struct strbuf *sb, uint32_t size)
104         strbuf_fread(sb, size, buf->infile);
107 void buffer_copy_bytes(struct line_buffer *buf, off_t len)
109         char byte_buffer[COPY_BUFFER_LEN];
110         uint32_t in;
111         while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
112                 in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
113                 in = fread(byte_buffer, 1, in, buf->infile);
114                 len -= in;
115                 fwrite(byte_buffer, 1, in, stdout);
116                 if (ferror(stdout)) {
117                         buffer_skip_bytes(buf, len);
118                         return;
119                 }
120         }
123 void buffer_skip_bytes(struct line_buffer *buf, off_t len)
125         char byte_buffer[COPY_BUFFER_LEN];
126         uint32_t in;
127         while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
128                 in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
129                 in = fread(byte_buffer, 1, in, buf->infile);
130                 len -= in;
131         }
134 void buffer_reset(struct line_buffer *buf)
136         strbuf_release(&buf->blob_buffer);