9#include "MantidKernel/DllConfig.h"
47 BinaryFile() : handle(nullptr), num_elements(0), offset(0) {}
51 BinaryFile(std::filesystem::path
const &filepath) { this->open(filepath); }
63 void open(std::filesystem::path
const &filepath) {
64 this->handle.reset(
nullptr);
65 if (!std::filesystem::exists(filepath)) {
66 throw std::invalid_argument(std::string(
"BinaryFile::open \"") + filepath.string() +
"\" was not found.");
69 this->handle = std::make_unique<std::ifstream>(filepath, std::ios::binary);
71 this->num_elements = this->getFileSize();
79 void close() { handle.reset(
nullptr); }
108 throw std::runtime_error(
"BinaryFile: file is not open.");
115 size_t buffer_size = getBufferSize(num_elements);
116 auto buffer = std::make_unique<T[]>(buffer_size);
120 handle->seekg(0, std::ios::beg);
122 while (offset < num_elements) {
124 const auto loaded_size = loadBlock(buffer.get(), buffer_size);
126 data.insert(data.end(), buffer.get(), std::next(buffer.get(), loaded_size));
144 throw std::runtime_error(
"BinaryFile: file is not open.");
149 size_t buffer_size = getBufferSize(num_elements);
150 auto buffer =
new T[buffer_size];
154 handle->seekg(0, std::ios::beg);
156 while (offset < num_elements) {
158 const auto loaded_size = loadBlock(buffer, buffer_size);
160 data.insert(data.end(), buffer, (buffer + loaded_size));
185 throw std::runtime_error(
"BinaryFile: file is not open.");
190 loaded_size = block_size;
191 if (offset + loaded_size > num_elements)
192 loaded_size = num_elements - offset;
194 handle->read(
reinterpret_cast<char *
>(buffer), loaded_size * obj_size);
195 offset += loaded_size;
212 size_t loadBlockAt(T *buffer,
size_t newOffset,
size_t block_size) {
214 throw std::runtime_error(
"BinaryFile: file is not open.");
218 handle->seekg(
sizeof(T) * offset, std::ios::beg);
219 return loadBlock(buffer, block_size);
229 this->obj_size =
sizeof(T);
232 throw std::runtime_error(
"BinaryFile::getFileSize: Cannot find the size "
233 "of a file from a null handle");
238 handle->seekg(0, std::ios::end);
239 size_t filesize =
static_cast<size_t>(handle->tellg());
240 handle->seekg(0, std::ios::beg);
243 if (filesize % obj_size != 0) {
244 std::stringstream msg;
245 msg <<
"BinaryFile::getFileSize: File size is not compatible with data "
247 msg << filesize <<
"%" << obj_size <<
"=";
248 msg << filesize % obj_size;
249 throw std::runtime_error(msg.str());
252 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.
BinaryFile(std::filesystem::path const &filepath)
Constructor - open a 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.
void open(std::filesystem::path const &filepath)
Open a file and keep a handle to the file.
size_t getBufferSize(const size_t num_items) const
Get a buffer size for loading blocks of data.
std::vector< T > loadAllIntoVector()
Loads the entire contents of the file into a std::vector.
constexpr size_t MIN_BLOCK_SIZE
Min size of a block (too small is inefficient)
constexpr size_t DEFAULT_BLOCK_SIZE
Default number of items to read in from any of the files.
constexpr 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.