gbranden pushed a commit to branch master in repository groff. commit ffcca436ace531354b1e06bf5a08f51c1f15be76 Author: G. Branden Robinson <g.branden.robin...@gmail.com> AuthorDate: Sat Mar 15 04:26:25 2025 -0500
[libgroff]: Support object dumping in JSON (1/3). * src/include/json-encode.h: * src/libs/libgroff/json_encode.cpp: Add new files. * src/libs/libgroff/libgroff.am (libgroff_a_SOURCES): Add "json_encode.cpp". --- ChangeLog | 10 +++++ src/include/json-encode.h | 29 ++++++++++++++ src/libs/libgroff/json_encode.cpp | 80 +++++++++++++++++++++++++++++++++++++++ src/libs/libgroff/libgroff.am | 3 +- 4 files changed, 121 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 516628973..3225d05ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2025-03-15 G. Branden Robinson <g.branden.robin...@gmail.com> + + [libgroff]: Support dumping of some object types in JSON format. + + * src/include/json-encode.h: + * src/libs/libgroff/json_encode.cpp: Add new files. + + * src/libs/libgroff/libgroff.am (libgroff_a_SOURCES): Add + "json_encode.cpp". + 2025-03-10 G. Branden Robinson <g.branden.robin...@gmail.com> Further rationalize header file inclusions. diff --git a/src/include/json-encode.h b/src/include/json-encode.h new file mode 100644 index 000000000..98bcbcf56 --- /dev/null +++ b/src/include/json-encode.h @@ -0,0 +1,29 @@ +/* Copyright (C) 2025 G. Branden Robinson + +This file is part of groff. + +groff is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation, either version 3 of the License, or +(at your option) any later version. + +groff is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +struct json_char { + char len; + char buf[7]; // '\uXXXX' + '\0' +}; + +json_char json_encode_char(unsigned char); + +// Local Variables: +// fill-column: 72 +// mode: C++ +// End: +// vim: set cindent noexpandtab shiftwidth=2 textwidth=72: diff --git a/src/libs/libgroff/json_encode.cpp b/src/libs/libgroff/json_encode.cpp new file mode 100644 index 000000000..a3bc47de7 --- /dev/null +++ b/src/libs/libgroff/json_encode.cpp @@ -0,0 +1,80 @@ +/* Copyright (C) 2025 G. Branden Robinson + +This file is part of groff. + +groff is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation, either version 3 of the License, or +(at your option) any later version. + +groff is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <assert.h> +#include <stdio.h> // snprintf() + +#include "cset.h" // csprint() +#include "json-encode.h" // json_char + +// Return pointer to mutable buffer representing character `c` as a JSON +// string. The caller must free the buffer. +json_char json_encode_char(unsigned char c) +{ + assert(c < 256); + json_char jc; + // Handle the most common cases first. + if (csprint(c)) { + jc.len = 1; + jc.buf[0] = c; + } + else if (('"' == c) || ('\\' == c) || ('/' == c)) { + jc.len = 2; + jc.buf[0] = '\\'; + jc.buf[1] = c; + } + else if ('\b' == c) { + jc.len = 2; + jc.buf[0] = '\\'; + jc.buf[1] = 'b'; + } + else if ('\t' == c) { + jc.len = 2; + jc.buf[0] = '\\'; + jc.buf[1] = 't'; + } + else if ('\n' == c) { + jc.len = 2; + jc.buf[0] = '\\'; + jc.buf[1] = 'n'; + } + else if ('\f' == c) { + jc.len = 2; + jc.buf[0] = '\\'; + jc.buf[1] = 'f'; + } + else if ('\r' == c) { + jc.len = 2; + jc.buf[0] = '\\'; + jc.buf[1] = 'r'; + } + else { + jc.len = 6; + (void) snprintf(jc.buf, sizeof jc.buf, "\\u%04X", c); + } + return jc; +} + +// Local Variables: +// fill-column: 72 +// mode: C++ +// End: +// vim: set cindent noexpandtab shiftwidth=2 textwidth=72: diff --git a/src/libs/libgroff/libgroff.am b/src/libs/libgroff/libgroff.am index 36b83d5ee..f0c53a3f0 100644 --- a/src/libs/libgroff/libgroff.am +++ b/src/libs/libgroff/libgroff.am @@ -1,4 +1,4 @@ -# Copyright (C) 2014-2024 Free Software Foundation, Inc. +# Copyright (C) 2014-2025 Free Software Foundation, Inc. # # This file is part of groff. # @@ -44,6 +44,7 @@ libgroff_a_SOURCES = \ src/libs/libgroff/iftoa.c \ src/libs/libgroff/invalid.cpp \ src/libs/libgroff/itoa.c \ + src/libs/libgroff/json_encode.cpp \ src/libs/libgroff/lf.cpp \ src/libs/libgroff/lineno.cpp \ src/libs/libgroff/localcharset.c \ _______________________________________________ groff-commit mailing list groff-commit@gnu.org https://lists.gnu.org/mailman/listinfo/groff-commit