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

80 lines
1.7 KiB
C++
Raw Permalink Normal View History

/** Bare text values.
*
* @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 <villas/formats/value.hpp>
#include <villas/sample.hpp>
#include <villas/signal.hpp>
2021-05-10 00:12:30 +02:00
using namespace villas::node;
int ValueFormat::sprint(char *buf, size_t len, size_t *wbytes, const struct Sample * const smps[], unsigned cnt)
{
unsigned i;
size_t off = 0;
const struct Sample *smp = smps[0];
assert(cnt == 1);
assert(smp->length <= 1);
buf[0] = '\0';
for (i = 0; i < smp->length; i++) {
auto sig = smp->signals->getByIndex(i);
if (!sig)
return -1;
off += smp->data[i].printString(sig->type, buf, len, real_precision);
off += snprintf(buf + off, len - off, "\n");
}
if (wbytes)
*wbytes = off;
return i;
}
int ValueFormat::sscan(const char *buf, size_t len, size_t *rbytes, struct Sample * const smps[], unsigned cnt)
{
unsigned i = 0, ret;
struct Sample *smp = smps[0];
const char *ptr = buf;
char *end;
assert(cnt == 1);
printf("Reading: %s", buf);
if (smp->capacity >= 1) {
auto sig = signals->getByIndex(i);
if (!sig)
return -1;
ret = smp->data[i].parseString(sig->type, ptr, &end);
if (ret || end == ptr) /* There are no valid values anymore. */
goto out;
i++;
ptr = end;
}
out: smp->flags = 0;
2021-05-10 00:12:30 +02:00
smp->signals = signals;
smp->length = i;
if (smp->length > 0)
smp->flags |= (int) SampleFlags::HAS_DATA;
if (rbytes)
*rbytes = ptr - buf;
return i;
}
2021-05-10 00:12:30 +02:00
static char n[] = "value";
static char d[] = "A bare text value without any headers";
static FormatPlugin<ValueFormat, n, d, (int) SampleFlags::HAS_DATA> p;