From a71138a43ad6de7a2166640cc7cabc3befc2b9aa Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Thu, 26 Dec 2013 18:37:17 -0600 Subject: [PATCH] Add jv_dumpf() and jv_show() jv_dumpf() takes a FILE *. jv_show() is intended for use in debuggers, so it dumps the jv to stderr and it does not jv_free() the jv, so it's safe to "call jv_show(some_jv, -1)" in a debugger. If flags == -1 then the jv will be shown pretty-printed and in color. --- jv.h | 3 +++ jv_print.c | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/jv.h b/jv.h index 7e22cb4..3fc966a 100644 --- a/jv.h +++ b/jv.h @@ -3,6 +3,7 @@ #include #include +#include typedef enum { JV_KIND_INVALID, @@ -124,7 +125,9 @@ jv jv_object_iter_value(jv, int); int jv_get_refcnt(jv); enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2, JV_PRINT_COLOUR = 4, JV_PRINT_SORTED = 8, JV_PRINT_UNBUFFERED = 16 }; +void jv_dumpf(jv, FILE *f, int flags); void jv_dump(jv, int flags); +void jv_show(jv, int flags); jv jv_dump_string(jv, int flags); jv jv_parse(const char* string); diff --git a/jv_print.c b/jv_print.c index d6eff04..4b7dd51 100644 --- a/jv_print.c +++ b/jv_print.c @@ -260,16 +260,26 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI } } -void jv_dump(jv x, int flags) { +void jv_dumpf(jv x, FILE *f, int flags) { struct dtoa_context C; jvp_dtoa_context_init(&C); - jv_dump_term(&C, x, flags, 0, stdout, 0); + jv_dump_term(&C, x, flags, 0, f, 0); jvp_dtoa_context_free(&C); if (flags & JV_PRINT_UNBUFFERED) { - fflush(stdout); + fflush(f); } } +void jv_dump(jv x, int flags) { + jv_dumpf(x, stdout, flags); +} + +void jv_show(jv x, int flags) { + if (flags == -1) + flags = JV_PRINT_PRETTY | JV_PRINT_COLOUR | JV_PRINT_UNBUFFERED; + jv_dumpf(jv_copy(x), stderr, flags); +} + jv jv_dump_string(jv x, int flags) { struct dtoa_context C; jvp_dtoa_context_init(&C); -- 2.40.0