Skip to content

Commit b311782

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 b311782

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

base/fs.cpp

Lines changed: 22 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,18 @@ 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+
#if LAF_WINDOWS
41+
return (path.size() > 2 && std::isalpha(path[0]) && path[1] == ':' &&
42+
path[2] == path_separator) ||
43+
// UNC network path
44+
(path.size() > 1 && path[0] == path_separator && path[1] == path_separator);
45+
#else
46+
return !path.empty() && path[0] == path_separator;
47+
#endif
48+
}
49+
3850
void make_all_directories(const std::string& path)
3951
{
4052
std::vector<std::string> parts;
@@ -194,7 +206,15 @@ std::string get_relative_path(const std::string& filename, const std::string& ba
194206
auto itFrom = baseDirs.begin();
195207
auto itTo = toParts.begin();
196208

197-
while (itFrom != baseDirs.end() && itTo != toParts.end() && *itFrom == *itTo) {
209+
while (itFrom != baseDirs.end() &&
210+
itTo != toParts.end()
211+
#if LAF_LINUX
212+
// The Linux file system is case sensitive
213+
&& *itFrom == *itTo
214+
#else
215+
&& base::string_to_lower(*itFrom) == base::string_to_lower(*itTo)
216+
#endif
217+
) {
198218
++itFrom;
199219
++itTo;
200220
}

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: 2 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.
@@ -213,7 +213,7 @@ std::string get_canonical_path(const std::string& path)
213213
std::string get_absolute_path(const std::string& path)
214214
{
215215
std::string full = path;
216-
if (!full.empty() && full[0] != '/')
216+
if (!full.empty() && !is_absolute_path(full))
217217
full = join_path(get_current_path(), full);
218218
full = normalize_path(full);
219219
if (!full.empty() && full.back() == path_separator)

base/fs_win32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ std::string get_canonical_path(const std::string& path)
161161
std::string get_absolute_path(const std::string& path)
162162
{
163163
std::string full;
164-
if (path.size() > 2 && path[1] != ':')
164+
if (!full.empty() && !base::is_absolute_path(path))
165165
full = base::join_path(base::get_current_path(), path);
166166
else
167167
full = path;

0 commit comments

Comments
 (0)