mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 10:07:59 +02:00
WIP
This commit is contained in:
@ -48,3 +48,7 @@ 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);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "sls/sls_detector_exceptions.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
@ -92,3 +93,13 @@ void mkdir_p(const std::string &path, std::string dir) {
|
||||
if (i + 1 < path.length())
|
||||
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);
|
||||
int file_size = ifs.tellg();
|
||||
ifs.seekg(current_pos);
|
||||
return file_size;
|
||||
}
|
||||
} // namespace sls
|
||||
|
@ -1,5 +1,6 @@
|
||||
target_sources(tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-bit_utils.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-file_utils.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-container_utils.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-network_utils.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test-string_utils.cpp
|
||||
|
28
slsSupportLib/tests/test-file_utils.cpp
Normal file
28
slsSupportLib/tests/test-file_utils.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "catch.hpp"
|
||||
#include "sls/file_utils.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
TEST_CASE("Get size of empty file") {
|
||||
char fname[] = "temfile_XXXXXX";
|
||||
int fh = mkstemp(fname);
|
||||
std::ifstream ifs(fname);
|
||||
auto size = sls::getFileSize(ifs);
|
||||
REQUIRE(size == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Get size of file with data") {
|
||||
constexpr size_t n_bytes = 137;
|
||||
std::vector<char> data(n_bytes);
|
||||
char fname[] = "temfile_XXXXXX";
|
||||
int fh = mkstemp(fname);
|
||||
write(fh, data.data(), n_bytes);
|
||||
|
||||
std::ifstream ifs(fname);
|
||||
auto size = sls::getFileSize(ifs);
|
||||
REQUIRE(size == n_bytes);
|
||||
REQUIRE(ifs.tellg() == 0); //getting size resets pos!
|
||||
}
|
||||
|
Reference in New Issue
Block a user