mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 18:17:59 +02:00
refactoring
This commit is contained in:
@ -8,6 +8,9 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace sls {
|
||||
|
||||
|
||||
/**
|
||||
* @param data array of data values
|
||||
* @param nch number of channels
|
||||
@ -22,6 +25,7 @@ int readDataFile(std::ifstream &infile, short int *data, int nch,
|
||||
*/
|
||||
int readDataFile(std::string fname, short int *data, int nch);
|
||||
|
||||
|
||||
std::vector<char> readBinaryFile(const std::string &fname,
|
||||
const std::string &errorPrefix);
|
||||
|
||||
@ -42,6 +46,8 @@ int writeDataFile(std::string fname, int nch, short int *data);
|
||||
// mkdir -p path implemented by recursive calls
|
||||
void mkdir_p(const std::string &path, std::string dir = "");
|
||||
|
||||
namespace sls {
|
||||
int getFileSize(std::ifstream &ifs);
|
||||
ssize_t getFileSize(FILE* fd, const std::string &prependErrorString);
|
||||
|
||||
std::string getFileNameFromFilePath(const std::string &fpath);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
#define APICTB 0x211110
|
||||
#define APIGOTTHARD 0x211110
|
||||
#define APIGOTTHARD2 0x211110
|
||||
#define APIJUNGFRAU 0x211110
|
||||
#define APIMYTHEN3 0x211110
|
||||
#define APIMOENCH 0x211110
|
||||
#define APIEIGER 0x211110
|
||||
#define APIJUNGFRAU 0x211110
|
||||
|
@ -11,6 +11,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace sls {
|
||||
|
||||
int readDataFile(std::ifstream &infile, short int *data, int nch, int offset) {
|
||||
int ichan, iline = 0;
|
||||
short int idata;
|
||||
@ -59,36 +61,27 @@ std::vector<char> readBinaryFile(const std::string &fname,
|
||||
struct stat st;
|
||||
if (stat(fname.c_str(), &st) != 0) {
|
||||
throw sls::RuntimeError(errorPrefix +
|
||||
std::string(": file does not exist"));
|
||||
std::string(" (file does not exist)"));
|
||||
}
|
||||
|
||||
FILE *fp = fopen(fname.c_str(), "rb");
|
||||
if (fp == nullptr) {
|
||||
throw sls::RuntimeError(errorPrefix +
|
||||
std::string(": Could not open file: ") + fname);
|
||||
std::string(" (Could not open file: ") + fname + std::string(")"));
|
||||
}
|
||||
|
||||
// get file size to print progress
|
||||
if (fseek(fp, 0, SEEK_END) != 0) {
|
||||
throw sls::RuntimeError(errorPrefix +
|
||||
std::string(": Seek error in src file"));
|
||||
}
|
||||
size_t filesize = ftell(fp);
|
||||
if (filesize <= 0) {
|
||||
throw sls::RuntimeError(errorPrefix +
|
||||
std::string(": Could not get length of file"));
|
||||
}
|
||||
rewind(fp);
|
||||
ssize_t filesize = sls::getFileSize(fp, errorPrefix);
|
||||
|
||||
std::vector<char> buffer(filesize, 0);
|
||||
if (fread(buffer.data(), sizeof(char), filesize, fp) != filesize) {
|
||||
if ((ssize_t)fread(buffer.data(), sizeof(char), filesize, fp) != filesize) {
|
||||
throw sls::RuntimeError(errorPrefix +
|
||||
std::string(": Could not read file"));
|
||||
std::string(" (Could not read file)"));
|
||||
}
|
||||
|
||||
if (fclose(fp) != 0) {
|
||||
throw sls::RuntimeError(errorPrefix +
|
||||
std::string(": Could not close file"));
|
||||
std::string(" (Could not close file)"));
|
||||
}
|
||||
|
||||
LOG(logDEBUG1) << "Read file into memory";
|
||||
@ -138,7 +131,6 @@ void mkdir_p(const std::string &path, std::string dir) {
|
||||
mkdir_p(path.substr(i + 1), dir);
|
||||
}
|
||||
|
||||
namespace sls {
|
||||
int getFileSize(std::ifstream &ifs) {
|
||||
auto current_pos = ifs.tellg();
|
||||
ifs.seekg(0, std::ios::end);
|
||||
@ -146,4 +138,27 @@ int getFileSize(std::ifstream &ifs) {
|
||||
ifs.seekg(current_pos);
|
||||
return file_size;
|
||||
}
|
||||
|
||||
std::string getFileNameFromFilePath(const std::string &fpath) {
|
||||
std::string fname(fpath);
|
||||
std::size_t slashPos = fpath.rfind('/');
|
||||
if (slashPos != std::string::npos) {
|
||||
fname = fpath.substr(slashPos + 1, fpath.size() - 1);
|
||||
}
|
||||
return fname;
|
||||
}
|
||||
|
||||
ssize_t getFileSize(FILE* fd, const std::string &prependErrorString) {
|
||||
if (fseek(fd, 0, SEEK_END) != 0) {
|
||||
throw RuntimeError(prependErrorString + std::string(" (Seek error in src file)"));
|
||||
}
|
||||
size_t fileSize = ftell(fd);
|
||||
if (fileSize <= 0) {
|
||||
throw RuntimeError(prependErrorString + std::string(" (Could not get length of source file)"));
|
||||
}
|
||||
rewind(fd);
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
|
||||
} // namespace sls
|
||||
|
Reference in New Issue
Block a user