]> vcs.slashdirt.org Git - sw/tic2json.git/commitdiff

vcs.slashdirt.org Git - sw/tic2json.git/commitdiff

git git / sw / tic2json.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 729beea)
tic2json: introduce PRINT2BUF
2021年9月26日 15:13:06 +0000 (17:13 +0200)
2021年9月28日 13:48:41 +0000 (15:48 +0200)
This commit implements a wrapper around print routines that will output
JSON to a buffer provided to tic2json_main(). If the `cb` parameter is
not NULL, this callback will be called before the content of the buffer
are overwritten, after each frame that would normally be printed by the
program.


diff --git a/tic2json.c b/tic2json.c
index 72ae641c932b03844bfe7aa669f6ae06f6ee81de..399b884c799405987ed56d117cab371f5ae50bd4 100644 (file)
--- a/tic2json.c
+++ b/tic2json.c
@@ -45,7 +45,42 @@
#ifdef BAREBUILD
#warning BAREBUILD currently requires defining only one version of supported TIC and does not provide main()
-#endif
+ #ifdef PRINT2BUF
+ static char * ticbuf;
+ static size_t ticbufsize, ticbufavail;
+
+ /**
+ * TIC frame callback
+ * @param buf the buffer received from tic2json_main(), filled with JSON data
+ * @param size the length of JSON data currently in the buffer
+ */
+ typedef void (*tic2json_framecb_t)(char * buf, size_t size);
+ static tic2json_framecb_t ticframecb;
+
+ #include <stdarg.h>
+ #undef ticprintf
+ int ticprintf(const char * restrict format, ...)
+ {
+ int ret;
+ va_list args;
+
+ va_start(args, format);
+ ret = vsnprintf(ticbuf + (ticbufsize - ticbufavail), ticbufavail, format, args);
+ va_end(args);
+
+ if (ret >= ticbufavail) {
+ fprintf(stderr, "ERROR: output buffer too small!\n");
+ return ticbufavail;
+ }
+ else if (ret < 0)
+ return ret;
+
+ ticbufavail -= ret;
+
+ return (ret);
+ }
+ #endif /* PRINT2BUF */
+#endif /* BAREBUILD */
#define TIC2JSON_VER "2.1"
@@ -257,7 +292,15 @@ void frame_sep(void)
tp.framecount = tp.skipframes;
if (tp.optflags & OPT_DICTOUT)
ticprintf("%c \"_tvalide\": %d", tp.fdelim, !tp.ferr);
+#ifdef PRINT2BUF
+ ticprintf("%c\n", tp.framedelims[1]);
+ if (ticframecb)
+ ticframecb(ticbuf, ticbufsize-ticbufavail);
+ ticbufavail = ticbufsize; // reset buffer
+ ticprintf("%c", tp.framedelims[0]);
+#else
ticprintf("%c\n%c", tp.framedelims[1], tp.framedelims[0]);
+#endif
}
tp.fdelim = ' ';
tp.ferr = 0;
@@ -408,9 +451,26 @@ int main(int argc, char **argv)
#else /* BAREBUILD */
+#ifdef PRINT2BUF
+/**
+ * tic2json_main(), print to buffer variant.
+ * @param buf an allocated buffer to write JSON data to
+ * @param size the size of the buffer
+ * @param an optional callback to call after each printed frame, before the buffer content is overwritten.
+ */
+void tic2json_main(char * buf, size_t size, tic2json_framecb_t cb)
+#else
void tic2json_main(void)
+#endif
{
ticinit();
+
+#ifdef PRINT2BUF
+ ticbuf = buf;
+ ticbufavail = ticbufsize = size;
+ ticframecb = cb;
+#endif
+
ticprintf("%c", tp.framedelims[0]);
#if defined(TICV01)
tic2json TIC parser/converter
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /