1#ifndef PACKING_UTILITY_READER_H_
2#define PACKING_UTILITY_READER_H_
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);
62 void seek(
int off, std::ios_base::seekdir dir = std::ios::beg) {
63 file_.seekg(off, dir);
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);
95 template <
typename WordType>
97 return tell() /
sizeof(WordType);
112 template <
typename WordType,
113 std::enable_if_t<std::is_integral<WordType>::value,
bool> =
true>
115 file_.
read(
reinterpret_cast<char*
>(w),
sizeof(WordType) * count);
132 template <
typename WordType,
133 std::enable_if_t<std::is_integral<WordType>::value,
bool> =
true>
151 template <
typename ObjectType,
152 std::enable_if_t<std::is_class<ObjectType>::value,
bool> =
true>
154 return o.
read(*
this);
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;
208 operator bool()
const {
return !
file_.fail(); }
Reader(const std::string &file_name)
Constructor that also opens the input file.
int tell()
Tell by number of words.
Reader & operator>>(WordType &w)
Stream the next word into the input handle.
void open(const std::string &file_name)
Open a file with this reader.
std::ifstream file_
file stream we are reading from
~Reader()=default
destructor, close the input file stream
Reader()
default constructor
bool eof()
check if file is done
bool operator!() const
Check if reader is in a fail state.
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Seek by number of words.
Reader & read(std::vector< ContentType > &vec, std::size_t count, std::size_t offset=0)
Read the next 'count' objects into the input vector.
int tell()
Tell us where the reader is.
Reader & operator>>(ObjectType &o)
Stream into a class object.
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
std::size_t file_size_
file size in bytes
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.