1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

io: generalize handling of header and footers

This commit is contained in:
Steffen Vogel 2018-05-12 11:07:35 +02:00
parent 39f955c947
commit 59ec38172b
5 changed files with 54 additions and 30 deletions

View file

@ -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);

View file

@ -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);
/** @} */
/** @{

View file

@ -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;

View file

@ -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,
}
};

View file

@ -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),
}
};