summaryrefslogtreecommitdiff
path: root/stream
diff options
context:
space:
mode:
Diffstat (limited to 'stream')
-rw-r--r--stream/upb_decoder.c14
-rw-r--r--stream/upb_stdio.c66
-rw-r--r--stream/upb_textprinter.c1
-rw-r--r--stream/upb_textprinter.h3
4 files changed, 43 insertions, 41 deletions
diff --git a/stream/upb_decoder.c b/stream/upb_decoder.c
index b4b32ff..e60915f 100644
--- a/stream/upb_decoder.c
+++ b/stream/upb_decoder.c
@@ -126,6 +126,7 @@ static bool upb_getbuf(upb_decoder *d, void *data, size_t bytes_wanted,
} else {
// End-of-buffer.
if (d->buf) d->buf_stream_offset += upb_string_len(d->buf);
+ upb_string_recycle(&d->buf);
if (!upb_bytesrc_getstr(d->bytesrc, d->buf, d->status)) return false;
s->ptr = upb_string_getrobuf(d->buf);
}
@@ -295,7 +296,15 @@ void upb_decoder_run(upb_src *src, upb_status *status) {
while(1) {
// Parse/handle tag.
upb_tag tag;
- CHECK(upb_decode_tag(d, &state, &tag));
+ if (!upb_decode_tag(d, &state, &tag)) {
+ if (status->code == UPB_EOF && d->top == d->stack) {
+ // Normal end-of-file.
+ CHECK_FLOW(upb_dispatch_endmsg(&d->dispatcher));
+ return;
+ } else {
+ goto err;
+ }
+ }
// Decode wire data. Hopefully this branch will predict pretty well
// since most types will read a varint here.
@@ -361,9 +370,6 @@ void upb_decoder_run(upb_src *src, upb_status *status) {
CHECK_FLOW(upb_dispatch_value(&d->dispatcher, f, val));
}
- CHECK_FLOW(upb_dispatch_endmsg(&d->dispatcher));
- return;
-
err:
if (upb_ok(status)) {
upb_seterr(status, UPB_ERROR, "Callback returned UPB_BREAK");
diff --git a/stream/upb_stdio.c b/stream/upb_stdio.c
index 820399b..7923664 100644
--- a/stream/upb_stdio.c
+++ b/stream/upb_stdio.c
@@ -23,44 +23,42 @@ void upb_stdio_reset(upb_stdio *stdio, FILE* file) {
stdio->file = file;
}
-static bool upb_stdio_read(upb_stdio *stdio, upb_string *str,
- int offset, size_t bytes_to_read) {
- char *buf = upb_string_getrwbuf(str, offset + bytes_to_read) + offset;
- size_t read = fread(buf, 1, bytes_to_read, stdio->file);
- if(read < bytes_to_read) {
+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.
- stdio->bytesrc.eof = feof(stdio->file);
- if(ferror(stdio->file)) {
- upb_seterr(&stdio->bytesrc.status, UPB_STATUS_ERROR,
- "Error reading from stdio stream.");
- return false;
+ 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;
}
- // Resize to actual read size.
- upb_string_getrwbuf(str, offset + read);
}
- return true;
+ return read;
}
-bool upb_stdio_get(upb_bytesrc *src, upb_string *str, upb_strlen_t minlen) {
- // We ignore "minlen" since the stdio interfaces always return a full read
- // unless they are at EOF.
- (void)minlen;
- return upb_stdio_read((upb_stdio*)src, str, 0, BLOCK_SIZE);
-}
-
-bool upb_stdio_append(upb_bytesrc *src, upb_string *str, upb_strlen_t len) {
- return upb_stdio_read((upb_stdio*)src, str, upb_string_len(str), len);
+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;
}
int32_t upb_stdio_put(upb_bytesink *sink, upb_string *str) {
upb_stdio *stdio = (upb_stdio*)((char*)sink - offsetof(upb_stdio, bytesink));
upb_strlen_t len = upb_string_len(str);
- size_t written = fwrite(upb_string_getrobuf(str), 1, len, stdio->file);
+ upb_strlen_t written = fwrite(upb_string_getrobuf(str), 1, len, stdio->file);
if(written < len) {
// Error or EOF.
stdio->bytesink.eof = feof(stdio->file);
if(ferror(stdio->file)) {
- upb_seterr(&stdio->bytesink.status, UPB_STATUS_ERROR,
+ upb_seterr(&stdio->bytesink.status, UPB_ERROR,
"Error writing to stdio stream.");
return 0;
}
@@ -68,19 +66,19 @@ int32_t upb_stdio_put(upb_bytesink *sink, upb_string *str) {
return written;
}
-static upb_bytesrc_vtable upb_stdio_bytesrc_vtbl = {
- (upb_bytesrc_get_fptr)upb_stdio_get,
- (upb_bytesrc_append_fptr)upb_stdio_append,
-};
+upb_stdio *upb_stdio_new() {
+ static upb_bytesrc_vtbl bytesrc_vtbl = {
+ upb_stdio_read,
+ upb_stdio_getstr,
+ };
-static upb_bytesink_vtable upb_stdio_bytesink_vtbl = {
- upb_stdio_put
-};
+ //static upb_bytesink_vtbl bytesink_vtbl = {
+ // upb_stdio_put
+ //};
-upb_stdio *upb_stdio_new() {
upb_stdio *stdio = malloc(sizeof(*stdio));
- upb_bytesrc_init(&stdio->bytesrc, &upb_stdio_bytesrc_vtbl);
- upb_bytesink_init(&stdio->bytesink, &upb_stdio_bytesink_vtbl);
+ upb_bytesrc_init(&stdio->bytesrc, &bytesrc_vtbl);
+ //upb_bytesink_init(&stdio->bytesink, &bytesink_vtbl);
return stdio;
}
diff --git a/stream/upb_textprinter.c b/stream/upb_textprinter.c
index 2d2e237..3a77ab1 100644
--- a/stream/upb_textprinter.c
+++ b/stream/upb_textprinter.c
@@ -12,7 +12,6 @@
#include "upb_string.h"
struct _upb_textprinter {
- upb_sink sink;
upb_bytesink *bytesink;
upb_string *str;
int indent_depth;
diff --git a/stream/upb_textprinter.h b/stream/upb_textprinter.h
index 7e35412..b40d9fa 100644
--- a/stream/upb_textprinter.h
+++ b/stream/upb_textprinter.h
@@ -20,8 +20,7 @@ upb_textprinter *upb_textprinter_new();
void upb_textprinter_free(upb_textprinter *p);
void upb_textprinter_reset(upb_textprinter *p, upb_bytesink *sink,
bool single_line);
-
-upb_sink *upb_textprinter_sink(upb_textprinter *p);
+void upb_textprinter_sethandlers(upb_textprinter *p, upb_handlers *h);
#ifdef __cplusplus
} /* extern "C" */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback