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));
}
}
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)
{
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.
// 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());
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;
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);
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);
}
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);
}
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)
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;
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);