From 6a1f3a66939308668ab8dce0d195afec16e02af9 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 14 Jul 2011 23:15:00 -0700 Subject: Major refactoring: upb_string is gone in favor of upb_strref. --- src/upb_stdio.c | 168 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 115 insertions(+), 53 deletions(-) (limited to 'src/upb_stdio.c') diff --git a/src/upb_stdio.c b/src/upb_stdio.c index c84d52a..20a3c15 100644 --- a/src/upb_stdio.c +++ b/src/upb_stdio.c @@ -9,96 +9,158 @@ #include #include -#include "upb_string.h" +#include // We can make this configurable if necessary. -#define BLOCK_SIZE 4096 +#define BUF_SIZE 32768 -struct upb_stdio { - upb_bytesrc bytesrc; - upb_bytesink bytesink; - FILE *file; -}; -void upb_stdio_reset(upb_stdio *stdio, FILE* file) { - stdio->file = file; +/* upb_bytesrc methods ********************************************************/ + +int upb_stdio_cmpbuf(const void *_key, const void *_elem) { + const uint64_t *ofs = _key; + const upb_stdio_buf *buf = _elem; + return (*ofs / BUF_SIZE) - (buf->ofs / BUF_SIZE); } +static upb_stdio_buf *upb_stdio_findbuf(upb_stdio *s, uint64_t ofs) { + // TODO: it is probably faster to linear search short lists, and to + // special-case the last one or two bufs. + return bsearch(&ofs, s->bufs, s->nbuf, sizeof(*s->bufs), &upb_stdio_cmpbuf); +} -/* upb_bytesrc methods ********************************************************/ +//static upb_strlen_t upb_stdio_read(void *src, uint32_t ofs, upb_buf *b, +// upb_status *status) { +// upb_stdio *stdio = (upb_stdio*)src; +// size_t read = fread(buf, 1, BLOCK_SIZE, stdio->file); +// if(read < (size_t)BLOCK_SIZE) { +// // Error or EOF. +// if(feof(stdio->file)) { +// upb_seterr(status, UPB_EOF, ""); +// } else if(ferror(stdio->file)) { +// upb_status_fromerrno(s); +// return 0; +// } +// } +// b->len = read; +// stdio->next_ofs += read; +// return stdio->next_ofs; +//} + +size_t upb_stdio_fetch(void *src, uint64_t ofs, upb_status *s) { + (void)src; + (void)ofs; + (void)s; + + return 0; +} -static upb_strlen_t upb_stdio_read(upb_bytesrc *src, void *buf, - upb_strlen_t count, upb_status *status) { - upb_stdio *stdio = (upb_stdio*)src; - assert(count > 0); - size_t read = fread(buf, 1, count, stdio->file); - if(read < (size_t)count) { - // Error or EOF. - if(feof(stdio->file)) { - upb_seterr(status, UPB_EOF, ""); - return read; - } else if(ferror(stdio->file)) { - upb_seterr(status, UPB_ERROR, "Error reading from stdio stream."); - return -1; - } +void upb_stdio_read(void *src, uint64_t src_ofs, size_t len, char *dst) { + upb_stdio_buf *buf = upb_stdio_findbuf(src, src_ofs); + src_ofs -= buf->ofs; + memcpy(dst, &buf->data[src_ofs], BUF_SIZE - src_ofs); + len -= (BUF_SIZE - src_ofs); + dst += (BUF_SIZE - src_ofs); + while (len > 0) { + ++buf; + size_t bytes = UPB_MIN(len, BUF_SIZE); + memcpy(dst, buf->data, bytes); + len -= bytes; + dst += bytes; } - return read; } -static bool upb_stdio_getstr(upb_bytesrc *src, upb_string *str, - upb_status *status) { - upb_strlen_t read = upb_stdio_read( - src, upb_string_getrwbuf(str, BLOCK_SIZE), BLOCK_SIZE, status); - if (read <= 0) return false; - upb_string_getrwbuf(str, read); - return true; +const char *upb_stdio_getptr(void *src, uint64_t ofs, size_t *len) { + upb_stdio_buf *buf = upb_stdio_findbuf(src, ofs); + ofs -= buf->ofs; + *len = BUF_SIZE - ofs; + return &buf->data[ofs]; +} + +void upb_stdio_refregion(void *src, uint64_t ofs, size_t len) { + upb_stdio_buf *buf = upb_stdio_findbuf(src, ofs); + len -= (BUF_SIZE - ofs); + ++buf->refcount; + while (len > 0) { + ++buf; + ++buf->refcount; + } +} + +void upb_stdio_unrefregion(void *src, uint64_t ofs, size_t len) { + (void)src; + (void)ofs; + (void)len; } /* upb_bytesink methods *******************************************************/ +#if 0 upb_strlen_t upb_stdio_putstr(upb_bytesink *sink, upb_string *str, upb_status *status) { - upb_stdio *stdio = (upb_stdio*)((char*)sink - offsetof(upb_stdio, bytesink)); + upb_stdio *stdio = (upb_stdio*)((char*)sink - offsetof(upb_stdio, sink)); upb_strlen_t len = upb_string_len(str); upb_strlen_t written = fwrite(upb_string_getrobuf(str), 1, len, stdio->file); if(written < len) { - upb_seterr(status, UPB_ERROR, "Error writing to stdio stream."); + upb_status_setf(status, UPB_ERROR, "Error writing to stdio stream."); return -1; } return written; } +#endif -upb_strlen_t upb_stdio_vprintf(upb_bytesink *sink, upb_status *status, - const char *fmt, va_list args) { - upb_stdio *stdio = (upb_stdio*)((char*)sink - offsetof(upb_stdio, bytesink)); - upb_strlen_t written = vfprintf(stdio->file, fmt, args); +uint32_t upb_stdio_vprintf(upb_bytesink *sink, upb_status *status, + const char *fmt, va_list args) { + upb_stdio *stdio = (upb_stdio*)((char*)sink - offsetof(upb_stdio, sink)); + int written = vfprintf(stdio->file, fmt, args); if (written < 0) { - upb_seterr(status, UPB_ERROR, "Error writing to stdio stream."); + upb_status_setf(status, UPB_ERROR, "Error writing to stdio stream."); return -1; } return written; } -upb_stdio *upb_stdio_new() { +void upb_stdio_init(upb_stdio *stdio) { static upb_bytesrc_vtbl bytesrc_vtbl = { + upb_stdio_fetch, upb_stdio_read, - upb_stdio_getstr, + upb_stdio_getptr, + upb_stdio_refregion, + upb_stdio_unrefregion, + NULL, + NULL }; + upb_bytesrc_init(&stdio->src, &bytesrc_vtbl); - static upb_bytesink_vtbl bytesink_vtbl = { - upb_stdio_putstr, - upb_stdio_vprintf - }; + //static upb_bytesink_vtbl bytesink_vtbl = { + // upb_stdio_putstr, + // upb_stdio_vprintf + //}; + //upb_bytesink_init(&stdio->bytesink, &bytesink_vtbl); +} - upb_stdio *stdio = malloc(sizeof(*stdio)); - upb_bytesrc_init(&stdio->bytesrc, &bytesrc_vtbl); - upb_bytesink_init(&stdio->bytesink, &bytesink_vtbl); - return stdio; +void upb_stdio_reset(upb_stdio* stdio, FILE *file) { + stdio->file = file; + stdio->should_close = false; +} + +void upb_stdio_open(upb_stdio *stdio, const char *filename, const char *mode, + upb_status *s) { + FILE *f = fopen(filename, mode); + if (!f) { + upb_status_fromerrno(s); + return; + } + setvbuf(stdio->file, NULL, _IONBF, 0); // Disable buffering; we do our own. + upb_stdio_reset(stdio, f); + stdio->should_close = true; } -void upb_stdio_free(upb_stdio *stdio) { - free(stdio); +void upb_stdio_uninit(upb_stdio *stdio) { + // Can't report status; caller should flush() to ensure data is written. + if (stdio->should_close) fclose(stdio->file); + stdio->file = NULL; } -upb_bytesrc* upb_stdio_bytesrc(upb_stdio *stdio) { return &stdio->bytesrc; } -upb_bytesink* upb_stdio_bytesink(upb_stdio *stdio) { return &stdio->bytesink; } +upb_bytesrc* upb_stdio_bytesrc(upb_stdio *stdio) { return &stdio->src; } +upb_bytesink* upb_stdio_bytesink(upb_stdio *stdio) { return &stdio->sink; } -- cgit v1.2.3