diff --git a/include/villas/io.h b/include/villas/io.h index 293ec9c20..51b9d95eb 100644 --- a/include/villas/io.h +++ b/include/villas/io.h @@ -40,8 +40,6 @@ struct io { int flags; struct { - int counter; /**< Number of samples, read or written by this IO instance */ - /** A format type can use this file handle or overwrite the * format::{open,close,eof,rewind} functions and the private * data in io::_vd. @@ -75,6 +73,10 @@ int io_open(struct io *io, const char *uri); int io_close(struct io *io); +void io_header(struct io *io); + +void io_footer(struct io *io); + int io_print(struct io *io, struct sample *smps[], unsigned cnt); int io_scan(struct io *io, struct sample *smps[], unsigned cnt); diff --git a/include/villas/io_format.h b/include/villas/io_format.h index 599acd34c..5f8ecb14a 100644 --- a/include/villas/io_format.h +++ b/include/villas/io_format.h @@ -76,6 +76,13 @@ struct io_format { int (*print)(struct io *io, struct sample *smps[], unsigned cnt); int (*scan)( struct io *io, struct sample *smps[], unsigned cnt); + + /** Print a header. */ + void (*header)(struct io *io); + + /** Print a footer. */ + void (*footer)(struct io *io); + /** @} */ /** @{ diff --git a/lib/io.c b/lib/io.c index 1f814185d..dfcdba997 100644 --- a/lib/io.c +++ b/lib/io.c @@ -218,16 +218,30 @@ int io_stream_fd(struct io *io) int io_open(struct io *io, const char *uri) { - return io->_vt->open + int ret; + + ret = io->_vt->open ? io->_vt->open(io, uri) : io_stream_open(io, uri); + if (ret) + return ret; + + io_header(io); + + return ret; } int io_close(struct io *io) { - return io->_vt->close + int ret; + + io_footer(io); + + ret = io->_vt->close ? io->_vt->close(io) : io_stream_close(io); + + return ret; } int io_flush(struct io *io) @@ -258,6 +272,18 @@ int io_fd(struct io *io) : io_stream_fd(io); } +void io_header(struct io *io) +{ + if (io->_vt->header) + io->_vt->header(io); +} + +void io_footer(struct io *io) +{ + if (io->_vt->footer) + io->_vt->footer(io); +} + int io_print(struct io *io, struct sample *smps[], unsigned cnt) { int ret; diff --git a/lib/io/csv.c b/lib/io/csv.c index 51e633550..227b19a56 100644 --- a/lib/io/csv.c +++ b/lib/io/csv.c @@ -194,6 +194,15 @@ int csv_fscan(FILE *f, struct sample *smps[], unsigned cnt, int flags) return i; } +void csv_header(struct io *io) +{ + FILE *f = io->mode == IO_MODE_ADVIO + ? io->output.stream.adv->file + : io->output.stream.std; + + fprintf(f, "# secs%cnsecs%coffset%csequence%cdata[]\n", CSV_SEPARATOR, CSV_SEPARATOR, CSV_SEPARATOR, CSV_SEPARATOR); +} + static struct plugin p = { .name = "csv", .description = "Tabulator-separated values", @@ -203,7 +212,8 @@ static struct plugin p = { .fscan = csv_fscan, .sprint = csv_sprint, .sscan = csv_sscan, - .size = 0 + .header = csv_header, + .size = 0, } }; diff --git a/lib/io/villas_human.c b/lib/io/villas_human.c index 367ab4ba5..c69fafa35 100644 --- a/lib/io/villas_human.c +++ b/lib/io/villas_human.c @@ -233,24 +233,13 @@ int villas_human_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags) return i; } -int villas_human_print(struct io *io, struct sample *smps[], unsigned cnt) +void villas_human_header(struct io *io) { - struct villas_human *h = (struct villas_human *) io->_vd; - FILE *f = io->mode == IO_MODE_ADVIO ? io->output.stream.adv->file : io->output.stream.std; - if (!h->header_written) { - fprintf(f, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]"); - - if (io->flags & IO_FLUSH) - io_flush(io); - - h->header_written = true; - } - - return villas_human_fprint(f, smps, cnt, io->flags); + fprintf(f, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]"); } int villas_human_fscan(FILE *f, struct sample *smps[], unsigned cnt, int flags) @@ -280,28 +269,18 @@ int villas_human_open(struct io *io, const char *uri) return 0; } -void villas_human_rewind(struct io *io) -{ - struct villas_human *h = (struct villas_human *) io->_vd; - - h->header_written = false; - - io_stream_rewind(io); -} - static struct plugin p = { .name = "villas.human", .description = "VILLAS human readable format", .type = PLUGIN_TYPE_IO, .io = { .open = villas_human_open, - .rewind = villas_human_rewind, - .print = villas_human_print, .fprint = villas_human_fprint, .fscan = villas_human_fscan, .sprint = villas_human_sprint, .sscan = villas_human_sscan, - .size = sizeof(struct villas_human) + .header = villas_human_header, + .size = sizeof(struct villas_human), } };