summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/core/platform.cpp5
-rw-r--r--source/core/slang-io.cpp68
-rw-r--r--source/core/slang-io.h15
-rw-r--r--source/slang/slang-file-system.cpp2
4 files changed, 84 insertions, 6 deletions
diff --git a/source/core/platform.cpp b/source/core/platform.cpp
index ff7d8e231..e7575d21e 100644
--- a/source/core/platform.cpp
+++ b/source/core/platform.cpp
@@ -150,9 +150,14 @@ SLANG_COMPILE_TIME_ASSERT(E_OUTOFMEMORY == SLANG_E_OUT_OF_MEMORY);
/* static */void SharedLibrary::appendPlatformFileName(const UnownedStringSlice& name, StringBuilder& dst)
{
+#if __CYGWIN__
+ dst.Append(name);
+ dst.Append(".dll");
+#else
dst.Append("lib");
dst.Append(name);
dst.Append(".so");
+#endif
}
#endif // _WIN32
diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp
index 7385934c3..332718cea 100644
--- a/source/core/slang-io.cpp
+++ b/source/core/slang-io.cpp
@@ -1,6 +1,8 @@
#include "slang-io.h"
#include "exception.h"
+#include "../../slang-com-helper.h"
+
#ifndef __STDC__
# define __STDC__ 1
#endif
@@ -152,7 +154,39 @@ namespace Slang
}
default: return false;
}
-
+ }
+
+ UnownedStringSlice Path::getFirstElement(const UnownedStringSlice& in)
+ {
+ const char* end = in.end();
+ const char* cur = in.begin();
+ // Find delimiter or the end
+ while (cur < end && !Path::isDelimiter(*cur)) ++cur;
+ return UnownedStringSlice(in.begin(), cur);
+ }
+
+ /* static */bool Path::isAbsolute(const UnownedStringSlice& path)
+ {
+ if (path.size() > 0 && isDelimiter(path[0]))
+ {
+ return true;
+ }
+
+#if SLANG_WINDOWS_FAMILY
+ // Check for the \\ network drive style
+ if (path.size() >= 2 && path[0] == '\\' && path[1] == '\\')
+ {
+ return true;
+ }
+
+ // Check for drive
+ if (isDriveSpecification(getFirstElement(path)))
+ {
+ return true;
+ }
+#endif
+
+ return false;
}
/* static */void Path::split(const UnownedStringSlice& path, List<UnownedStringSlice>& splitOut)
@@ -186,7 +220,7 @@ namespace Slang
}
}
- /* static */bool Path::isRelative(const UnownedStringSlice& path)
+ /* static */bool Path::hasRelativeElement(const UnownedStringSlice& path)
{
List<UnownedStringSlice> splitPath;
split(path, splitPath);
@@ -316,6 +350,8 @@ namespace Slang
::free(absPath);
return SLANG_OK;
#else
+# if 1
+
// http://man7.org/linux/man-pages/man3/realpath.3.html
char* canonicalPath = ::realpath(path.begin(), nullptr);
if (canonicalPath)
@@ -325,6 +361,34 @@ namespace Slang
return SLANG_OK;
}
return SLANG_FAIL;
+# else
+ // This is a mechanism to get an approximation of canonical path if we don't have 'realpath'
+ // We only can get if the file exists. This checks that the ../. etc are really valid
+ SlangPathType pathType;
+ SLANG_RETURN_ON_FAIL(getPathType(path, &pathType));
+ if (isAbsolute(path))
+ {
+ // If it's absolute, we can just simplify as is
+ canonicalPathOut = Path::simplify(path);
+ return SLANG_OK;
+ }
+ else
+ {
+ char buffer[PATH_MAX];
+ // https://linux.die.net/man/3/getcwd
+ const char* getCwdPath = getcwd(buffer, SLANG_COUNT_OF(buffer));
+ if (!getCwdPath)
+ {
+ return SLANG_FAIL;
+ }
+
+ // Okay combine the paths
+ String combinedPaths = Path::combine(String(getCwdPath), path);
+ // Simplify
+ canonicalPathOut = Path::simplify(combinedPaths);
+ return SLANG_OK;
+ }
+# endif
#endif
}
diff --git a/source/core/slang-io.h b/source/core/slang-io.h
index a982b7adc..9df8d8d57 100644
--- a/source/core/slang-io.h
+++ b/source/core/slang-io.h
@@ -45,9 +45,13 @@ namespace Slang
static String simplify(const UnownedStringSlice& path);
static String simplify(const String& path) { return simplify(path.getUnownedSlice()); }
- /// Returns true if a path contains a . or ..
- static bool isRelative(const UnownedStringSlice& path);
- static bool isRelative(const String& path) { return isRelative(path.getUnownedSlice()); }
+ /// Returns true if the path is absolute
+ static bool isAbsolute(const UnownedStringSlice& path);
+ static bool isAbsolute(const String& path) { return isAbsolute(path.getUnownedSlice()); }
+
+ /// Returns true if path contains contains an element of . or ..
+ static bool hasRelativeElement(const UnownedStringSlice& path);
+ static bool hasRelativeElement(const String& path) { return hasRelativeElement(path.getUnownedSlice()); }
/// Determines the type of file at the path
/// @param path The path to test
@@ -66,6 +70,11 @@ namespace Slang
/// @return The path in platform native format. Returns empty string if failed.
static String getExecutablePath();
+ /// Returns the first element of the path or an empty slice if there is none
+ /// This broadly equivalent to returning the first element of split
+ /// @param path Path to extract first element from
+ /// @return The first element of the path, or empty
+ static UnownedStringSlice getFirstElement(const UnownedStringSlice& path);
};
}
diff --git a/source/slang/slang-file-system.cpp b/source/slang/slang-file-system.cpp
index 2583a7562..58cda679d 100644
--- a/source/slang/slang-file-system.cpp
+++ b/source/slang/slang-file-system.cpp
@@ -249,7 +249,7 @@ SlangResult CacheFileSystem::_calcUniqueIdentity(const String& path, String& out
{
outUniqueIdentity = Path::simplify(path);
// If it still has relative elements can't uniquely identify, so give up
- return Path::isRelative(outUniqueIdentity) ? SLANG_FAIL : SLANG_OK;
+ return Path::hasRelativeElement(outUniqueIdentity) ? SLANG_FAIL : SLANG_OK;
}
case UniqueIdentityMode::SimplifyPathAndHash:
case UniqueIdentityMode::Hash: