namespace Util {
-std::string_view
-base_name(std::string_view path)
-{
-#ifdef _WIN32
- const char delim[] = "/\\";
-#else
- const char delim[] = "/";
-#endif
- size_t n = path.find_last_of(delim);
- return n == std::string::npos ? path : path.substr(n + 1);
-}
-
std::string
change_extension(std::string_view path, std::string_view new_ext)
{
namespace Util {
-// Get base name of path.
-std::string_view base_name(std::string_view path);
-
// Remove the extension via `remove_extension()`, then add `new_ext`. `new_ext`
// should start with a dot, no extra dot is inserted.
std::string change_extension(std::string_view path, std::string_view new_ext);
-// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
} else {
extension = get_default_object_file_extension(ctx.config);
}
- args_info.output_obj +=
- Util::change_extension(Util::base_name(args_info.input_file), extension);
+ args_info.output_obj += Util::change_extension(
+ fs::path(args_info.input_file).filename().string(), extension);
}
args_info.orig_output_obj = args_info.output_obj;
// GCC strangely uses the base name of the source file but with a .o
// extension.
dep_target = Util::change_extension(
- Util::base_name(args_info.orig_input_file),
+ fs::path(args_info.orig_input_file).filename().string(),
get_default_object_file_extension(ctx.config));
} else {
// How other compilers behave is currently unknown, so bail out.
-// Copyright (C) 2009-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2009-2024 Joel Rosdahl and other contributors
// Copyright (C) 2002-2007 Andrew Tridgell
//
// See doc/AUTHORS.adoc for a complete list of contributors.
// Also hash the compiler name as some compilers use hard links and behave
// differently depending on the real name.
hash.hash_delimiter("cc_name");
- hash.hash(Util::base_name(args[0]));
+ hash.hash(fs::path(args[0]).filename().string());
// Hash variables that may affect the compilation.
const char* always_hash_env_vars[] = {
} else {
dir = util::real_path(Util::dir_name(ctx.args_info.output_obj));
}
- std::string_view stem =
- Util::remove_extension(Util::base_name(ctx.args_info.output_obj));
+ std::string_view stem = Util::remove_extension(
+ fs::path(ctx.args_info.output_obj).filename().string());
std::string gcda_path = FMT("{}/{}.gcda", dir, stem);
LOG("Hashing coverage path {}", gcda_path);
hash.hash_delimiter("gcda");
// In case ccache is masquerading as the compiler, use only base_name so
// the real compiler can be determined.
: (masquerading_as_compiler
- ? std::string(Util::base_name(ctx.orig_args[0]))
+ ? fs::path(ctx.orig_args[0]).filename().string()
: ctx.orig_args[0]);
const std::string resolved_compiler =
try {
if (is_ccache_executable(argv[0])) {
if (argc < 2) {
- PRINT_RAW(stderr, core::get_usage_text(Util::base_name(argv[0])));
+ PRINT_RAW(stderr,
+ core::get_usage_text(fs::path(argv[0]).filename().string()));
exit(EXIT_FAILURE);
}
// If the first argument isn't an option, then assume we are being
-// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
#include <util/environment.hpp>
#include <util/expected.hpp>
#include <util/file.hpp>
+#include <util/filesystem.hpp>
#include <util/fmtmacros.hpp>
#include <util/logging.hpp>
#include <util/string.hpp>
}
#endif
+namespace fs = util::filesystem;
+
using util::DirEntry;
namespace core {
}
case 'h': // --help
- PRINT(stdout, USAGE_TEXT, Util::base_name(argv[0]));
+ PRINT(stdout, USAGE_TEXT, fs::path(argv[0]).filename());
return EXIT_SUCCESS;
case 'k': // --get-config
case 'V': // --version
{
- std::string_view name = Util::base_name(argv[0]);
+ std::string name = fs::path(argv[0]).filename().string();
#ifdef _WIN32
name = Util::remove_extension(name);
#endif
break;
default:
- PRINT(stderr, USAGE_TEXT, Util::base_name(argv[0]));
+ PRINT(stderr, USAGE_TEXT, fs::path(argv[0]).filename());
return EXIT_FAILURE;
}
}
LOG("Moving {} to {}", cache_file_path, wanted_path);
fs::rename(cache_file_path, wanted_path);
for (const auto& raw_file : m_added_raw_files) {
- fs::rename(
- raw_file,
- FMT("{}/{}", Util::dir_name(wanted_path), Util::base_name(raw_file)));
+ fs::rename(raw_file,
+ FMT("{}/{}",
+ Util::dir_name(wanted_path),
+ fs::path(raw_file).filename()));
}
}
}
#include "Util.hpp"
#include <util/assertions.hpp>
+#include <util/filesystem.hpp>
#include <util/fmtmacros.hpp>
+namespace fs = util::filesystem;
+
namespace util {
void
-handle_failed_assertion(const char* file,
+handle_failed_assertion(const fs::path& file,
size_t line,
const char* function,
const char* condition)
{
PRINT(stderr,
"ccache: {}:{}: {}: failed assertion: {}\n",
- Util::base_name(file),
+ file.filename(),
line,
function,
condition);
abort();
}
+
} // namespace util
#pragma once
#include <cstddef>
+#include <filesystem>
#ifdef _MSC_VER
# define CCACHE_FUNCTION __func__
namespace util {
-[[noreturn]] void handle_failed_assertion(const char* file,
+[[noreturn]] void handle_failed_assertion(const std::filesystem::path& file,
size_t line,
const char* function,
const char* condition);
TEST_SUITE_BEGIN("Util");
-TEST_CASE("Util::base_name")
-{
- CHECK(Util::base_name("") == "");
- CHECK(Util::base_name(".") == ".");
- CHECK(Util::base_name("foo") == "foo");
- CHECK(Util::base_name("/") == "");
- CHECK(Util::base_name("/foo") == "foo");
- CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt");
-}
-
TEST_CASE("Util::change_extension")
{
CHECK(Util::change_extension("", "") == "");