From a75a305c77acd6800b81204f387f7a437a62fe6b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 25 Feb 2011 18:31:22 -0800 Subject: Implemented upb_stringsink, upb_msgtotext, and exposed the latter to Lua. --- src/upb_glue.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/upb_glue.c') diff --git a/src/upb_glue.c b/src/upb_glue.c index a3d4e7d..86022d8 100644 --- a/src/upb_glue.c +++ b/src/upb_glue.c @@ -8,6 +8,7 @@ #include "upb_msg.h" #include "upb_decoder.h" #include "upb_strstream.h" +#include "upb_textprinter.h" void upb_strtomsg(upb_string *str, upb_msg *msg, upb_msgdef *md, upb_status *status) { @@ -37,6 +38,29 @@ void upb_strtomsg(upb_string *str, upb_msg *msg, upb_msgdef *md, upb_handlers_uninit(&h); } +void upb_msgtotext(upb_string *str, upb_msg *msg, upb_msgdef *md, + bool single_line) { + upb_stringsink strsink; + upb_stringsink_init(&strsink); + upb_stringsink_reset(&strsink, str); + + upb_textprinter *p = upb_textprinter_new(); + upb_handlers h; + upb_handlers_init(&h); + upb_textprinter_reset(p, &h, upb_stringsink_bytesink(&strsink), single_line); + + upb_status status = UPB_STATUS_INIT; + upb_msg_runhandlers(msg, md, &h, &status); + // None of {upb_msg_runhandlers, upb_textprinter, upb_stringsink} should be + // capable of returning an error. + assert(upb_ok(&status)); + upb_status_uninit(&status); + + upb_stringsink_uninit(&strsink); + upb_textprinter_free(p); + upb_handlers_uninit(&h); +} + void upb_parsedesc(upb_symtab *symtab, upb_string *str, upb_status *status) { upb_stringsrc strsrc; upb_stringsrc_init(&strsrc); -- cgit v1.2.3