1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/io.c

525 lines
10 KiB
C
Raw Normal View History

2017-07-28 18:11:52 +02:00
/** Reading and writing simulation samples in various formats.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:39 +01:00
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
2017-07-28 18:11:52 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <stdlib.h>
2017-08-05 21:02:09 +02:00
#include <stdio.h>
#include <string.h>
2017-09-04 18:03:53 +02:00
#include <fcntl.h>
#include <ctype.h>
2017-07-28 18:11:52 +02:00
2017-12-09 02:19:28 +08:00
#include <villas/io.h>
2018-05-12 13:56:12 +02:00
#include <villas/format_type.h>
2017-12-09 02:19:28 +08:00
#include <villas/utils.h>
#include <villas/sample.h>
2017-07-28 18:11:52 +02:00
static int io_print_lines(struct io *io, struct sample *smps[], unsigned cnt)
{
int ret, i;
FILE *f = io_stream_output(io);
for (i = 0; i < cnt; i++) {
size_t wbytes;
2018-08-09 08:39:27 +02:00
ret = io_sprint(io, io->out.buffer, io->out.buflen, &wbytes, &smps[i], 1);
if (ret < 0)
return ret;
2018-08-09 08:39:27 +02:00
fwrite(io->out.buffer, wbytes, 1, f);
}
return i;
}
static int io_scan_lines(struct io *io, struct sample *smps[], unsigned cnt)
{
int ret, i;
FILE *f = io_stream_input(io);
for (i = 0; i < cnt; i++) {
size_t rbytes;
ssize_t bytes;
char *ptr;
2018-08-09 08:39:27 +02:00
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 */
2018-08-09 08:39:27 +02:00
for (ptr = io->in.buffer; isspace(*ptr); ptr++);
if (ptr[0] == '\0' || ptr[0] == '#')
goto skip;
2018-08-09 08:39:27 +02:00
ret = io_sscan(io, io->in.buffer, bytes, &rbytes, &smps[i], 1);
if (ret < 0)
return ret;
}
return i;
}
2019-01-07 10:28:55 +01:00
int io_init(struct io *io, const struct format_type *fmt, struct vlist *signals, int flags)
2017-07-28 18:11:52 +02:00
{
2018-05-12 18:02:37 +02:00
int ret;
assert(io->state == STATE_DESTROYED);
2017-07-28 18:11:52 +02:00
io->_vt = fmt;
io->_vd = alloc(fmt->size);
2018-08-20 18:31:27 +02:00
io->flags = flags | (io_type(io)->flags & ~SAMPLE_HAS_ALL);
io->delimiter = io_type(io)->delimiter ? io_type(io)->delimiter : '\n';
io->separator = io_type(io)->separator ? io_type(io)->separator : '\t';
2018-08-09 08:39:27 +02:00
io->in.buflen =
io->out.buflen = 4096;
2018-08-09 08:39:27 +02:00
io->in.buffer = alloc(io->in.buflen);
io->out.buffer = alloc(io->out.buflen);
2017-07-28 18:11:52 +02:00
2018-08-20 18:31:27 +02:00
io->signals = signals;
2018-05-12 15:25:29 +02:00
2018-08-20 18:31:27 +02:00
ret = io_type(io)->init ? io_type(io)->init(io) : 0;
2018-05-12 18:02:37 +02:00
if (ret)
return ret;
io->state = STATE_INITIALIZED;
return 0;
2017-07-28 18:11:52 +02:00
}
2018-08-20 18:31:27 +02:00
int io_init_auto(struct io *io, const struct format_type *fmt, int len, int flags)
{
int ret;
2019-01-07 10:28:55 +01:00
struct vlist *signals;
2018-08-20 18:31:27 +02:00
2019-01-07 10:28:55 +01:00
signals = alloc(sizeof(struct vlist));
2018-08-20 18:31:27 +02:00
signals->state = STATE_DESTROYED;
2019-01-07 10:28:55 +01:00
ret = vlist_init(signals);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
ret = signal_list_generate(signals, len, SIGNAL_TYPE_AUTO);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
flags |= IO_DESTROY_SIGNALS;
return io_init(io, fmt, signals, flags);
}
2017-07-28 18:11:52 +02:00
int io_destroy(struct io *io)
{
int ret;
2018-08-20 18:31:27 +02:00
assert(io->state == STATE_CLOSED || io->state == STATE_INITIALIZED || io->state == STATE_CHECKED);
2018-05-12 18:02:37 +02:00
2018-08-20 18:31:27 +02:00
ret = io_type(io)->destroy ? io_type(io)->destroy(io) : 0;
2017-07-28 18:11:52 +02:00
if (ret)
return ret;
free(io->_vd);
2018-08-09 08:39:27 +02:00
free(io->in.buffer);
free(io->out.buffer);
2017-07-28 18:11:52 +02:00
2018-08-20 18:31:27 +02:00
if (io->flags & IO_DESTROY_SIGNALS) {
2019-01-07 10:28:55 +01:00
ret = vlist_destroy(io->signals, (dtor_cb_t) signal_decref, false);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
}
2018-05-12 18:02:37 +02:00
io->state = STATE_DESTROYED;
2017-07-28 18:11:52 +02:00
return 0;
}
2018-08-20 18:31:27 +02:00
int io_check(struct io *io)
{
assert(io->state != STATE_DESTROYED);
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(io->signals); i++) {
struct signal *sig = (struct signal *) vlist_at(io->signals, i);
2018-08-20 18:31:27 +02:00
if (sig->type == SIGNAL_TYPE_AUTO) {
if (io_type(io)->flags & IO_AUTO_DETECT_FORMAT)
continue;
return -1;
}
}
io->state = STATE_CHECKED;
return 0;
}
int io_stream_open(struct io *io, const char *uri)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
int ret;
if (uri) {
if (!strcmp(uri, "-")) {
goto stdio;
}
else if (aislocal(uri) == 1) {
2017-08-05 21:02:09 +02:00
io->mode = IO_MODE_STDIO;
2018-08-09 08:39:27 +02:00
io->out.stream.std = fopen(uri, "a+");
if (io->out.stream.std == NULL)
2017-08-05 21:02:09 +02:00
return -1;
2018-08-09 08:39:27 +02:00
io->in.stream.std = fopen(uri, "r");
if (io->in.stream.std == NULL)
2017-08-05 21:02:09 +02:00
return -1;
}
else {
io->mode = IO_MODE_ADVIO;
2018-08-09 08:39:27 +02:00
io->out.stream.adv = afopen(uri, "a+");
if (io->out.stream.adv == NULL)
2017-08-05 21:02:09 +02:00
return -1;
2018-08-09 08:39:27 +02:00
io->in.stream.adv = afopen(uri, "a+");
if (io->in.stream.adv == NULL)
return -2;
2017-08-05 21:02:09 +02:00
}
}
2017-07-28 18:11:52 +02:00
else {
stdio: io->mode = IO_MODE_STDIO;
io->flags |= IO_FLUSH;
2017-08-05 21:02:09 +02:00
2018-08-09 08:39:27 +02:00
io->in.stream.std = stdin;
io->out.stream.std = stdout;
}
2017-09-04 18:03:53 +02:00
/* Make stream non-blocking if desired */
if (io->flags & IO_NONBLOCK) {
int ret, fd, flags;
fd = io_fd(io);
if (fd < 0)
return fd;
flags = fcntl(fd, F_GETFL);
if (flags < 0)
return flags;
flags |= O_NONBLOCK;
ret = fcntl(fd, F_SETFL, flags);
if (ret)
return ret;
}
/* Enable line buffering on stdio */
if (io->mode == IO_MODE_STDIO) {
2018-08-09 08:39:27 +02:00
ret = setvbuf(io->in.stream.std, NULL, _IOLBF, BUFSIZ);
2017-08-05 21:02:09 +02:00
if (ret)
2017-07-28 18:11:52 +02:00
return -1;
2018-08-09 08:39:27 +02:00
ret = setvbuf(io->out.stream.std, NULL, _IOLBF, BUFSIZ);
2017-08-05 21:02:09 +02:00
if (ret)
return -1;
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
return 0;
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
int io_stream_close(struct io *io)
2017-07-28 18:11:52 +02:00
{
int ret;
2017-08-05 21:02:09 +02:00
switch (io->mode) {
case IO_MODE_ADVIO:
2018-08-09 08:39:27 +02:00
ret = afclose(io->in.stream.adv);
if (ret)
return ret;
2018-08-09 08:39:27 +02:00
ret = afclose(io->out.stream.adv);
if (ret)
return ret;
return 0;
2017-08-05 21:02:09 +02:00
case IO_MODE_STDIO:
2018-08-09 08:39:27 +02:00
if (io->in.stream.std == stdin)
return 0;
2018-08-09 08:39:27 +02:00
ret = fclose(io->in.stream.std);
if (ret)
return ret;
2018-08-09 08:39:27 +02:00
ret = fclose(io->out.stream.std);
if (ret)
return ret;
return 0;
2017-08-05 21:02:09 +02:00
case IO_MODE_CUSTOM:
return 0;
}
return -1;
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
int io_stream_flush(struct io *io)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
switch (io->mode) {
case IO_MODE_ADVIO:
2018-08-09 08:39:27 +02:00
return afflush(io->out.stream.adv);
2017-08-05 21:02:09 +02:00
case IO_MODE_STDIO:
2018-08-09 08:39:27 +02:00
return fflush(io->out.stream.std);
2017-08-05 21:02:09 +02:00
case IO_MODE_CUSTOM:
return 0;
}
2017-07-28 18:11:52 +02:00
2017-08-05 21:02:09 +02:00
return -1;
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
int io_stream_eof(struct io *io)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
switch (io->mode) {
case IO_MODE_ADVIO:
2018-08-09 08:39:27 +02:00
return afeof(io->in.stream.adv);
2017-08-05 21:02:09 +02:00
case IO_MODE_STDIO:
2018-08-09 08:39:27 +02:00
return feof(io->in.stream.std);
2017-08-05 21:02:09 +02:00
case IO_MODE_CUSTOM:
return 0;
}
2017-07-28 18:11:52 +02:00
2017-08-05 21:02:09 +02:00
return -1;
}
2017-07-28 18:11:52 +02:00
2017-08-05 21:02:09 +02:00
void io_stream_rewind(struct io *io)
{
switch (io->mode) {
case IO_MODE_ADVIO:
2018-08-09 08:39:27 +02:00
arewind(io->in.stream.adv);
break;
2017-08-05 21:02:09 +02:00
case IO_MODE_STDIO:
2018-08-09 08:39:27 +02:00
rewind(io->in.stream.std);
break;
2017-08-05 21:02:09 +02:00
case IO_MODE_CUSTOM: { }
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
}
2017-07-28 18:11:52 +02:00
int io_stream_fd(struct io *io)
{
switch (io->mode) {
case IO_MODE_ADVIO:
2018-08-09 08:39:27 +02:00
return afileno(io->in.stream.adv);
case IO_MODE_STDIO:
2018-08-09 08:39:27 +02:00
return fileno(io->in.stream.std);
case IO_MODE_CUSTOM:
return -1;
}
return -1;
}
int io_open(struct io *io, const char *uri)
2017-08-05 21:02:09 +02:00
{
int ret;
2018-08-20 18:31:27 +02:00
assert(io->state == STATE_CHECKED);
2018-05-12 14:14:59 +02:00
2018-08-20 18:31:27 +02:00
ret = io_type(io)->open
? io_type(io)->open(io, uri)
: io_stream_open(io, uri);
if (ret)
return ret;
2018-08-20 18:31:27 +02:00
io->header_printed = false;
2018-05-12 14:14:59 +02:00
io->state = STATE_OPENED;
return 0;
2017-08-05 21:02:09 +02:00
}
int io_close(struct io *io)
{
int ret;
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
io_footer(io);
2018-08-20 18:31:27 +02:00
ret = io_type(io)->close
? io_type(io)->close(io)
2017-08-05 21:02:09 +02:00
: io_stream_close(io);
2018-05-12 14:14:59 +02:00
if (ret)
return ret;
2018-05-12 14:14:59 +02:00
io->state = STATE_CLOSED;
return 0;
2017-08-05 21:02:09 +02:00
}
int io_flush(struct io *io)
{
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
return io_type(io)->flush
? io_type(io)->flush(io)
2017-08-05 21:02:09 +02:00
: io_stream_flush(io);
2017-07-28 18:11:52 +02:00
}
int io_eof(struct io *io)
{
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
return io_type(io)->eof
? io_type(io)->eof(io)
2017-08-05 21:02:09 +02:00
: io_stream_eof(io);
2017-07-28 18:11:52 +02:00
}
void io_rewind(struct io *io)
{
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
if (io_type(io)->rewind)
io_type(io)->rewind(io);
else
io_stream_rewind(io);
2017-08-05 21:02:09 +02:00
}
int io_fd(struct io *io)
{
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
return io_type(io)->fd
? io_type(io)->fd(io)
: io_stream_fd(io);
}
2018-08-20 18:31:27 +02:00
const struct format_type * io_type(struct io *io)
{
return io->_vt;
}
void io_header(struct io *io, const struct sample *smp)
{
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
if (io_type(io)->header)
io_type(io)->header(io, smp);
io->header_printed = true;
}
void io_footer(struct io *io)
{
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
if (io_type(io)->footer)
io_type(io)->footer(io);
}
2017-08-22 12:20:55 +02:00
int io_print(struct io *io, struct sample *smps[], unsigned cnt)
2017-08-05 21:02:09 +02:00
{
int ret;
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-08-20 18:31:27 +02:00
if (!io->header_printed && cnt > 0)
io_header(io, smps[0]);
2018-05-12 15:25:29 +02:00
if (io->flags & IO_NEWLINES)
ret = io_print_lines(io, smps, cnt);
2018-08-20 18:31:27 +02:00
else if (io_type(io)->print)
ret = io_type(io)->print(io, smps, cnt);
else if (io_type(io)->sprint) {
FILE *f = io_stream_output(io);
2018-05-12 15:25:29 +02:00
size_t wbytes;
2017-08-05 21:02:09 +02:00
2018-08-09 08:39:27 +02:00
ret = io_sprint(io, io->out.buffer, io->out.buflen, &wbytes, smps, cnt);
2018-08-09 08:39:27 +02:00
fwrite(io->out.buffer, wbytes, 1, f);
2017-08-05 21:02:09 +02:00
}
2018-05-12 15:25:29 +02:00
else
ret = -1;
2017-08-05 21:02:09 +02:00
if (io->flags & IO_FLUSH)
2017-08-05 21:02:09 +02:00
io_flush(io);
return ret;
}
2017-08-22 12:20:55 +02:00
int io_scan(struct io *io, struct sample *smps[], unsigned cnt)
2017-08-05 21:02:09 +02:00
{
int ret;
2018-05-12 14:14:59 +02:00
assert(io->state == STATE_OPENED);
2018-05-12 15:25:29 +02:00
if (io->flags & IO_NEWLINES)
ret = io_scan_lines(io, smps, cnt);
2018-08-20 18:31:27 +02:00
else if (io_type(io)->scan)
ret = io_type(io)->scan(io, smps, cnt);
else if (io_type(io)->sscan) {
FILE *f = io_stream_input(io);
2018-05-12 15:25:29 +02:00
size_t bytes, rbytes;
2018-08-09 08:39:27 +02:00
bytes = fread(io->in.buffer, 1, io->in.buflen, f);
2018-08-09 08:39:27 +02:00
ret = io_sscan(io, io->in.buffer, bytes, &rbytes, smps, cnt);
2017-08-05 21:02:09 +02:00
}
2018-05-12 15:25:29 +02:00
else
ret = -1;
return ret;
}
FILE * io_stream_output(struct io *io) {
2018-05-12 14:14:59 +02:00
if (io->state != STATE_OPENED)
return 0;
return io->mode == IO_MODE_ADVIO
2018-08-09 08:39:27 +02:00
? io->out.stream.adv->file
: io->out.stream.std;
}
FILE * io_stream_input(struct io *io) {
2018-05-12 14:14:59 +02:00
if (io->state != STATE_OPENED)
return 0;
return io->mode == IO_MODE_ADVIO
2018-08-09 08:39:27 +02:00
? io->in.stream.adv->file
: io->in.stream.std;
}
2018-05-12 15:25:29 +02:00
2018-08-20 18:31:27 +02:00
int io_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt)
2018-05-12 15:25:29 +02:00
{
2018-08-20 18:31:27 +02:00
assert(io->state == STATE_CHECKED || io->state == STATE_OPENED);
2018-05-12 15:25:29 +02:00
2018-08-20 18:31:27 +02:00
return io_type(io)->sscan ? io_type(io)->sscan(io, buf, len, rbytes, smps, cnt) : -1;
2018-05-12 15:25:29 +02:00
}
int io_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt)
{
2018-08-20 18:31:27 +02:00
assert(io->state == STATE_CHECKED || io->state == STATE_OPENED);
2018-05-12 15:25:29 +02:00
2018-08-20 18:31:27 +02:00
return io_type(io)->sprint ? io_type(io)->sprint(io, buf, len, wbytes, smps, cnt) : -1;
2018-05-12 15:25:29 +02:00
}