From f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Tue, 29 Oct 2024 14:49:26 +0800 Subject: format * format * Minor test fixes * enable checking cpp format in ci --- source/core/slang-file-system.cpp | 362 ++++++++++++++++++++++++-------------- 1 file changed, 226 insertions(+), 136 deletions(-) (limited to 'source/core/slang-file-system.cpp') diff --git a/source/core/slang-file-system.cpp b/source/core/slang-file-system.cpp index d578e63d5..3e129f7bf 100644 --- a/source/core/slang-file-system.cpp +++ b/source/core/slang-file-system.cpp @@ -1,19 +1,24 @@ #include "slang-file-system.h" -#include "slang-com-ptr.h" #include "../core/slang-io.h" #include "../core/slang-string-util.h" +#include "slang-com-ptr.h" namespace Slang { -SLANG_FORCE_INLINE static SlangResult _checkExt(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Ext) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; } -SLANG_FORCE_INLINE static SlangResult _checkMutable(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Mutable) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; } +SLANG_FORCE_INLINE static SlangResult _checkExt(FileSystemStyle style) +{ + return Index(style) >= Index(FileSystemStyle::Ext) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; +} +SLANG_FORCE_INLINE static SlangResult _checkMutable(FileSystemStyle style) +{ + return Index(style) >= Index(FileSystemStyle::Mutable) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; +} SLANG_FORCE_INLINE static bool _canCast(FileSystemStyle style, const Guid& guid) { - if (guid == ISlangUnknown::getTypeGuid() || - guid == ISlangCastable::getTypeGuid() || + if (guid == ISlangUnknown::getTypeGuid() || guid == ISlangCastable::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid()) { return true; @@ -35,11 +40,14 @@ static FileSystemStyle _getFileSystemStyle(ISlangFileSystem* system, ComPtrqueryInterface(ISlangMutableFileSystem::getTypeGuid(), (void**)out.writeRef()))) + if (SLANG_SUCCEEDED( + system->queryInterface(ISlangMutableFileSystem::getTypeGuid(), (void**)out.writeRef()))) { - style = FileSystemStyle::Mutable; + style = FileSystemStyle::Mutable; } - else if (SLANG_SUCCEEDED(system->queryInterface(ISlangFileSystemExt::getTypeGuid(), (void**)out.writeRef()))) + else if (SLANG_SUCCEEDED(system->queryInterface( + ISlangFileSystemExt::getTypeGuid(), + (void**)out.writeRef()))) { style = FileSystemStyle::Ext; } @@ -54,17 +62,21 @@ static FileSystemStyle _getFileSystemStyle(ISlangFileSystem* system, ComPtr 0 && remaining[0] == '/') @@ -447,16 +480,17 @@ SlangResult CacheFileSystem::enumeratePathContents(const char* path, FileSystemC remaining = UnownedStringSlice(remaining.begin() + 1, remaining.end()); } - // If it has a path separator then it's either not simplified - so we ignore (we only want to invoke on the simplified path version as there is only one - // of these for every PathInfo) - // or it is a child file/directory, and so we ignore that too. + // If it has a path separator then it's either not simplified - so we ignore (we only want + // to invoke on the simplified path version as there is only one of these for every + // PathInfo) or it is a child file/directory, and so we ignore that too. if (remaining.indexOf('/') >= 0 || remaining.indexOf('\\') >= 0) { continue; } - // We *know* that remaining comes from the end of currentPath .We also know currentPath is zero terminated. - // So we can just use (normally this would be a problem because UnownedStringSlice is generally *not* followed by zero termination. + // We *know* that remaining comes from the end of currentPath .We also know currentPath is + // zero terminated. So we can just use (normally this would be a problem because + // UnownedStringSlice is generally *not* followed by zero termination. const char* foundPath = remaining.begin(); // Let's check that fact... SLANG_ASSERT(foundPath[remaining.getLength()] == 0); @@ -474,35 +508,42 @@ SlangResult CacheFileSystem::enumeratePathContents(const char* path, FileSystemC } -SlangResult CacheFileSystem::_calcUniqueIdentity(const String& path, String& outUniqueIdentity, ComPtr& outFileContents) +SlangResult CacheFileSystem::_calcUniqueIdentity( + const String& path, + String& outUniqueIdentity, + ComPtr& outFileContents) { switch (m_uniqueIdentityMode) { - case UniqueIdentityMode::FileSystemExt: + case UniqueIdentityMode::FileSystemExt: { // Try getting the uniqueIdentity by asking underlying file system ComPtr uniqueIdentity; - SLANG_RETURN_ON_FAIL(m_fileSystemExt->getFileUniqueIdentity(path.getBuffer(), uniqueIdentity.writeRef())); + SLANG_RETURN_ON_FAIL(m_fileSystemExt->getFileUniqueIdentity( + path.getBuffer(), + uniqueIdentity.writeRef())); // Get the path as a string outUniqueIdentity = StringUtil::getString(uniqueIdentity); return SLANG_OK; } - case UniqueIdentityMode::Path: + case UniqueIdentityMode::Path: { outUniqueIdentity = path; return SLANG_OK; } - case UniqueIdentityMode::SimplifyPath: + case UniqueIdentityMode::SimplifyPath: { outUniqueIdentity = Path::simplify(path); // If it still has relative elements can't uniquely identify, so give up return Path::hasRelativeElement(outUniqueIdentity) ? SLANG_FAIL : SLANG_OK; } - case UniqueIdentityMode::SimplifyPathAndHash: - case UniqueIdentityMode::Hash: + case UniqueIdentityMode::SimplifyPathAndHash: + case UniqueIdentityMode::Hash: { - // If m_uniqueIdentityMode is SimplifyPathAndHash, the path will already be simplified before this function is hit (and it hasn't been found - // via path lookup). That being the case only option left is to 'hash' (or fallback to backing impls uniqueIdentity impl) + // If m_uniqueIdentityMode is SimplifyPathAndHash, the path will already be simplified + // before this function is hit (and it hasn't been found via path lookup). That being + // the case only option left is to 'hash' (or fallback to backing impls uniqueIdentity + // impl) // If we don't have a file system -> assume cannot be found if (m_fileSystem == nullptr) @@ -519,27 +560,32 @@ SlangResult CacheFileSystem::_calcUniqueIdentity(const String& path, String& out // If that failed, we may be able to do something if m_fileSystemExt is available if (SLANG_FAILED(res)) { - // If we have m_fileSystemExt interface we can just use it's implementation, as a fallback. - // Doing so will mean the uniqueIdentity will work if say it's a directory + // If we have m_fileSystemExt interface we can just use it's implementation, as a + // fallback. Doing so will mean the uniqueIdentity will work if say it's a directory if (m_fileSystemExt) { ComPtr uniqueIdentity; - SLANG_RETURN_ON_FAIL(m_fileSystemExt->getFileUniqueIdentity(path.getBuffer(), uniqueIdentity.writeRef())); + SLANG_RETURN_ON_FAIL(m_fileSystemExt->getFileUniqueIdentity( + path.getBuffer(), + uniqueIdentity.writeRef())); // Get the path as a string outUniqueIdentity = StringUtil::getString(uniqueIdentity); return SLANG_OK; } - - // If we can't access as a file (or use the backing implementations impl), we are in a tricky situation. - // The ISlangFileSystem interface provides no way to determine if the path is a directory for example - - // so there is no way of determining if something along the path exists. - // + + // If we can't access as a file (or use the backing implementations impl), we are in + // a tricky situation. The ISlangFileSystem interface provides no way to determine + // if the path is a directory for example - so there is no way of determining if + // something along the path exists. + // // So we just return the error. return res; } - + // Calculate the hash on the contents - const StableHashCode64 hash = getStableHashCode64((const char*)outFileContents->getBufferPointer(), outFileContents->getBufferSize()); + const StableHashCode64 hash = getStableHashCode64( + (const char*)outFileContents->getBufferPointer(), + outFileContents->getBufferSize()); String hashString = Path::getFileName(path); hashString = hashString.toLower(); @@ -582,8 +628,8 @@ CacheFileSystem::PathInfo* CacheFileSystem::_resolveUniqueIdentityCacheInfo(cons // At this point they must have same uniqueIdentity SLANG_ASSERT(pathInfo->getUniqueIdentity() == uniqueIdentity); - // If we have the file contents (because of calc-ing uniqueIdentity), and there isn't a read file blob already - // store the data as if read, so doesn't get read again + // If we have the file contents (because of calc-ing uniqueIdentity), and there isn't a read + // file blob already store the data as if read, so doesn't get read again if (fileContents && !pathInfo->m_fileBlob) { pathInfo->m_fileBlob = fileContents; @@ -595,7 +641,8 @@ CacheFileSystem::PathInfo* CacheFileSystem::_resolveUniqueIdentityCacheInfo(cons CacheFileSystem::PathInfo* CacheFileSystem::_resolveSimplifiedPathCacheInfo(const String& path) { - // If we can simplify the path, try looking up in path cache with simplified path (as long as it's different!) + // If we can simplify the path, try looking up in path cache with simplified path (as long as + // it's different!) if (_canSimplifyPath(m_uniqueIdentityMode)) { const String simplifiedPath = Path::simplify(path); @@ -607,7 +654,7 @@ CacheFileSystem::PathInfo* CacheFileSystem::_resolveSimplifiedPathCacheInfo(cons } } - return _resolveUniqueIdentityCacheInfo(path); + return _resolveUniqueIdentityCacheInfo(path); } CacheFileSystem::PathInfo* CacheFileSystem::_resolvePathCacheInfo(const String& path) @@ -636,10 +683,11 @@ SlangResult CacheFileSystem::loadFile(char const* pathIn, ISlangBlob** blobOut) { return SLANG_FAIL; } - + if (info->m_loadFileResult == CompressedResult::Uninitialized) { - info->m_loadFileResult = toCompressedResult(m_fileSystem->loadFile(path.getBuffer(), info->m_fileBlob.writeRef())); + info->m_loadFileResult = toCompressedResult( + m_fileSystem->loadFile(path.getBuffer(), info->m_fileBlob.writeRef())); } *blobOut = info->m_fileBlob; @@ -663,16 +711,20 @@ SlangResult CacheFileSystem::getFileUniqueIdentity(const char* path, ISlangBlob* return SLANG_OK; } -SlangResult CacheFileSystem::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) +SlangResult CacheFileSystem::calcCombinedPath( + SlangPathType fromPathType, + const char* fromPath, + const char* path, + ISlangBlob** pathOut) { // Just defer to contained implementation switch (m_pathStyle) { - case PathStyle::FileSystemExt: + case PathStyle::FileSystemExt: { return m_fileSystemExt->calcCombinedPath(fromPathType, fromPath, path, pathOut); } - default: + default: { // Just use the default implementation return _calcCombinedPath(fromPathType, fromPath, path, pathOut); @@ -680,20 +732,25 @@ SlangResult CacheFileSystem::calcCombinedPath(SlangPathType fromPathType, const } } -SlangResult CacheFileSystem::_getPathType(PathInfo* info, const char* inPath, SlangPathType* outPathType) +SlangResult CacheFileSystem::_getPathType( + PathInfo* info, + const char* inPath, + SlangPathType* outPathType) { if (info->m_getPathTypeResult == CompressedResult::Uninitialized) { if (m_fileSystemExt) { - info->m_getPathTypeResult = toCompressedResult(m_fileSystemExt->getPathType(inPath, &info->m_pathType)); + info->m_getPathTypeResult = + toCompressedResult(m_fileSystemExt->getPathType(inPath, &info->m_pathType)); } else { // Okay try to load the file if (info->m_loadFileResult == CompressedResult::Uninitialized) { - info->m_loadFileResult = toCompressedResult(m_fileSystem->loadFile(inPath, info->m_fileBlob.writeRef())); + info->m_loadFileResult = + toCompressedResult(m_fileSystem->loadFile(inPath, info->m_fileBlob.writeRef())); } // Make the getPathResult the same as the load result @@ -722,9 +779,9 @@ SlangResult CacheFileSystem::getPath(PathKind kind, const char* path, ISlangBlob { switch (kind) { - case PathKind::Simplified: return _getSimplifiedPath(path, outPath); - case PathKind::Canonical: return _getCanonicalPath(path, outPath); - default: break; + case PathKind::Simplified: return _getSimplifiedPath(path, outPath); + case PathKind::Canonical: return _getCanonicalPath(path, outPath); + default: break; } if (m_fileSystemExt) @@ -746,17 +803,17 @@ SlangResult CacheFileSystem::_getSimplifiedPath(const char* path, ISlangBlob** o // If we have a ISlangFileSystemExt we can just pass on the request to it switch (m_pathStyle) { - case PathStyle::FileSystemExt: + case PathStyle::FileSystemExt: { return m_fileSystemExt->getPath(PathKind::Simplified, path, outSimplifiedPath); } - case PathStyle::Simplifiable: + case PathStyle::Simplifiable: { String simplifiedPath = Path::simplify(path); *outSimplifiedPath = StringUtil::createStringBlob(simplifiedPath).detach(); return SLANG_OK; } - default: return SLANG_E_NOT_IMPLEMENTED; + default: return SLANG_E_NOT_IMPLEMENTED; } } @@ -764,7 +821,7 @@ SlangResult CacheFileSystem::_getCanonicalPath(const char* path, ISlangBlob** ou { *outCanonicalPath = nullptr; - // A file must exist to get a canonical path... + // A file must exist to get a canonical path... PathInfo* info = _resolvePathCacheInfo(path); if (!info) { @@ -781,7 +838,8 @@ SlangResult CacheFileSystem::_getCanonicalPath(const char* path, ISlangBlob** ou // Try getting the canonicalPath by asking underlying file system ComPtr canonicalPathBlob; - SlangResult res = m_fileSystemExt->getPath(PathKind::Canonical, path, canonicalPathBlob.writeRef()); + SlangResult res = + m_fileSystemExt->getPath(PathKind::Canonical, path, canonicalPathBlob.writeRef()); if (SLANG_SUCCEEDED(res)) { @@ -808,9 +866,11 @@ SlangResult CacheFileSystem::_getCanonicalPath(const char* path, ISlangBlob** ou /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RelativeFileSystem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -RelativeFileSystem::RelativeFileSystem(ISlangFileSystem* fileSystem, const String& relativePath, bool stripPath) : - m_relativePath(relativePath), - m_stripPath(stripPath) +RelativeFileSystem::RelativeFileSystem( + ISlangFileSystem* fileSystem, + const String& relativePath, + bool stripPath) + : m_relativePath(relativePath), m_stripPath(stripPath) { m_style = _getFileSystemStyle(fileSystem, m_fileSystem); @@ -849,7 +909,11 @@ void* RelativeFileSystem::castAs(const Guid& guid) return getObject(guid); } -SlangResult RelativeFileSystem::_calcCombinedPathInner(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** outPath) +SlangResult RelativeFileSystem::_calcCombinedPathInner( + SlangPathType fromPathType, + const char* fromPath, + const char* path, + ISlangBlob** outPath) { ISlangFileSystemExt* fileSystem = _getExt(); if (fileSystem) @@ -866,14 +930,15 @@ SlangResult RelativeFileSystem::_getCanonicalPath(const char* path, String& outP { if (m_stripPath) { - // We are just using the filename. There is no path that could go outside of the the relative path so we can use as is + // We are just using the filename. There is no path that could go outside of the the + // relative path so we can use as is outPath = Path::getFileName(path); } else { // NOTE that we don't want the canonical path to be absolute with a leading "/" // because paths specified which aren't absolute, would produce a different path. - // + // // Ie we want (and get with these options) // "a" -> "a" // "/a" -> "a". @@ -881,15 +946,16 @@ SlangResult RelativeFileSystem::_getCanonicalPath(const char* path, String& outP // If we allowed the root to be included then... // "a" -> "a" // "/a" -> "/a" - // + // // Two identical paths would match to different paths, which wouldn't be canonical. - // - // This could be fixed by making all paths absolute with '/' too, but it's easier to just make all not - // have "/" + // + // This could be fixed by making all paths absolute with '/' too, but it's easier to just + // make all not have "/" StringBuilder canonicalPath; // We want the input path to be local to this file system - SLANG_RETURN_ON_FAIL(Path::simplify(path, Path::SimplifyStyle::AbsoluteOnlyAndNoRoot, canonicalPath)); + SLANG_RETURN_ON_FAIL( + Path::simplify(path, Path::SimplifyStyle::AbsoluteOnlyAndNoRoot, canonicalPath)); outPath = canonicalPath; } return SLANG_OK; @@ -901,10 +967,14 @@ SlangResult RelativeFileSystem::_getFixedPath(const char* path, String& outPath) String canonicalPath; SLANG_RETURN_ON_FAIL(_getCanonicalPath(path, canonicalPath)); - - SLANG_RETURN_ON_FAIL(_calcCombinedPathInner(SLANG_PATH_TYPE_DIRECTORY, m_relativePath.getBuffer(), canonicalPath.getBuffer(), blob.writeRef())); + + SLANG_RETURN_ON_FAIL(_calcCombinedPathInner( + SLANG_PATH_TYPE_DIRECTORY, + m_relativePath.getBuffer(), + canonicalPath.getBuffer(), + blob.writeRef())); outPath = StringUtil::getString(blob); - + return SLANG_OK; } @@ -915,20 +985,28 @@ SlangResult RelativeFileSystem::loadFile(char const* path, ISlangBlob** outBlob) return m_fileSystem->loadFile(fixedPath.getBuffer(), outBlob); } -SlangResult RelativeFileSystem::getFileUniqueIdentity(const char* path, ISlangBlob** outUniqueIdentity) +SlangResult RelativeFileSystem::getFileUniqueIdentity( + const char* path, + ISlangBlob** outUniqueIdentity) { auto fileSystem = _getExt(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); return fileSystem->getFileUniqueIdentity(fixedPath.getBuffer(), outUniqueIdentity); } -SlangResult RelativeFileSystem::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** outPath) +SlangResult RelativeFileSystem::calcCombinedPath( + SlangPathType fromPathType, + const char* fromPath, + const char* path, + ISlangBlob** outPath) { auto fileSystem = _getExt(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedFromPath; SLANG_RETURN_ON_FAIL(_getFixedPath(fromPath, fixedFromPath)); @@ -939,7 +1017,8 @@ SlangResult RelativeFileSystem::calcCombinedPath(SlangPathType fromPathType, con SlangResult RelativeFileSystem::getPathType(const char* path, SlangPathType* outPathType) { auto fileSystem = _getExt(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); @@ -949,28 +1028,30 @@ SlangResult RelativeFileSystem::getPathType(const char* path, SlangPathType* out SlangResult RelativeFileSystem::getPath(PathKind kind, const char* path, ISlangBlob** outPath) { auto fileSystem = _getExt(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; - + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; + switch (kind) { - case PathKind::Simplified: + case PathKind::Simplified: { return fileSystem->getPath(kind, path, outPath); } - case PathKind::Display: + case PathKind::Display: { // If not backed by OS, just use simplified path, else use the Operating system path - kind = (fileSystem->getOSPathKind() == OSPathKind::None) ? PathKind::Simplified : PathKind::OperatingSystem; + kind = (fileSystem->getOSPathKind() == OSPathKind::None) ? PathKind::Simplified + : PathKind::OperatingSystem; return getPath(kind, path, outPath); } - case PathKind::Canonical: - { + case PathKind::Canonical: + { String canonicalPath; - SLANG_RETURN_ON_FAIL(_getCanonicalPath(path, canonicalPath)); + SLANG_RETURN_ON_FAIL(_getCanonicalPath(path, canonicalPath)); *outPath = StringBlob::moveCreate(canonicalPath).detach(); return SLANG_OK; } - case PathKind::OperatingSystem: + case PathKind::OperatingSystem: { String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); @@ -984,15 +1065,20 @@ SlangResult RelativeFileSystem::getPath(PathKind kind, const char* path, ISlangB void RelativeFileSystem::clearCache() { auto fileSystem = _getExt(); - if (!fileSystem) return; + if (!fileSystem) + return; fileSystem->clearCache(); } -SlangResult RelativeFileSystem::enumeratePathContents(const char* path, FileSystemContentsCallBack callback, void* userData) +SlangResult RelativeFileSystem::enumeratePathContents( + const char* path, + FileSystemContentsCallBack callback, + void* userData) { auto fileSystem = _getExt(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); @@ -1002,7 +1088,8 @@ SlangResult RelativeFileSystem::enumeratePathContents(const char* path, FileSyst SlangResult RelativeFileSystem::saveFile(const char* path, const void* data, size_t size) { auto fileSystem = _getMutable(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); @@ -1012,8 +1099,9 @@ SlangResult RelativeFileSystem::saveFile(const char* path, const void* data, siz SlangResult RelativeFileSystem::saveFileBlob(const char* path, ISlangBlob* dataBlob) { auto fileSystem = _getMutable(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; - + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; + String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); return fileSystem->saveFileBlob(fixedPath.getBuffer(), dataBlob); @@ -1022,7 +1110,8 @@ SlangResult RelativeFileSystem::saveFileBlob(const char* path, ISlangBlob* dataB SlangResult RelativeFileSystem::remove(const char* path) { auto fileSystem = _getMutable(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); @@ -1032,11 +1121,12 @@ SlangResult RelativeFileSystem::remove(const char* path) SlangResult RelativeFileSystem::createDirectory(const char* path) { auto fileSystem = _getMutable(); - if (!fileSystem) return SLANG_E_NOT_IMPLEMENTED; + if (!fileSystem) + return SLANG_E_NOT_IMPLEMENTED; String fixedPath; SLANG_RETURN_ON_FAIL(_getFixedPath(path, fixedPath)); return fileSystem->createDirectory(fixedPath.getBuffer()); } -} +} // namespace Slang -- cgit v1.2.3