DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
BinarySignalReader.hxx
Go to the documentation of this file.
1
8
9#ifndef TPGLIBS_TESTAPP_BINARYSIGNALREADER_HXX_
10#define TPGLIBS_TESTAPP_BINARYSIGNALREADER_HXX_
11
13#include <iostream>
14
15namespace tpglibs {
16namespace testapp {
17
18template<typename T>
20 : m_file(filepath, std::ios::binary)
21 , m_eof_reached(false)
22{
23 if (!m_file.is_open()) {
24 throw std::runtime_error("Failed to open file: " + filepath);
25 }
26}
27
28template<typename T>
29std::vector<T>
31{
32 if (m_eof_reached || n == 0) {
33 return std::vector<T>();
34 }
35
36 std::vector<T> result;
37 result.reserve(n);
38
39 for (size_t i = 0; i < n; ++i) {
40 T value;
41 m_file.read(reinterpret_cast<char*>(&value), sizeof(T));
42
43 if (m_file.gcount() != sizeof(T)) {
44 m_eof_reached = true;
45 break;
46 }
47
48 result.push_back(value);
49 }
50
51 return result;
52}
53
54template<typename T>
55bool
57{
58 // Check if we're at the end of the file by comparing position with file size
59 std::streampos current_pos = m_file.tellg();
60 m_file.seekg(0, std::ios::end);
61 std::streampos end_pos = m_file.tellg();
62 m_file.seekg(current_pos);
63
64 if (current_pos >= end_pos) {
65 m_eof_reached = true;
66 return true;
67 }
68
69 m_eof_reached = false;
70 return false;
71}
72
73template<typename T>
74std::streampos
76{
77 return m_file.tellg();
78}
79
80template<typename T>
81void
83{
84 m_file.seekg(pos);
85 eof(); // Update m_eof_reached based on actual position
86}
87
88} // namespace testapp
89} // namespace tpglibs
90
91#endif // TPGLIBS_TESTAPP_BINARYSIGNALREADER_HXX_
std::streampos tellg()
Get current file position.
std::vector< T > next(size_t n)
Read next chunk of data.
void seekg(std::streampos pos)
Seek to specific position.
bool eof()
Check if we've reached end of file.
BinarySignalReader(const std::string &filepath)
Constructor - opens the file for reading.