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
9namespace packing {
10namespace utility {
11
19class Reader {
20 public:
26 Reader() { file_.unsetf(std::ios::skipws); }
27
35 void open(const std::string& file_name) {
36 file_.open(file_name, std::ios::in | std::ios::binary);
37 file_.seekg(0, std::ios::end);
38 file_size_ = file_.tellg();
39 file_.seekg(0);
40 }
41
47 Reader(const std::string& file_name) : Reader() { this->open(file_name); }
48
50 ~Reader() = default;
51
61 void seek(int off, std::ios_base::seekdir dir = std::ios::beg) {
62 file_.seekg(off, dir);
63 }
64
76 template <typename WordType,
77 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
78 void seek(int off, std::ios_base::seekdir dir = std::ios::beg) {
79 seek(off * sizeof(WordType), dir);
80 }
81
87 int tell() { return file_.tellg(); }
88
94 template <typename WordType>
95 int tell() {
96 return tell() / sizeof(WordType);
97 }
98
111 template <typename WordType,
112 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
113 Reader& read(WordType* w, std::size_t count) {
114 file_.read(reinterpret_cast<char*>(w), sizeof(WordType) * count);
115 return *this;
116 }
117
131 template <typename WordType,
132 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
133 Reader& operator>>(WordType& w) {
134 return read(&w, 1);
135 }
136
150 template <typename ObjectType,
151 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
152 Reader& operator>>(ObjectType& o) {
153 return o.read(*this);
154 }
155
171 template <typename ContentType>
172 Reader& read(std::vector<ContentType>& vec, std::size_t count) {
173 vec.resize(count);
174 for (auto& w : vec) {
175 if (!(*this >> w)) return *this;
176 }
177 return *this;
178 }
179
188 bool operator!() const { return file_.fail(); }
189
205 operator bool() const { return !file_.fail(); }
206
214 bool eof() { return file_.eof() or file_.tellg() == file_size_; }
215
216 private:
218 std::ifstream file_;
220 std::size_t file_size_;
221}; // RawDataFile
222
223} // namespace utility
224} // namespace packing
225
226#endif // PACKING_UTILITY_READER_H_
Reading a raw data file.
Definition Reader.h:19
Reader & read(std::vector< ContentType > &vec, std::size_t count)
Read the next 'count' objects into the input vector.
Definition Reader.h:172
Reader(const std::string &file_name)
Constructor that also opens the input file.
Definition Reader.h:47
int tell()
Tell by number of words.
Definition Reader.h:95
Reader & operator>>(WordType &w)
Stream the next word into the input handle.
Definition Reader.h:133
void open(const std::string &file_name)
Open a file with this reader.
Definition Reader.h:35
std::ifstream file_
file stream we are reading from
Definition Reader.h:218
~Reader()=default
destructor, close the input file stream
Reader()
default constructor
Definition Reader.h:26
bool eof()
check if file is done
Definition Reader.h:214
bool operator!() const
Check if reader is in a fail state.
Definition Reader.h:188
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Seek by number of words.
Definition Reader.h:78
int tell()
Tell us where the reader is.
Definition Reader.h:87
Reader & operator>>(ObjectType &o)
Stream into a class object.
Definition Reader.h:152
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
Definition Reader.h:113
std::size_t file_size_
file size in bytes
Definition Reader.h:220
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.
Definition Reader.h:61