From ac83eeff9bba12330595460fa47996e6c1ba9d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Mon, 27 Oct 2025 15:46:31 +0100 Subject: [PATCH] added tell and better error checking to cluster file (#239) - Check feof and ferror when reading frames - added tell member function to ClusterFile --- RELEASE.md | 1 + include/aare/ClusterFile.hpp | 21 ++++++++++++++++++--- python/src/bind_ClusterFile.hpp | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 02aa857..7f7fed8 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -5,6 +5,7 @@ Bugfixes: - File supports reading new master json file format (multiple ROI's not supported yet) +- Added tell to ClusterFile. Returns position in bytes for debugging ### 2025.8.22 diff --git a/include/aare/ClusterFile.hpp b/include/aare/ClusterFile.hpp index 1f6961a..ccdace3 100644 --- a/include/aare/ClusterFile.hpp +++ b/include/aare/ClusterFile.hpp @@ -189,6 +189,16 @@ class ClusterFile { } } + /** + * @brief Return the current position in the file (bytes) + */ + int64_t tell() { + if (!fp) { + throw std::runtime_error(LOCATION + "File not opened"); + } + return ftell(fp); + } + /** @brief Open the file in specific mode * */ @@ -354,15 +364,20 @@ template ClusterVector ClusterFile::read_frame_without_cut() { if (m_mode != "r") { - throw std::runtime_error("File not opened for reading"); + throw std::runtime_error(LOCATION + "File not opened for reading"); } if (m_num_left) { throw std::runtime_error( - "There are still photons left in the last frame"); + LOCATION + "There are still photons left in the last frame"); } int32_t frame_number; if (fread(&frame_number, sizeof(frame_number), 1, fp) != 1) { - throw std::runtime_error(LOCATION + "Could not read frame number"); + if (feof(fp)) + throw std::runtime_error(LOCATION + "Unexpected end of file"); + else if (ferror(fp)) + throw std::runtime_error(LOCATION + "Error reading from file"); + + throw std::runtime_error(LOCATION + "Unexpected error (not feof or ferror) when reading frame number"); } int32_t n_clusters; // Saved as 32bit integer in the cluster file diff --git a/python/src/bind_ClusterFile.hpp b/python/src/bind_ClusterFile.hpp index 5a707a4..f4a692c 100644 --- a/python/src/bind_ClusterFile.hpp +++ b/python/src/bind_ClusterFile.hpp @@ -45,6 +45,7 @@ void define_ClusterFile(py::module &m, const std::string &typestr) { return v; }) .def("set_roi", &ClusterFile::set_roi, py::arg("roi")) + .def("tell", &ClusterFile::tell) .def( "set_noise_map", [](ClusterFile &self, py::array_t noise_map) {