SRS-control 0.1.4
Loading...
Searching...
No Matches
RawFrameReader.cpp
Go to the documentation of this file.
2#include <array>
3#include <cstddef>
4#include <cstdint>
5#include <expected>
6#include <fmt/format.h>
7#include <fstream>
8#include <ios>
9#include <spdlog/spdlog.h>
11#include <stdexcept>
12#include <string>
13#include <string_view>
14#include <vector>
15#include <zpp_bits.h>
16
17namespace srs::reader
18{
19
20 RawFrame::RawFrame(const std::string& filename)
21 : input_filename_{ filename }
22 , input_file_{ filename, std::ios::binary }
23 {
24 if (input_file_.fail())
25 {
26 throw std::runtime_error{ fmt::format("Cannot open the file {:?}", input_filename_) };
27 }
28 spdlog::debug("Open the binary file {:?}", input_filename_);
29 }
30
31 auto RawFrame::read_one_frame(std::vector<char>& binary_data, std::ifstream& input_file)
32 -> std::expected<std::size_t, std::string>
33 {
34 if (not input_file.is_open())
35 {
36 return std::unexpected(fmt::format("The input file {} is not open!", input_filename_));
37 }
38 binary_data.reserve(common::LARGE_READ_MSG_BUFFER_SIZE);
39 std::array<char, sizeof(common::RawDelimSizeType)> size_buffer{};
40 auto size = common::RawDelimSizeType{};
41 if (input_file.eof())
42 {
43 return 0;
44 // return std::unexpected("End of the binary file.");
45 }
46
47 input_file.read(size_buffer.data(), static_cast<int64_t>(size_buffer.size()));
48 auto serialize_to = zpp::bits::in{ size_buffer, zpp::bits::endian::big{} };
49 serialize_to(size).or_throw();
50 binary_data.resize(size, 0);
51 auto read_size = static_cast<std::size_t>(input_file.read(binary_data.data(), size).gcount());
52
53 if (read_size != size)
54 {
55 spdlog::warn("Binary Data is not extracted correctly from the input file");
56 }
57
58 return read_size;
59 }
60
62 {
63 input_file_.clear();
64 input_file_.seekg(0, std::ios::beg);
65 }
66
67 auto RawFrame::read_one_frame() -> std::expected<std::string_view, std::string>
68 {
70 if (res.has_value())
71 {
72 return std::string_view{ input_buffer_.data(), input_buffer_.size() };
73 }
74 return std::unexpected{ res.error() };
75 }
76} // namespace srs::reader
auto read_one_frame(std::vector< char > &binary_data, std::ifstream &input_file) -> std::expected< std::size_t, std::string >
Read one frame of the bianry data to a vector.
std::string input_filename_
Input binary file name.
std::vector< char > input_buffer_
internal binary data buffer
RawFrame()=default
Default Constructor. No memory allocation.
std::ifstream input_file_
Input binary file handler.
uint32_t RawDelimSizeType
constexpr auto LARGE_READ_MSG_BUFFER_SIZE
size of the data array for reading a UDP package