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>
maint
Jonathan Nieder 2010-10-10 21:39:21 -05:00
parent deadcef4c1
commit d350822fa7
2 changed files with 33 additions and 23 deletions

View File

@ -7,17 +7,16 @@
#include "line_buffer.h" #include "line_buffer.h"
#include "strbuf.h" #include "strbuf.h"


#define LINE_BUFFER_LEN 10000
#define COPY_BUFFER_LEN 4096 #define COPY_BUFFER_LEN 4096

static struct line_buffer buf_ = LINE_BUFFER_INIT;
static char line_buffer[LINE_BUFFER_LEN]; static struct line_buffer *buf;
static struct strbuf blob_buffer = STRBUF_INIT;
static FILE *infile;


int buffer_init(const char *filename) int buffer_init(const char *filename)
{ {
infile = filename ? fopen(filename, "r") : stdin; buf = &buf_;
if (!infile)
buf->infile = filename ? fopen(filename, "r") : stdin;
if (!buf->infile)
return -1; return -1;
return 0; return 0;
} }
@ -25,10 +24,10 @@ int buffer_init(const char *filename)
int buffer_deinit(void) int buffer_deinit(void)
{ {
int err; int err;
if (infile == stdin) if (buf->infile == stdin)
return ferror(infile); return ferror(buf->infile);
err = ferror(infile); err = ferror(buf->infile);
err |= fclose(infile); err |= fclose(buf->infile);
return err; return err;
} }


@ -36,13 +35,13 @@ int buffer_deinit(void)
char *buffer_read_line(void) char *buffer_read_line(void)
{ {
char *end; 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. */ /* Error or data exhausted. */
return NULL; return NULL;
end = line_buffer + strlen(line_buffer); end = buf->line_buffer + strlen(buf->line_buffer);
if (end[-1] == '\n') if (end[-1] == '\n')
end[-1] = '\0'; end[-1] = '\0';
else if (feof(infile)) else if (feof(buf->infile))
; /* No newline at end of file. That's fine. */ ; /* No newline at end of file. That's fine. */
else else
/* /*
@ -51,23 +50,23 @@ char *buffer_read_line(void)
* but for now let's return an error. * but for now let's return an error.
*/ */
return NULL; return NULL;
return line_buffer; return buf->line_buffer;
} }


char *buffer_read_string(uint32_t len) char *buffer_read_string(uint32_t len)
{ {
strbuf_reset(&blob_buffer); strbuf_reset(&buf->blob_buffer);
strbuf_fread(&blob_buffer, len, infile); strbuf_fread(&buf->blob_buffer, len, buf->infile);
return ferror(infile) ? NULL : blob_buffer.buf; return ferror(buf->infile) ? NULL : buf->blob_buffer.buf;
} }


void buffer_copy_bytes(uint32_t len) void buffer_copy_bytes(uint32_t len)
{ {
char byte_buffer[COPY_BUFFER_LEN]; char byte_buffer[COPY_BUFFER_LEN];
uint32_t in; 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 = 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; len -= in;
fwrite(byte_buffer, 1, in, stdout); fwrite(byte_buffer, 1, in, stdout);
if (ferror(stdout)) { if (ferror(stdout)) {
@ -81,14 +80,14 @@ void buffer_skip_bytes(uint32_t len)
{ {
char byte_buffer[COPY_BUFFER_LEN]; char byte_buffer[COPY_BUFFER_LEN];
uint32_t in; 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 = 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; len -= in;
} }
} }


void buffer_reset(void) void buffer_reset(void)
{ {
strbuf_release(&blob_buffer); strbuf_release(&buf->blob_buffer);
} }

View File

@ -1,6 +1,17 @@
#ifndef LINE_BUFFER_H_ #ifndef LINE_BUFFER_H_
#define 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_init(const char *filename);
int buffer_deinit(void); int buffer_deinit(void);
char *buffer_read_line(void); char *buffer_read_line(void);