LDMX Software
Reader.h
1#ifndef PACKING_UTILITY_READER_H_
2#define PACKING_UTILITY_READER_H_
3
4#include <fstream>
5#include <iostream> //debuggin
6#include <string>
7#include <type_traits>
8#include <vector>
9
10namespace packing {
11namespace utility {
12
20class Reader {
21 public:
27 Reader() { file_.unsetf(std::ios::skipws); }
28
36 void open(const std::string& file_name) {
37 file_.open(file_name, std::ios::in | std::ios::binary);
38 file_.seekg(0, std::ios::end);
39 file_size_ = file_.tellg();
40 file_.seekg(0);
41 }
42
48 Reader(const std::string& file_name) : Reader() { this->open(file_name); }
49
51 ~Reader() = default;
52
62 void seek(int off, std::ios_base::seekdir dir = std::ios::beg) {
63 file_.seekg(off, dir);
64 }
65
77 template <typename WordType,
78 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
79 void seek(int off, std::ios_base::seekdir dir = std::ios::beg) {
80 seek(off * sizeof(WordType), dir);
81 }
82
88 int tell() { return file_.tellg(); }
89
95 template <typename WordType>
96 int tell() {
97 return tell() / sizeof(WordType);
98 }
99
112 template <typename WordType,
113 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
114 Reader& read(WordType* w, std::size_t count) {
115 file_.read(reinterpret_cast<char*>(w), sizeof(WordType) * count);
116 return *this;
117 }
118
132 template <typename WordType,
133 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
134 Reader& operator>>(WordType& w) {
135 return read(&w, 1);
136 }
137
151 template <typename ObjectType,
152 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
153 Reader& operator>>(ObjectType& o) {
154 return o.read(*this);
155 }
156
173 template <typename ContentType>
174 Reader& read(std::vector<ContentType>& vec, std::size_t count,
175 std::size_t offset = 0) {
176 vec.resize(offset + count);
177 for (std::size_t i{offset}; i < vec.size(); i++) {
178 if (!(*this >> vec[i])) return *this;
179 }
180 return *this;
181 }
182
191 bool operator!() const { return file_.fail(); }
192
208 operator bool() const { return !file_.fail(); }
209
217 bool eof() { return file_.eof() or file_.tellg() == file_size_; }
218
219 private:
221 std::ifstream file_;
223 std::size_t file_size_;
224}; // RawDataFile
225
226} // namespace utility
227} // namespace packing
228
229#endif // PACKING_UTILITY_READER_H_
Reading a raw data file.
Definition Reader.h:20
Reader(const std::string &file_name)
Constructor that also opens the input file.
Definition Reader.h:48
int tell()
Tell by number of words.
Definition Reader.h:96
Reader & operator>>(WordType &w)
Stream the next word into the input handle.
Definition Reader.h:134
void open(const std::string &file_name)
Open a file with this reader.
Definition Reader.h:36
std::ifstream file_
file stream we are reading from
Definition Reader.h:221
~Reader()=default
destructor, close the input file stream
Reader()
default constructor
Definition Reader.h:27
bool eof()
check if file is done
Definition Reader.h:217
bool operator!() const
Check if reader is in a fail state.
Definition Reader.h:191
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Seek by number of words.
Definition Reader.h:79
Reader & read(std::vector< ContentType > &vec, std::size_t count, std::size_t offset=0)
Read the next 'count' objects into the input vector.
Definition Reader.h:174
int tell()
Tell us where the reader is.
Definition Reader.h:88
Reader & operator>>(ObjectType &o)
Stream into a class object.
Definition Reader.h:153
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
Definition Reader.h:114
std::size_t file_size_
file size in bytes
Definition Reader.h:223
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.
Definition Reader.h:62