Created
October 1, 2019 13:03
-
-
Save demotomohiro/81702d8a0bf951d6a46b9954093d69bb to your computer and use it in GitHub Desktop.
Test PathIsSameRootW and PathIsSameRootA Windows API in Nim language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import winlean, macros | |
when useWinUnicode: | |
proc PathIsSameRootW(pszPath1: WideCString, | |
pszPath2: WideCString): WINBOOL {. | |
stdcall, dynlib: "Shlwapi", importc: "PathIsSameRootW".} | |
else: | |
proc PathIsSameRootA(pszPath1: cstring, | |
pszPath2: cstring): WINBOOL {. | |
stdcall, dynlib: "Shlwapi", importc: "PathIsSameRootA".} | |
when defined(windows): | |
when useWinUnicode: | |
proc sameRoot(path1, path2: string): bool = | |
PathIsSameRootW(newWideCString(path1), newWideCString(path2)) != 0 | |
else: | |
proc sameRoot(path1, path2: string): bool = | |
PathIsSameRootA(path1, path2) != 0 | |
macro debug(n: varargs[typed]): untyped = | |
result = newNimNode(nnkStmtList, n) | |
for x in n: | |
# we can bind symbols in scope via 'bindSym': | |
add(result, newCall(bindSym"write", bindSym"stdout", toStrLit(x))) | |
add(result, newCall(bindSym"write", bindSym"stdout", newStrLitNode(": "))) | |
add(result, newCall(bindSym"writeLine", bindSym"stdout", x)) | |
debug sameRoot(r"c:\", r"c:\") | |
debug sameRoot(r"c:\", r"C:\") | |
debug sameRoot(r"c:\aa", r"c:\bb") | |
debug sameRoot(r"d:\aa", r"D:\bb") | |
debug sameRoot(r"c:\aa", r"d:\bb") | |
debug sameRoot(r"\a", r"\a") | |
debug sameRoot(r"\a", r"\b") | |
debug sameRoot(r"\a", r"\a\b") | |
debug sameRoot(r"\a\b\c", r"\a\b\d") | |
debug sameRoot(r"\\server\share\", r"\\server\share\") | |
debug sameRoot(r"\\server\share\a", r"\\server\share\b") | |
debug sameRoot(r"\\server\share\a", r"\\server\share2\a") | |
debug sameRoot(r"\\server2\share\x", r"\\server\share\x") | |
debug sameRoot(r"\\.\c:\a", r"\\.\c:\b") | |
debug sameRoot(r"\\.\c:\a", r"\\.\C:\b") | |
debug sameRoot(r"\\.\c:\a", r"\\.\d:\a") | |
debug sameRoot(r"\\.\e:\a", r"e:\b") | |
debug sameRoot(r"\\.\UNC\server\share\a", r"\\.\UNC\server\share\b") | |
debug sameRoot(r"\\.\UNC\server\share\a", r"\\.\UNC\server\share1\a") | |
debug sameRoot(r"\\.\UNC\server\share\a", r"\\.\UNC\server1\share\a") | |
debug sameRoot(r"\\.\Volume{b75e2c83-0000-0000-0000-602f00000000}\a", r"\\.\Volume{b75e2c83-0000-0000-0000-602f00000000}\b") | |
debug sameRoot(r"\\.\Volume{b75e2c83-0000-0000-0000-602f00000000}\a", r"\\.\Volume{b75e2c83-0000-0000-0000-602f000000XX}\a") | |
debug sameRoot(r"/foo/bar", r"/foo") | |
debug sameRoot(r"/foo/bar", r"/foo/bar") | |
debug sameRoot(r"/foo/bar/a", r"/foo/bar/a") | |
debug sameRoot(r"/foo/bar/a/b", r"/foo/bar/a/b") | |
debug sameRoot(r"\foo\bar/a/b", r"\foo\bar/a/b") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
On Windows 8.1 64bit