byteme
Read/write bytes from various sources
Loading...
Searching...
No Matches
RawFileReader.hpp
Go to the documentation of this file.
1#ifndef BYTEME_RAW_FILE_READER_HPP
2#define BYTEME_RAW_FILE_READER_HPP
3
4#include <vector>
5#include <stdexcept>
6#include <cstdio>
7#include <cstddef>
8#include <optional>
9
10#include "Reader.hpp"
11#include "SelfClosingFILE.hpp"
12
19namespace byteme {
20
30 std::optional<std::size_t> buffer_size;
31};
32
38class RawFileReader final : public Reader {
39public:
44 RawFileReader(const char* path, const RawFileReaderOptions& options) : my_file(path, "rb") {
45 set_optional_bufsiz(my_file, options.buffer_size);
46 }
47
48public:
49 std::size_t read(unsigned char* buffer, std::size_t n) {
50 auto& handle = my_file.handle;
51 auto read = std::fread(buffer, sizeof(unsigned char), n, handle);
52 if (read < n && std::ferror(handle)) {
53 throw std::runtime_error("file read failed");
54 }
55 return read;
56 }
57
58private:
59 SelfClosingFILE my_file;
60};
61
62}
63
64#endif
Read an input source.
Read bytes from a file, usually text.
Definition RawFileReader.hpp:38
std::size_t read(unsigned char *buffer, std::size_t n)
Definition RawFileReader.hpp:49
RawFileReader(const char *path, const RawFileReaderOptions &options)
Definition RawFileReader.hpp:44
Virtual class for reading bytes from a source.
Definition Reader.hpp:17
Simple byte readers and writers.
Definition BufferedReader.hpp:21
Options for the RawFileReader constructor.
Definition RawFileReader.hpp:24
std::optional< std::size_t > buffer_size
Definition RawFileReader.hpp:30