diff options
author | Joshua Haberman <joshua@reverberate.org> | 2011-02-25 18:31:22 -0800 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2011-02-25 18:31:22 -0800 |
commit | a75a305c77acd6800b81204f387f7a437a62fe6b (patch) | |
tree | dbd83530befface0868b7fdca37769590b61d5ed /src/upb_strstream.c | |
parent | abfc897b50532e5ed64f7f5497f80ef56abd3b26 (diff) |
Implemented upb_stringsink, upb_msgtotext, and exposed the latter to Lua.
Diffstat (limited to 'src/upb_strstream.c')
-rw-r--r-- | src/upb_strstream.c | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/src/upb_strstream.c b/src/upb_strstream.c index a7967d4..c957648 100644 --- a/src/upb_strstream.c +++ b/src/upb_strstream.c @@ -9,6 +9,9 @@ #include <stdlib.h> #include "upb_string.h" + +/* upb_stringsrc **************************************************************/ + 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; @@ -39,12 +42,12 @@ static bool upb_stringsrc_getstr(upb_bytesrc *_src, upb_string *str, } void upb_stringsrc_init(upb_stringsrc *s) { - static upb_bytesrc_vtbl bytesrc_vtbl = { + static upb_bytesrc_vtbl vtbl = { upb_stringsrc_read, upb_stringsrc_getstr, }; + upb_bytesrc_init(&s->bytesrc, &vtbl); s->str = NULL; - upb_bytesrc_init(&s->bytesrc, &bytesrc_vtbl); } void upb_stringsrc_reset(upb_stringsrc *s, upb_string *str) { @@ -63,3 +66,52 @@ void upb_stringsrc_uninit(upb_stringsrc *s) { upb_bytesrc *upb_stringsrc_bytesrc(upb_stringsrc *s) { return &s->bytesrc; } + + +/* upb_stringsink *************************************************************/ + +void upb_stringsink_uninit(upb_stringsink *s) { + upb_string_unref(s->str); +} + +// Resets the stringsink to a state where it will append to the given string. +// The string must be newly created or recycled. The stringsink will take a +// reference on the string, so the caller need not ensure that it outlives the +// stringsink. A stringsink can be reset multiple times. +void upb_stringsink_reset(upb_stringsink *s, upb_string *str) { + if (str != s->str) { + upb_string_unref(s->str); + s->str = upb_string_getref(str); + } + // Resize to 0. + upb_string_getrwbuf(s->str, 0); +} + +upb_bytesink *upb_stringsink_bytesink(upb_stringsink *s) { + return &s->bytesink; +} + +static upb_strlen_t upb_stringsink_vprintf(upb_bytesink *_sink, upb_status *s, + const char *fmt, va_list args) { + (void)s; // No errors can occur. + upb_stringsink *sink = (upb_stringsink*)_sink; + return upb_string_vprintf_at(sink->str, upb_string_len(sink->str), fmt, args); +} + +static upb_strlen_t upb_stringsink_putstr(upb_bytesink *_sink, upb_string *str, + upb_status *s) { + (void)s; // No errors can occur. + upb_stringsink *sink = (upb_stringsink*)_sink; + upb_strcat(sink->str, str); + return upb_string_len(str); +} + +void upb_stringsink_init(upb_stringsink *s) { + static upb_bytesink_vtbl vtbl = { + NULL, + upb_stringsink_putstr, + upb_stringsink_vprintf + }; + upb_bytesink_init(&s->bytesink, &vtbl); + s->str = NULL; +} |