From: David Barr <david.barr@xxxxxxxxxxxx> This library provides facilities to read streams into buffers. It maintains a couple of static buffers and provides an API to use them. Signed-off-by: David Barr <david.barr@xxxxxxxxxxxx> Signed-off-by: Ramkumar Ramachandra <artagnon@xxxxxxxxx> --- vcs-svn/line_buffer.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++ vcs-svn/line_buffer.h | 14 +++++ 2 files changed, 148 insertions(+), 0 deletions(-) create mode 100644 vcs-svn/line_buffer.c create mode 100644 vcs-svn/line_buffer.h diff --git a/vcs-svn/line_buffer.c b/vcs-svn/line_buffer.c new file mode 100644 index 0000000..8ff1fb6 --- /dev/null +++ b/vcs-svn/line_buffer.c @@ -0,0 +1,134 @@ +/* + * Licensed under a two-clause BSD-style license. + * See LICENSE for details. + */ + +#include "git-compat-util.h" + +#include "line_buffer.h" +#include "obj_pool.h" + +#define LINE_BUFFER_LEN 10000 +#define COPY_BUFFER_LEN 4096 + +/* Create memory pool for char sequence of known length */ +obj_pool_gen(blob, char, 4096); + +static char line_buffer[LINE_BUFFER_LEN]; +static char byte_buffer[COPY_BUFFER_LEN]; +static uint32_t line_buffer_len = 0; +static uint32_t line_len = 0; +static FILE *infile; + +int buffer_init(char *filename) +{ + infile = fopen(filename, "r"); + if(!infile) + return 1; + return 0; +} + +int buffer_deinit() +{ + fclose(infile); + return 0; +} + +char *buffer_read_line(void) +{ + char *end; + uint32_t n_read; + + if (line_len) { + memmove(line_buffer, &line_buffer[line_len], + line_buffer_len - line_len); + line_buffer_len -= line_len; + line_len = 0; + } + + end = memchr(line_buffer, '\n', line_buffer_len); + while (line_buffer_len < LINE_BUFFER_LEN - 1 && + !feof(infile) && ferror(infile) && NULL == end) { + n_read = fread(&line_buffer[line_buffer_len], 1, + LINE_BUFFER_LEN - 1 - line_buffer_len, + infile); + end = memchr(&line_buffer[line_buffer_len], '\n', n_read); + line_buffer_len += n_read; + } + + if (ferror(infile)) + return NULL; + + if (end != NULL) { + line_len = end - line_buffer; + line_buffer[line_len++] = '\0'; + } else { + line_len = line_buffer_len; + line_buffer[line_buffer_len] = '\0'; + } + + if (line_len == 0) + return NULL; + + return line_buffer; +} + +char *buffer_read_string(uint32_t len) +{ + char *s; + blob_free(blob_pool.size); + s = blob_pointer(blob_alloc(len + 1)); + uint32_t offset = 0; + if (line_buffer_len > line_len) { + offset = line_buffer_len - line_len; + if (offset > len) + offset = len; + memcpy(s, &line_buffer[line_len], offset); + line_len += offset; + } + if (offset < len) + offset += fread(&s[offset], 1, len - offset, infile); + s[offset] = '\0'; + return s; +} + +void buffer_copy_bytes(uint32_t len) +{ + uint32_t in; + if (line_buffer_len > line_len) { + in = line_buffer_len - line_len; + if (in > len) + in = len; + fwrite(&line_buffer[line_len], 1, in, stdout); + len -= in; + line_len += in; + } + while (len > 0 && !feof(infile)) { + in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN; + in = fread(byte_buffer, 1, in, infile); + len -= in; + fwrite(byte_buffer, 1, in, stdout); + } +} + +void buffer_skip_bytes(uint32_t len) +{ + uint32_t in; + if (line_buffer_len > line_len) { + in = line_buffer_len - line_len; + if (in > len) + in = len; + line_len += in; + len -= in; + } + while (len > 0 && !feof(infile) && !ferror(infile)) { + in = len < COPY_BUFFER_LEN ? len : COPY_BUFFER_LEN; + in = fread(byte_buffer, 1, in, infile); + len -= in; + } +} + +void buffer_reset(void) +{ + blob_reset(); +} diff --git a/vcs-svn/line_buffer.h b/vcs-svn/line_buffer.h new file mode 100644 index 0000000..a6c42d7 --- /dev/null +++ b/vcs-svn/line_buffer.h @@ -0,1 +1,14 @@ +#ifndef LINE_BUFFER_H_ +#define LINE_BUFFER_H_ + +#include "git-compat-util.h" + +int buffer_init(char *filename); +int buffer_deinit(void); +char *buffer_read_line(void); +char *buffer_read_string(uint32_t len); +void buffer_copy_bytes(uint32_t len); +void buffer_skip_bytes(uint32_t len); +void buffer_reset(void); + +#endif -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html