2021-05-10 00:12:30 +02:00
|
|
|
/** Comma-separated values.
|
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2021-05-10 00:12:30 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <villas/formats/column.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/signal.hpp>
|
|
|
|
#include <villas/timing.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
|
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
size_t ColumnLineFormat::sprintLine(char *buf, size_t len, const struct Sample *smp)
|
2021-05-10 00:12:30 +02:00
|
|
|
{
|
|
|
|
size_t off = 0;
|
|
|
|
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_ORIGIN)
|
|
|
|
off += snprintf(buf + off, len - off, "%lld%c%09lld", (long long) smp->ts.origin.tv_sec, separator,
|
|
|
|
(long long) smp->ts.origin.tv_nsec);
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "nan%cnan", separator);
|
|
|
|
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_RECEIVED)
|
|
|
|
off += snprintf(buf + off, len - off, "%c%.09f", separator, time_delta(&smp->ts.origin, &smp->ts.received));
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "%cnan", separator);
|
|
|
|
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_SEQUENCE)
|
|
|
|
off += snprintf(buf + off, len - off, "%c%" PRIu64, separator, smp->sequence);
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "%cnan", separator);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < smp->length; i++) {
|
2021-08-10 10:12:48 -04:00
|
|
|
auto sig = smp->signals->getByIndex(i);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (!sig)
|
|
|
|
break;
|
|
|
|
|
|
|
|
off += snprintf(buf + off, len - off, "%c", separator);
|
2021-08-10 10:12:48 -04:00
|
|
|
off += smp->data[i].printString(sig->type, buf + off, len - off, real_precision);
|
2021-05-10 00:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
off += snprintf(buf + off, len - off, "%c", delimiter);
|
|
|
|
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
size_t ColumnLineFormat::sscanLine(const char *buf, size_t len, struct Sample *smp)
|
2021-05-10 00:12:30 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned i = 0;
|
|
|
|
const char *ptr = buf;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
struct timespec offset;
|
|
|
|
|
|
|
|
smp->flags = 0;
|
|
|
|
smp->signals = signals;
|
|
|
|
|
|
|
|
smp->ts.origin.tv_sec = strtoul(ptr, &end, 10);
|
|
|
|
if (end == ptr || *end == delimiter)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ptr = end + 1;
|
|
|
|
|
|
|
|
smp->ts.origin.tv_nsec = strtoul(ptr, &end, 10);
|
|
|
|
if (end == ptr || *end == delimiter)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ptr = end + 1;
|
|
|
|
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
|
|
|
|
|
|
|
offset = time_from_double(strtof(ptr, &end));
|
|
|
|
if (end == ptr || *end == delimiter)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
smp->ts.received = time_add(&smp->ts.origin, &offset);
|
|
|
|
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_RECEIVED;
|
|
|
|
|
|
|
|
ptr = end + 1;
|
|
|
|
|
|
|
|
smp->sequence = strtoul(ptr, &end, 10);
|
|
|
|
if (end == ptr || *end == delimiter)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_SEQUENCE;
|
|
|
|
|
|
|
|
for (ptr = end + 1, i = 0; i < smp->capacity; ptr = end + 1, i++) {
|
|
|
|
if (*end == delimiter)
|
|
|
|
goto out;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
auto sig = smp->signals->getByIndex(i);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (!sig)
|
|
|
|
goto out;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = smp->data[i].parseString(sig->type, ptr, &end);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (ret || end == ptr) /* There are no valid values anymore. */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out: if (*end == delimiter)
|
|
|
|
end++;
|
|
|
|
|
|
|
|
smp->length = i;
|
|
|
|
if (smp->length > 0)
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_DATA;
|
|
|
|
|
|
|
|
return end - buf;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void ColumnLineFormat::header(FILE *f, const SignalList::Ptr sigs)
|
2021-05-10 00:12:30 +02:00
|
|
|
{
|
|
|
|
/* Abort if we are not supposed to, or have already printed the header */
|
|
|
|
if (!print_header || header_printed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (comment)
|
|
|
|
fprintf(f, "%c", comment);
|
|
|
|
|
|
|
|
if (flags & (int) SampleFlags::HAS_TS_ORIGIN)
|
|
|
|
fprintf(f, "secs%cnsecs%c", separator, separator);
|
|
|
|
|
|
|
|
if (flags & (int) SampleFlags::HAS_OFFSET)
|
|
|
|
fprintf(f, "offset%c", separator);
|
|
|
|
|
|
|
|
if (flags & (int) SampleFlags::HAS_SEQUENCE)
|
|
|
|
fprintf(f, "sequence%c", separator);
|
|
|
|
|
|
|
|
if (flags & (int) SampleFlags::HAS_DATA) {
|
2021-08-10 10:12:48 -04:00
|
|
|
for (unsigned i = 0; i < sigs->size(); i++) {
|
|
|
|
auto sig = sigs->getByIndex(i);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (!sig)
|
|
|
|
break;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!sig->name.empty())
|
|
|
|
fprintf(f, "%s", sig->name.c_str());
|
2021-05-10 00:12:30 +02:00
|
|
|
else
|
|
|
|
fprintf(f, "signal%u", i);
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!sig->unit.empty())
|
|
|
|
fprintf(f, "[%s]", sig->unit.c_str());
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (i + 1 < sigs->size())
|
2021-05-10 00:12:30 +02:00
|
|
|
fprintf(f, "%c", separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "%c", delimiter);
|
|
|
|
|
|
|
|
LineFormat::header(f, sigs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnLineFormat::parse(json_t *json)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
const char *sep = nullptr;
|
|
|
|
|
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s }",
|
|
|
|
"separator", &sep
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-format-column", "Failed to parse format configuration");
|
|
|
|
|
|
|
|
if (sep) {
|
|
|
|
if (strlen(sep) != 1)
|
|
|
|
throw ConfigError(json, "node-config-format-column-separator", "Column separator must be a single character!");
|
|
|
|
|
|
|
|
separator = sep[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
LineFormat::parse(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char n1[] = "csv";
|
|
|
|
static char d1[] = "Comma-separated values";
|
|
|
|
static ColumnLineFormatPlugin<n1, d1, (int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA, '\n', ','> p1;
|
|
|
|
|
|
|
|
static char n2[] = "tsv";
|
|
|
|
static char d2[] = "Tabulator-separated values";
|
|
|
|
static ColumnLineFormatPlugin<n2, d2, (int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA, '\n', '\t'> p2;
|