diff --git a/Core/Tools/FileSystemUtils.cpp b/Core/Tools/FileSystemUtils.cpp index 66321b8681720067712259a636b9b41d8b4d304d..ea15997f5655b5fdc6548000e17bfb04e6be9210 100644 --- a/Core/Tools/FileSystemUtils.cpp +++ b/Core/Tools/FileSystemUtils.cpp @@ -19,9 +19,16 @@ #include <cassert> #include <stdexcept> -std::string FileSystemUtils::extension(const std::string& name) +std::string FileSystemUtils::extension(const std::string& path) { - return boost::filesystem::extension(name.c_str()); + return boost::filesystem::extension(path.c_str()); +} + +std::string FileSystemUtils::extensions(const std::string& path) +{ + auto name = FileSystemUtils::filename(path); + auto npos =name.find_first_of('.'); + return npos != std::string::npos ? name.substr(npos, name.size()-npos) : std::string(); } bool FileSystemUtils::createDirectory(const std::string& dir_name) @@ -70,3 +77,16 @@ std::vector<std::string> FileSystemUtils::glob(const std::string& dir, const std ret.push_back(fname); return ret; } + +std::string FileSystemUtils::stem(const std::string& path) +{ + return boost::filesystem::path(path).stem().string(); +} + +std::string FileSystemUtils::stem_ext(const std::string& path) +{ + auto name = FileSystemUtils::filename(path); + auto npos =name.find_first_of('.'); + return npos != std::string::npos ? name.substr(0, npos) : std::string(); +} + diff --git a/Core/Tools/FileSystemUtils.h b/Core/Tools/FileSystemUtils.h index 3a2b28465c006b7fa3dd48c98b2dcb526c8e1df3..4dd44e0886e5970a147cec13102898e1f532c6a1 100644 --- a/Core/Tools/FileSystemUtils.h +++ b/Core/Tools/FileSystemUtils.h @@ -24,7 +24,12 @@ namespace FileSystemUtils { //! Returns extension of given filename. -BA_CORE_API_ std::string extension(const std::string& fname); +//! "/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".gz" +BA_CORE_API_ std::string extension(const std::string& path); + +//! Returns extension(s) of given filename. +//! "/home/user/filename.int" -> ".int", "/home/user/filename.int.gz" -> ".int.gz" +BA_CORE_API_ std::string extensions(const std::string& path); //! Creates directory in current directory BA_CORE_API_ bool createDirectory(const std::string& dir_name); @@ -38,6 +43,14 @@ BA_CORE_API_ std::string jointPath(const std::string& spath1, const std::string& //! Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz") BA_CORE_API_ std::string filename(const std::string& path); +//! Returns filename without extension. +//! "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename.int" +BA_CORE_API_ std::string stem(const std::string& path); + +//! Returns filename without extension(s). +//! "/home/user/filename.int" -> "filename", "/home/user/filename.int.gz" -> "filename" +BA_CORE_API_ std::string stem_ext(const std::string& path); + //! Returns file names that agree with a regex glob pattern. BA_CORE_API_ std::vector<std::string> glob(const std::string& dir, const std::string& pattern); diff --git a/Tests/UnitTests/Core/Other/FileSystemUtilsTest.h b/Tests/UnitTests/Core/Other/FileSystemUtilsTest.h new file mode 100644 index 0000000000000000000000000000000000000000..bba945a3669d91d141b03fd8be9085ca4d46d62c --- /dev/null +++ b/Tests/UnitTests/Core/Other/FileSystemUtilsTest.h @@ -0,0 +1,45 @@ +#include "google_test.h" +#include "FileSystemUtils.h" + +class FileSystemUtilsTest : public ::testing::Test +{ +protected: + ~FileSystemUtilsTest(); +}; + +FileSystemUtilsTest::~FileSystemUtilsTest() = default; + +TEST_F(FileSystemUtilsTest, extention) +{ + EXPECT_EQ(FileSystemUtils::extension(""), ""); + EXPECT_EQ(FileSystemUtils::extension("/home/james/file.txt"), ".txt"); + EXPECT_EQ(FileSystemUtils::extension("/home/james/file.txt.gz"), ".gz"); +} + +TEST_F(FileSystemUtilsTest, extentions) +{ + EXPECT_EQ(FileSystemUtils::extensions(""), ""); + EXPECT_EQ(FileSystemUtils::extensions("/home/james/file.txt"), ".txt"); + EXPECT_EQ(FileSystemUtils::extensions("/home/james/file.txt.gz"), ".txt.gz"); +} + +TEST_F(FileSystemUtilsTest, filename) +{ + EXPECT_EQ(FileSystemUtils::filename(""), ""); + EXPECT_EQ(FileSystemUtils::filename("/home/james/file.txt"), "file.txt"); + EXPECT_EQ(FileSystemUtils::filename("/home/james/file"), "file"); +} + +TEST_F(FileSystemUtilsTest, stem) +{ + EXPECT_EQ(FileSystemUtils::stem(""), ""); + EXPECT_EQ(FileSystemUtils::stem("/home/james/filename.txt"), "filename"); + EXPECT_EQ(FileSystemUtils::stem("/home/james/filename.txt.gz"), "filename.txt"); +} + +TEST_F(FileSystemUtilsTest, stem_ext) +{ + EXPECT_EQ(FileSystemUtils::stem_ext(""), ""); + EXPECT_EQ(FileSystemUtils::stem_ext("/home/james/filename.txt"), "filename"); + EXPECT_EQ(FileSystemUtils::stem_ext("/home/james/filename.txt.gz"), "filename"); +} diff --git a/Tests/UnitTests/Core/Other/testlist.h b/Tests/UnitTests/Core/Other/testlist.h index 53d3d087c1540505b7af408b10e158fbeddcce1e..7e9cbcd40466554b10f27dd70b106f1d8ebf7d72 100644 --- a/Tests/UnitTests/Core/Other/testlist.h +++ b/Tests/UnitTests/Core/Other/testlist.h @@ -17,4 +17,6 @@ #include "SampleProviderTest.h" #include "FourierTransformTest.h" #include "BeamFootprintTest.h" +#include "FileSystemUtilsTest.h" +