From 666042c5b7fe1381ec3f5c56db6a8a24ad8b2790 Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Wed, 27 May 2020 11:18:08 +0200 Subject: [PATCH] Implement boilerplate for BufferBinaryReader.cpp --- sf-replay/src/BufferBinaryReader.cpp | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 sf-replay/src/BufferBinaryReader.cpp diff --git a/sf-replay/src/BufferBinaryReader.cpp b/sf-replay/src/BufferBinaryReader.cpp new file mode 100644 index 0000000..15df0d6 --- /dev/null +++ b/sf-replay/src/BufferBinaryReader.cpp @@ -0,0 +1,76 @@ +#include "BufferBinaryReader.hpp" + +#include +#include +#include +#include +#include + +using namespace std; + +BufferBinaryReader::BufferBinaryReader( + const std::string &device, + const std::string &channel_name) : + device_(device), + channel_name_(channel_name), + current_input_file_(""), + input_file_fd_(-1) +{ + +} + +BufferBinaryReader::~BufferBinaryReader() +{ + close_current_file(); +} + +void BufferBinaryReader::get_block( + const uint64_t block_number, BufferBinaryBlock *buffer) +{ + +} + +void BufferBinaryReader::open_file(const std::string& filename) +{ + close_current_file(); + + input_file_fd_ = open(filename.c_str(), O_RDONLY); + + if (input_file_fd_ < 0) { + stringstream err_msg; + + using namespace date; + using namespace chrono; + err_msg << "[" << system_clock::now() << "]"; + err_msg << "[BufferBinaryReader::open_file]"; + err_msg << " Cannot open file "; + err_msg << filename << ": "; + err_msg << strerror(errno) << endl; + + throw runtime_error(err_msg.str()); + } + + current_input_file_ = filename; +} + +void BufferBinaryReader::close_current_file() +{ + if (input_file_fd_ != -1) { + if (close(input_file_fd_) < 0) { + stringstream err_msg; + + using namespace date; + using namespace chrono; + err_msg << "[" << system_clock::now() << "]"; + err_msg << "[BinaryWriter::close_current_file]"; + err_msg << " Error while closing file "; + err_msg << current_input_file_ << ": "; + err_msg << strerror(errno) << endl; + + throw runtime_error(err_msg.str()); + } + + input_file_fd_ = -1; + current_input_file_ = ""; + } +}