1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/formats/csv.c

132 lines
3.2 KiB
C
Raw Normal View History

2017-07-28 18:11:52 +02:00
/** Comma-separated values.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @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>
#include "formats/csv.h"
#include "plugin.h"
#include "sample.h"
2017-08-05 21:02:09 +02:00
#include "timing.h"
2017-07-28 18:11:52 +02:00
2017-08-05 21:02:09 +02:00
int io_format_csv_fprint_single(FILE *f, struct sample *s, int flags)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
fprintf(f, "%ld %09ld %d", s->ts.origin.tv_sec, s->ts.origin.tv_nsec, s->sequence);
2017-07-28 18:11:52 +02:00
for (int i = 0; i < s->length; i++) {
switch ((s->format >> i) & 0x1) {
case SAMPLE_DATA_FORMAT_FLOAT:
2017-08-05 21:02:09 +02:00
fprintf(f, "%c%.6f", CSV_SEPARATOR, s->data[i].f);
2017-07-28 18:11:52 +02:00
break;
case SAMPLE_DATA_FORMAT_INT:
2017-08-05 21:02:09 +02:00
fprintf(f, "%c%d", CSV_SEPARATOR, s->data[i].i);
2017-07-28 18:11:52 +02:00
break;
}
}
2017-08-05 21:02:09 +02:00
fputc('\n', f);
2017-07-28 18:11:52 +02:00
return 0;
}
2017-08-05 21:02:09 +02:00
size_t io_format_csv_sscan_single(const char *buf, size_t len, struct sample *s, int *flags)
2017-07-28 18:11:52 +02:00
{
int ret, off;
2017-08-05 21:02:09 +02:00
ret = sscanf(buf, "%ld %ld %d %n", &s->ts.origin.tv_sec, &s->ts.origin.tv_nsec, &s->sequence, &off);
if (ret != 3)
2017-07-28 18:11:52 +02:00
return -1;
int i;
2017-08-05 21:02:09 +02:00
for (i = 0; i < s->capacity; i++) {
switch (s->format & (1 << i)) {
2017-07-28 18:11:52 +02:00
case SAMPLE_DATA_FORMAT_FLOAT:
2017-08-05 21:02:09 +02:00
ret = sscanf(buf + off, "%f %n", &s->data[i].f, &off);
2017-07-28 18:11:52 +02:00
break;
case SAMPLE_DATA_FORMAT_INT:
2017-08-05 21:02:09 +02:00
ret = sscanf(buf + off, "%d %n", &s->data[i].i, &off);
2017-07-28 18:11:52 +02:00
break;
}
if (ret != 2)
break;
}
2017-08-05 21:02:09 +02:00
s->length = i;
s->ts.received = time_now();
2017-07-28 18:11:52 +02:00
2017-08-05 21:02:09 +02:00
return 0;
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
int io_format_csv_fscan_single(FILE *f, struct sample *s, int *flags)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
char *ptr, line[4096];
skip: if (fgets(line, sizeof(line), f) == NULL)
return -1; /* An error occured */
/* Skip whitespaces, empty and comment lines */
for (ptr = line; isspace(*ptr); ptr++);
if (*ptr == '\0' || *ptr == '#')
goto skip;
return io_format_csv_sscan_single(line, strlen(line), s, flags);
}
int io_format_csv_fprint(FILE *f, struct sample *smps[], size_t cnt, int flags)
{
int ret, i;
for (i = 0; i < cnt; i++) {
ret = io_format_csv_fprint_single(f, smps[i], flags);
if (ret < 0)
break;
}
return i;
2017-07-28 18:11:52 +02:00
}
2017-08-05 21:02:09 +02:00
int io_format_csv_fscan(FILE *f, struct sample *smps[], size_t cnt, int *flags)
2017-07-28 18:11:52 +02:00
{
2017-08-05 21:02:09 +02:00
int ret, i;
for (i = 0; i < cnt; i++) {
ret = io_format_csv_fscan_single(f, smps[i], flags);
if (ret < 0) {
warn("Failed to read CSV line: %d", ret);
break;
}
}
return i;
2017-07-28 18:11:52 +02:00
}
static struct plugin p = {
.name = "csv",
.description = "Tabulator-separated values",
.type = PLUGIN_TYPE_FORMAT,
.io = {
2017-08-05 21:02:09 +02:00
.fprint = io_format_csv_fprint,
.fscan = io_format_csv_fscan,
2017-07-28 18:11:52 +02:00
.size = 0
}
};
REGISTER_PLUGIN(&p);