Skip to content

Commit 80c93f4

Browse files
committed
Merge branch 'main' into beta
2 parents 99563ab + dfa6367 commit 80c93f4

File tree

7 files changed

+64
-13
lines changed

7 files changed

+64
-13
lines changed

base/fs.cpp

+22-2
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

+2-1
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

+25
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ TEST(FS, GetAbsolutePath)
259259
#endif
260260
}
261261

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

base/fs_unix.h

+2-2
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

+1-1
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;

base/platform.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ struct Platform {
2828
};
2929

3030
enum class Arch {
31-
x86, // Windows 32-bit, Linux 32-bit
32-
x64, // Windows 64-bit, Mac Intel
33-
arm64, // Mac Apple Silicon (M1, M1 Pro), Raspberry Pi?
31+
x86, // Windows 32-bit, Linux 32-bit
32+
x64, // Windows 64-bit, Mac Intel
33+
arm64, // Mac Apple Silicon (M1, M1 Pro), Raspberry Pi?
34+
riscv64, // Linux RISC-V 64-bit
3435
};
3536

3637
static constexpr OS os =
@@ -46,6 +47,8 @@ struct Platform {
4647
static constexpr Arch arch =
4748
#if defined(__arm64__) || defined(__aarch64__)
4849
Arch::arm64
50+
#elif defined(__riscv) && __riscv_xlen == 64
51+
Arch::riscv64
4952
#elif defined(__x86_64__) || defined(_WIN64)
5053
Arch::x64
5154
#else
@@ -54,7 +57,8 @@ struct Platform {
5457
;
5558

5659
static_assert((arch == Arch::x86 && sizeof(void*) == 4) ||
57-
((arch == Arch::x64 || arch == Arch::arm64) && sizeof(void*) == 8),
60+
((arch == Arch::x64 || arch == Arch::arm64 || arch == Arch::riscv64) &&
61+
sizeof(void*) == 8),
5862
"Invalid identification of CPU architecture");
5963

6064
Version osVer;

examples/show_platform.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ int app_main(int argc, char* argv[])
3030
#endif
3131
}
3232
switch (p.arch) {
33-
case base::Platform::Arch::x86: std::printf(" (x86)"); break;
34-
case base::Platform::Arch::x64: std::printf(" (x64)"); break;
35-
case base::Platform::Arch::arm64: std::printf(" (arm64)"); break;
33+
case base::Platform::Arch::x86: std::printf(" (x86)"); break;
34+
case base::Platform::Arch::x64: std::printf(" (x64)"); break;
35+
case base::Platform::Arch::arm64: std::printf(" (arm64)"); break;
36+
case base::Platform::Arch::riscv64: std::printf(" (riscv64)"); break;
3637
}
3738
std::printf("\n");
3839
return 0;

0 commit comments

Comments
 (0)