From d77879fa7d4bff755bf898c47052387e1a922909 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 9 Aug 2018 08:39:27 +0200 Subject: [PATCH] io: refactor input/output -> in/out --- include/villas/io.h | 4 +- lib/formats/json_reserve.c | 24 +++++------ lib/io.c | 88 +++++++++++++++++++------------------- lib/nodes/file.c | 2 +- tests/unit/io.c | 2 +- 5 files changed, 60 insertions(+), 60 deletions(-) diff --git a/include/villas/io.h b/include/villas/io.h index c9e8a07a6..9aca8fa92 100644 --- a/include/villas/io.h +++ b/include/villas/io.h @@ -47,7 +47,7 @@ struct io { char delimiter; /**< Newline delimiter. */ char separator; /**< Column separator (used by csv and villas.human formats only) */ - struct { + struct io_direction { /** A format type can use this file handle or overwrite the * format::{open,close,eof,rewind} functions and the private * data in io::_vd. @@ -62,7 +62,7 @@ struct io { struct list *signals; struct node *node; - } input, output; + } in, out; enum { IO_MODE_STDIO, diff --git a/lib/formats/json_reserve.c b/lib/formats/json_reserve.c index b484e9dd5..57dc9a31f 100644 --- a/lib/formats/json_reserve.c +++ b/lib/formats/json_reserve.c @@ -49,8 +49,8 @@ static int json_reserve_pack_sample(struct io *io, json_t **j, struct sample *sm json_data = json_array(); for (int i = 0; i < smp->length; i++) { - if (io->output.signals) - sig = (struct signal *) list_at_safe(io->output.signals, i); + if (io->out.signals) + sig = (struct signal *) list_at_safe(io->out.signals, i); else sig = NULL; @@ -98,9 +98,9 @@ static int json_reserve_pack_sample(struct io *io, json_t **j, struct sample *sm return -1; #ifdef JSON_RESERVE_INTEGER_TARGET - if (io->output.node) { + if (io->out.node) { char *endptr; - char *id_str = strrchr(io->output.node->name, '_'); + char *id_str = strrchr(io->out.node->name, '_'); if (!id_str) return -1; @@ -111,8 +111,8 @@ static int json_reserve_pack_sample(struct io *io, json_t **j, struct sample *sm json_object_set_new(*j, "target", json_integer(id)); } #else - if (io->output.node) - json_object_set_new(*j, "target", json_string(io->output.node->name)); + if (io->out.node) + json_object_set_new(*j, "target", json_string(io->out.node->name)); #endif return 0; @@ -137,12 +137,12 @@ static int json_reserve_unpack_sample(struct io *io, json_t *json_smp, struct sa return -1; #ifdef JSON_RESERVE_INTEGER_TARGET - if (json_target && io->input.node) { + if (json_target && io->in.node) { if (!json_is_integer(json_target)) return -1; char *endptr; - char *id_str = strrchr(io->input.node->name, '_'); + char *id_str = strrchr(io->in.node->name, '_'); if (!id_str) return -1; @@ -154,12 +154,12 @@ static int json_reserve_unpack_sample(struct io *io, json_t *json_smp, struct sa return 0; } #else - if (json_target && io->input.node) { + if (json_target && io->in.node) { const char *target = json_string_value(json_target); if (!target) return -1; - if (strcmp(target, io->input.node->name)) + if (strcmp(target, io->in.node->name)) return 0; } #endif @@ -185,12 +185,12 @@ static int json_reserve_unpack_sample(struct io *io, json_t *json_smp, struct sa struct signal *sig; - sig = (struct signal *) list_lookup(io->input.signals, name); + sig = (struct signal *) list_lookup(io->in.signals, name); if (sig) { if (!sig->enabled) continue; - idx = list_index(io->input.signals, sig); + idx = list_index(io->in.signals, sig); } else { ret = sscanf(name, "signal_%d", &idx); diff --git a/lib/io.c b/lib/io.c index 689a46e6d..74a1f9661 100644 --- a/lib/io.c +++ b/lib/io.c @@ -40,11 +40,11 @@ static int io_print_lines(struct io *io, struct sample *smps[], unsigned cnt) for (i = 0; i < cnt; i++) { size_t wbytes; - ret = io_sprint(io, io->output.buffer, io->output.buflen, &wbytes, &smps[i], 1); + ret = io_sprint(io, io->out.buffer, io->out.buflen, &wbytes, &smps[i], 1); if (ret < 0) return ret; - fwrite(io->output.buffer, wbytes, 1, f); + fwrite(io->out.buffer, wbytes, 1, f); } return i; @@ -61,17 +61,17 @@ static int io_scan_lines(struct io *io, struct sample *smps[], unsigned cnt) ssize_t bytes; char *ptr; -skip: bytes = getdelim(&io->input.buffer, &io->input.buflen, io->delimiter, f); +skip: bytes = getdelim(&io->in.buffer, &io->in.buflen, io->delimiter, f); if (bytes < 0) return -1; /* An error or eof occured */ /* Skip whitespaces, empty and comment lines */ - for (ptr = io->input.buffer; isspace(*ptr); ptr++); + for (ptr = io->in.buffer; isspace(*ptr); ptr++); if (ptr[0] == '\0' || ptr[0] == '#') goto skip; - ret = io_sscan(io, io->input.buffer, bytes, &rbytes, &smps[i], 1); + ret = io_sscan(io, io->in.buffer, bytes, &rbytes, &smps[i], 1); if (ret < 0) return ret; } @@ -92,11 +92,11 @@ int io_init(struct io *io, struct format_type *fmt, struct node *n, int flags) io->delimiter = io->_vt->delimiter ? io->_vt->delimiter : '\n'; io->separator = io->_vt->separator ? io->_vt->separator : '\t'; - io->input.buflen = - io->output.buflen = 4096; + io->in.buflen = + io->out.buflen = 4096; - io->input.buffer = alloc(io->input.buflen); - io->output.buffer = alloc(io->output.buflen); + io->in.buffer = alloc(io->in.buflen); + io->out.buffer = alloc(io->out.buflen); io->input.node = n; io->output.node = n; @@ -130,8 +130,8 @@ int io_destroy(struct io *io) return ret; free(io->_vd); - free(io->input.buffer); - free(io->output.buffer); + free(io->in.buffer); + free(io->out.buffer); io->state = STATE_DESTROYED; @@ -149,23 +149,23 @@ int io_stream_open(struct io *io, const char *uri) else if (aislocal(uri) == 1) { io->mode = IO_MODE_STDIO; - io->output.stream.std = fopen(uri, "a+"); - if (io->output.stream.std == NULL) + io->out.stream.std = fopen(uri, "a+"); + if (io->out.stream.std == NULL) return -1; - io->input.stream.std = fopen(uri, "r"); - if (io->input.stream.std == NULL) + io->in.stream.std = fopen(uri, "r"); + if (io->in.stream.std == NULL) return -1; } else { io->mode = IO_MODE_ADVIO; - io->output.stream.adv = afopen(uri, "a+"); - if (io->output.stream.adv == NULL) + io->out.stream.adv = afopen(uri, "a+"); + if (io->out.stream.adv == NULL) return -1; - io->input.stream.adv = afopen(uri, "a+"); - if (io->input.stream.adv == NULL) + io->in.stream.adv = afopen(uri, "a+"); + if (io->in.stream.adv == NULL) return -2; } } @@ -173,8 +173,8 @@ int io_stream_open(struct io *io, const char *uri) stdio: io->mode = IO_MODE_STDIO; io->flags |= IO_FLUSH; - io->input.stream.std = stdin; - io->output.stream.std = stdout; + io->in.stream.std = stdin; + io->out.stream.std = stdout; } /* Make stream non-blocking if desired */ @@ -198,11 +198,11 @@ stdio: io->mode = IO_MODE_STDIO; /* Enable line buffering on stdio */ if (io->mode == IO_MODE_STDIO) { - ret = setvbuf(io->input.stream.std, NULL, _IOLBF, BUFSIZ); + ret = setvbuf(io->in.stream.std, NULL, _IOLBF, BUFSIZ); if (ret) return -1; - ret = setvbuf(io->output.stream.std, NULL, _IOLBF, BUFSIZ); + ret = setvbuf(io->out.stream.std, NULL, _IOLBF, BUFSIZ); if (ret) return -1; } @@ -216,25 +216,25 @@ int io_stream_close(struct io *io) switch (io->mode) { case IO_MODE_ADVIO: - ret = afclose(io->input.stream.adv); + ret = afclose(io->in.stream.adv); if (ret) return ret; - ret = afclose(io->output.stream.adv); + ret = afclose(io->out.stream.adv); if (ret) return ret; return 0; case IO_MODE_STDIO: - if (io->input.stream.std == stdin) + if (io->in.stream.std == stdin) return 0; - ret = fclose(io->input.stream.std); + ret = fclose(io->in.stream.std); if (ret) return ret; - ret = fclose(io->output.stream.std); + ret = fclose(io->out.stream.std); if (ret) return ret; @@ -251,9 +251,9 @@ int io_stream_flush(struct io *io) { switch (io->mode) { case IO_MODE_ADVIO: - return afflush(io->output.stream.adv); + return afflush(io->out.stream.adv); case IO_MODE_STDIO: - return fflush(io->output.stream.std); + return fflush(io->out.stream.std); case IO_MODE_CUSTOM: return 0; } @@ -265,9 +265,9 @@ int io_stream_eof(struct io *io) { switch (io->mode) { case IO_MODE_ADVIO: - return afeof(io->input.stream.adv); + return afeof(io->in.stream.adv); case IO_MODE_STDIO: - return feof(io->input.stream.std); + return feof(io->in.stream.std); case IO_MODE_CUSTOM: return 0; } @@ -279,10 +279,10 @@ void io_stream_rewind(struct io *io) { switch (io->mode) { case IO_MODE_ADVIO: - arewind(io->input.stream.adv); + arewind(io->in.stream.adv); break; case IO_MODE_STDIO: - rewind(io->input.stream.std); + rewind(io->in.stream.std); break; case IO_MODE_CUSTOM: { } } @@ -292,9 +292,9 @@ int io_stream_fd(struct io *io) { switch (io->mode) { case IO_MODE_ADVIO: - return afileno(io->input.stream.adv); + return afileno(io->in.stream.adv); case IO_MODE_STDIO: - return fileno(io->input.stream.std); + return fileno(io->in.stream.std); case IO_MODE_CUSTOM: return -1; } @@ -407,9 +407,9 @@ int io_print(struct io *io, struct sample *smps[], unsigned cnt) FILE *f = io_stream_output(io); size_t wbytes; - ret = io_sprint(io, io->output.buffer, io->output.buflen, &wbytes, smps, cnt); + ret = io_sprint(io, io->out.buffer, io->out.buflen, &wbytes, smps, cnt); - fwrite(io->output.buffer, wbytes, 1, f); + fwrite(io->out.buffer, wbytes, 1, f); } else ret = -1; @@ -434,9 +434,9 @@ int io_scan(struct io *io, struct sample *smps[], unsigned cnt) FILE *f = io_stream_input(io); size_t bytes, rbytes; - bytes = fread(io->input.buffer, 1, io->input.buflen, f); + bytes = fread(io->in.buffer, 1, io->in.buflen, f); - ret = io_sscan(io, io->input.buffer, bytes, &rbytes, smps, cnt); + ret = io_sscan(io, io->in.buffer, bytes, &rbytes, smps, cnt); } else ret = -1; @@ -449,8 +449,8 @@ FILE * io_stream_output(struct io *io) { return 0; return io->mode == IO_MODE_ADVIO - ? io->output.stream.adv->file - : io->output.stream.std; + ? io->out.stream.adv->file + : io->out.stream.std; } FILE * io_stream_input(struct io *io) { @@ -458,8 +458,8 @@ FILE * io_stream_input(struct io *io) { return 0; return io->mode == IO_MODE_ADVIO - ? io->input.stream.adv->file - : io->input.stream.std; + ? io->in.stream.adv->file + : io->in.stream.std; } int io_sscan(struct io *io, char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt) diff --git a/lib/nodes/file.c b/lib/nodes/file.c index d45e9106b..0ecde7ffc 100644 --- a/lib/nodes/file.c +++ b/lib/nodes/file.c @@ -313,7 +313,7 @@ retry: ret = io_scan(&f->io, smps, cnt); /* Try to download more data if this is a remote file. */ if (f->io.mode == IO_MODE_ADVIO) - adownload(f->io.input.stream.adv, 1); + adownload(f->io.in.stream.adv, 1); goto retry; diff --git a/tests/unit/io.c b/tests/unit/io.c index b10854dff..c4140d5a1 100644 --- a/tests/unit/io.c +++ b/tests/unit/io.c @@ -275,7 +275,7 @@ ParameterizedTest(char *fmt, io, highlevel) io_rewind(&io); if (io.mode == IO_MODE_ADVIO) - adownload(io.input.stream.adv, 0); + adownload(io.in.stream.adv, 0); cnt = io_scan(&io, smpt, NUM_SAMPLES); cr_assert_gt(cnt, 0, "Failed to read samples back: cnt=%d", cnt);