Skip to content
Snippets Groups Projects
Commit 3c44c5fe authored by Matthias's avatar Matthias
Browse files

fix errors shown by additional unit tests; migrate to std::filesystem

parent 9fcaf9ff
No related branches found
No related tags found
No related merge requests found
...@@ -14,67 +14,65 @@ ...@@ -14,67 +14,65 @@
#include "Base/Utils/FileSystemUtils.h" #include "Base/Utils/FileSystemUtils.h"
#include "Base/Utils/Assert.h" #include "Base/Utils/Assert.h"
#include <boost/filesystem.hpp>
#include <codecvt> #include <codecvt>
#include <locale> #include <locale>
#include <regex> #include <regex>
#include <stdexcept> #include <stdexcept>
#include <filesystem>
namespace fs = std::filesystem; // make the code more readable
std::string FileSystemUtils::extension(const std::string& path) { std::string FileSystemUtils::extension(const std::string& path) {
return boost::filesystem::extension(path.c_str()); return fs::path(path).extension().string();
} }
std::string FileSystemUtils::extensions(const std::string& path) { std::string FileSystemUtils::extensions(const std::string& path) {
auto name = FileSystemUtils::filename(path); const auto name = FileSystemUtils::filename(path);
auto npos = name.find_first_of('.'); if (name == "..")
return npos != std::string::npos ? name.substr(npos, name.size() - npos) : std::string(); return {};
const auto pos = name.find_first_of('.', 1); // 1: ignore any file-is-hidden dot
return pos != std::string::npos ? name.substr(pos, name.size() - pos) : std::string();
} }
bool FileSystemUtils::createDirectory(const std::string& dir_name) { bool FileSystemUtils::createDirectory(const std::string& dir_name) {
#ifdef _WIN32 #ifdef _WIN32
boost::filesystem::path path(convert_utf8_to_utf16(dir_name)); return fs::create_directory(convert_utf8_to_utf16(dir_name));
#else #else
boost::filesystem::path path(dir_name); return fs::create_directory(dir_name);
#endif #endif
return boost::filesystem::create_directory(dir_name);
} }
bool FileSystemUtils::createDirectories(const std::string& dir_name) { bool FileSystemUtils::createDirectories(const std::string& dir_name) {
#ifdef _WIN32 #ifdef _WIN32
boost::filesystem::path path(convert_utf8_to_utf16(dir_name)); return fs::create_directories(convert_utf8_to_utf16(dir_name));
#else #else
boost::filesystem::path path(dir_name); return fs::create_directories(dir_name);
#endif #endif
return boost::filesystem::create_directories(path);
} }
std::vector<std::string> FileSystemUtils::filesInDirectory(const std::string& dir_name) { std::vector<std::string> FileSystemUtils::filesInDirectory(const std::string& dir_name) {
std::vector<std::string> ret; std::vector<std::string> ret;
if (!boost::filesystem::exists(dir_name)) if (!fs::exists(dir_name))
throw std::runtime_error("FileSystemUtils::filesInDirectory '" + dir_name throw std::runtime_error("FileSystemUtils::filesInDirectory '" + dir_name
+ "' does not exist"); + "' does not exist");
boost::filesystem::directory_iterator end_it; // default construction yields past-the-end
for (boost::filesystem::directory_iterator it(dir_name); for (const auto& entry : fs::directory_iterator(dir_name))
it != boost::filesystem::directory_iterator(); ++it) { if (entry.is_regular_file())
if (!boost::filesystem::is_regular_file(it->status())) ret.push_back(entry.path().filename().string());
continue;
ret.push_back(it->path().filename().string());
}
return ret; return ret;
} }
std::string FileSystemUtils::jointPath(const std::string& spath1, const std::string& spath2) { std::string FileSystemUtils::jointPath(const std::string& path1, const std::string& path2) {
ASSERT(spath1 != ""); ASSERT(path1 != "");
ASSERT(spath2 != ""); ASSERT(path2 != "");
boost::filesystem::path path1(spath1);
boost::filesystem::path path2(spath2); return (fs::path(path1) / fs::path(path2)).string();
boost::filesystem::path full_path = path1 / path2;
return full_path.string();
} }
std::string FileSystemUtils::filename(const std::string& path) { std::string FileSystemUtils::filename(const std::string& path) {
return boost::filesystem::path(path).filename().string(); return fs::path(path).filename().string();
} }
std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std::string& pattern) { std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std::string& pattern) {
...@@ -86,13 +84,16 @@ std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std ...@@ -86,13 +84,16 @@ std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std
} }
std::string FileSystemUtils::stem(const std::string& path) { std::string FileSystemUtils::stem(const std::string& path) {
return boost::filesystem::path(path).stem().string(); return fs::path(path).stem().string();
} }
std::string FileSystemUtils::stem_ext(const std::string& path) { std::string FileSystemUtils::stem_ext(const std::string& path) {
auto name = FileSystemUtils::filename(path); const auto name = FileSystemUtils::filename(path);
auto npos = name.find_first_of('.'); if (name == "..")
return npos != std::string::npos ? name.substr(0, npos) : std::string(); return name;
const auto pos = name.find_first_of('.', 1); // // 1: ignore any file-is-hidden dot
return pos != std::string::npos ? name.substr(0, pos) : name;
} }
std::wstring FileSystemUtils::convert_utf8_to_utf16(const std::string& str) { std::wstring FileSystemUtils::convert_utf8_to_utf16(const std::string& str) {
...@@ -100,11 +101,10 @@ std::wstring FileSystemUtils::convert_utf8_to_utf16(const std::string& str) { ...@@ -100,11 +101,10 @@ std::wstring FileSystemUtils::convert_utf8_to_utf16(const std::string& str) {
return converter.from_bytes(str); return converter.from_bytes(str);
} }
bool FileSystemUtils::IsFileExists(const std::string& str) { bool FileSystemUtils::IsFileExists(const std::string& path) {
#ifdef _WIN32 #ifdef _WIN32
boost::filesystem::path path(convert_utf8_to_utf16(str)); return fs::exists(convert_utf8_to_utf16(path));
#else #else
boost::filesystem::path path(str); return fs::exists(path);
#endif #endif
return boost::filesystem::exists(path);
} }
...@@ -40,12 +40,12 @@ bool createDirectories(const std::string& dir_name); ...@@ -40,12 +40,12 @@ bool createDirectories(const std::string& dir_name);
std::vector<std::string> filesInDirectory(const std::string& dir_name); std::vector<std::string> filesInDirectory(const std::string& dir_name);
//! Returns joint path name. //! Returns joint path name.
std::string jointPath(const std::string& spath1, const std::string& spath2); std::string jointPath(const std::string& path1, const std::string& path2);
//! Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz") //! Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
std::string filename(const std::string& path); std::string filename(const std::string& path);
//! Returns filename without extension. //! Returns filename without (last) extension.
//! "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename.int" //! "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename.int"
std::string stem(const std::string& path); std::string stem(const std::string& path);
...@@ -60,7 +60,7 @@ std::vector<std::string> glob(const std::string& dir, const std::string& pattern ...@@ -60,7 +60,7 @@ std::vector<std::string> glob(const std::string& dir, const std::string& pattern
std::wstring convert_utf8_to_utf16(const std::string& str); std::wstring convert_utf8_to_utf16(const std::string& str);
//! Returns true if file with given name exists on disk. //! Returns true if file with given name exists on disk.
bool IsFileExists(const std::string& str); bool IsFileExists(const std::string& path);
} // namespace FileSystemUtils } // namespace FileSystemUtils
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "Device/Histo/IntensityDataIOFactory.h" #include "Device/Histo/IntensityDataIOFactory.h"
#include "Device/Instrument/IntensityDataFunctions.h" #include "Device/Instrument/IntensityDataFunctions.h"
#include "Tests/GTestWrapper/google_test.h" #include "Tests/GTestWrapper/google_test.h"
#include <boost/filesystem.hpp> #include <filesystem>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
...@@ -46,16 +46,16 @@ TEST_F(CoreIOPathTest, CoreIOPath) { ...@@ -46,16 +46,16 @@ TEST_F(CoreIOPathTest, CoreIOPath) {
const char dirname_rus[] = const char dirname_rus[] =
"\xd0\xb4\xd0\xb8\xd1\x80\xd0\xb5\xd0\xba\xd1\x82\xd0\xbe\xd1\x80\xd0\xb8\xd1\x8f"; "\xd0\xb4\xd0\xb8\xd1\x80\xd0\xb5\xd0\xba\xd1\x82\xd0\xbe\xd1\x80\xd0\xb8\xd1\x8f";
const boost::filesystem::path test_dir(BATesting::TestOutDir_Core()); const std::filesystem::path test_dir(BATesting::TestOutDir_Core());
FileSystemUtils::createDirectories(test_dir.string()); FileSystemUtils::createDirectories(test_dir.string());
// tests file writing when file name contains cyrillic characters // tests file writing when file name contains cyrillic characters
boost::filesystem::path test_file(filename_rus); std::filesystem::path test_file(filename_rus);
EXPECT_TRUE(test_io(data.get(), (test_dir / test_file).string())); EXPECT_TRUE(test_io(data.get(), (test_dir / test_file).string()));
// tests file writing and directory creation when dirname contains cyrillic characters // tests file writing and directory creation when dirname contains cyrillic characters
boost::filesystem::path test_subdir_rus(dirname_rus); std::filesystem::path test_subdir_rus(dirname_rus);
FileSystemUtils::createDirectories((test_dir / test_subdir_rus).string()); FileSystemUtils::createDirectories((test_dir / test_subdir_rus).string());
EXPECT_TRUE(test_io(data.get(), (test_dir / test_subdir_rus / test_file).string())); EXPECT_TRUE(test_io(data.get(), (test_dir / test_subdir_rus / test_file).string()));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment