From f6d1db9a28bd265e67512cab88356cd808b9dbba Mon Sep 17 00:00:00 2001 From: Leon Merten Lohse Date: Thu, 3 Oct 2024 14:20:24 +0200 Subject: [PATCH 1/2] implement reading into existing buffer --- include/npy.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/include/npy.hpp b/include/npy.hpp index 3898e87..489c358 100644 --- a/include/npy.hpp +++ b/include/npy.hpp @@ -503,6 +503,43 @@ inline npy_data read_npy(std::istream &in) { return data; } +template +inline npy_data_ptr read_npy(std::istream &in, It dst_buf, size_t count) { + static_assert(is_same::value_type>, + "Scalar and It::value_type must be identical"); + + std::string header_s = read_header(in); + + // parse header + header_t header = parse_header(header_s); + + // check if the typestring matches the given one + const dtype_t dtype = dtype_map.at(std::type_index(typeid(Scalar))); + + if (header.dtype.tie() != dtype.tie()) { + throw std::runtime_error("formatting error: typestrings not matching"); + } + + // compute the data size based on the shape + auto size = static_cast(comp_size(header.shape)); + + if (size > count) { + throw std::runtime_error("dst_buf too small to hold file contents"); + } + + npy_data_ptr data; + + data.shape = header.shape; + data.fortran_order = header.fortran_order; + + data.data_ptr = &(*dst_buf); + + // read the data + in.read(reinterpret_cast(data.data_ptr), sizeof(Scalar) * size); + + return data; +} + template inline npy_data read_npy(const std::string &filename) { std::ifstream stream(filename, std::ifstream::binary); @@ -513,6 +550,16 @@ inline npy_data read_npy(const std::string &filename) { return read_npy(stream); } +template +inline npy_data_ptr read_npy(const std::string &filename, It dst_buf, size_t count) { + std::ifstream stream(filename, std::ifstream::binary); + if (!stream) { + throw std::runtime_error("io error: failed to open a file."); + } + + return read_npy(stream, dst_buf, count); +} + template inline void write_npy(std::ostream &out, const npy_data &data) { // static_assert(has_typestring::value, "scalar type not From 859e6ef32257c73c7e015aba93ee0d60fef4db0e Mon Sep 17 00:00:00 2001 From: Leon Merten Lohse Date: Thu, 3 Oct 2024 14:31:12 +0200 Subject: [PATCH 2/2] fix static_assert --- include/npy.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/npy.hpp b/include/npy.hpp index 489c358..cd5a284 100644 --- a/include/npy.hpp +++ b/include/npy.hpp @@ -505,8 +505,8 @@ inline npy_data read_npy(std::istream &in) { template inline npy_data_ptr read_npy(std::istream &in, It dst_buf, size_t count) { - static_assert(is_same::value_type>, - "Scalar and It::value_type must be identical"); + static_assert(std::is_same::value_type>::value, + "Scalar and It::value_type must be identical"); std::string header_s = read_header(in);