utils/file: add basename/dirname helpers and use them

This commit is contained in:
Dirk-Jan C. Binnema 2023-09-10 10:10:57 +03:00
parent b89e9354ec
commit c8568eecd4
5 changed files with 43 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb.bulk@gmail.com>
** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb.bulk@gmail.com>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
@ -18,6 +18,7 @@
*/
#include "mu-message-file.hh"
#include "utils/mu-utils-file.hh"
using namespace Mu;
@ -73,17 +74,11 @@ Mu::base_message_dir_file(const std::string& path)
{
constexpr auto newdir{"/new"};
char *dirname{g_path_get_dirname(path.c_str())};
bool is_new{!!g_str_has_suffix(dirname, newdir)};
const auto dname{dirname(path)};
bool is_new{!!g_str_has_suffix(dname.c_str(), newdir)};
std::string mdir{dirname, ::strlen(dirname) - 4};
g_free(dirname);
char *basename{g_path_get_basename(path.c_str())};
std::string bname{basename};
g_free(basename);
return Ok(DirFile{std::move(mdir), std::move(bname), is_new});
std::string mdir{dname.substr(0, dname.size() - 4)};
return Ok(DirFile{std::move(mdir), basename(path), is_new});
}
Mu::Result<Mu::Flags>
@ -116,8 +111,6 @@ Mu::flags_from_path(const std::string& path)
}
#ifdef BUILD_TESTS
#include "utils/mu-test-utils.hh"

View File

@ -19,9 +19,9 @@
#include "mu-message-part.hh"
#include "glibconfig.h"
#include "mu-mime-object.hh"
#include "utils/mu-utils.hh"
#include "utils/mu-utils-file.hh"
#include <string>
using namespace Mu;
@ -48,7 +48,7 @@ cook(const std::string& fname, const std::vector<char>& forbidden)
std::string clean;
clean.reserve(fname.length());
for (auto& c: to_string_gchar(g_path_get_basename(fname.c_str())))
for (auto& c: basename(fname))
if (seq_some(forbidden,[&](char fc){return ::iscntrl(c) || c == fc;}))
clean += '-';
else

View File

@ -155,7 +155,7 @@ get_target_fullpath(const std::string& src, const std::string& targetpath,
if (auto&& res = check_subdir(src, in_cur); !res)
return Err(std::move(res.error()));
const auto srcfile{to_string_gchar(g_path_get_basename(src.c_str()))};
const auto srcfile{basename(src)};
/* create target-path; note: make the filename *cough* unique by
* including a hash of the srcname in the targetname. This helps if

View File

@ -151,6 +151,18 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t
return str;
}
std::string
Mu::basename(const std::string& path)
{
return to_string_gchar(g_path_get_basename(path.c_str()));
}
std::string
Mu::dirname(const std::string& path)
{
return to_string_gchar(g_path_get_dirname(path.c_str()));
}
Result<std::string>
Mu::make_temp_dir()
{

View File

@ -85,6 +85,28 @@ std::string canonicalize_filename(const std::string& path, const std::string& re
Result<std::string> expand_path(const std::string& str);
/**
* Get the basename for path, i.e. without leading directory component,
* @see g_path_get_basename
*
* @param path
*
* @return the basename
*/
std::string basename(const std::string& path);
/**
* Get the dirname for path, i.e. without leading directory component,
* @see g_path_get_dirname
*
* @param path
*
* @return the dirname
*/
std::string dirname(const std::string& path);
/*
* for OSs with out support for direntry->d_type, like Solaris
*/