srs-control 0.2.2
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::sink
16{
17 BinaryFile::BinaryFile(const std::string& filename, process::DataConvertOptions convert_mode, std::size_t n_lines)
18 : SinkTask{ "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
39 if (auto* report = get_report(); report != nullptr)
40 {
41 report->register_output_sink_result(file_name_, output_data_);
42 }
43 spdlog::info("Writer: Binary file writer with the base name {:?} is closed successfully.", file_name_);
44 }
45
47 {
48 for (auto& file_stream : output_streams_)
49 {
50 file_stream.close();
51 }
52 }
53} // namespace srs::sink
SinkTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition BaseTask.hpp:109
std::vector< OutputType > output_data_
BinaryFile(const std::string &filename, process::DataConvertOptions convert_mode, std::size_t n_lines)
std::vector< std::ofstream > output_streams_
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string