member filePath in Implementation is std::filesystem::path, some refactoring

This commit is contained in:
2026-02-10 18:25:38 +01:00
parent 1c18803dc9
commit ff65fa1d70
11 changed files with 79 additions and 56 deletions

View File

@@ -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 <filesystem>
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"))) {

View File

@@ -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};

View File

@@ -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) {

View File

@@ -15,6 +15,7 @@
#include "receiver_defs.h"
#include <atomic>
#include <filesystem>
#include <mutex>
#include <vector>
@@ -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,

View File

@@ -6,6 +6,7 @@
#include "sls/sls_detector_defs.h"
#include <array>
#include <filesystem>
#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";

View File

@@ -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<std::mutex> lock(*hdf5Lib);

View File

@@ -4,6 +4,7 @@
#include "File.h"
#include <filesystem>
#include <mutex>
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};

View File

@@ -16,6 +16,7 @@
#include <cstdlib> //system
#include <cstring>
#include <cstring> //strcpy
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sys/stat.h> // 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();

View File

@@ -11,6 +11,7 @@
#include <atomic>
#include <chrono>
#include <exception>
#include <filesystem>
#include <map>
#include <memory>
#include <mutex>
@@ -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};

View File

@@ -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<std::mutex> lock(*hdf5LibMutex);

View File

@@ -4,13 +4,14 @@
#include "MasterAttributes.h"
#include <filesystem>
#include <mutex>
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,