Skip to content

Commit 207ed40

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 207ed40

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
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 && path[1] == ':' && path[2] == path_separator) ||
42+
// Absolute network path
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

0 commit comments

Comments
 (0)