1
0
Fork 0
mirror of https://github.com/gwm17/DaqGrimoire.git synced 2024-11-30 06:08:49 -05:00
DaqGrimoire/include/FileIO/DYFileReader.h

139 lines
3.1 KiB
C
Raw Normal View History

2022-09-29 20:36:44 -04:00
#ifndef DY_FILE_READER_H
#define DY_FILE_READER_H
#include "../DYListData.h"
#include <filesystem>
#include <memory>
#include <fstream>
#include <vector>
namespace DaqGrimoire {
class DYFileReader
{
public:
DYFileReader() :
2022-09-29 20:04:36 -04:00
m_fileHandle(nullptr), m_bufferSizeEvents(200000), m_isEOF(false), m_isHitUsed(false), m_fileSizeBytes(0), m_fileSizeEvents(0), m_bufferIter(nullptr), m_bufferEnd(nullptr)
2022-09-29 20:36:44 -04:00
{
}
DYFileReader(std::size_t bufferSize) :
2022-09-29 20:04:36 -04:00
m_fileHandle(nullptr), m_bufferSizeEvents(bufferSize), m_isEOF(false), m_isHitUsed(false), m_fileSizeBytes(0), m_fileSizeEvents(0), m_bufferIter(nullptr), m_bufferEnd(nullptr)
2022-09-29 20:36:44 -04:00
{
}
DYFileReader(const std::filesystem::path& filepath, std::size_t bufferSize = 200000) :
2022-09-29 20:04:36 -04:00
m_fileHandle(nullptr), m_bufferSizeEvents(bufferSize), m_isEOF(false), m_isHitUsed(false), m_fileSizeBytes(0), m_fileSizeEvents(0), m_bufferIter(nullptr), m_bufferEnd(nullptr)
2022-09-29 20:36:44 -04:00
{
Open(filepath);
}
~DYFileReader()
{
Close();
}
void Open(const std::filesystem::path& filepath)
{
m_filepath = filepath;
m_fileHandle->open(filepath, std::ios::binary | std::ios::in);
m_fileHandle->seekg(0, std::ios_base::end);
m_fileSizeBytes = m_fileHandle->tellg();
//Replace this
if (m_fileSizeBytes == 0 || m_fileSizeBytes < Data::Size)
{
m_isEOF = true;
return;
}
else if (m_fileSizeBytes % Data::Size != 0)
{
m_isEOF = true;
return;
}
m_fileSizeEvents = m_fileSizeBytes / Data::Size;
m_rawBuffer.resize(m_bufferSizeEvents * Data::Size);
}
void Close()
{
if (IsOpen())
m_fileHandle->close();
}
bool ReadNextEvent()
2022-09-29 20:36:44 -04:00
{
if (!IsOpen() || IsEOF())
return false;
if (m_bufferIter == m_bufferEnd)
{
FillBuffer();
if (IsEOF())
return false;
}
Utils::GetDataEventFromBuffer(m_bufferIter, m_dataEvent);
2022-09-29 20:36:44 -04:00
2022-09-29 20:04:36 -04:00
m_isHitUsed = false;
2022-09-29 20:36:44 -04:00
return true;
}
2022-10-01 09:45:00 -04:00
const DYListData& GetCurrentEvent() const
2022-09-29 20:04:36 -04:00
{
return m_dataEvent;
}
2022-09-29 21:04:30 -04:00
void SetHitUsed()
{
m_isHitUsed = true;
}
2022-09-29 20:36:44 -04:00
const bool IsOpen() const { return m_fileHandle == nullptr ? false : m_fileHandle->is_open(); }
const bool IsEOF() const { return m_isEOF; }
2022-09-29 20:04:36 -04:00
const bool IsHitUsed() const { return m_isHitUsed; }
2022-09-29 20:36:44 -04:00
const std::size_t GetFileSizeBytes() const { return m_fileSizeBytes; }
const std::size_t GetFileSizeEvents() const { return m_fileSizeEvents; }
2022-09-29 19:42:02 -04:00
const std::filesystem::path& GetFilePath() const { return m_filepath; }
2022-09-29 20:36:44 -04:00
private:
void FillBuffer()
{
if (m_fileHandle->eof())
{
m_isEOF = true;
return;
}
m_fileHandle->read(m_rawBuffer.data(), m_rawBuffer.size());
m_bufferIter = m_rawBuffer.data();
m_bufferEnd = m_bufferIter + m_fileHandle->gcount(); //one past the last datum
}
std::filesystem::path m_filepath;
std::shared_ptr<std::ifstream> m_fileHandle;
std::vector<char> m_rawBuffer;
std::size_t m_bufferSizeEvents; //in units of data events
std::size_t m_fileSizeBytes; //in bytes
std::size_t m_fileSizeEvents; //in data events
DYListData m_dataEvent;
2022-09-29 20:36:44 -04:00
bool m_isEOF;
2022-09-29 20:04:36 -04:00
bool m_isHitUsed;
2022-09-29 20:36:44 -04:00
char* m_bufferIter;
char* m_bufferEnd;
};
}
#endif