Mercurial > libjeffpc > bad-patches
view 2020-01-08-3-WIP__nvlist__sexpr_packing.patch @ 1:2df0a7f85837 default tip
today's batch
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Wed, 08 Jan 2020 15:37:43 -0500 |
parents | |
children |
line wrap: on
line source
# HG changeset patch # User Josef 'Jeff' Sipek <jeffpc@josefsipek.net> # Date 1504011466 -10800 # Tue Aug 29 15:57:46 2017 +0300 # Node ID fece0dcaf90f8cb0580eb0f084eaf08921f86dac # Parent dc73a1af970bb6b4846a2c68659ee0c866f73d99 # EXP-Topic nvlist-sexpr-packing WIP: nvlist: sexpr packing diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ add_library(jeffpc SHARED nvl_dump.c nvl_fmt_cbor.c nvl_fmt_json.c + nvl_fmt_sexpr.c nvl_pack.c nvl_unpack.c padding.c diff --git a/include/jeffpc/nvl.h b/include/jeffpc/nvl.h --- a/include/jeffpc/nvl.h +++ b/include/jeffpc/nvl.h @@ -49,6 +49,7 @@ enum nvtype { enum nvformat { NVF_CBOR, /* RFC 7049 */ NVF_JSON, /* RFC 7159 */ + NVF_SEXPR, }; /* do not access these directly */ diff --git a/nvl_fmt_sexpr.c b/nvl_fmt_sexpr.c new file mode 100644 --- /dev/null +++ b/nvl_fmt_sexpr.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include <jeffpc/nvl.h> + +#include "nvl_impl.h" + +/* + * nvl packing interface + */ + +static int sexpr_nvl_prologue(struct buffer *buffer, struct nvlist *nvl) +{ + return buffer_append_c(buffer, '('); +} + +static int sexpr_nvl_epilogue(struct buffer *buffer, struct nvlist *nvl) +{ + return buffer_append_c(buffer, ')'); +} + +static int sexpr_array_prologue(struct buffer *buffer, const struct nvval *vals, + size_t nelem) +{ + return buffer_append_c(buffer, '('); +} + +static int sexpr_array_epilogue(struct buffer *buffer, const struct nvval *vals, + size_t nelem) +{ + return buffer_append_c(buffer, ')'); +} + +static int sexpr_val_bool(struct buffer *buffer, bool b) +{ + return buffer_append_cstr(buffer, b ? "#t" : "#f"); +} + +static int sexpr_val_int(struct buffer *buffer, uint64_t i) +{ + return -ENOTSUP; // TODO: we need to support ints +} + +static int sexpr_val_null(struct buffer *buffer) +{ + return buffer_append_cstr(buffer, "#n"); +} + +static int sexpr_val_str(struct buffer *buffer, const char *str) +{ + return -ENOTSUP; // TODO: we need to support strings +} + +const struct nvops nvops_sexpr = { + .pack = { + .nvl_prologue = sexpr_nvl_prologue, + .nvl_epilogue = sexpr_nvl_epilogue, + + .array_prologue = sexpr_array_prologue, + .array_epilogue = sexpr_array_epilogue, + + .val_bool = sexpr_val_bool, + .val_int = sexpr_val_int, + .val_null = sexpr_val_null, + .val_str = sexpr_val_str, + } +}; diff --git a/nvl_impl.h b/nvl_impl.h --- a/nvl_impl.h +++ b/nvl_impl.h @@ -117,6 +117,7 @@ struct nvops { extern const struct nvops nvops_cbor; extern const struct nvops nvops_json; +extern const struct nvops nvops_sexpr; static inline const struct nvops *select_ops(enum nvformat format) { @@ -125,6 +126,8 @@ static inline const struct nvops *select return &nvops_cbor; case NVF_JSON: return &nvops_json; + case NVF_SEXPR: + return &nvops_sexpr; } return NULL;