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