Replace dirname implementation with std::filesystem

main
Luis Díaz Más 3 years ago committed by Luis Diaz
parent f1c8d5bbb4
commit d64c2aad67

@ -2067,11 +2067,11 @@ namespace {
std::string newFilePath(const std::string& path, const std::string& ext)
{
std::string directory = Params::instance().directory_;
if (directory.empty()) directory = Util::dirname(path);
directory = Exiv2::fileProtocol(path) == Exiv2::pFile
? directory + EXV_SEPARATOR_STR
: "" // use current directory for remote files
;
if (directory.empty())
directory = Util::dirname(path);
directory = Exiv2::fileProtocol(path) == Exiv2::pFile ? directory + EXV_SEPARATOR_STR
: "" // use current directory for remote files
;
return directory + Util::basename(path, true) + ext;
}

@ -21,6 +21,9 @@
#include "utils.hpp"
#include <filesystem>
namespace fs = std::filesystem;
namespace Util {
@ -29,25 +32,10 @@ namespace Util {
std::string dirname(const std::string& path)
{
if (path.empty())
auto p = fs::path(path).parent_path();
if (p.empty())
return ".";
// Strip trailing slashes or backslashes
std::string p = path;
while ( p.length() > 1
&& (p[p.length()-1] == '\\' || p[p.length()-1] == '/')) {
p = p.substr(0, p.length()-1);
}
if (p == "\\" || p == "/") return p;
if (p.length() == 2 && p[1] == ':') return p; // For Windows paths
std::string::size_type idx = p.find_last_of("\\/");
if (idx == std::string::npos) return ".";
if (idx == 1 && p.at(0) == '\\' && p.at(1) == '\\') return p; // For Windows paths
p = p.substr(0, idx == 0 ? 1 : idx);
while ( p.length() > 1
&& (p[p.length()-1] == '\\' || p[p.length()-1] == '/')) {
p = p.substr(0, p.length()-1);
}
return p;
return p.string();
}
std::string basename(const std::string& path, bool delsuffix)

@ -6,18 +6,39 @@
namespace {
const std::string pathLinux("/home/luis/file.txt");
const std::string pathWindows("c:\\luis\\file.txt");
const std::string pathWindows("c:/luis/file.txt");
}
TEST(dirname, returnsDirNameWithValidPathOnLinux)
{
ASSERT_EQ("/home/luis", Util::dirname(pathLinux));
}
#ifdef _WIN32
TEST(dirname, returnsDirNameWithValidPathOnWindows)
{
ASSERT_EQ("c:\\luis", Util::dirname(pathWindows));
}
TEST(basename, returnsStemWithExtensionWithValidPathOnWindows)
{
const bool delSuffix = false;
ASSERT_EQ("file.txt", Util::basename(pathWindows, delSuffix));
}
TEST(basename, returnsStemWithoutExtensionWithValidPathOnWindows)
{
const bool delSuffix = true;
ASSERT_EQ("file", Util::basename(pathWindows, delSuffix));
}
TEST(suffix, returnsExtensionWithValidWindowsPath)
{
ASSERT_EQ(".txt", Util::suffix(pathWindows));
}
#else
TEST(dirname, returnsDirNameWithValidPathOnLinux)
{
ASSERT_EQ("/home/luis", Util::dirname(pathLinux));
ASSERT_EQ("/tmp", Util::dirname("/tmp/file.jpg"));
}
TEST(dirname, returnsDotWithRelativePath)
{
@ -42,28 +63,14 @@ TEST(basename, returnsStemWithoutExtensionWithValidPathOnLinux)
ASSERT_EQ("file", Util::basename(pathLinux, delSuffix));
}
TEST(basename, returnsStemWithExtensionWithValidPathOnWindows)
{
const bool delSuffix = false;
ASSERT_EQ("file.txt", Util::basename(pathWindows, delSuffix));
}
TEST(basename, returnsStemWithoutExtensionWithValidPathOnWindows)
{
const bool delSuffix = true;
ASSERT_EQ("file", Util::basename(pathWindows, delSuffix));
}
TEST(suffix, returnsExtensionWithValidLinuxPath)
{
ASSERT_EQ(".txt", Util::suffix(pathLinux));
}
#endif
TEST(suffix, returnsExtensionWithValidWindowsPath)
{
ASSERT_EQ(".txt", Util::suffix(pathWindows));
}
TEST(suffix, returnsEmptyStringWithFilesWithoutExtension)
{

Loading…
Cancel
Save