byteme
C++ wrappers for buffered inputs
Loading...
Searching...
No Matches
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 "Reader.hpp"
5#include "RawFileReader.hpp"
6#include "GzipFileReader.hpp"
7#include "magic_numbers.hpp"
8#include <memory>
9#include <cstdio>
10
17namespace byteme {
18
25class SomeFileReader : public Reader {
26public:
31 SomeFileReader(const char* path, size_t buffer_size = 65536) {
32 unsigned char header[3];
33 size_t read;
34 {
35 SelfClosingFILE file(path, "rb");
36 read = std::fread(header, sizeof(unsigned char), 3, file.handle);
37 }
38
39 if (is_gzip(header, read)) {
40 my_source.reset(new GzipFileReader(path, buffer_size));
41 } else {
42 my_source.reset(new RawFileReader(path, buffer_size));
43 }
44 }
45
50 SomeFileReader(const std::string& path, size_t buffer_size = 65536) : SomeFileReader(path.c_str(), buffer_size) {}
51
52public:
53 bool load() {
54 return my_source->load();
55 }
56
57 const unsigned char* buffer() const {
58 return my_source->buffer();
59 }
60
61 size_t available() const {
62 return my_source->available();
63 }
64
65private:
66 std::unique_ptr<Reader> my_source;
67};
68
69}
70
71#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:24
Read bytes from a file, usually text.
Definition RawFileReader.hpp:25
Virtual class for reading bytes from a source.
Definition Reader.hpp:15
Read a file that may or may not be Gzipped.
Definition SomeFileReader.hpp:25
SomeFileReader(const std::string &path, size_t buffer_size=65536)
Definition SomeFileReader.hpp:50
const unsigned char * buffer() const
Definition SomeFileReader.hpp:57
SomeFileReader(const char *path, size_t buffer_size=65536)
Definition SomeFileReader.hpp:31
bool load()
Definition SomeFileReader.hpp:53
size_t available() const
Definition SomeFileReader.hpp:61
Simple byte readers and writers.