diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-06-18 18:22:43 -0400 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-06-18 15:22:43 -0700 |
| commit | e213e394d5008cb7bf08bbf875acd494259f5847 (patch) | |
| tree | 75a82a19aaf6be8204c1f8b4ceb446d02a1f06d7 /source/core | |
| parent | 896e460dc8146bc9a39296ea72f320fe3db28cee (diff) | |
* Added Path::combineBuilder, so as two sub paths can be combined into a StringBuilder (#988)
* Renamed and improved comments on SharedLibrary
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-io.cpp | 39 | ||||
| -rw-r--r-- | source/core/slang-io.h | 5 | ||||
| -rw-r--r-- | source/core/slang-platform.cpp | 32 | ||||
| -rw-r--r-- | source/core/slang-platform.h | 16 | ||||
| -rw-r--r-- | source/core/slang-shared-library.cpp | 4 |
5 files changed, 70 insertions, 26 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); } |
