diff --git a/dist-files/Makefile.jxpretty b/dist-files/Makefile.jxpretty new file mode 100644 index 00000000..199247ee --- /dev/null +++ b/dist-files/Makefile.jxpretty @@ -0,0 +1,8 @@ +# +# Example Makefile for building the jxpretty example +# + +jxpretty: + gcc -o $@ -std=c99 -Wall -Wextra -O2 -Isrc \ + src/duktape.c examples/jxpretty/jxpretty.c \ + -lm diff --git a/examples/jxpretty/README.rst b/examples/jxpretty/README.rst new file mode 100644 index 00000000..5ab43a84 --- /dev/null +++ b/examples/jxpretty/README.rst @@ -0,0 +1,5 @@ +================ +Jxpretty example +================ + +Simple command line utility to pretty print JSON in the JX format. diff --git a/examples/jxpretty/jxpretty.c b/examples/jxpretty/jxpretty.c new file mode 100644 index 00000000..1e483efb --- /dev/null +++ b/examples/jxpretty/jxpretty.c @@ -0,0 +1,63 @@ +/* + * Pretty print JSON from stdin into indented JX. + */ + +#include +#include +#include "duktape.h" + +static duk_ret_t do_jxpretty(duk_context *ctx) { + FILE *f = stdin; + char buf[4096]; + size_t ret; + + for (;;) { + if (ferror(f)) { + duk_error(ctx, DUK_ERR_ERROR, "ferror() on stdin"); + } + if (feof(f)) { + break; + } + + ret = fread(buf, 1, sizeof(buf), f); +#if 0 + fprintf(stderr, "Read: %ld\n", (long) ret); + fflush(stderr); +#endif + if (ret == 0) { + break; + } + + duk_require_stack(ctx, 1); + duk_push_lstring(ctx, (const char *) buf, ret); + } + + duk_concat(ctx, duk_get_top(ctx)); + + duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', JSON.parse(v), null, 4)); })"); + duk_insert(ctx, -2); + duk_call(ctx, 1); + + return 0; +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + duk_int_t rc; + + /* suppress warnings */ + (void) argc; + (void) argv; + + ctx = duk_create_heap_default(); + + rc = duk_safe_call(ctx, do_jxpretty, 0 /*nargs*/, 1 /*nrets*/); + if (rc) { + fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1)); + fflush(stderr); + } + + duk_destroy_heap(ctx); + + return 0; +}