byteme
C++ wrappers for buffered inputs
All Classes Namespaces Files Functions Variables Pages
SomeFileReader.hpp
Go to the documentation of this file.
1#ifndef BYTEME_SOME_FILE_READER_HPP
2#define BYTEME_SOME_FILE_READER_HPP
3
4#include <memory>
5#include <cstdio>
6#include <cstddef>
7
8#include "Reader.hpp"
9#include "RawFileReader.hpp"
10#include "GzipFileReader.hpp"
11#include "magic_numbers.hpp"
12
19namespace byteme {
20
29 std::size_t buffer_size = 65536;
30};
31
38class SomeFileReader final : public Reader {
39public:
44 SomeFileReader(const char* path, const SomeFileReaderOptions& options) {
45 unsigned char header[3];
46 std::size_t read;
47 {
48 SelfClosingFILE file(path, "rb");
49 read = std::fread(header, sizeof(unsigned char), 3, file.handle);
50 }
51
52 if (is_gzip(header, read)) {
54 gopt.buffer_size = options.buffer_size;
55 my_source.reset(new GzipFileReader(path, gopt));
56 } else {
58 ropt.buffer_size = options.buffer_size;
59 my_source.reset(new RawFileReader(path, ropt));
60 }
61 }
62
63public:
64 bool load() {
65 return my_source->load();
66 }
67
68 const unsigned char* buffer() const {
69 return my_source->buffer();
70 }
71
72 std::size_t available() const {
73 return my_source->available();
74 }
75
76private:
77 std::unique_ptr<Reader> my_source;
78};
79
80}
81
82#endif
Read a Gzip-compressed file.
Read a file without any extra transformations.
Read an input source.
Read uncompressed bytes from a Gzip-compressed file.
Definition GzipFileReader.hpp:46
Read bytes from a file, usually text.
Definition RawFileReader.hpp:46
Virtual class for reading bytes from a source.
Definition Reader.hpp:17
Read a file that may or may not be Gzipped.
Definition SomeFileReader.hpp:38
SomeFileReader(const char *path, const SomeFileReaderOptions &options)
Definition SomeFileReader.hpp:44
const unsigned char * buffer() const
Definition SomeFileReader.hpp:68
std::size_t available() const
Definition SomeFileReader.hpp:72
bool load()
Definition SomeFileReader.hpp:64
Simple byte readers and writers.
Options for GzipFileReader construction.
Definition GzipFileReader.hpp:26
std::size_t buffer_size
Definition GzipFileReader.hpp:31
Options for the RawFileReader constructor.
Definition RawFileReader.hpp:25
std::size_t buffer_size
Definition RawFileReader.hpp:30
Options for the SomeFileReader constructor.
Definition SomeFileReader.hpp:24
std::size_t buffer_size
Definition SomeFileReader.hpp:29