summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: deadcef)
raw | patch | inline | side by side (parent: deadcef)
author | Jonathan Nieder <jrnieder@gmail.com> | |
Mon, 11 Oct 2010 02:39:21 +0000 (21:39 -0500) | ||
committer | Jonathan Nieder <jrnieder@gmail.com> | |
Sat, 26 Feb 2011 10:57:58 +0000 (04:57 -0600) |
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>
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>
vcs-svn/line_buffer.c | patch | blob | history | |
vcs-svn/line_buffer.h | patch | blob | history |
diff --git a/vcs-svn/line_buffer.c b/vcs-svn/line_buffer.c
index 6f32f28e5436f208a0a85bdcafe4d38a1f683631..e7bc230fcbf5dc021e8a5e8aa6f9ec66ce06ad6d 100644 (file)
--- a/vcs-svn/line_buffer.c
+++ b/vcs-svn/line_buffer.c
#include "line_buffer.h"
#include "strbuf.h"
-#define LINE_BUFFER_LEN 10000
#define COPY_BUFFER_LEN 4096
-
-static char line_buffer[LINE_BUFFER_LEN];
-static struct strbuf blob_buffer = STRBUF_INIT;
-static FILE *infile;
+static struct line_buffer buf_ = LINE_BUFFER_INIT;
+static struct line_buffer *buf;
int buffer_init(const char *filename)
{
- infile = filename ? fopen(filename, "r") : stdin;
- if (!infile)
+ buf = &buf_;
+
+ buf->infile = filename ? fopen(filename, "r") : stdin;
+ if (!buf->infile)
return -1;
return 0;
}
int buffer_deinit(void)
{
int err;
- if (infile == stdin)
- return ferror(infile);
- err = ferror(infile);
- err |= fclose(infile);
+ if (buf->infile == stdin)
+ return ferror(buf->infile);
+ err = ferror(buf->infile);
+ err |= fclose(buf->infile);
return err;
}
char *buffer_read_line(void)
{
char *end;
- if (!fgets(line_buffer, sizeof(line_buffer), infile))
+ if (!fgets(buf->line_buffer, sizeof(buf->line_buffer), buf->infile))
/* Error or data exhausted. */
return NULL;
- end = line_buffer + strlen(line_buffer);
+ end = buf->line_buffer + strlen(buf->line_buffer);
if (end[-1] == '\n')
end[-1] = '\0';
- else if (feof(infile))
+ else if (feof(buf->infile))
; /* No newline at end of file. That's fine. */
else
/*
* but for now let's return an error.
*/
return NULL;
- return line_buffer;
+ return buf->line_buffer;
}
char *buffer_read_string(uint32_t len)
{
- strbuf_reset(&blob_buffer);
- strbuf_fread(&blob_buffer, len, infile);
- return ferror(infile) ? NULL : blob_buffer.buf;
+ strbuf_reset(&buf->blob_buffer);
+ strbuf_fread(&buf->blob_buffer, len, buf->infile);
+ return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
}
void buffer_copy_bytes(uint32_t len)
{
char byte_buffer[COPY_BUFFER_LEN];
uint32_t in;
- while (len > 0 && !feof(infile) && !ferror(infile)) {
+ while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
- in = fread(byte_buffer, 1, in, infile);
+ in = fread(byte_buffer, 1, in, buf->infile);
len -= in;
fwrite(byte_buffer, 1, in, stdout);
if (ferror(stdout)) {
{
char byte_buffer[COPY_BUFFER_LEN];
uint32_t in;
- while (len > 0 && !feof(infile) && !ferror(infile)) {
+ while (len > 0 && !feof(buf->infile) && !ferror(buf->infile)) {
in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN;
- in = fread(byte_buffer, 1, in, infile);
+ in = fread(byte_buffer, 1, in, buf->infile);
len -= in;
}
}
void buffer_reset(void)
{
- strbuf_release(&blob_buffer);
+ strbuf_release(&buf->blob_buffer);
}
diff --git a/vcs-svn/line_buffer.h b/vcs-svn/line_buffer.h
index 9c78ae11a1e0774611b61a5407fab6cabfec226a..4ae1133a92993f6cc205f873adf8a8860f992909 100644 (file)
--- a/vcs-svn/line_buffer.h
+++ b/vcs-svn/line_buffer.h
#ifndef LINE_BUFFER_H_
#define LINE_BUFFER_H_
+#include "strbuf.h"
+
+#define LINE_BUFFER_LEN 10000
+
+struct line_buffer {
+ char line_buffer[LINE_BUFFER_LEN];
+ struct strbuf blob_buffer;
+ FILE *infile;
+};
+#define LINE_BUFFER_INIT {"", STRBUF_INIT, NULL}
+
int buffer_init(const char *filename);
int buffer_deinit(void);
char *buffer_read_line(void);