diff options
author | Joshua Haberman <joshua@reverberate.org> | 2011-01-29 20:16:34 -0800 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2011-01-29 20:16:34 -0800 |
commit | 93099cccd1e6428d6be45553c7dd7746bbd65e93 (patch) | |
tree | 7c24a5349daa132cfd374ef1f40ed9d2e3247447 | |
parent | 8536bbc5f88a4d2bb98d6875cf317154c263d473 (diff) |
upb_strstream compiles again.
That covers all source files except upb_msg!
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | stream/upb_strstream.c | 50 |
2 files changed, 31 insertions, 23 deletions
@@ -64,9 +64,9 @@ SRC=core/upb.c \ core/upb_def.c \ stream/upb_decoder.c \ stream/upb_stdio.c \ - stream/upb_textprinter.c + stream/upb_textprinter.c \ + stream/upb_strstream.c \ # core/upb_msg.c \ -# stream/upb_strstream.c \ $(SRC): perf-cppflags # Parts of core that are yet to be converted. diff --git a/stream/upb_strstream.c b/stream/upb_strstream.c index 7ed761b..d3fd4e0 100644 --- a/stream/upb_strstream.c +++ b/stream/upb_strstream.c @@ -12,6 +12,7 @@ struct upb_stringsrc { upb_bytesrc bytesrc; upb_string *str; + upb_strlen_t offset; }; void upb_stringsrc_reset(upb_stringsrc *s, upb_string *str) { @@ -27,34 +28,41 @@ void upb_stringsrc_free(upb_stringsrc *s) { free(s); } -static bool upb_stringsrc_get(upb_stringsrc *src, upb_string *str, - upb_strlen_t minlen) { - // We ignore "minlen" since we always return the entire string. - (void)minlen; - upb_string_substr(str, src->str, 0, upb_string_len(src->str)); - src->bytesrc.eof = true; - return true; +static upb_strlen_t upb_stringsrc_read(upb_bytesrc *_src, void *buf, + upb_strlen_t count, upb_status *status) { + upb_stringsrc *src = (upb_stringsrc*)_src; + if (src->offset == upb_string_len(src->str)) { + upb_seterr(status, UPB_EOF, ""); + return -1; + } else { + upb_strlen_t to_read = UPB_MIN(count, upb_string_len(src->str) - src->offset); + memcpy(buf, upb_string_getrobuf(src->str) + src->offset, to_read); + src->offset += to_read; + return to_read; + } } -static bool upb_stringsrc_append(upb_stringsrc *src, upb_string *str, - upb_strlen_t len) { - // Unimplemented; since we return the string via "get" all in one go, - // this method probably isn't very useful. - (void)src; - (void)str; - (void)len; - return false; +static bool upb_stringsrc_getstr(upb_bytesrc *_src, upb_string *str, + upb_status *status) { + upb_stringsrc *src = (upb_stringsrc*)_src; + if (src->offset == upb_string_len(str)) { + upb_seterr(status, UPB_EOF, ""); + return false; + } else { + upb_string_substr(str, src->str, 0, upb_string_len(src->str)); + return true; + } } -static upb_bytesrc_vtable upb_stringsrc_vtbl = { - (upb_bytesrc_get_fptr)upb_stringsrc_get, - (upb_bytesrc_append_fptr)upb_stringsrc_append, -}; - upb_stringsrc *upb_stringsrc_new() { + static upb_bytesrc_vtbl bytesrc_vtbl = { + upb_stringsrc_read, + upb_stringsrc_getstr, + }; + upb_stringsrc *s = malloc(sizeof(*s)); s->str = NULL; - upb_bytesrc_init(&s->bytesrc, &upb_stringsrc_vtbl); + upb_bytesrc_init(&s->bytesrc, &bytesrc_vtbl); return s; } |