From 65c8f2c7d8840319c372739f3776820fe319016f Mon Sep 17 00:00:00 2001 From: AliceMazzoleni99 Date: Tue, 17 Feb 2026 17:11:33 +0100 Subject: [PATCH] dev/handes trailing / in filepath (#1391) * member filePath in Implementation is std::filesystem::path, some refactoring * PR Review * adapted function signature - compiled with hdf5 on --- slsReceiverSoftware/src/BinaryDataFile.cpp | 18 ++++---- slsReceiverSoftware/src/BinaryDataFile.h | 4 +- slsReceiverSoftware/src/DataProcessor.cpp | 11 +++-- slsReceiverSoftware/src/DataProcessor.h | 8 ++-- slsReceiverSoftware/src/File.h | 13 +++--- slsReceiverSoftware/src/HDF5DataFile.cpp | 13 ++++-- slsReceiverSoftware/src/HDF5DataFile.h | 7 +-- slsReceiverSoftware/src/Implementation.cpp | 45 ++++++++++-------- slsReceiverSoftware/src/Implementation.h | 3 +- slsReceiverSoftware/src/MasterFileUtility.cpp | 46 ++++++++++--------- slsReceiverSoftware/src/MasterFileUtility.h | 7 +-- 11 files changed, 100 insertions(+), 75 deletions(-) diff --git a/slsReceiverSoftware/src/BinaryDataFile.cpp b/slsReceiverSoftware/src/BinaryDataFile.cpp index 4f5b90f4d..bc8438277 100644 --- a/slsReceiverSoftware/src/BinaryDataFile.cpp +++ b/slsReceiverSoftware/src/BinaryDataFile.cpp @@ -19,16 +19,15 @@ void BinaryDataFile::CloseFile() { fd = nullptr; } -void BinaryDataFile::CreateFirstBinaryDataFile(const std::string &fNamePrefix, - const uint64_t fIndex, - const bool ovEnable, - const bool sMode, - const uint16_t uPortNumber, - const uint32_t mFramesPerFile) { +void BinaryDataFile::CreateFirstBinaryDataFile( + const std::filesystem::path &filePath, const std::string &fNamePrefix, + const uint64_t fIndex, const bool ovEnable, const bool sMode, + const uint16_t uPortNumber, const uint32_t mFramesPerFile) { subFileIndex = 0; numFramesInFile = 0; + m_filePath = filePath; fileNamePrefix = fNamePrefix; fileIndex = fIndex; overWriteEnable = ovEnable; @@ -42,9 +41,10 @@ void BinaryDataFile::CreateFirstBinaryDataFile(const std::string &fNamePrefix, void BinaryDataFile::CreateFile() { numFramesInFile = 0; - std::ostringstream os; - os << fileNamePrefix << "_f" << subFileIndex << '_' << fileIndex << ".raw"; - fileName = os.str(); + std::filesystem::path p = + m_filePath / (fileNamePrefix + "_f" + std::to_string(subFileIndex) + + '_' + std::to_string(fileIndex) + ".raw"); + fileName = p.string(); if (!overWriteEnable) { if (nullptr == (fd = fopen(fileName.c_str(), "wx"))) { diff --git a/slsReceiverSoftware/src/BinaryDataFile.h b/slsReceiverSoftware/src/BinaryDataFile.h index 79daffa61..5fc43f4cb 100644 --- a/slsReceiverSoftware/src/BinaryDataFile.h +++ b/slsReceiverSoftware/src/BinaryDataFile.h @@ -14,7 +14,8 @@ class BinaryDataFile : private virtual slsDetectorDefs, public File { fileFormat GetFileFormat() const override; void CloseFile() override; - void CreateFirstBinaryDataFile(const std::string &fNamePrefix, + void CreateFirstBinaryDataFile(const std::filesystem::path &filePath, + const std::string &fNamePrefix, const uint64_t fIndex, const bool ovEnable, const bool sMode, const uint16_t uPortNumber, const uint32_t mFramesPerFile) override; @@ -29,6 +30,7 @@ class BinaryDataFile : private virtual slsDetectorDefs, public File { uint32_t index; FILE *fd{nullptr}; std::string fileName; + std::filesystem::path m_filePath; uint32_t numFramesInFile{0}; uint32_t subFileIndex{0}; diff --git a/slsReceiverSoftware/src/DataProcessor.cpp b/slsReceiverSoftware/src/DataProcessor.cpp index 8631af36c..e6303c188 100644 --- a/slsReceiverSoftware/src/DataProcessor.cpp +++ b/slsReceiverSoftware/src/DataProcessor.cpp @@ -148,7 +148,8 @@ void DataProcessor::SetupFileWriter(const bool filewriteEnable, } } -void DataProcessor::CreateFirstFiles(const std::string &fileNamePrefix, +void DataProcessor::CreateFirstFiles(const std::filesystem::path &filePath, + const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, @@ -178,14 +179,14 @@ void DataProcessor::CreateFirstFiles(const std::string &fileNamePrefix, #ifdef HDF5C case HDF5: dataFile->CreateFirstHDF5DataFile( - fileNamePrefix, fileIndex, overWriteEnable, silentMode, + filePath, fileNamePrefix, fileIndex, overWriteEnable, silentMode, udpPortNumber, generalData->framesPerFile, nTotalFrames, nx, ny, generalData->dynamicRange); break; #endif case BINARY: dataFile->CreateFirstBinaryDataFile( - fileNamePrefix, fileIndex, overWriteEnable, silentMode, + filePath, fileNamePrefix, fileIndex, overWriteEnable, silentMode, udpPortNumber, generalData->framesPerFile); break; default: @@ -203,7 +204,7 @@ uint32_t DataProcessor::GetFilesInAcquisition() const { } std::string DataProcessor::CreateVirtualFile( - const std::string &filePath, const std::string &fileNamePrefix, + const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, const int modulePos, const int numModX, const int numModY, std::mutex *hdf5LibMutex, bool gotthard25um) { @@ -247,7 +248,7 @@ void DataProcessor::LinkFileInMaster(const std::string &masterFileName, #endif std::string DataProcessor::CreateMasterFile( - const std::string &filePath, const std::string &fileNamePrefix, + const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, bool silentMode, const fileFormat fileFormatType, MasterAttributes *attr, std::mutex *hdf5LibMutex) { diff --git a/slsReceiverSoftware/src/DataProcessor.h b/slsReceiverSoftware/src/DataProcessor.h index dfed87819..9aef85445 100644 --- a/slsReceiverSoftware/src/DataProcessor.h +++ b/slsReceiverSoftware/src/DataProcessor.h @@ -15,6 +15,7 @@ #include "receiver_defs.h" #include +#include #include #include @@ -59,12 +60,13 @@ class DataProcessor : private virtual slsDetectorDefs, public ThreadObject { const fileFormat fileFormatType, std::mutex *hdf5LibMutex); - void CreateFirstFiles(const std::string &fileNamePrefix, + void CreateFirstFiles(const std::filesystem::path &filePath, + const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, const bool detectorDataStream); #ifdef HDF5C uint32_t GetFilesInAcquisition() const; - std::string CreateVirtualFile(const std::string &filePath, + std::string CreateVirtualFile(const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, @@ -76,7 +78,7 @@ class DataProcessor : private virtual slsDetectorDefs, public ThreadObject { const bool silentMode, std::mutex *hdf5LibMutex); #endif - std::string CreateMasterFile(const std::string &filePath, + std::string CreateMasterFile(const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, bool silentMode, diff --git a/slsReceiverSoftware/src/File.h b/slsReceiverSoftware/src/File.h index c30a86238..18bc32496 100644 --- a/slsReceiverSoftware/src/File.h +++ b/slsReceiverSoftware/src/File.h @@ -6,6 +6,7 @@ #include "sls/sls_detector_defs.h" #include +#include #ifdef HDF5C #include "H5Cpp.h" @@ -56,6 +57,7 @@ class File : private virtual slsDetectorDefs { }; virtual void CreateFirstHDF5DataFile( + const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, const uint16_t udpPortNumber, const uint32_t maxFramesPerFile, @@ -66,12 +68,11 @@ class File : private virtual slsDetectorDefs { "should be overloaded by a derived class"; }; #endif - virtual void CreateFirstBinaryDataFile(const std::string &fileNamePrefix, - const uint64_t fileIndex, - const bool overWriteEnable, - const bool silentMode, - const uint16_t udpPortNumber, - const uint32_t maxFramesPerFile) { + virtual void CreateFirstBinaryDataFile( + const std::filesystem::path &filePath, + const std::string &fileNamePrefix, const uint64_t fileIndex, + const bool overWriteEnable, const bool silentMode, + const uint16_t udpPortNumber, const uint32_t maxFramesPerFile) { LOG(logERROR) << "This is a generic function CreateFirstBinaryDataFile that " "should be overloaded by a derived class"; diff --git a/slsReceiverSoftware/src/HDF5DataFile.cpp b/slsReceiverSoftware/src/HDF5DataFile.cpp index 2b267f474..8dc2f4598 100644 --- a/slsReceiverSoftware/src/HDF5DataFile.cpp +++ b/slsReceiverSoftware/src/HDF5DataFile.cpp @@ -87,8 +87,9 @@ void HDF5DataFile::CloseFile() { } void HDF5DataFile::CreateFirstHDF5DataFile( - const std::string &fNamePrefix, const uint64_t fIndex, const bool owEnable, - const bool sMode, const uint16_t uPortNumber, const uint32_t mFramesPerFile, + const std::filesystem::path &filePath, const std::string &fNamePrefix, + const uint64_t fIndex, const bool owEnable, const bool sMode, + const uint16_t uPortNumber, const uint32_t mFramesPerFile, const uint64_t nImages, const uint32_t nX, const uint32_t nY, const uint32_t dr) { @@ -103,6 +104,7 @@ void HDF5DataFile::CreateFirstHDF5DataFile( nPixelsY = nY; dynamicRange = dr; + m_filePath = filePath; fileNamePrefix = fNamePrefix; fileIndex = fIndex; overWriteEnable = owEnable; @@ -129,9 +131,10 @@ void HDF5DataFile::CreateFile() { numFramesInFile = 0; numFilesInAcquisition++; - std::ostringstream os; - os << fileNamePrefix << "_f" << subFileIndex << '_' << fileIndex << ".h5"; - fileName = os.str(); + std::filesystem::path p = + m_filePath / (fileNamePrefix + "_f" + std::to_string(subFileIndex) + + '_' + std::to_string(fileIndex) + ".h5"); + fileName = p.string(); std::lock_guard lock(*hdf5Lib); diff --git a/slsReceiverSoftware/src/HDF5DataFile.h b/slsReceiverSoftware/src/HDF5DataFile.h index e3a7b19b3..06dabe2d2 100644 --- a/slsReceiverSoftware/src/HDF5DataFile.h +++ b/slsReceiverSoftware/src/HDF5DataFile.h @@ -3,7 +3,6 @@ #pragma once #include "File.h" - #include namespace sls { @@ -23,7 +22,8 @@ class HDF5DataFile : private virtual slsDetectorDefs, public File { void CloseFile() override; - void CreateFirstHDF5DataFile(const std::string &fNamePrefix, + void CreateFirstHDF5DataFile(const std::filesystem::path &filePath, + const std::string &fNamePrefix, const uint64_t fIndex, const bool owEnable, const bool sMode, const uint16_t uPortNumber, const uint32_t mFramesPerFile, @@ -65,7 +65,8 @@ class HDF5DataFile : private virtual slsDetectorDefs, public File { uint32_t nPixelsY{0}; uint32_t dynamicRange{0}; - std::string fileNamePrefix; + std::filesystem::path m_filePath{}; + std::string fileNamePrefix{}; uint64_t fileIndex{0}; bool overWriteEnable{false}; bool silentMode{false}; diff --git a/slsReceiverSoftware/src/Implementation.cpp b/slsReceiverSoftware/src/Implementation.cpp index 84b248835..292c5df29 100644 --- a/slsReceiverSoftware/src/Implementation.cpp +++ b/slsReceiverSoftware/src/Implementation.cpp @@ -483,10 +483,14 @@ void Implementation::setFileFormat(const fileFormat f) { LOG(logINFO) << "File Format: " << ToString(fileFormatType); } -std::string Implementation::getFilePath() const { return filePath; } +std::string Implementation::getFilePath() const { return filePath.string(); } void Implementation::setFilePath(const std::string &c) { - filePath = c; + // check if filePath empty and throw error + if (c.empty()) { + throw ReceiverError("File path cannot be empty"); + } + filePath = std::filesystem::path(c); LOG(logINFO) << "File path: " << filePath; } @@ -651,7 +655,7 @@ void Implementation::startReceiver() { generalData->dynamicRange, numPorts, static_cast(generalData->imageSize), - filePath, + filePath.string(), fileName, fileIndex, quadEnable, @@ -851,9 +855,7 @@ void Implementation::ResetParametersforNewAcquisition() { it->ResetParametersforNewAcquisition(); if (dataStreamEnable) { - std::ostringstream os; - os << filePath << '/' << fileName; - std::string fnametostream = os.str(); + std::string fnametostream = (filePath / fileName).string(); for (const auto &it : dataStreamer) it->ResetParametersforNewAcquisition(fnametostream); } @@ -874,21 +876,28 @@ void Implementation::CreateUDPSockets() { void Implementation::SetupWriter() { try { - // check if filePath empty and throw error - if (filePath.empty()) { - throw ReceiverError("File path cannot be empty"); - } // check if folder exists and throw if it cant create - mkdir_p(filePath); + if (filePath.empty()) { + throw RuntimeError("File path cannot be empty. Please set file " + "path before starting acquisition."); + } + if (!std::filesystem::exists(filePath)) { + try { + std::filesystem::create_directories(filePath); + } catch (const std::filesystem::filesystem_error &e) { + throw RuntimeError("Could not create directory: " + + filePath.string() + ". Error: " + e.what()); + } + } // create first files for (unsigned int i = 0; i < dataProcessor.size(); ++i) { - std::ostringstream os; - os << filePath << "/" << fileName << "_d" - << (modulePos * generalData->numUDPInterfaces + i); - std::string fileNamePrefix = os.str(); - dataProcessor[i]->CreateFirstFiles(fileNamePrefix, fileIndex, - overwriteEnable, silentMode, - detectorDataStream[i]); + + std::string fileNamePrefix = + fileName + "_d" + + std::to_string(modulePos * generalData->numUDPInterfaces + i); + dataProcessor[i]->CreateFirstFiles( + filePath, fileNamePrefix, fileIndex, overwriteEnable, + silentMode, detectorDataStream[i]); } } catch (const RuntimeError &e) { shutDownUDPSockets(); diff --git a/slsReceiverSoftware/src/Implementation.h b/slsReceiverSoftware/src/Implementation.h index 2699bbfc4..3680cb84e 100644 --- a/slsReceiverSoftware/src/Implementation.h +++ b/slsReceiverSoftware/src/Implementation.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -316,7 +317,7 @@ class Implementation : private virtual slsDetectorDefs { // file parameters fileFormat fileFormatType{BINARY}; - std::string filePath{}; + std::filesystem::path filePath{}; std::string fileName{"run"}; uint64_t fileIndex{0}; bool fileWriteEnable{false}; diff --git a/slsReceiverSoftware/src/MasterFileUtility.cpp b/slsReceiverSoftware/src/MasterFileUtility.cpp index a4e782744..ffead18a9 100644 --- a/slsReceiverSoftware/src/MasterFileUtility.cpp +++ b/slsReceiverSoftware/src/MasterFileUtility.cpp @@ -9,16 +9,17 @@ namespace sls { namespace masterFileUtility { -std::string CreateMasterBinaryFile(const std::string &filePath, +std::string CreateMasterBinaryFile(const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, MasterAttributes *attr) { - std::ostringstream os; - os << filePath << "/" << fileNamePrefix << "_master" - << "_" << fileIndex << ".json"; - std::string fileName = os.str(); + + std::filesystem::path p = filePath / (fileNamePrefix + "_master_" + + std::to_string(fileIndex) + ".json"); + + std::string fileName = p.string(); std::string mode = "w"; if (!overWriteEnable) @@ -112,17 +113,16 @@ void LinkHDF5FileInMaster(std::string &masterFileName, } } -std::string CreateMasterHDF5File(const std::string &filePath, +std::string CreateMasterHDF5File(const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, MasterAttributes *attr, std::mutex *hdf5LibMutex) { - std::ostringstream os; - os << filePath << "/" << fileNamePrefix << "_master" - << "_" << fileIndex << ".h5"; - std::string fileName = os.str(); + std::filesystem::path p = filePath / (fileNamePrefix + "_master_" + + std::to_string(fileIndex) + ".json"); + std::string fileName = p.string(); std::lock_guard lock(*hdf5LibMutex); @@ -185,7 +185,7 @@ int GetNumPortsInRoi(const defs::ROI roi, const defs::xy portSize) { /** Will not be called if dynamic range is 4 and roi enabled */ std::string CreateVirtualHDF5File( - const std::string &filePath, const std::string &fileNamePrefix, + const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, const int modulePos, const int numUnitsPerReadout, const uint32_t maxFramesPerFile, const int nPixelsX, const int nPixelsY, @@ -202,10 +202,11 @@ std::string CreateVirtualHDF5File( } // virtual file name - std::ostringstream osfn; - osfn << filePath << "/" << fileNamePrefix << "_virtual" - << "_" << fileIndex << ".h5"; - std::string fileName = osfn.str(); + std::filesystem::path p = filePath / (fileNamePrefix + "_virtual_" + + std::to_string(fileIndex) + ".h5"); + + std::string fileName = p.string(); + unsigned int paraSize = parameterNames.size(); std::lock_guard lock(*hdf5LibMutex); std::unique_ptr fd{nullptr}; @@ -331,12 +332,15 @@ std::string CreateVirtualHDF5File( H5S_SELECT_SET, numBlocksPara, startLocationPara, strideBetweenBlocksPara, blockSizePara); - // source file name - std::ostringstream os; - os << filePath << "/" << fileNamePrefix << "_d" - << (modulePos * numUnitsPerReadout + iReadout) << "_f" - << iFile << '_' << fileIndex << ".h5"; - std::string srcFileName = os.str(); + std::filesystem::path p = + filePath / + (fileNamePrefix + "_d" + + std::to_string(modulePos * numUnitsPerReadout + + iReadout) + + "_f" + std::to_string(iFile) + '_' + + std::to_string(fileIndex) + ".h5"); + + std::string srcFileName = p.string(); LOG(logDEBUG1) << srcFileName; // find relative path diff --git a/slsReceiverSoftware/src/MasterFileUtility.h b/slsReceiverSoftware/src/MasterFileUtility.h index cbf68be39..795607079 100644 --- a/slsReceiverSoftware/src/MasterFileUtility.h +++ b/slsReceiverSoftware/src/MasterFileUtility.h @@ -4,13 +4,14 @@ #include "MasterAttributes.h" +#include #include namespace sls { namespace masterFileUtility { -std::string CreateMasterBinaryFile(const std::string &filePath, +std::string CreateMasterBinaryFile(const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, @@ -24,7 +25,7 @@ void LinkHDF5FileInMaster(std::string &masterFileName, const bool silentMode, std::mutex *hdf5LibMutex, size_t multiRoiSize); -std::string CreateMasterHDF5File(const std::string &filePath, +std::string CreateMasterHDF5File(const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, @@ -35,7 +36,7 @@ defs::ROI GetGlobalPortRoi(const int iPort, const defs::xy portSize, int GetNumPortsInRoi(const defs::ROI roi, const defs::xy portSize); std::string CreateVirtualHDF5File( - const std::string &filePath, const std::string &fileNamePrefix, + const std::filesystem::path &filePath, const std::string &fileNamePrefix, const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, const int modulePos, const int numUnitsPerReadout, const uint32_t maxFramesPerFile, const int nPixelsX, const int nPixelsY,