32#include <unordered_map>
49 in.read(
reinterpret_cast<char*
>(&value),
sizeof(T));
52 if constexpr (std::is_same_v<T, int8_t>)
53 out << std::format(
"{}",
static_cast<int>(value));
54 else if constexpr (std::is_same_v<T, uint8_t>)
55 out << std::format(
"{}",
static_cast<unsigned int>(value));
57 out << std::format(
"{}", value);
78 return filename.parent_path() / filename.stem();
89 using FieldDeserializer =
90 std::function<void(std::ifstream&, std::ofstream&)>;
92 using DeserializeInstructions = std::vector<FieldDeserializer>;
104 std::ofstream createCSV(
const std::string& typeName);
116 void registerType(
const std::string& typeName, std::ifstream& file);
121 DeserializeInstructions instructions;
124 std::unordered_map<std::string, MappingRecord> typeMap;
126 std::filesystem::path filename;
130 : filename(
std::move(filename))
134std::ofstream Deserializer::createCSV(
const std::string& typeName)
137 std::ofstream stream(outputFile);
141 std::cerr <<
"Error opening file " << outputFile << std::endl;
142 throw std::runtime_error(
"Error opening file " + outputFile.string());
151void Deserializer::registerType(
const std::string& typeName,
155 auto outFile = createCSV(typeName);
158 uint8_t fieldCount = file.get();
163 DeserializeInstructions deserializeInstructions;
164 deserializeInstructions.reserve(fieldCount);
167 for (
int i = 0; i < fieldCount; i++)
170 std::string fieldName;
171 std::getline(file, fieldName,
'\0');
173 header += fieldName +
",";
176 char fieldType = file.get();
183 deserializeInstructions.push_back(
184 detail::deserializeField<bool>);
187 deserializeInstructions.push_back(
188 detail::deserializeField<char>);
191 deserializeInstructions.push_back(
192 detail::deserializeField<int8_t>);
195 deserializeInstructions.push_back(
196 detail::deserializeField<uint8_t>);
199 deserializeInstructions.push_back(
200 detail::deserializeField<int16_t>);
203 deserializeInstructions.push_back(
204 detail::deserializeField<uint16_t>);
207 deserializeInstructions.push_back(
208 detail::deserializeField<int32_t>);
211 deserializeInstructions.push_back(
212 detail::deserializeField<uint32_t>);
215 deserializeInstructions.push_back(
216 detail::deserializeField<int64_t>);
219 deserializeInstructions.push_back(
220 detail::deserializeField<uint64_t>);
223 deserializeInstructions.push_back(
224 detail::deserializeField<float32_t>);
227 deserializeInstructions.push_back(
228 detail::deserializeField<float64_t>);
231 std::cerr <<
"Unknown field type: " << fieldType << std::endl
232 <<
"Aborting deserialization" << std::endl;
233 throw std::runtime_error(
"Unknown field type: " +
234 std::to_string(fieldType));
239 outFile << header.substr(0, header.size() - 1) <<
'\n';
241 auto result = typeMap.emplace(
243 MappingRecord{std::move(outFile), std::move(deserializeInstructions)});
247 std::cerr <<
"Duplicate type: " << typeName << std::endl;
248 throw std::runtime_error(
"Duplicate type: " + typeName);
252 std::cout <<
"\tFound type " << typeName <<
" with " << (int)fieldCount
253 <<
" fields" << std::endl;
260 std::ifstream file(filename, std::ios::binary);
263 std::cerr << filename <<
" does not exists." << std::endl;
271 std::string typeName;
272 std::getline(file, typeName,
'\0');
285 registerType(typeName.substr(1), file);
290 auto& type = typeMap.at(typeName);
292 for (
size_t i = 0; i < type.instructions.size(); ++i)
294 type.instructions[i](file, type.file);
296 if (i == type.instructions.size() - 1)
302 catch (
const std::out_of_range& e)
304 std::cerr <<
"Type not found: " << typeName << std::endl;
307 catch (std::runtime_error& e)
309 std::cerr <<
"Error deserializing type: " << typeName <<
" - "
310 << e.what() << std::endl;
Class used to deserialize the binary logs created using fedetft's logger into csv files.
std::filesystem::path outputDirectory() const
Returns the path to the directory where the log will be decoded.
Deserializer(std::filesystem::path fileName)
Initializes the deserializer with the filename provided.
bool deserialize()
Deserializes the provided file.
void deserializeField(std::ifstream &in, std::ofstream &out)
Driver for the VN100S IMU.
constexpr char MappingMarker
Marker character for a type mapping.