-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathformat.cpp
More file actions
148 lines (108 loc) · 3.54 KB
/
format.cpp
File metadata and controls
148 lines (108 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Reading and writing simulation samples in various formats.
*
* 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
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <villas/exceptions.hpp>
#include <villas/format.hpp>
#include <villas/node/config.hpp>
#include <villas/sample.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
using namespace villas::utils;
Format *FormatFactory::make(json_t *json) {
std::string type;
Format *f;
if (json_is_string(json)) {
type = json_string_value(json);
return FormatFactory::make(type);
} else if (json_is_object(json)) {
json_t *json_type = json_object_get(json, "type");
type = json_string_value(json_type);
f = FormatFactory::make(type);
if (!f)
return nullptr;
f->parse(json);
return f;
} else
throw ConfigError(json, "node-config-format", "Invalid format config");
}
Format *FormatFactory::make(const std::string &format) {
FormatFactory *ff = plugin::registry->lookup<FormatFactory>(format);
if (!ff)
throw RuntimeError("Unknown format: {}", format);
return ff->make();
}
Format::Format(int fl) : flags(fl), real_precision(17), signals(nullptr) {
in.buflen = out.buflen = DEFAULT_FORMAT_BUFFER_LENGTH;
in.buffer = new char[in.buflen];
out.buffer = new char[out.buflen];
if (!in.buffer || !out.buffer)
throw MemoryAllocationError();
}
Format::~Format() {
int ret __attribute__((unused));
delete[] in.buffer;
delete[] out.buffer;
}
void Format::start(SignalList::Ptr sigs, int fl) {
flags &= fl;
signals = sigs;
start();
}
void Format::start(const std::string &dtypes, int fl) {
flags &= fl;
signals = std::make_shared<SignalList>(dtypes.c_str());
if (!signals)
throw MemoryAllocationError();
start();
}
int Format::print(FILE *f, const struct Sample *const smps[], unsigned cnt) {
int ret;
size_t wbytes;
ret = sprint(out.buffer, out.buflen, &wbytes, smps, cnt);
fwrite(out.buffer, wbytes, 1, f);
return ret;
}
int Format::scan(FILE *f, struct Sample *const smps[], unsigned cnt) {
size_t bytes, rbytes;
bytes = fread(in.buffer, 1, in.buflen, f);
return sscan(in.buffer, bytes, &rbytes, smps, cnt);
}
void Format::parse(json_t *json) {
int ret;
json_error_t err;
int ts_origin = -1;
int ts_received = -1;
int sequence = -1;
int data = -1;
int offset = -1;
ret = json_unpack_ex(json, &err, 0,
"{ s?: b, s?: b, s?: b, s?: b, s?: b, s?: i }",
"ts_origin", &ts_origin, "ts_received", &ts_received,
"sequence", &sequence, "data", &data, "offset", &offset,
"real_precision", &real_precision);
if (ret)
throw ConfigError(json, err, "node-config-format",
"Failed to parse format configuration");
if (real_precision < 0 || real_precision > 31)
throw ConfigError(json, err, "node-config-format-precision",
"The valid range for the real_precision setting is "
"between 0 and 31 (inclusive)");
if (ts_origin == 0)
flags &= ~(int)SampleFlags::HAS_TS_ORIGIN;
if (ts_received == 0)
flags &= ~(int)SampleFlags::HAS_TS_RECEIVED;
if (sequence == 0)
flags &= ~(int)SampleFlags::HAS_SEQUENCE;
if (data == 0)
flags &= ~(int)SampleFlags::HAS_DATA;
if (offset == 0)
flags &= ~(int)SampleFlags::HAS_OFFSET;
}