From 011971920d05d5014b4c5a1f23eb54e7d228d85e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 12 May 2018 15:25:47 +0200 Subject: [PATCH] io: several fixes --- lib/formats/json.c | 17 +++++++++++------ lib/formats/json_reserve.c | 17 +++++++++++------ lib/formats/raw.c | 4 ++-- lib/formats/villas.pb-c.c | 2 +- lib/formats/villas.pb-c.h | 8 ++++---- lib/formats/villas_binary.c | 4 ++-- tests/unit/io.c | 10 +++++++--- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/lib/formats/json.c b/lib/formats/json.c index bffe67c11..639f6a968 100644 --- a/lib/formats/json.c +++ b/lib/formats/json.c @@ -23,6 +23,7 @@ #include #include #include +#include #include static json_t * json_pack_timestamps(struct sample *smp) @@ -244,13 +245,15 @@ int json_sscan(char *buf, size_t len, size_t *rbytes, struct sample *smps[], uns return ret; } -int json_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags) +int json_print(struct io *io, struct sample *smps[], unsigned cnt) { int ret, i; json_t *json; + FILE *f = io_stream_output(io); + for (i = 0; i < cnt; i++) { - ret = json_pack_sample(&json, smps[i], flags); + ret = json_pack_sample(&json, smps[i], io->flags); if (ret) return ret; @@ -263,18 +266,20 @@ int json_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags) return i; } -int json_fscan(FILE *f, struct sample *smps[], unsigned cnt, int flags) +int json_scan(struct io *io, struct sample *smps[], unsigned cnt) { int i, ret; json_t *json; json_error_t err; + FILE *f = io_stream_input(io); + for (i = 0; i < cnt; i++) { skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err); if (!json) break; - ret = json_unpack_sample(json, smps[i], flags); + ret = json_unpack_sample(json, smps[i], io->flags); if (ret) goto skip; @@ -289,8 +294,8 @@ static struct plugin p = { .description = "Javascript Object Notation", .type = PLUGIN_TYPE_FORMAT, .io = { - .fscan = json_fscan, - .fprint = json_fprint, + .scan = json_scan, + .print = json_print, .sscan = json_sscan, .sprint = json_sprint, .size = 0 diff --git a/lib/formats/json_reserve.c b/lib/formats/json_reserve.c index 31e31705e..b77c09cdd 100644 --- a/lib/formats/json_reserve.c +++ b/lib/formats/json_reserve.c @@ -26,6 +26,7 @@ #include #include #include +#include #include int json_reserve_pack_sample(json_t **j, struct sample *smp, int flags) @@ -208,13 +209,15 @@ int json_reserve_sscan(char *buf, size_t len, size_t *rbytes, struct sample *smp return ret; } -int json_reserve_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags) +int json_reserve_print(struct io *io, struct sample *smps[], unsigned cnt) { int ret, i; json_t *json; + FILE *f = io_stream_output(io); + for (i = 0; i < cnt; i++) { - ret = json_reserve_pack_sample(&json, smps[i], flags); + ret = json_reserve_pack_sample(&json, smps[i], io->flags); if (ret) return ret; @@ -227,18 +230,20 @@ int json_reserve_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags) return i; } -int json_reserve_fscan(FILE *f, struct sample *smps[], unsigned cnt, int flags) +int json_reserve_scan(struct io *io, struct sample *smps[], unsigned cnt) { int i, ret; json_t *json; json_error_t err; + FILE *f = io_stream_input(io); + for (i = 0; i < cnt; i++) { skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err); if (!json) break; - ret = json_reserve_unpack_sample(json, smps[i], flags); + ret = json_reserve_unpack_sample(json, smps[i], io->flags); if (ret) goto skip; @@ -253,8 +258,8 @@ static struct plugin p = { .description = "RESERVE JSON format", .type = PLUGIN_TYPE_FORMAT, .io = { - .fscan = json_reserve_fscan, - .fprint = json_reserve_fprint, + .scan = json_reserve_scan, + .print = json_reserve_print, .sscan = json_reserve_sscan, .sprint = json_reserve_sprint, .size = 0 diff --git a/lib/formats/raw.c b/lib/formats/raw.c index bfb8c3f57..270a9c6e7 100644 --- a/lib/formats/raw.c +++ b/lib/formats/raw.c @@ -242,9 +242,9 @@ int raw_sscan(char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsi static struct plugin i = { \ .name = n, \ .description = d, \ - .type = PLUGIN_TYPE_FORMAT, \ + .type = PLUGIN_TYPE_FORMAT, \ .io = { \ - .flags = f | format_type_BINARY, \ + .flags = f | FORMAT_TYPE_BINARY,\ .sprint = raw_sprint, \ .sscan = raw_sscan \ } \ diff --git a/lib/formats/villas.pb-c.c b/lib/formats/villas.pb-c.c index b8ddd5ec2..500bafdd2 100644 --- a/lib/formats/villas.pb-c.c +++ b/lib/formats/villas.pb-c.c @@ -1,5 +1,5 @@ /* Generated by the protocol buffer compiler. DO NOT EDIT! */ -/* Generated from: lib/io/villas.proto */ +/* Generated from: lib/formats/villas.proto */ /* Do not generate deprecated warnings for self */ #ifndef PROTOBUF_C__NO_DEPRECATED diff --git a/lib/formats/villas.pb-c.h b/lib/formats/villas.pb-c.h index 18c048be2..471cce2a0 100644 --- a/lib/formats/villas.pb-c.h +++ b/lib/formats/villas.pb-c.h @@ -1,8 +1,8 @@ /* Generated by the protocol buffer compiler. DO NOT EDIT! */ -/* Generated from: lib/io/villas.proto */ +/* Generated from: lib/formats/villas.proto */ -#ifndef PROTOBUF_C_lib_2fio_2fvillas_2eproto__INCLUDED -#define PROTOBUF_C_lib_2fio_2fvillas_2eproto__INCLUDED +#ifndef PROTOBUF_C_lib_2fformats_2fvillas_2eproto__INCLUDED +#define PROTOBUF_C_lib_2fformats_2fvillas_2eproto__INCLUDED #include @@ -218,4 +218,4 @@ extern const ProtobufCMessageDescriptor villas__node__value__descriptor; PROTOBUF_C__END_DECLS -#endif /* PROTOBUF_C_lib_2fio_2fvillas_2eproto__INCLUDED */ +#endif /* PROTOBUF_C_lib_2fformats_2fvillas_2eproto__INCLUDED */ diff --git a/lib/formats/villas_binary.c b/lib/formats/villas_binary.c index c4646e698..3b6690d29 100644 --- a/lib/formats/villas_binary.c +++ b/lib/formats/villas_binary.c @@ -120,7 +120,7 @@ static struct plugin p1 = { .sprint = villas_binary_sprint, .sscan = villas_binary_sscan, .size = 0, - .flags = format_type_BINARY + .flags = FORMAT_TYPE_BINARY }, }; @@ -133,7 +133,7 @@ static struct plugin p2 = { .sprint = villas_binary_sprint, .sscan = villas_binary_sscan, .size = 0, - .flags = format_type_BINARY | VILLAS_BINARY_WEB + .flags = FORMAT_TYPE_BINARY | VILLAS_BINARY_WEB }, }; diff --git a/tests/unit/io.c b/tests/unit/io.c index e8217028b..2d4e223af 100644 --- a/tests/unit/io.c +++ b/tests/unit/io.c @@ -183,6 +183,7 @@ ParameterizedTest(char *fmt, io, lowlevel) struct pool p = { .state = STATE_DESTROYED }; struct sample *smps[NUM_SAMPLES]; struct sample *smpt[NUM_SAMPLES]; + struct io io; ret = pool_init(&p, 2 * NUM_SAMPLES, SAMPLE_LEN(NUM_VALUES), &memtype_hugepage); cr_assert_eq(ret, 0); @@ -194,10 +195,13 @@ ParameterizedTest(char *fmt, io, lowlevel) f = format_type_lookup(fmt); cr_assert_not_null(f, "Format '%s' does not exist", fmt); - ret = format_type_sprint(f, buf, sizeof(buf), &wbytes, smps, NUM_SAMPLES, SAMPLE_HAS_ALL); + ret = io_init(&io, f, NULL, SAMPLE_HAS_ALL); + cr_assert_eq(ret, 0); + + ret = io_sprint(&io, buf, sizeof(buf), &wbytes, smps, NUM_SAMPLES); cr_assert_eq(ret, NUM_SAMPLES); - ret = format_type_sscan(f, buf, wbytes, &rbytes, smpt, NUM_SAMPLES, 0); + ret = io_sscan(&io, buf, wbytes, &rbytes, smpt, NUM_SAMPLES); cr_assert_eq(rbytes, wbytes); cr_assert_eq_samples(f, smps, smpt, ret); @@ -247,7 +251,7 @@ ParameterizedTest(char *fmt, io, highlevel) f = format_type_lookup(fmt); cr_assert_not_null(f, "Format '%s' does not exist", fmt); - ret = io_init(&io, f, SAMPLE_HAS_ALL); + ret = io_init(&io, f, NULL, SAMPLE_HAS_ALL); cr_assert_eq(ret, 0); ret = io_open(&io, fn);