Skip to content
Snippets Groups Projects
Commit 1904f708 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

Handling multiple filename extensions in FileSystemUtils, unit tests.

parent d86d8d27
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......@@ -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);
......
#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");
}
......@@ -17,4 +17,6 @@
#include "SampleProviderTest.h"
#include "FourierTransformTest.h"
#include "BeamFootprintTest.h"
#include "FileSystemUtilsTest.h"
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