Skip to content

Commit bf96a8e

Browse files
Add slash argument to GetRelativePath
Allows callers to specify which slash character to use when constructing relative paths.
1 parent 29cbcc6 commit bf96a8e

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

Platforms/Basic/interface/BasicFileSystem.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,16 @@ struct BasicFileSystem
229229
/// \param [in] PathTo - Path that defines the endpoint of the relative path.
230230
/// Must not be null.
231231
/// \param [in] IsToDirectory - Indicates if PathTo is a directory.
232+
/// \param [in] Slash - Slash symbol to use in the returned path.
233+
/// If 0, platform-specific slash is used.
232234
///
233235
/// \return Relative path from PathFrom to PathTo.
234236
/// If no relative path exists, PathFrom is returned.
235237
static std::string GetRelativePath(const Char* PathFrom,
236238
bool IsFromDirectory,
237239
const Char* PathTo,
238-
bool IsToDirectory);
240+
bool IsToDirectory,
241+
Char Slash = 0);
239242

240243
static std::string FileDialog(const FileDialogAttribs& DialogAttribs);
241244
static std::string OpenFolderDialog(const char* Title);

Platforms/Basic/src/BasicFileSystem.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,15 @@ std::string BasicFileSystem::SimplifyPath(const Char* Path, Char Slash)
289289
std::string BasicFileSystem::GetRelativePath(const Char* PathFrom,
290290
bool IsFromDirectory,
291291
const Char* PathTo,
292-
bool /*IsToDirectory*/)
292+
bool /*IsToDirectory*/,
293+
Char Slash)
293294
{
294295
DEV_CHECK_ERR(PathFrom != nullptr, "Source path must not be null");
295296
DEV_CHECK_ERR(PathTo != nullptr, "Destination path must not be null");
297+
if (Slash != 0)
298+
DEV_CHECK_ERR(IsSlash(Slash), "Incorrect slash symbol");
299+
else
300+
Slash = SlashSymbol;
296301

297302
const auto FromPathComps = SplitPath(PathFrom, true);
298303
const auto ToPathComps = SplitPath(PathTo, true);
@@ -321,15 +326,15 @@ std::string BasicFileSystem::GetRelativePath(const Char* PathFrom,
321326
}
322327

323328
if (!RelPath.empty())
324-
RelPath.push_back(SlashSymbol);
329+
RelPath.push_back(Slash);
325330
RelPath.append("..");
326331
}
327332

328333
for (; to_it != ToPathComps.end(); ++to_it)
329334
{
330335
// IsToDirectory is in fact irrelevant
331336
if (!RelPath.empty())
332-
RelPath.push_back(SlashSymbol);
337+
RelPath.push_back(Slash);
333338
RelPath.append(*to_it);
334339
}
335340

Tests/DiligentCoreTest/src/Platforms/FileSystemTest.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -372,30 +372,33 @@ TEST(Platforms_FileSystem, GetRelativePath)
372372
return Path;
373373
};
374374

375-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from", true, "a/b/c", true).c_str(), BuildPath({".."}));
376-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c", true).c_str(), BuildPath({"..", ".."}));
377-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c", true).c_str(), BuildPath({".."}));
378-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir/file", false, "a/b/c", true).c_str(), BuildPath({"..", ".."}));
379-
380-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from", true, "a/b/c/file", false).c_str(), BuildPath({"..", "file"}));
381-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c/file", false).c_str(), BuildPath({"..", "..", "file"}));
382-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/file", false).c_str(), BuildPath({"..", "file"}));
383-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir/file", false, "a/b/c/file", false).c_str(), BuildPath({"..", "..", "file"}));
384-
385-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/to", true).c_str(), BuildPath({"to"}));
386-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/to/dir", true).c_str(), BuildPath({"to", "dir"}));
387-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/to", true).c_str(), BuildPath({"to"}));
388-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/to/dir", true).c_str(), BuildPath({"to", "dir"}));
389-
390-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/file", false).c_str(), BuildPath({"file"}));
391-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/to/file", false).c_str(), BuildPath({"to", "file"}));
392-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/file2", false).c_str(), BuildPath({"file2"}));
393-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/to/file", false).c_str(), BuildPath({"to", "file"}));
394-
395-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c/to/dir", true).c_str(), BuildPath({"..", "..", "to", "dir"}));
396-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/to/dir", true).c_str(), BuildPath({"..", "to", "dir"}));
397-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c/to/file", false).c_str(), BuildPath({"..", "..", "to", "file"}));
398-
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/to/file", false).c_str(), BuildPath({"..", "to", "file"}));
375+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from", true, "a/b/c", true), BuildPath({".."}));
376+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c", true), BuildPath({"..", ".."}));
377+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c", true), BuildPath({".."}));
378+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir/file", false, "a/b/c", true), BuildPath({"..", ".."}));
379+
380+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from", true, "a/b/c/file", false), BuildPath({"..", "file"}));
381+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c/file", false), BuildPath({"..", "..", "file"}));
382+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/file", false), BuildPath({"..", "file"}));
383+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir/file", false, "a/b/c/file", false), BuildPath({"..", "..", "file"}));
384+
385+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/to", true), BuildPath({"to"}));
386+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/to/dir", true), BuildPath({"to", "dir"}));
387+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/to", true), BuildPath({"to"}));
388+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/to/dir", true), BuildPath({"to", "dir"}));
389+
390+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/file", false), BuildPath({"file"}));
391+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c", true, "a/b/c/to/file", false), BuildPath({"to", "file"}));
392+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/file2", false), BuildPath({"file2"}));
393+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/file", false, "a/b/c/to/file", false), BuildPath({"to", "file"}));
394+
395+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c/to/dir", true), BuildPath({"..", "..", "to", "dir"}));
396+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/to/dir", true), BuildPath({"..", "to", "dir"}));
397+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/dir", true, "a/b/c/to/file", false), BuildPath({"..", "..", "to", "file"}));
398+
EXPECT_EQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/to/file", false), BuildPath({"..", "to", "file"}));
399+
400+
EXPECT_STREQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/to/file", false, '/').c_str(), "../to/file");
401+
EXPECT_STREQ(FileSystem::GetRelativePath("a/b/c/from/file", false, "a/b/c/to/file", false, '\\').c_str(), "..\\to\\file");
399402
}
400403

401404

0 commit comments

Comments
 (0)