2017-07-28 18:11:52 +02:00
|
|
|
/** Comma-separated values.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2018-08-20 18:39:04 +02:00
|
|
|
* @copyright 2017-2018, 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 <ctype.h>
|
2017-08-14 14:42:07 +02:00
|
|
|
#include <inttypes.h>
|
2017-11-02 13:08:12 +01:00
|
|
|
#include <string.h>
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-05-12 13:47:35 +02:00
|
|
|
#include <villas/io.h>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/formats/csv.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/sample.h>
|
2018-05-12 18:02:18 +02:00
|
|
|
#include <villas/signal.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/timing.h>
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
static size_t csv_sprint_single(struct io *io, char *buf, size_t len, const struct sample *smp)
|
2017-07-28 18:11:52 +02:00
|
|
|
{
|
2017-09-04 14:28:55 +02:00
|
|
|
size_t off = 0;
|
2018-08-20 18:30:24 +02:00
|
|
|
struct signal *sig;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
if (io->flags & SAMPLE_HAS_TS_ORIGIN) {
|
|
|
|
if (io->flags & SAMPLE_HAS_TS_ORIGIN)
|
|
|
|
off += snprintf(buf + off, len - off, "%ld%c%09ld", smp->ts.origin.tv_sec, io->separator, smp->ts.origin.tv_nsec);
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "nan%cnan", io->separator);
|
|
|
|
}
|
2018-05-12 11:08:35 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
if (io->flags & SAMPLE_HAS_OFFSET) {
|
|
|
|
if (smp->flags & SAMPLE_HAS_TS_RECEIVED)
|
|
|
|
off += snprintf(buf + off, len - off, "%c%.09f", io->separator, time_delta(&smp->ts.origin, &smp->ts.received));
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "%cnan", io->separator);
|
|
|
|
}
|
2017-08-22 12:21:17 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
if (io->flags & SAMPLE_HAS_SEQUENCE) {
|
|
|
|
if (smp->flags & SAMPLE_HAS_SEQUENCE)
|
|
|
|
off += snprintf(buf + off, len - off, "%c%" PRIu64, io->separator, smp->sequence);
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "%cnan", io->separator);
|
|
|
|
}
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
if (io->flags & SAMPLE_HAS_DATA) {
|
|
|
|
for (int i = 0; i < smp->length; i++) {
|
|
|
|
sig = list_at_safe(smp->signals, i);
|
|
|
|
if (!sig)
|
2017-07-28 18:11:52 +02:00
|
|
|
break;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
off += snprintf(buf + off, len - off, "%c", io->separator);
|
|
|
|
off += signal_data_snprint(&smp->data[i], sig, buf + off, len - off);
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 10:45:11 +02:00
|
|
|
off += snprintf(buf + off, len - off, "%c", io->delimiter);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
return off;
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
static size_t csv_sscan_single(struct io *io, const char *buf, size_t len, struct sample *smp)
|
2017-07-28 18:11:52 +02:00
|
|
|
{
|
2018-08-20 18:30:24 +02:00
|
|
|
int ret, i = 0;
|
2017-08-22 12:21:17 +02:00
|
|
|
const char *ptr = buf;
|
2018-08-20 18:30:24 +02:00
|
|
|
char *end, *next;
|
2017-08-22 12:21:17 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->flags = 0;
|
|
|
|
smp->signals = io->signals;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->ts.origin.tv_sec = strtoul(ptr, &end, 10);
|
2018-05-24 10:45:11 +02:00
|
|
|
if (end == ptr || *end == io->delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
goto out;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-06-16 20:55:42 +02:00
|
|
|
ptr = end + 1;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->ts.origin.tv_nsec = strtoul(ptr, &end, 10);
|
2018-05-24 10:45:11 +02:00
|
|
|
if (end == ptr || *end == io->delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
goto out;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-06-16 20:55:42 +02:00
|
|
|
ptr = end + 1;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->flags |= SAMPLE_HAS_TS_ORIGIN;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-12 11:08:35 +02:00
|
|
|
double offset __attribute__((unused)) = strtof(ptr, &end);
|
2018-05-24 10:45:11 +02:00
|
|
|
if (end == ptr || *end == io->delimiter)
|
2018-05-12 11:08:35 +02:00
|
|
|
goto out;
|
|
|
|
|
2018-06-16 20:55:42 +02:00
|
|
|
ptr = end + 1;
|
2018-05-12 11:08:35 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->sequence = strtoul(ptr, &end, 10);
|
2018-05-24 10:45:11 +02:00
|
|
|
if (end == ptr || *end == io->delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
goto out;
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->flags |= SAMPLE_HAS_SEQUENCE;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
for (ptr = end + 1, i = 0; i < smp->capacity; ptr = end + 1, i++) {
|
2018-06-16 20:55:42 +02:00
|
|
|
|
2018-05-24 10:45:11 +02:00
|
|
|
if (*end == io->delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
goto out;
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
struct signal *sig = (struct signal *) list_at_safe(smp->signals, i);
|
|
|
|
if (!sig)
|
|
|
|
goto out;
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
/* Perform signal detection only once */
|
|
|
|
if (sig->type == SIGNAL_TYPE_AUTO) {
|
2018-08-01 14:40:50 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
/* Find end of the current column */
|
|
|
|
next = strpbrk(ptr, (char[]) { io->separator, io->delimiter, 0 });
|
|
|
|
if (next == NULL)
|
|
|
|
goto out;
|
2018-08-01 14:40:50 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
/* Copy value to temporary '\0' terminated buffer */
|
|
|
|
size_t len = next - ptr;
|
|
|
|
char val[len+1];
|
|
|
|
strncpy(val, ptr, len);
|
|
|
|
val[len] = '\0';
|
2018-08-01 14:40:50 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
sig->type = signal_type_detect(val);
|
|
|
|
|
|
|
|
debug(LOG_IO | 5, "Learned data type for index %u: %s", i, signal_type_to_str(sig->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = signal_data_parse_str(&smp->data[i], sig, ptr, &end);
|
|
|
|
if (ret || end == ptr) /* There are no valid values anymore. */
|
2017-08-22 12:21:17 +02:00
|
|
|
goto out;
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-24 10:45:11 +02:00
|
|
|
out: if (*end == io->delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
end++;
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->length = i;
|
|
|
|
if (smp->length > 0)
|
|
|
|
smp->flags |= SAMPLE_HAS_DATA;
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
return end - buf;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
int csv_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt)
|
2017-08-22 12:21:17 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t off = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < cnt && off < len; i++)
|
2018-05-12 18:01:48 +02:00
|
|
|
off += csv_sprint_single(io, buf + off, len - off, smps[i]);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (wbytes)
|
|
|
|
*wbytes = off;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
int csv_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt)
|
2017-08-22 12:21:17 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t off = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < cnt && off < len; i++)
|
2018-05-12 18:01:48 +02:00
|
|
|
off += csv_sscan_single(io, buf + off, len - off, smps[i]);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (rbytes)
|
|
|
|
*rbytes = off;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
void csv_header(struct io *io, const struct sample *smp)
|
2018-05-12 11:07:35 +02:00
|
|
|
{
|
2018-05-12 13:47:35 +02:00
|
|
|
FILE *f = io_stream_output(io);
|
2018-05-12 11:07:35 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
fprintf(f, "# ");
|
|
|
|
if (io->flags & SAMPLE_HAS_TS_ORIGIN)
|
|
|
|
fprintf(f, "secs%cnsecs%c", io->separator, io->separator);
|
2018-05-12 18:02:18 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
if (io->flags & SAMPLE_HAS_OFFSET)
|
|
|
|
fprintf(f, "offset%c", io->separator);
|
2018-05-12 18:02:18 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
if (io->flags & SAMPLE_HAS_SEQUENCE)
|
|
|
|
fprintf(f, "sequence%c", io->separator);
|
|
|
|
|
|
|
|
if (io->flags & SAMPLE_HAS_DATA) {
|
|
|
|
for (int i = 0; i < smp->length; i++) {
|
|
|
|
struct signal *sig = (struct signal *) list_at(smp->signals, i);
|
|
|
|
|
|
|
|
if (sig->name)
|
|
|
|
fprintf(f, "%s", sig->name);
|
|
|
|
else
|
|
|
|
fprintf(f, "signal%d", i);
|
2018-05-12 18:02:18 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
if (sig->unit)
|
|
|
|
fprintf(f, "[%s]", sig->unit);
|
|
|
|
|
|
|
|
if (i+1 < smp->length)
|
|
|
|
fprintf(f, "%c", io->separator);
|
|
|
|
}
|
2018-05-12 18:02:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 10:45:11 +02:00
|
|
|
fprintf(f, "%c", io->delimiter);
|
2018-05-12 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 10:45:52 +02:00
|
|
|
static struct plugin p1 = {
|
|
|
|
.name = "tsv",
|
2017-07-28 18:11:52 +02:00
|
|
|
.description = "Tabulator-separated values",
|
2018-05-12 13:56:12 +02:00
|
|
|
.type = PLUGIN_TYPE_FORMAT,
|
2018-05-13 13:51:28 +02:00
|
|
|
.format = {
|
2017-08-22 12:21:17 +02:00
|
|
|
.sprint = csv_sprint,
|
|
|
|
.sscan = csv_sscan,
|
2018-05-12 11:07:35 +02:00
|
|
|
.header = csv_header,
|
2018-05-13 12:47:55 +02:00
|
|
|
.size = 0,
|
2018-05-24 10:45:52 +02:00
|
|
|
.flags = IO_NEWLINES,
|
|
|
|
.separator = '\t'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct plugin p2 = {
|
|
|
|
.name = "csv",
|
|
|
|
.description = "Comma-separated values",
|
|
|
|
.type = PLUGIN_TYPE_FORMAT,
|
|
|
|
.format = {
|
|
|
|
.sprint = csv_sprint,
|
|
|
|
.sscan = csv_sscan,
|
|
|
|
.header = csv_header,
|
|
|
|
.size = 0,
|
2018-08-20 18:30:24 +02:00
|
|
|
.flags = IO_NEWLINES | IO_AUTO_DETECT_FORMAT |
|
|
|
|
SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA,
|
2018-05-24 10:45:52 +02:00
|
|
|
.separator = ','
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-24 10:45:52 +02:00
|
|
|
REGISTER_PLUGIN(&p1);
|
|
|
|
REGISTER_PLUGIN(&p2);
|