@matheusbsbs / matheus-bagliano / commits / d350822fa7
Fork 0

vcs-svn: collect line_buffer data in a struct

Prepare for the line_buffer lib to support input from multiple files, by collecting global state in a struct that can be easily passed around. No API change yet. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Oct 10, 2010 at 21:39 UTC d350822fa7d14052713bea0ec62ff1246d8a2f7a
2 files changed +33 -23
vcs-svn/line_buffer.c
+22 -23
@@ -7,17 +7,16 @@
7 #include "line_buffer.h"
8 #include "strbuf.h"
9
10 -#define LINE_BUFFER_LEN 10000
10 #define COPY_BUFFER_LEN 4096
12 -
13 -static char line_buffer[LINE_BUFFER_LEN];
14 -static struct strbuf blob_buffer = STRBUF_INIT;
15 -static FILE *infile;
11 +static struct line_buffer buf_ = LINE_BUFFER_INIT;
12 +static struct line_buffer *buf;
13
14 int buffer_init(const char *filename)
15 {
19 - infile = filename ? fopen(filename, "r") : stdin;
20 - if (!infile)
16 + buf = &buf_;
17 +
18 + buf->infile = filename ? fopen(filename, "r") : stdin;
19 + if (!buf->infile)
20 return -1;
21 return 0;
22 }
@@ -25,10 +24,10 @@ int buffer_init(const char *filename)
24 int buffer_deinit(void)
25 {
26 int err;
28 - if (infile == stdin)
29 - return ferror(infile);
30 - err = ferror(infile);
31 - err |= fclose(infile);
27 + if (buf->infile == stdin)
28 + return ferror(buf->infile);
29 + err = ferror(buf->infile);
30 + err |= fclose(buf->infile);
31 return err;
32 }
33
@@ -36,13 +35,13 @@ int buffer_deinit(void)
35 char *buffer_read_line(void)
36 {
37 char *end;
39 - if (!fgets(line_buffer, sizeof(line_buffer), infile))
38 + if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
39 /* Error or data exhausted. */
40 return NULL;
42 - end = line_buffer + strlen(line_buffer);
41 + end = buf->line_buffer + strlen(buf->line_buffer);
42 if (end[-1] == '\n')
43 end[-1] = '\0';
45 - else if (feof(infile))
44 + else if (feof(buf->infile))
45 ; /* No newline at end of file. That's fine. */
46 else
47 /*
@@ -51,23 +50,23 @@ char *buffer_read_line(void)
50 * but for now let's return an error.
51 */
52 return NULL;
54 - return line_buffer;
53 + return buf->line_buffer;
54 }
55
56 char *buffer_read_string(uint32_t len)
57 {
59 - strbuf_reset(&blob_buffer);
60 - strbuf_fread(&blob_buffer, len, infile);
61 - return ferror(infile) ? NULL : blob_buffer.buf;
58 + strbuf_reset(&buf->blob_buffer);
59 + strbuf_fread(&buf->blob_buffer, len, buf->infile);
60 + return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
61 }
62
63 void buffer_copy_bytes(uint32_t len)
64 {
65 char byte_buffer[COPY_BUFFER_LEN];
66 uint32_t in;
68 - while (len > 0 && !feof(infile) && !ferror(infile)) {
67 + while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
68 in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
70 - in = fread(byte_buffer, 1, in, infile);
69 + in = fread(byte_buffer, 1, in, buf->infile);
70 len -= in;
71 fwrite(byte_buffer, 1, in, stdout);
72 if (ferror(stdout)) {
@@ -81,14 +80,14 @@ void buffer_skip_bytes(uint32_t len)
80 {
81 char byte_buffer[COPY_BUFFER_LEN];
82 uint32_t in;
84 - while (len > 0 && !feof(infile) && !ferror(infile)) {
83 + while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
84 in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
86 - in = fread(byte_buffer, 1, in, infile);
85 + in = fread(byte_buffer, 1, in, buf->infile);
86 len -= in;
87 }
88 }
89
90 void buffer_reset(void)
91 {
93 - strbuf_release(&blob_buffer);
92 + strbuf_release(&buf->blob_buffer);
93 }
vcs-svn/line_buffer.h
+11
@@ -1,6 +1,17 @@
1 #ifndef LINE_BUFFER_H_
2 #define LINE_BUFFER_H_
3
4 +#include "strbuf.h"
5 +
6 +#define LINE_BUFFER_LEN 10000
7 +
8 +struct line_buffer {
9 + char line_buffer[LINE_BUFFER_LEN];
10 + struct strbuf blob_buffer;
11 + FILE *infile;
12 +};
13 +#define LINE_BUFFER_INIT {"", STRBUF_INIT, NULL}
14 +
15 int buffer_init(const char *filename);
16 int buffer_deinit(void);
17 char *buffer_read_line(void);