summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-06-18 18:22:43 -0400
committerTim Foley <tfoleyNV@users.noreply.github.com>2019-06-18 15:22:43 -0700
commite213e394d5008cb7bf08bbf875acd494259f5847 (patch)
tree75a82a19aaf6be8204c1f8b4ceb446d02a1f06d7
parent896e460dc8146bc9a39296ea72f320fe3db28cee (diff)
* Added Path::combineBuilder, so as two sub paths can be combined into a StringBuilder (#988)
* Renamed and improved comments on SharedLibrary
-rw-r--r--source/core/slang-io.cpp39
-rw-r--r--source/core/slang-io.h5
-rw-r--r--source/core/slang-platform.cpp32
-rw-r--r--source/core/slang-platform.h16
-rw-r--r--source/core/slang-shared-library.cpp4
-rw-r--r--tools/slang-test/slang-test-main.cpp2
-rw-r--r--tools/slang-test/test-context.cpp2
7 files changed, 72 insertions, 28 deletions
diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp
index 83f21d5dd..01c8b38e0 100644
--- a/source/core/slang-io.cpp
+++ b/source/core/slang-io.cpp
@@ -137,13 +137,40 @@ namespace Slang
String Path::combine(const String& path1, const String& path2)
{
if (path1.getLength() == 0) return path2;
- StringBuilder sb(path1.getLength()+path2.getLength()+2);
- sb.Append(path1);
- if (!path1.endsWith('\\') && !path1.endsWith('/'))
- sb.Append(kPathDelimiter);
- sb.Append(path2);
- return sb.ProduceString();
+
+ StringBuilder sb;
+ combineBuilder(path1.getUnownedSlice(), path2.getUnownedSlice(), sb);
+ return sb.ProduceString();
}
+ /* static */void Path::combineBuilder(const UnownedStringSlice& path1, const UnownedStringSlice& path2, StringBuilder& outBuilder)
+ {
+ outBuilder.Clear();
+ // Make sure we have the space for the full result
+ outBuilder.EnsureCapacity(path1.size() + 2 + path2.size());
+
+ if (path1.size() == 0)
+ {
+ outBuilder.append(path2);
+ return;
+ }
+
+ outBuilder.append(path1);
+
+ // If path1 doesn't end in a delimiter, add one
+ if (!isDelimiter(path1[path1.size() - 1]))
+ {
+ outBuilder.append(kPathDelimiter);
+ }
+
+ // Check that path2 doesn't start with a path delimiter
+ if (path2.size() > 0)
+ {
+ SLANG_ASSERT(!isDelimiter(path2[0]));
+ }
+ // Append the second path
+ outBuilder.append(path2);
+ }
+
String Path::combine(const String& path1, const String& path2, const String& path3)
{
StringBuilder sb(path1.getLength()+path2.getLength()+path3.getLength()+3);
diff --git a/source/core/slang-io.h b/source/core/slang-io.h
index 4fa921662..91cfaf290 100644
--- a/source/core/slang-io.h
+++ b/source/core/slang-io.h
@@ -29,8 +29,13 @@ namespace Slang
static String getFileNameWithoutExt(const String& path);
static String getFileExt(const String& path);
static String getParentDirectory(const String& path);
+
static String combine(const String& path1, const String& path2);
static String combine(const String& path1, const String& path2, const String& path3);
+
+ /// Combine path sections and store the result in outBuilder
+ static void combineBuilder(const UnownedStringSlice& path1, const UnownedStringSlice& path2, StringBuilder& outBuilder);
+
static bool createDirectory(const String& path);
/// Accept either style of delimiter
diff --git a/source/core/slang-platform.cpp b/source/core/slang-platform.cpp
index 82f914f7d..8055d3e55 100644
--- a/source/core/slang-platform.cpp
+++ b/source/core/slang-platform.cpp
@@ -19,32 +19,40 @@ namespace Slang
{
// SharedLibrary
-/* static */SlangResult SharedLibrary::load(const char* filename, SharedLibrary::Handle& handleOut)
+/* static */SlangResult SharedLibrary::load(const char* path, SharedLibrary::Handle& handleOut)
{
StringBuilder builder;
- appendPlatformFileName(UnownedStringSlice(filename), builder);
- return loadWithPlatformFilename(builder.begin(), handleOut);
+ calcPlatformPath(UnownedStringSlice(path), builder);
+ return loadWithPlatformPath(builder.begin(), handleOut);
}
-/* static */String SharedLibrary::calcPlatformPath(const UnownedStringSlice& path)
+/* static */void SharedLibrary::calcPlatformPath(const UnownedStringSlice& path, StringBuilder& outPath)
{
// Work out the shared library name
String parent = Path::getParentDirectory(path);
String filename = Path::getFileName(path);
-
- StringBuilder builder;
- SharedLibrary::appendPlatformFileName(filename.getUnownedSlice(), builder);
if (parent.getLength() > 0)
{
- return Path::combine(parent, builder);
+ // Work out the filename platform name (as in add .dll say on windows)
+ StringBuilder platformFileNameBuilder;
+ SharedLibrary::appendPlatformFileName(filename.getUnownedSlice(), platformFileNameBuilder);
+
+ Path::combineBuilder(parent.getUnownedSlice(), platformFileNameBuilder.getUnownedSlice(), outPath);
}
else
- {
- return builder;
+ {
+ appendPlatformFileName(filename.getUnownedSlice(), outPath);
}
}
+/* static */String SharedLibrary::calcPlatformPath(const UnownedStringSlice& path)
+{
+ StringBuilder builder;
+ calcPlatformPath(path, builder);
+ return builder.ToString();
+}
+
#ifdef _WIN32
// Make sure SlangResult match for common standard window HRESULT
@@ -80,7 +88,7 @@ SLANG_COMPILE_TIME_ASSERT(E_OUTOFMEMORY == SLANG_E_OUT_OF_MEMORY);
return SLANG_FAIL;
}
-/* static */SlangResult SharedLibrary::loadWithPlatformFilename(char const* platformFileName, SharedLibrary::Handle& handleOut)
+/* static */SlangResult SharedLibrary::loadWithPlatformPath(char const* platformFileName, SharedLibrary::Handle& handleOut)
{
handleOut = nullptr;
// https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibrarya
@@ -136,7 +144,7 @@ SLANG_COMPILE_TIME_ASSERT(E_OUTOFMEMORY == SLANG_E_OUT_OF_MEMORY);
return SLANG_E_NOT_IMPLEMENTED;
}
-/* static */SlangResult SharedLibrary::loadWithPlatformFilename(char const* platformFileName, Handle& handleOut)
+/* static */SlangResult SharedLibrary::loadWithPlatformPath(char const* platformFileName, Handle& handleOut)
{
handleOut = nullptr;
diff --git a/source/core/slang-platform.h b/source/core/slang-platform.h
index c322d0568..0e6d12cb6 100644
--- a/source/core/slang-platform.h
+++ b/source/core/slang-platform.h
@@ -15,24 +15,27 @@ namespace Slang
typedef void(*FuncPtr)(void);
- /// Load via an unadorned filename.
+ /// Load via an unadorned path/filename.
///
- /// The unadorned filename here means without any platform specific filename elements. This typically means no extension and no prefix.
+ /// The unadorned path here means without a path without any platform specific filename elements.
+ /// This typically means no extension and no prefix.
/// On windows this means without the '.dll' extension.
/// On linux this means without the 'lib' prefix and '.so' extension.
/// To load with a platform specific filename use the 'loadWithPlatformFilename' API
+ /// Most platforms have a built in mechanism to search for shared libraries, such that
+ /// shared libraries are often just passed by filename.
///
- /// @param the unadorned filename
+ /// @param the unadorned filename/path
/// @return Returns a non null handle for the shared library on success. nullptr indicated failure
- static SlangResult load(const char* filename, Handle& handleOut);
+ static SlangResult load(const char* path, Handle& handleOut);
/// Attempt to load a shared library for
/// the current platform. Returns null handle on failure
/// The platform specific filename can be generated from a call to appendPlatformFileName
///
- /// @param platformFileName the platform specific file name.
+ /// @param platform the platform specific file name/ or path
/// @return Returns a non null handle for the shared library on success. nullptr indicated failure
- static SlangResult loadWithPlatformFilename(char const* platformFileName, Handle& handleOut);
+ static SlangResult loadWithPlatformPath(char const* platformPath, Handle& handleOut);
/// Unload the library that was returned from load as handle
/// @param The valid handle returned from load
@@ -49,6 +52,7 @@ namespace Slang
/// Given a path, calculate that path with the filename replaced with the platform filename (using appendPlatformFilename)
static String calcPlatformPath(const UnownedStringSlice& path);
+ static void calcPlatformPath(const UnownedStringSlice& path, StringBuilder& outBuilder);
private:
/// Not constructible!
diff --git a/source/core/slang-shared-library.cpp b/source/core/slang-shared-library.cpp
index 009abf921..9932db0b5 100644
--- a/source/core/slang-shared-library.cpp
+++ b/source/core/slang-shared-library.cpp
@@ -99,7 +99,7 @@ SlangResult ConfigurableSharedLibraryLoader::loadSharedLibrary(const char* path,
{
SLANG_UNUSED(pathIn);
// The replacement is the *whole* string
- return SharedLibrary::loadWithPlatformFilename(entryString.begin(), handleOut);
+ return SharedLibrary::loadWithPlatformPath(entryString.begin(), handleOut);
}
/* static */Result ConfigurableSharedLibraryLoader::changePath(const char* pathIn, const String& entryString, SharedLibrary::Handle& handleOut )
@@ -109,7 +109,7 @@ SlangResult ConfigurableSharedLibraryLoader::loadSharedLibrary(const char* path,
SharedLibrary::appendPlatformFileName(UnownedStringSlice(pathIn), builder);
String path = Path::combine(entryString, builder);
- return SharedLibrary::loadWithPlatformFilename(path.begin(), handleOut);
+ return SharedLibrary::loadWithPlatformPath(path.begin(), handleOut);
}
diff --git a/tools/slang-test/slang-test-main.cpp b/tools/slang-test/slang-test-main.cpp
index e792abe72..6eae194e6 100644
--- a/tools/slang-test/slang-test-main.cpp
+++ b/tools/slang-test/slang-test-main.cpp
@@ -1234,7 +1234,7 @@ static TestResult runCPPCompilerSharedLibrary(TestContext* context, TestInput& i
}
SharedLibrary::Handle handle;
- if (SLANG_FAILED(SharedLibrary::loadWithPlatformFilename(sharedLibraryPath.getBuffer(), handle)))
+ if (SLANG_FAILED(SharedLibrary::loadWithPlatformPath(sharedLibraryPath.getBuffer(), handle)))
{
return TestResult::Fail;
}
diff --git a/tools/slang-test/test-context.cpp b/tools/slang-test/test-context.cpp
index 90052c4c4..85e0a121b 100644
--- a/tools/slang-test/test-context.cpp
+++ b/tools/slang-test/test-context.cpp
@@ -61,7 +61,7 @@ TestContext::InnerMainFunc TestContext::getInnerMainFunc(const String& dirPath,
SharedLibraryTool tool = {};
- if (SLANG_SUCCEEDED(SharedLibrary::loadWithPlatformFilename(path.begin(), tool.m_sharedLibrary)))
+ if (SLANG_SUCCEEDED(SharedLibrary::loadWithPlatformPath(path.begin(), tool.m_sharedLibrary)))
{
tool.m_func = (InnerMainFunc)SharedLibrary::findFuncByName(tool.m_sharedLibrary, "innerMain");
}