Module Name: othersrc
Committed By: agc
Date: Fri Mar 7 01:34:19 UTC 2014
Modified Files:
othersrc/external/bsd/transit/bin: Makefile
othersrc/external/bsd/transit/dist: libtransit.3 main.c transit.c
transit.h
Added Files:
othersrc/external/bsd/transit/bin: 17.expected
Log Message:
Changes to transit-20140306
+ add a function to format JSON in-memory as part of the transit library
+ remove similar functionality from the program
+ update documentation
To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 othersrc/external/bsd/transit/bin/17.expected
cvs rdiff -u -r1.4 -r1.5 othersrc/external/bsd/transit/bin/Makefile
cvs rdiff -u -r1.2 -r1.3 othersrc/external/bsd/transit/dist/libtransit.3
cvs rdiff -u -r1.4 -r1.5 othersrc/external/bsd/transit/dist/main.c \
othersrc/external/bsd/transit/dist/transit.c \
othersrc/external/bsd/transit/dist/transit.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: othersrc/external/bsd/transit/bin/Makefile
diff -u othersrc/external/bsd/transit/bin/Makefile:1.4 othersrc/external/bsd/transit/bin/Makefile:1.5
--- othersrc/external/bsd/transit/bin/Makefile:1.4 Wed Mar 5 04:58:51 2014
+++ othersrc/external/bsd/transit/bin/Makefile Fri Mar 7 01:34:19 2014
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.4 2014/03/05 04:58:51 agc Exp $
+# $NetBSD: Makefile,v 1.5 2014/03/07 01:34:19 agc Exp $
.include <bsd.own.mk>
@@ -98,3 +98,8 @@ t: ${PROG}
env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d -j 16.enc > 16.out
diff 16.expected 16.out
rm -f 16.enc 16.out
+ echo "17. json output"
+ env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -j '[1,2,3,4,[5,6,7,[8,[9,[0,10]]]]]' > 17.enc
+ env LD_LIBRARY_PATH=${LIB_TRANSIT_DIR} ./${PROG} -d -j 17.enc > 17.out
+ diff 17.expected 17.out
+ rm -f 17.out 17.enc
Index: othersrc/external/bsd/transit/dist/libtransit.3
diff -u othersrc/external/bsd/transit/dist/libtransit.3:1.2 othersrc/external/bsd/transit/dist/libtransit.3:1.3
--- othersrc/external/bsd/transit/dist/libtransit.3:1.2 Wed Mar 5 04:58:51 2014
+++ othersrc/external/bsd/transit/dist/libtransit.3 Fri Mar 7 01:34:19 2014
@@ -1,4 +1,4 @@
-.\" $NetBSD: libtransit.3,v 1.2 2014/03/05 04:58:51 agc Exp $
+.\" $NetBSD: libtransit.3,v 1.3 2014/03/07 01:34:19 agc Exp $
.\"
.\" Copyright (c) 2014 Alistair Crooks <[email protected]>
.\" All rights reserved.
@@ -23,7 +23,7 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd March 4, 2014
+.Dd March 6, 2014
.Dt LIBTRANSIT 3
.Os
.Sh NAME
@@ -54,6 +54,10 @@
.Fo transit_read_json
.Fa "transit_t *transit" "const char *in" "size_t length"
.Fc
+.Ft int
+.Fo transit_format_json
+.Fa "transit_t *transit" "const char *out" "size_t bufsize"
+.Fc
.Ft transit_t *
.Fo transit_new
.Fa "void"
@@ -154,6 +158,9 @@ by using the
functions.
Encoded JSON can be converted into the transit protocol using the
.Fn transit_read_json
+function, whilst
+the data can be formatted into output as JSON using the
+.Fn transit_format_json
function.
.Pp
To decode serialised input, possibly at the other end of a connection,
Index: othersrc/external/bsd/transit/dist/main.c
diff -u othersrc/external/bsd/transit/dist/main.c:1.4 othersrc/external/bsd/transit/dist/main.c:1.5
--- othersrc/external/bsd/transit/dist/main.c:1.4 Tue Feb 25 20:19:35 2014
+++ othersrc/external/bsd/transit/dist/main.c Fri Mar 7 01:34:19 2014
@@ -177,57 +177,6 @@ walk(transit_t *t, FILE *fp, int recursi
return 1;
}
-/* output JSON */
-static int
-output_json(transit_t *t, uint64_t a, FILE *fp)
-{
- transit_atom_t *atom;
- uint64_t *v;
- unsigned i;
-
- atom = transit_atom(t, a);
- switch(transit_atom_type(atom)) {
- case TRANSIT_NUMBER:
- fprintf(fp, "%" PRIu64, transit_atom_size(atom));
- return 1;
- case TRANSIT_STRING:
- fputc('"', fp);
- fwrite(transit_atom_ptr(atom), 1, transit_atom_size(atom), fp);
- fputc('"', fp);
- return 1;
- case TRANSIT_LIST:
- fprintf(fp, "[");
- v = transit_atom_ptr(atom);
- for (i = 0 ; i < transit_atom_size(atom) ; i++) {
- output_json(t, v[i], fp);
- if (i < transit_atom_size(atom) - 2) {
- fprintf(fp, ",");
- }
- }
- fprintf(fp, "]");
- return 1;
- case TRANSIT_DICT:
- fprintf(fp, "{");
- v = transit_atom_ptr(atom);
- for (i = 0 ; i < transit_atom_size(atom) - 1 ; i += 2) {
- output_json(t, v[i], fp);
- fprintf(fp, ":");
- output_json(t, v[i + 1], fp);
- if (i < transit_atom_size(atom) - 3) {
- fprintf(fp, ",");
- }
- }
- fprintf(fp, "}");
- return 1;
- case TRANSIT_END:
- return 1;
- default:
- fprintf(stderr, "unrecognised atom: %d\n", transit_atom_type(atom));
- return t->c;
- }
- fprintf(fp, "\n");
-}
-
int
main(int argc, char **argv)
@@ -235,6 +184,7 @@ main(int argc, char **argv)
transit_t t;
size_t size;
char *in;
+ char buf[8192];
int recursive;
int decoding;
int json;
@@ -276,8 +226,9 @@ main(int argc, char **argv)
}
}
if (json) {
- output_json(&t, 0, stdout);
- fprintf(stdout, "\n");
+ cc = transit_format_json(&t, buf, sizeof(buf));
+ fwrite(buf, 1, cc, stdout);
+ printf("\n");
} else {
walk(&t, stdout, recursive);
}
Index: othersrc/external/bsd/transit/dist/transit.c
diff -u othersrc/external/bsd/transit/dist/transit.c:1.4 othersrc/external/bsd/transit/dist/transit.c:1.5
--- othersrc/external/bsd/transit/dist/transit.c:1.4 Wed Mar 5 04:58:51 2014
+++ othersrc/external/bsd/transit/dist/transit.c Fri Mar 7 01:34:19 2014
@@ -24,6 +24,7 @@
*/
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/param.h>
#include <inttypes.h>
#include <stdio.h>
@@ -188,6 +189,60 @@ decode_atoms(transit_t *trans, const cha
return cc;
}
+/* output JSON */
+static int
+format_json(transit_t *t, uint64_t a, char *buf, size_t size)
+{
+ transit_atom_t *atom;
+ uint64_t *v;
+ unsigned i;
+ size_t n;
+ int cc;
+
+ atom = transit_atom(t, a);
+ cc = 0;
+ switch(atom->type) {
+ case TRANSIT_NUMBER:
+ return snprintf(buf, size, "%" PRIu64, atom->n);
+ case TRANSIT_STRING:
+ buf[cc++] = '"';
+ n = MIN(atom->n, size - 1 - 1);
+ memcpy(&buf[cc], atom->v, n);
+ cc += n;
+ buf[cc++] = '"';
+ return cc;
+ case TRANSIT_LIST:
+ buf[cc++] = '[';
+ v = atom->v;
+ for (i = 0 ; i < atom->n ; i++) {
+ cc += format_json(t, v[i], &buf[cc], size - cc);
+ if (i < atom->n - 2) {
+ buf[cc++] = ',';
+ }
+ }
+ buf[cc++] = ']';
+ return cc;
+ case TRANSIT_DICT:
+ buf[cc++] = '{';
+ v = atom->v;
+ for (i = 0 ; i < atom->n - 1 ; i += 2) {
+ cc += format_json(t, v[i], &buf[cc], size - cc);
+ buf[cc++] = ':';
+ cc += format_json(t, v[i + 1], &buf[cc], size - cc);
+ if (i < atom->n - 3) {
+ buf[cc++] = ',';
+ }
+ }
+ buf[cc++] = '}';
+ return cc;
+ case TRANSIT_END:
+ return 0;
+ default:
+ fprintf(stderr, "unrecognised atom: %d\n", transit_atom_type(atom));
+ return 0;
+ }
+}
+
/*******************************************/
/* encode a number */
@@ -343,6 +398,16 @@ transit_read_json(transit_t *t, const ch
return (int)jsize;
}
+/* format as JSON */
+int
+transit_format_json(transit_t *t, char *buf, size_t size)
+{
+ if (t && buf) {
+ return format_json(t, 0, buf, size);
+ }
+ return 0;
+}
+
/* create a new structure */
transit_t *
transit_new(void)
Index: othersrc/external/bsd/transit/dist/transit.h
diff -u othersrc/external/bsd/transit/dist/transit.h:1.4 othersrc/external/bsd/transit/dist/transit.h:1.5
--- othersrc/external/bsd/transit/dist/transit.h:1.4 Wed Mar 5 04:58:51 2014
+++ othersrc/external/bsd/transit/dist/transit.h Fri Mar 7 01:34:19 2014
@@ -23,7 +23,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TRANSIT_H_
-#define TRANSIT_H_ 20140304
+#define TRANSIT_H_ 20140306
#include <sys/types.h>
@@ -69,7 +69,10 @@ int transit_encode_string(transit_t */*t
int transit_encode_special(transit_t */*trans*/, uint8_t /*type*/);
int transit_decode(transit_t */*trans*/, const char */*in*/, size_t /*insize*/);
+
+/* JSON input and output */
int transit_read_json(transit_t */*t*/, const char */*json*/, size_t /*jsize*/);
+int transit_format_json(transit_t */*t*/, char */*buf*/, size_t /*size*/);
transit_t *transit_new(void);
void transit_free(transit_t */*t*/);
Added files:
Index: othersrc/external/bsd/transit/bin/17.expected
diff -u /dev/null othersrc/external/bsd/transit/bin/17.expected:1.1
--- /dev/null Fri Mar 7 01:34:19 2014
+++ othersrc/external/bsd/transit/bin/17.expected Fri Mar 7 01:34:19 2014
@@ -0,0 +1 @@
+[1,2,3,4,[5,6,7,[8,[9,[0,10]]]]]