#include "exceptions.hpp"
#include "fmtmacros.hpp"
+#include <util/path_utils.hpp>
#include <util/string_utils.hpp>
#include "third_party/fmt/core.h"
void
verify_absolute_path(const std::string& value)
{
- if (!Util::is_absolute_path(value)) {
+ if (!util::is_absolute_path(value)) {
throw Error("not an absolute path: \"{}\"", value);
}
}
#include "Logging.hpp"
#include "assertions.hpp"
+#include <util/path_utils.hpp>
+
static inline bool
is_blank(const std::string& s)
{
const auto& token = tokens[i];
bool token_rewritten = false;
- if (Util::is_absolute_path(token)) {
+ if (util::is_absolute_path(token)) {
const auto new_path = Util::make_relative_path(ctx, token);
if (new_path != token) {
adjusted_file_content.append(new_path);
#include "exceptions.hpp"
#include "fmtmacros.hpp"
+#include <util/path_utils.hpp>
+
#include <algorithm>
// Result data format
{
const auto& output_obj = ctx.args_info.output_obj;
const std::string abs_output_obj =
- Util::is_absolute_path(output_obj)
+ util::is_absolute_path(output_obj)
? output_obj
: FMT("{}/{}", ctx.apparent_cwd, output_obj);
std::string hashified_obj = abs_output_obj;
#include "fmtmacros.hpp"
#include <util/Tokenizer.hpp>
+#include <util/path_utils.hpp>
extern "C" {
#include "third_party/base32hex.h"
return actual_cwd;
#else
auto pwd = getenv("PWD");
- if (!pwd || !Util::is_absolute_path(pwd)) {
+ if (!pwd || !util::is_absolute_path(pwd)) {
return actual_cwd;
}
std::string
get_relative_path(string_view dir, string_view path)
{
- ASSERT(Util::is_absolute_path(dir));
- ASSERT(Util::is_absolute_path(path));
+ ASSERT(util::is_absolute_path(dir));
+ ASSERT(util::is_absolute_path(path));
#ifdef _WIN32
// Paths can be escaped by a slash for use with e.g. -isystem.
#endif
}
-bool
-is_absolute_path(string_view path)
-{
-#ifdef _WIN32
- if (path.length() >= 2 && path[1] == ':'
- && (path[2] == '/' || path[2] == '\\')) {
- return true;
- }
-#endif
- return !path.empty() && path[0] == '/';
-}
-
#if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
int
is_nfs_fd(int fd, bool* is_nfs)
std::string
normalize_absolute_path(string_view path)
{
- if (!is_absolute_path(path)) {
+ if (!util::is_absolute_path(path)) {
return std::string(path);
}
buffer[0] = value;
}
-// Return whether `path` is absolute.
-bool is_absolute_path(nonstd::string_view path);
-
// Test if a file is on nfs.
//
// Sets is_nfs to the result if fstatfs is available and no error occurred.
#include "language.hpp"
#include <core/types.hpp>
+#include <util/path_utils.hpp>
#include "third_party/fmt/core.h"
#include "third_party/nonstd/optional.hpp"
if (symlink_value.empty()) {
break;
}
- if (Util::is_absolute_path(symlink_value)) {
+ if (util::is_absolute_path(symlink_value)) {
compiler_path = symlink_value;
} else {
compiler_path =
// p and q span the include file path.
std::string inc_path(p, q - p);
if (!ctx.has_absolute_include_headers) {
- ctx.has_absolute_include_headers = Util::is_absolute_path(inc_path);
+ ctx.has_absolute_include_headers = util::is_absolute_path(inc_path);
}
inc_path = Util::make_relative_path(ctx, inc_path);
continue;
}
if (!ctx.has_absolute_include_headers) {
- ctx.has_absolute_include_headers = Util::is_absolute_path(token);
+ ctx.has_absolute_include_headers = util::is_absolute_path(token);
}
std::string path = Util::make_relative_path(ctx, token);
remember_include_file(ctx, path, hash, false, &hash);
// the profile filename so we need to include the same information in the
// hash.
const std::string profile_path =
- Util::is_absolute_path(ctx.args_info.profile_path)
+ util::is_absolute_path(ctx.args_info.profile_path)
? ctx.args_info.profile_path
: FMT("{}/{}", ctx.apparent_cwd, ctx.args_info.profile_path);
LOG("Adding profile directory {} to our hash", profile_path);
#include "Util.hpp"
#include "fmtmacros.hpp"
+#include <util/path_utils.hpp>
+
#ifdef _WIN32
# include "Finalizer.hpp"
# include "Win32Util.hpp"
const std::string& name,
const std::string& exclude_name)
{
- if (Util::is_absolute_path(name)) {
+ if (util::is_absolute_path(name)) {
return name;
}
set(
sources
- ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Tokenizer.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/path_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/string_utils.cpp
)
--- /dev/null
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// 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 Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "path_utils.hpp"
+
+#include <Logging.hpp>
+#include <Util.hpp>
+#include <fmtmacros.hpp>
+
+namespace util {
+
+bool
+is_absolute_path(nonstd::string_view path)
+{
+#ifdef _WIN32
+ if (path.length() >= 2 && path[1] == ':'
+ && (path[2] == '/' || path[2] == '\\')) {
+ return true;
+ }
+#endif
+ return !path.empty() && path[0] == '/';
+}
+
+} // namespace util
--- /dev/null
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// 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 Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include <third_party/nonstd/string_view.hpp>
+
+#include <string>
+
+namespace util {
+
+// Return whether `path` is absolute.
+bool is_absolute_path(nonstd::string_view path);
+
+} // namespace util
test_hashutil.cpp
test_util_Tokenizer.cpp
test_util_string_utils.cpp
+ test_util_path_utils.cpp
)
if(INODE_CACHE_SUPPORTED)
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("/C:\\foo/fie")); // MSYS/Cygwin path
- CHECK(!Util::is_absolute_path(""));
- CHECK(!Util::is_absolute_path("foo\\fie/fum"));
- CHECK(!Util::is_absolute_path("C:foo/fie"));
-#endif
- CHECK(Util::is_absolute_path("/"));
- CHECK(Util::is_absolute_path("/foo/fie"));
- CHECK(!Util::is_absolute_path(""));
- CHECK(!Util::is_absolute_path("foo/fie"));
-}
-
TEST_CASE("Util::is_dir_separator")
{
CHECK(!Util::is_dir_separator('x'));
--- /dev/null
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// 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 Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include <util/path_utils.hpp>
+
+#include <third_party/doctest.h>
+
+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("/C:\\foo/fie")); // MSYS/Cygwin path
+ CHECK(!util::is_absolute_path(""));
+ CHECK(!util::is_absolute_path("foo\\fie/fum"));
+ CHECK(!util::is_absolute_path("C:foo/fie"));
+#endif
+ CHECK(util::is_absolute_path("/"));
+ CHECK(util::is_absolute_path("/foo/fie"));
+ CHECK(!util::is_absolute_path(""));
+ CHECK(!util::is_absolute_path("foo/fie"));
+}