9#include "MantidKernel/DllConfig.h"
48 BinaryFile() : handle(nullptr), num_elements(0), offset(0) {}
52 BinaryFile(std::string filename) { this->open(filename); }
64 void open(
const std::string &filename) {
65 this->handle.reset(
nullptr);
66 if (!Poco::File(filename).
exists()) {
67 std::stringstream msg;
68 msg <<
"BinaryFile::open: File " << filename <<
" was not found.";
69 throw std::invalid_argument(
"File does not exist.");
72 this->handle = std::make_unique<std::ifstream>(filename.c_str(), std::ios::binary);
74 this->num_elements = this->getFileSize();
82 void close() { handle.reset(
nullptr); }
111 throw std::runtime_error(
"BinaryFile: file is not open.");
118 size_t buffer_size = getBufferSize(num_elements);
119 auto buffer = std::make_unique<T[]>(buffer_size);
123 handle->seekg(0, std::ios::beg);
125 while (offset < num_elements) {
127 const auto loaded_size = loadBlock(buffer.get(), buffer_size);
129 data.insert(data.end(), buffer.get(), std::next(buffer.get(), loaded_size));
147 throw std::runtime_error(
"BinaryFile: file is not open.");
152 size_t buffer_size = getBufferSize(num_elements);
153 auto buffer =
new T[buffer_size];
157 handle->seekg(0, std::ios::beg);
159 while (offset < num_elements) {
161 const auto loaded_size = loadBlock(buffer, buffer_size);
163 data.insert(data.end(), buffer, (buffer + loaded_size));
188 throw std::runtime_error(
"BinaryFile: file is not open.");
193 loaded_size = block_size;
194 if (offset + loaded_size > num_elements)
195 loaded_size = num_elements - offset;
197 handle->read(
reinterpret_cast<char *
>(buffer), loaded_size * obj_size);
198 offset += loaded_size;
215 size_t loadBlockAt(T *buffer,
size_t newOffset,
size_t block_size) {
217 throw std::runtime_error(
"BinaryFile: file is not open.");
221 handle->seekg(
sizeof(T) * offset, std::ios::beg);
222 return loadBlock(buffer, block_size);
232 this->obj_size =
sizeof(T);
235 throw std::runtime_error(
"BinaryFile::getFileSize: Cannot find the size "
236 "of a file from a null handle");
241 handle->seekg(0, std::ios::end);
242 size_t filesize =
static_cast<size_t>(handle->tellg());
243 handle->seekg(0, std::ios::beg);
246 if (filesize % obj_size != 0) {
247 std::stringstream msg;
248 msg <<
"BinaryFile::getFileSize: File size is not compatible with data "
250 msg << filesize <<
"%" << obj_size <<
"=";
251 msg << filesize % obj_size;
252 throw std::runtime_error(msg.str());
255 return filesize /
sizeof(T);
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
The BinaryFile template is a helper function for loading simple binary files.
void open(const std::string &filename)
Open a file and keep a handle to the file.
size_t getFileSize()
Get the size of a file as a multiple of a particular data type.
size_t loadBlock(T *buffer, size_t block_size)
Loads a single block from file and returns a pointer to a vector containing it.
size_t obj_size
Size of each object.
size_t getNumElements() const
Returns the # of elements in the file (cached result of getFileSize)
BinaryFile()
Empty constructor.
std::unique_ptr< std::ifstream > handle
File stream.
size_t offset
Offset into the file, if loading in blocks.
size_t getOffset() const
Returns the current offset into the file.
size_t loadBlockAt(T *buffer, size_t newOffset, size_t block_size)
Loads a single block from file and returns a pointer to a vector containing it.
std::vector< T > loadAll()
Loads the entire contents of the file into a pointer to a std::vector.
void close()
Close the file.
~BinaryFile()
Destructor, close the file if needed.
size_t num_elements
Number of elements of size T in the file.
size_t getBufferSize(const size_t num_items) const
Get a buffer size for loading blocks of data.
BinaryFile(std::string filename)
Constructor - open a file.
std::vector< T > loadAllIntoVector()
Loads the entire contents of the file into a std::vector.
bool exists(::NeXus::File &file, const std::string &name)
Based on the current group in the file, does the named sub-entry exist?
static const size_t DEFAULT_BLOCK_SIZE
Default number of items to read in from any of the files.
static const size_t MIN_BLOCK_SIZE
Min size of a block (too small is inefficient)
static const size_t MAX_BLOCK_SIZE
Max size block to read from a file (memory limitations)
Helper class which provides the Collimation Length for SANS instruments.