diff --git a/slsReceiverSoftware/src/BinaryDataFile.cpp b/slsReceiverSoftware/src/BinaryDataFile.cpp index 4f5b90f4d..12b27ac7e 100644 --- a/slsReceiverSoftware/src/BinaryDataFile.cpp +++ b/slsReceiverSoftware/src/BinaryDataFile.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: LGPL-3.0-or-other // Copyright (C) 2021 Contributors to the SLS Detector Package #include "BinaryDataFile.h" +#include namespace sls { @@ -19,16 +20,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 +42,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..9d555fbf5 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: @@ -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..83ddb4bb7 100644 --- a/slsReceiverSoftware/src/DataProcessor.h +++ b/slsReceiverSoftware/src/DataProcessor.h @@ -15,6 +15,7 @@ #include "receiver_defs.h" #include +#include #include #include @@ -59,7 +60,8 @@ 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 @@ -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..4de6d51b4 100644 --- a/slsReceiverSoftware/src/HDF5DataFile.h +++ b/slsReceiverSoftware/src/HDF5DataFile.h @@ -4,6 +4,7 @@ #include "File.h" +#include #include namespace sls { @@ -23,7 +24,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 +67,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..ceebf72cb 100644 --- a/slsReceiverSoftware/src/Implementation.cpp +++ b/slsReceiverSoftware/src/Implementation.cpp @@ -16,6 +16,7 @@ #include //system #include #include //strcpy +#include #include #include #include // stat @@ -483,10 +484,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; } @@ -874,21 +879,24 @@ 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 (!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..24408c64a 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); diff --git a/slsReceiverSoftware/src/MasterFileUtility.h b/slsReceiverSoftware/src/MasterFileUtility.h index cbf68be39..af2555afa 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,