DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
BinarySignalReader.hxx
Go to the documentation of this file.
1
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>
19BinarySignalReader<T>::BinarySignalReader(const std::string& filepath)
20 : m_file(filepath, std::ios::binary), m_eof_reached(false) {
21 if (!m_file.is_open()) {
22 throw std::runtime_error("Failed to open file: " + filepath);
23 }
24}
25
26template<typename T>
27std::vector<T> BinarySignalReader<T>::next(size_t n) {
28 if (m_eof_reached || n == 0) {
29 return std::vector<T>();
30 }
31
32 std::vector<T> result;
33 result.reserve(n);
34
35 for (size_t i = 0; i < n; ++i) {
36 T value;
37 m_file.read(reinterpret_cast<char*>(&value), sizeof(T));
38
39 if (m_file.gcount() != sizeof(T)) {
40 m_eof_reached = true;
41 break;
42 }
43
44 result.push_back(value);
45 }
46
47 return result;
48}
49
50template<typename T>
52 // Check if we're at the end of the file by comparing position with file size
53 std::streampos current_pos = m_file.tellg();
54 m_file.seekg(0, std::ios::end);
55 std::streampos end_pos = m_file.tellg();
56 m_file.seekg(current_pos);
57
58 if (current_pos >= end_pos) {
59 m_eof_reached = true;
60 return true;
61 }
62
63 m_eof_reached = false;
64 return false;
65}
66
67template<typename T>
69 return m_file.tellg();
70}
71
72template<typename T>
73void BinarySignalReader<T>::seekg(std::streampos pos) {
74 m_file.seekg(pos);
75 eof(); // Update m_eof_reached based on actual position
76}
77
78} // namespace testapp
79} // namespace tpglibs
80
81#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.