Skip to content

Commit 4d4c5fd

Browse files
committed
Add 'is_absolute_path()' function
Also, the 'get_relative_path(pathA, pathB)' function has been made case-insensitive. This is done to correctly calculate common folders in case one of the entered paths has a case difference.
1 parent bd36a11 commit 4d4c5fd

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

base/fs.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Base Library
2-
// Copyright (c) 2021-2024 Igara Studio S.A.
2+
// Copyright (c) 2021-2025 Igara Studio S.A.
33
// Copyright (c) 2001-2018 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -35,6 +35,17 @@ const std::string::value_type* path_separators = "\\/";
3535
const std::string::value_type* path_separators = "/";
3636
#endif
3737

38+
bool is_absolute_path(const std::string& path)
39+
{
40+
#ifdef LAF_WINDOWS
41+
return (path.size() > 2 && std::isalpha(path[0]) && path[1] == ':' && path[2] == path_separator)
42+
// Network path (UNC)
43+
|| (path.size() > 1 && path[0] == path_separator && path[1] == path_separator);
44+
#else
45+
return !path.empty() && path[0] == path_separator;
46+
#endif
47+
}
48+
3849
void make_all_directories(const std::string& path)
3950
{
4051
std::vector<std::string> parts;
@@ -194,7 +205,8 @@ std::string get_relative_path(const std::string& filename, const std::string& ba
194205
auto itFrom = baseDirs.begin();
195206
auto itTo = toParts.begin();
196207

197-
while (itFrom != baseDirs.end() && itTo != toParts.end() && *itFrom == *itTo) {
208+
while (itFrom != baseDirs.end() && itTo != toParts.end() &&
209+
base::string_to_lower(*itFrom) == base::string_to_lower((*itTo))) {
198210
++itFrom;
199211
++itTo;
200212
}

base/fs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Base Library
2-
// Copyright (c) 2020-2024 Igara Studio S.A.
2+
// Copyright (c) 2020-2025 Igara Studio S.A.
33
// Copyright (c) 2001-2018 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -28,6 +28,7 @@ extern const std::string::value_type* path_separators;
2828

2929
bool is_file(const std::string& path);
3030
bool is_directory(const std::string& path);
31+
bool is_absolute_path(const std::string& path);
3132

3233
size_t file_size(const std::string& path);
3334

base/fs_tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,31 @@ TEST(FS, GetAbsolutePath)
258258
#endif
259259
}
260260

261+
TEST(FS, IsAbsolutePath)
262+
{
263+
EXPECT_FALSE(is_absolute_path(""));
264+
EXPECT_FALSE(is_absolute_path("a"));
265+
EXPECT_FALSE(is_absolute_path("./a"));
266+
EXPECT_FALSE(is_absolute_path("../a"));
267+
EXPECT_FALSE(is_absolute_path("."));
268+
EXPECT_FALSE(is_absolute_path("./."));
269+
EXPECT_FALSE(is_absolute_path("./a/.."));
270+
EXPECT_FALSE(is_absolute_path(".\\//."));
271+
272+
#if LAF_WINDOWS
273+
EXPECT_TRUE(is_absolute_path("C:\\path\\..\\file"));
274+
EXPECT_TRUE(is_absolute_path("\\\\network\\path"));
275+
EXPECT_FALSE(is_absolute_path("C:user\\name\\"));
276+
EXPECT_FALSE(is_absolute_path("C\\user\\name\\"));
277+
EXPECT_FALSE(is_absolute_path("\\user\\path\\"));
278+
EXPECT_FALSE(is_absolute_path(".:\\user\\name"));
279+
EXPECT_FALSE(is_absolute_path("\\:\\user\\name"));
280+
#else
281+
EXPECT_TRUE(is_absolute_path("/path/../file"));
282+
EXPECT_FALSE(is_absolute_path("path/../file"));
283+
#endif
284+
}
285+
261286
TEST(FS, GetCanonicalPath)
262287
{
263288
const auto cp = get_current_path();

base/fs_unix.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Base Library
2-
// Copyright (c) 2021-2024 Igara Studio S.A.
2+
// Copyright (c) 2021-2025 Igara Studio S.A.
33
// Copyright (c) 2001-2018 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -212,9 +212,8 @@ std::string get_canonical_path(const std::string& path)
212212

213213
std::string get_absolute_path(const std::string& path)
214214
{
215-
std::string full = path;
216-
if (!full.empty() && full[0] != '/')
217-
full = join_path(get_current_path(), full);
215+
std::string full(is_absolute_path(path) ?
216+
path : join_path(get_current_path(), path));
218217
full = normalize_path(full);
219218
if (!full.empty() && full.back() == path_separator)
220219
full.erase(full.size() - 1);

base/fs_win32.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,8 @@ std::string get_canonical_path(const std::string& path)
160160

161161
std::string get_absolute_path(const std::string& path)
162162
{
163-
std::string full;
164-
if (path.size() > 2 && path[1] != ':')
165-
full = base::join_path(base::get_current_path(), path);
166-
else
167-
full = path;
163+
std::string full(base::is_absolute_path(path) ?
164+
path : base::join_path(base::get_current_path(), path));
168165

169166
TCHAR buffer[MAX_PATH + 1] = {};
170167
GetFullPathName(from_utf8(full).c_str(), sizeof(buffer) / sizeof(TCHAR), buffer, nullptr);

0 commit comments

Comments
 (0)