From 775a9b47351aef261ac5d9aa28acd95e5eca186f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 1 Jun 2020 08:52:43 +0000 Subject: [PATCH] added the ctx parameter to the json write callback --- mio/bin/t03.c | 4 ++-- mio/lib/json.c | 7 ++++--- mio/lib/mio-json.h | 8 +++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mio/bin/t03.c b/mio/bin/t03.c index 3ebe603..1cb2215 100644 --- a/mio/bin/t03.c +++ b/mio/bin/t03.c @@ -90,7 +90,7 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level return 0; } -static int write_json_element (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen) +static int write_json_element (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen, void* ctx) { write (1, dptr, dlen); return 0; @@ -146,7 +146,7 @@ int main (int argc, char* argv[]) jsonwr = mio_jsonwr_open (mio, 0); - mio_jsonwr_setwritecb (jsonwr, write_json_element); + mio_jsonwr_setwritecb (jsonwr, write_json_element, MIO_NULL); mio_jsonwr_startarray (jsonwr); diff --git a/mio/lib/json.c b/mio/lib/json.c index aee8a9d..6ff2695 100644 --- a/mio/lib/json.c +++ b/mio/lib/json.c @@ -815,7 +815,7 @@ void mio_jsonwr_close (mio_jsonwr_t* jsonwr) mio_freemem (jsonwr->mio, jsonwr); } -static int write_nothing (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen) +static int write_nothing (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen, void* ctx) { return 0; } @@ -837,7 +837,7 @@ static int flush_wbuf (mio_jsonwr_t* jsonwr) { int ret = 0; - if (jsonwr->writecb(jsonwr, jsonwr->wbuf, jsonwr->wbuf_len) <= -1) ret = -1; + if (jsonwr->writecb(jsonwr, jsonwr->wbuf, jsonwr->wbuf_len, jsonwr->wctx) <= -1) ret = -1; jsonwr->wbuf_len = 0; /* reset the buffer length regardless of writing result */ return ret; @@ -851,9 +851,10 @@ void mio_jsonwr_fini (mio_jsonwr_t* jsonwr) /* ========================================================================= */ -void mio_jsonwr_setwritecb (mio_jsonwr_t* jsonwr, mio_jsonwr_writecb_t writecb) +void mio_jsonwr_setwritecb (mio_jsonwr_t* jsonwr, mio_jsonwr_writecb_t writecb, void* ctx) { jsonwr->writecb = writecb; + jsonwr->wctx = ctx; } static int escape_char (mio_uch_t uch, mio_uch_t* xch) diff --git a/mio/lib/mio-json.h b/mio/lib/mio-json.h index e5f3e9a..c58bec0 100644 --- a/mio/lib/mio-json.h +++ b/mio/lib/mio-json.h @@ -133,9 +133,9 @@ typedef struct mio_jsonwr_t mio_jsonwr_t; typedef int (*mio_jsonwr_writecb_t) ( mio_jsonwr_t* jsonwr, - /*mio_oow_t level,*/ const mio_bch_t* dptr, - mio_oow_t dlen + mio_oow_t dlen, + void* ctx ); typedef struct mio_jsonwr_state_node_t mio_jsonwr_state_node_t; @@ -156,6 +156,7 @@ struct mio_jsonwr_t mio_jsonwr_state_node_t* state_stack; int pretty; + void* wctx; mio_bch_t wbuf[8192]; mio_oow_t wbuf_len; }; @@ -265,7 +266,8 @@ static MIO_INLINE void* mio_jsonwr_getxtn (mio_jsonwr_t* jsonwr) { return (void* MIO_EXPORT void mio_jsonwr_setwritecb ( mio_jsonwr_t* jsonwr, - mio_jsonwr_writecb_t writecb + mio_jsonwr_writecb_t writecb, + void* ctx ); MIO_EXPORT int mio_jsonwr_write (