SRS-control 0.1.4
Loading...
Searching...
No Matches
BinaryFileWriter.cpp
Go to the documentation of this file.
5#include <cassert>
6#include <cstddef>
7#include <fmt/format.h>
8#include <fmt/ranges.h>
9#include <ios>
10#include <ranges>
11#include <spdlog/spdlog.h>
12#include <stdexcept>
13#include <string>
14
15namespace srs::writer
16{
17 BinaryFile::BinaryFile(const std::string& filename, process::DataConvertOptions convert_mode, std::size_t n_lines)
18 : WriterTask{ "BinaryWriter", convert_mode, n_lines }
19 , file_name_{ filename }
20 {
21 assert(n_lines > 0);
22 output_data_.resize(n_lines);
23 output_streams_.reserve(n_lines);
24 for (auto idx : std::views::iota(0, static_cast<int>(n_lines)))
25 {
26 auto full_filename = (n_lines == 1) ? filename : common::insert_index_to_filename(filename, idx);
27 auto& ofstream = output_streams_.emplace_back(full_filename, std::ios::trunc);
28 if (not ofstream.is_open())
29 {
30 throw std::runtime_error(fmt::format("Filename {:?} cannot be open!", filename));
31 }
32 }
33 }
34
36 {
37 close();
38 spdlog::debug(
39 "Writer: Binary file {} data size written: \n\t{}",
41 fmt::join(
42 std::views::zip(std::views::iota(0), output_data_) |
43 std::views::transform(
44 [](const auto& idex_data_size) -> std::string
45 { return fmt::format("{}: {}", std::get<0>(idex_data_size), std::get<1>(idex_data_size)); }),
46 "\n\t"));
47 spdlog::info("Writer: Binary file writer with the base name {:?} is closed successfully.", file_name_);
48 }
50 {
51 for (auto& file_stream : output_streams_)
52 {
53 file_stream.close();
54 }
55 }
56} // namespace srs::writer
WriterTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
std::vector< std::ofstream > output_streams_
BinaryFile(const std::string &filename, process::DataConvertOptions convert_mode, std::size_t n_lines)
std::vector< OutputType > output_data_
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string