Newer
Older
/*
* PacketStructure.cpp
*
* Created on: Dec 7, 2022
* Author: valerio
*/
#include <PacketStructureJson.h>
using namespace inaf::oasbo::Packets;
std::vector<std::tuple<int, std::string, int>> PacketStructureJson::readStructureFromSource(std::string source) {
std::cerr << "Error: Could not open file: " << source << std::endl;
exit (EXIT_FAILURE);
}
size_t count = 0;
nlohmann::ordered_json data;
file >> data;
this->structure = convertToTupleVector(data, count);
file.close();
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
}
std::vector<std::tuple<int, std::string, int>> PacketStructureJson::convertToTupleVector(
const nlohmann::ordered_json &data, size_t &count) {
std::vector<std::tuple<int, std::string, int>> out;
for (auto it = data.begin(); it != data.end(); ++it) {
if (it.value().is_object()) {
if (!it.value().count("size") || !it.value().count("fields")
|| it.value().size() != 2) {
std::cerr << "Error in file: " << it.value().dump()
<< std::endl;
exit (EXIT_FAILURE);
}
for (auto i = 0; i < it.value()["size"]; i++) {
std::vector<std::tuple<int, std::string, int>> subArray =
convertToTupleVector(it.value()["fields"], count);
std::for_each(subArray.begin(), subArray.end(),
[i](std::tuple<int, std::string, int> &line) {
std::get < 1
> (line).append("_#").append(
std::to_string(i));
});
out.insert(out.end(), subArray.begin(), subArray.end());
}
} else {
if (!it.value().is_number_integer()) {
std::cerr << "Error in value, not an int: " << it.value().dump()
<< std::endl;
exit (EXIT_FAILURE);
}
std::string fieldName(it.key());
std::transform(fieldName.begin(), fieldName.end(),
fieldName.begin(), ::tolower);
out.push_back(std::make_tuple(count, fieldName, it.value()));
count += 1;
}
}
return out;
}