Mercurial > libjeffpc
changeset 471:450424ffde09
val: factor out dump indentation code
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sun, 01 Apr 2018 14:59:37 -0400 |
parents | 21c09afa0b4f |
children | bd1080eff37f |
files | val_dump.c |
diffstat | 1 files changed, 20 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/val_dump.c Sun Apr 01 14:27:39 2018 -0400 +++ b/val_dump.c Sun Apr 01 14:59:37 2018 -0400 @@ -24,50 +24,55 @@ #include <jeffpc/val.h> +static inline void doindent(FILE *out, int indent) +{ + fprintf(out, "%*s", indent, ""); +} + void val_dump_file(FILE *out, struct val *val, int indent) { if (!val) return; + doindent(out, indent); + switch (val->type) { case VT_NULL: - fprintf(out, "%*snull\n", indent, ""); + fprintf(out, "null\n"); break; case VT_STR: case VT_SYM: - fprintf(out, "%*s'%s'\n", indent, "", val_cstr(val)); + fprintf(out, "'%s'\n", val_cstr(val)); break; case VT_INT: - fprintf(out, "%*s%"PRIu64"\n", indent, "", val->i); + fprintf(out, "%"PRIu64"\n", val->i); break; case VT_BOOL: - fprintf(out, "%*s%s\n", indent, "", - val->b ? "true" : "false"); + fprintf(out, "%s\n", val->b ? "true" : "false"); break; case VT_CHAR: if (isprint(val->i)) - fprintf(out, "%*s\\u%04"PRIX64": '%c'\n", - indent, "", val->i, (char) val->i); + fprintf(out, "\\u%04"PRIX64": '%c'\n", + val->i, (char) val->i); else - fprintf(out, "%*s\\u%04"PRIX64"\n", indent, - "", val->i); + fprintf(out, "\\u%04"PRIX64"\n", val->i); break; case VT_CONS: - fprintf(out, "%*scons head:\n", indent, ""); + fprintf(out, "cons head:\n"); val_dump(val->cons.head, indent + 2); - fprintf(out, "%*scons tail:\n", indent, ""); + doindent(out, indent); + fprintf(out, "cons tail:\n"); val_dump(val->cons.tail, indent + 2); break; case VT_BLOB: - fprintf(out, "%*sblob @ %p.%zu (%s)\n", indent, "", + fprintf(out, "blob @ %p.%zu (%s)\n", val->blob.ptr, val->blob.size, val->static_alloc ? "static" : "heap"); break; case VT_ARRAY: { size_t i; - fprintf(out, "%*sarray[%zu]:\n", indent, "", - val->array.nelem); + fprintf(out, "array[%zu]:\n", val->array.nelem); for (i = 0; i < val->array.nelem; i++) val_dump_file(out, val->array.vals[i], @@ -76,7 +81,7 @@ } case VT_NVL: /* TODO: dump the contents of the pairs */ - fprintf(out, "%*snvlist\n", indent, ""); + fprintf(out, "nvlist\n"); break; default: fprintf(out, "Unknown type %d\n", val->type);