summaryrefslogtreecommitdiffstats
path: root/source/slang/include-file-system.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2018-10-16 18:49:11 -0400
committerGitHub <noreply@github.com>2018-10-16 18:49:11 -0400
commit3e74d39f24fdfaa547ce900be177863e2bfe2dea (patch)
tree9a5143e6de4caa27b23fc870003011e96129905f /source/slang/include-file-system.cpp
parent204fb3c75b520a2cbb1c25f995a8c424ec2753f3 (diff)
Feature/include refactor (#675)
* Refactor of path handling. * Added PathInfo * Changed ISlangFileSystem - such that has separate concepts of reading a file, getting a relative path and getting a canonical path * Added support for getting a canonical path for windows/linux * Made maps/testing around canonicalPaths * User output remains around 'foundPath' - which is the same as before * Small improvements around PathInfo * Added a type and make constructors to make clear the different 'path' uses * Fixed bug in findViewRecursively * Checking and reporting for ignored #pragma once. * Removed SLANG_PATH_TYPE_NONE as doesn't serve any useful purpose. * Improve comments in slang.h aroung ISlangFileSystem * Remove the need for <windows.h> in slang-io.cpp * Ran premake5. * Improvements and fixes around PathInfo. * Fix typo on linix GetCanonical * Make the ISlangFileSystem the same as before, and ISlangFileSystem contain the new methods. Internally it always uses the ISlangFileSystemExt, and will wrap a ISlangFileSystem with WrapFileSystem, if it is determined (via queryInterface) that it doesn't implement the full interface.
Diffstat (limited to 'source/slang/include-file-system.cpp')
-rw-r--r--source/slang/include-file-system.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/source/slang/include-file-system.cpp b/source/slang/include-file-system.cpp
new file mode 100644
index 000000000..d2c1670fe
--- /dev/null
+++ b/source/slang/include-file-system.cpp
@@ -0,0 +1,101 @@
+#include "include-file-system.h"
+
+#include "../../slang-com-ptr.h"
+#include "../core/slang-io.h"
+#include "compiler.h"
+
+namespace Slang
+{
+
+// Allocate static const storage for the various interface IDs that the Slang API needs to expose
+static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown;
+static const Guid IID_ISlangFileSystem = SLANG_UUID_ISlangFileSystem;
+static const Guid IID_ISlangFileSystemExt = SLANG_UUID_ISlangFileSystemExt;
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!! IncludeFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+/* static */ISlangFileSystemExt* IncludeFileSystem::getDefault()
+{
+ static IncludeFileSystem s_includeFileSystem;
+ s_includeFileSystem.ensureRef();
+ return &s_includeFileSystem;
+}
+
+ISlangUnknown* IncludeFileSystem::getInterface(const Guid& guid)
+{
+ return (guid == IID_ISlangUnknown || guid == IID_ISlangFileSystem || guid == IID_ISlangFileSystemExt) ? static_cast<ISlangFileSystemExt*>(this) : nullptr;
+}
+
+SlangResult IncludeFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
+{
+ String canonicalPath;
+ SLANG_RETURN_ON_FAIL(Path::GetCanonical(path, canonicalPath));
+
+ *canonicalPathOut = createStringBlob(canonicalPath).detach();
+ return SLANG_OK;
+}
+
+SlangResult IncludeFileSystem::calcRelativePath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
+{
+ String relPath;
+ switch (fromPathType)
+ {
+ case SLANG_PATH_TYPE_FILE:
+ {
+ relPath = Path::Combine(Path::GetDirectoryName(fromPath), path);
+ break;
+ }
+ case SLANG_PATH_TYPE_DIRECTORY:
+ {
+ relPath = Path::Combine(fromPath, path);
+ break;
+ }
+ }
+
+ *pathOut = createStringBlob(relPath).detach();
+ return SLANG_OK;
+}
+
+SlangResult SLANG_MCALL IncludeFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
+{
+ // Otherwise, fall back to a default implementation that uses the `core`
+ // libraries facilities for talking to the OS filesystem.
+ //
+ // TODO: we might want to conditionally compile these in, so that
+ // a user could create a build of Slang that doesn't include any OS
+ // filesystem calls.
+ //
+
+ if (!File::Exists(path))
+ {
+ return SLANG_E_NOT_FOUND;
+ }
+
+ try
+ {
+ String sourceString = File::ReadAllText(path);
+ ComPtr<ISlangBlob> sourceBlob = createStringBlob(sourceString);
+ *outBlob = sourceBlob.detach();
+ return SLANG_OK;
+ }
+ catch (...)
+ {
+ }
+ return SLANG_E_CANNOT_OPEN;
+}
+
+/* !!!!!!!!!!!!!!!!!!!!!!!!!! WrapFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
+
+SlangResult SLANG_MCALL WrapFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
+{
+ return m_fileSystem->loadFile(path, outBlob);
+}
+
+SlangResult WrapFileSystem::getCanoncialPath(const char* path, ISlangBlob** canonicalPathOut)
+{
+ String canonicalPath(path);
+ *canonicalPathOut = createStringBlob(canonicalPath).detach();
+ return SLANG_OK;
+}
+
+} \ No newline at end of file