Date: Mon, 2 Jun 2014 14:21:10 GMT From: zkorchev@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r268957 - soc2014/zkorchev/freebsd_head/lib/libsol Message-ID: <201406021421.s52ELAXU063491@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: zkorchev Date: Mon Jun 2 14:21:10 2014 New Revision: 268957 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=268957 Log: Added libsol as an independent library Added: soc2014/zkorchev/freebsd_head/lib/libsol/ soc2014/zkorchev/freebsd_head/lib/libsol/Makefile soc2014/zkorchev/freebsd_head/lib/libsol/sol.c soc2014/zkorchev/freebsd_head/lib/libsol/sol.h Added: soc2014/zkorchev/freebsd_head/lib/libsol/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2014/zkorchev/freebsd_head/lib/libsol/Makefile Mon Jun 2 14:21:10 2014 (r268957) @@ -0,0 +1,16 @@ +# @(#)Makefile 8.1 (Berkeley) 6/6/93 +# $FreeBSD: soc2014/zkorchev/freebsd_head/usr.bin/vmstat/Makefile 268778 2014-05-28 17:03:05Z zkorchev $ + +LIB= sol +SHLIBDIR?= /lib +SHLIB_MAJOR= 0 +SRCS= sol.c +INCS= sol.h + +#MAN= vmstat.8 +LDFLAGS+= -L/usr/local/lib -lyajl +CFLAGS+= -I/usr/local/include + +WARNS?= 1 + +.include <bsd.lib.mk> Added: soc2014/zkorchev/freebsd_head/lib/libsol/sol.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2014/zkorchev/freebsd_head/lib/libsol/sol.c Mon Jun 2 14:21:10 2014 (r268957) @@ -0,0 +1,130 @@ +/*- + * Copyright (c) 2014 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <unistd.h> + +#include "sol.h" + +// TODO error checks + +// TODO track whether a key or a value is expected and generate error when appropriate? + +int sol_init(struct sol_stream *restrict stream, enum sol_format format) +{ + stream->f = format; + stream->ctx.g = yajl_gen_alloc(0); + + //sol.f = format; + //sol.ctx.g = yajl_gen_alloc(0); + + return 0; +} + +void sol_term(struct sol_stream *restrict stream) +{ + const char *buffer; + size_t length; + + switch (stream->f) + { + case SOL_JSON: + yajl_gen_get_buf(stream->ctx.g, (const unsigned char **)&buffer, &length); + write(1, buffer, length); + write(1, "\n", 1); // TODO change this + yajl_gen_clear(stream->ctx.g); + break; + } + + yajl_gen_free(stream->ctx.g); + + /*switch (sol.f) + { + case SOL_JSON: + yajl_gen_get_buf(sol.ctx.g, (const unsigned char **)&buffer, &length); + write(1, buffer, length); + write(1, "\n", 1); // TODO change this + yajl_gen_clear(sol.ctx.g); + break; + } + + yajl_gen_free(sol.ctx.g);*/ +} + +int sol_array_start(struct sol_stream *restrict stream) +{ + yajl_gen_array_open(stream->ctx.g); + + return 0; +} + +int sol_array_end(struct sol_stream *restrict stream) +{ + yajl_gen_array_close(stream->ctx.g); + + return 0; +} + +int sol_map_start(struct sol_stream *restrict stream) +{ + yajl_gen_map_open(stream->ctx.g); + + return 0; +} + +int sol_map_end(struct sol_stream *restrict stream) +{ + yajl_gen_map_close(stream->ctx.g); + + return 0; +} + +int sol_map_key(struct sol_stream *restrict stream, const char *key, size_t length) +{ + yajl_gen_string(stream->ctx.g, (const unsigned char *)key, length); + + return 0; +} + +int sol_string(struct sol_stream *restrict stream, const char *data, size_t length) +{ + yajl_gen_string(stream->ctx.g, (const unsigned char *)data, length); + + return 0; +} + +int sol_integer(struct sol_stream *restrict stream, long long value) +{ + yajl_gen_integer(stream->ctx.g, value); + + return 0; +} + +int sol_float(struct sol_stream *restrict stream, double value) +{ + // todo round mantissa? + yajl_gen_double(stream->ctx.g, value); + + return 0; +} Added: soc2014/zkorchev/freebsd_head/lib/libsol/sol.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2014/zkorchev/freebsd_head/lib/libsol/sol.h Mon Jun 2 14:21:10 2014 (r268957) @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2014 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <yajl/yajl_gen.h> // libyajl + +enum sol_format +{ + SOL_JSON = 1, +}; + +struct sol_stream +{ + enum sol_format f; + union + { + yajl_gen g; + } ctx; +}; + +int sol_init(struct sol_stream *restrict stream, enum sol_format format); +void sol_term(struct sol_stream *restrict stream); + +int sol_array_start(struct sol_stream *restrict stream); +int sol_array_end(struct sol_stream *restrict stream); + +int sol_map_start(struct sol_stream *restrict stream); +int sol_map_end(struct sol_stream *restrict stream); +int sol_map_key(struct sol_stream *restrict stream, const char *key, size_t length); + +int sol_integer(struct sol_stream *restrict stream, long long value); +int sol_string(struct sol_stream *restrict stream, const char *data, size_t length); +int sol_float(struct sol_stream *restrict stream, double value);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201406021421.s52ELAXU063491>