From: Joel Rosdahl Date: Sun, 23 Feb 2020 08:43:41 +0000 (+0100) Subject: C++-ify is_absolute_path X-Git-Tag: v4.0~598 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=29f698498adfaae564f6d34a22cc90d6e9e9424c;p=thirdparty%2Fccache.git C++-ify is_absolute_path --- diff --git a/src/Config.cpp b/src/Config.cpp index 682efe9b2..831dd9b1d 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -363,7 +363,7 @@ parse_unsigned(const std::string& value) void verify_absolute_path(const std::string& value) { - if (!is_absolute_path(value.c_str())) { + if (!Util::is_absolute_path(value)) { throw Error(fmt::format("not an absolute path: \"{}\"", value)); } } diff --git a/src/Util.cpp b/src/Util.cpp index 058a1cc72..35d8635ae 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -288,6 +288,17 @@ get_truncated_base_name(string_view path, size_t max_length) return input_base.substr(0, truncate_pos); } +bool +is_absolute_path(string_view path) +{ +#ifdef _WIN32 + return path.length() >= 2 && path[1] == ':' + && (path[2] == '/' || path[2] == '\\'); +#else + return !path.empty() && path[0] == '/'; +#endif +} + int parse_int(const std::string& value) { diff --git a/src/Util.hpp b/src/Util.hpp index c438171cd..bc05d42c0 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -189,6 +189,9 @@ int_to_big_endian(int8_t value, uint8_t* buffer) buffer[0] = value; } +// Return whether `path` is absolute. +bool is_absolute_path(nonstd::string_view path); + // Parse a string into an integer. // // Throws Error on error. diff --git a/src/ccache.cpp b/src/ccache.cpp index 56a6ad2a9..f238a5150 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -812,7 +812,7 @@ process_preprocessed_file(Context& ctx, // p and q span the include file path. char* inc_path = x_strndup(p, q - p); if (!ctx.has_absolute_include_headers) { - ctx.has_absolute_include_headers = is_absolute_path(inc_path); + ctx.has_absolute_include_headers = Util::is_absolute_path(inc_path); } char* saved_inc_path = inc_path; inc_path = x_strdup(make_relative_path(ctx, inc_path).c_str()); @@ -921,7 +921,7 @@ use_relative_paths_in_depfile(const Context& ctx) char* token = strtok_r(buf, " \t", &saveptr); while (token) { char* relpath = nullptr; - if (is_absolute_path(token) + if (Util::is_absolute_path(token) && str_startswith(token, ctx.config.base_dir().c_str())) { relpath = x_strdup(make_relative_path(ctx, token).c_str()); result = true; @@ -1000,7 +1000,7 @@ result_name_from_depfile(Context& ctx, struct hash* hash) continue; } if (!ctx.has_absolute_include_headers) { - ctx.has_absolute_include_headers = is_absolute_path(token); + ctx.has_absolute_include_headers = Util::is_absolute_path(token); } std::string path = make_relative_path(ctx, token); remember_include_file(ctx, path, hash, false, hash); diff --git a/src/execute.cpp b/src/execute.cpp index 8b4068870..b47741950 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -304,7 +304,7 @@ execute(char** argv, int fd_out, int fd_err, pid_t* pid) char* find_executable(const Context& ctx, const char* name, const char* exclude_name) { - if (is_absolute_path(name)) { + if (Util::is_absolute_path(name)) { return x_strdup(name); } diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index 40ada66bc..f7c43b324 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -740,10 +740,10 @@ get_relative_path(const char* from, const char* to) size_t common_prefix_len; char* result; - assert(from && is_absolute_path(from)); + assert(from && Util::is_absolute_path(from)); assert(to); - if (!*to || !is_absolute_path(to)) { + if (!*to || !Util::is_absolute_path(to)) { return x_strdup(to); } @@ -784,17 +784,6 @@ get_relative_path(const char* from, const char* to) return result; } -// Return whether path is absolute. -bool -is_absolute_path(const char* path) -{ -#ifdef _WIN32 - return path[0] && path[1] == ':'; -#else - return path[0] == '/'; -#endif -} - // Return whether the argument is a full path. bool is_full_path(const char* path) diff --git a/src/legacy_util.hpp b/src/legacy_util.hpp index 4c549a277..9acd09781 100644 --- a/src/legacy_util.hpp +++ b/src/legacy_util.hpp @@ -56,7 +56,6 @@ const char* get_home_directory(); bool same_executable_name(const char* s1, const char* s2); size_t common_dir_prefix_length(const char* s1, const char* s2); char* get_relative_path(const char* from, const char* to); -bool is_absolute_path(const char* path); bool is_full_path(const char* path); void update_mtime(const char* path); void x_exit(int status) ATTR_NORETURN; diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 7432e0cd5..7f6bb79b0 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -301,6 +301,22 @@ TEST_CASE("Util::int_to_big_endian") CHECK(bytes[7] == 0xca); } +TEST_CASE("Util::is_absolute_path") +{ +#ifdef _WIN32 + CHECK(Util::is_absolute_path("C:/")); + CHECK(Util::is_absolute_path("C:\\foo/fie")); + CHECK(!Util::is_absolute_path("")); + CHECK(!Util::is_absolute_path("foo\\fie/fum")); + CHECK(!Util::is_absolute_path("C:foo/fie")); +#else + CHECK(Util::is_absolute_path("/")); + CHECK(Util::is_absolute_path("/foo/fie")); + CHECK(!Util::is_absolute_path("")); + CHECK(!Util::is_absolute_path("foo/fie")); +#endif +} + TEST_CASE("Util::parse_int") { CHECK(Util::parse_int("0") == 0);