Skip to content

Instantly share code, notes, and snippets.

@demotomohiro
Created October 1, 2019 13:03
Show Gist options
  • Save demotomohiro/81702d8a0bf951d6a46b9954093d69bb to your computer and use it in GitHub Desktop.
Save demotomohiro/81702d8a0bf951d6a46b9954093d69bb to your computer and use it in GitHub Desktop.
Test PathIsSameRootW and PathIsSameRootA Windows API in Nim language.
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")
@demotomohiro
Copy link
Author

demotomohiro commented Oct 1, 2019

Output:

sameRoot(r"c:\", r"c:\"): true
sameRoot(r"c:\", r"C:\"): true
sameRoot(r"c:\aa", r"c:\bb"): true
sameRoot(r"d:\aa", r"D:\bb"): true
sameRoot(r"c:\aa", r"d:\bb"): false
sameRoot(r"\a", r"\a"): true
sameRoot(r"\a", r"\b"): true
sameRoot(r"\a", r"\a\b"): true
sameRoot(r"\a\b\c", r"\a\b\d"): true
sameRoot(r"\\server\share\", r"\\server\share\"): true
sameRoot(r"\\server\share\a", r"\\server\share\b"): true
sameRoot(r"\\server\share\a", r"\\server\share2\a"): false
sameRoot(r"\\server2\share\x", r"\\server\share\x"): false
sameRoot(r"\\.\c:\a", r"\\.\c:\b"): true
sameRoot(r"\\.\c:\a", r"\\.\C:\b"): true
sameRoot(r"\\.\c:\a", r"\\.\d:\a"): false
sameRoot(r"\\.\e:\a", r"e:\b"): false
sameRoot(r"\\.\UNC\server\share\a", r"\\.\UNC\server\share\b"): true
sameRoot(r"\\.\UNC\server\share\a", r"\\.\UNC\server\share1\a"): true
sameRoot(r"\\.\UNC\server\share\a", r"\\.\UNC\server1\share\a"): true
sameRoot(r"\\.\Volume{b75e2c83-0000-0000-0000-602f00000000}\a",
         r"\\.\Volume{b75e2c83-0000-0000-0000-602f00000000}\b"): true
sameRoot(r"\\.\Volume{b75e2c83-0000-0000-0000-602f00000000}\a",
         r"\\.\Volume{b75e2c83-0000-0000-0000-602f000000XX}\a"): false
sameRoot(r"/foo/bar", r"/foo"): false
sameRoot(r"/foo/bar", r"/foo/bar"): false
sameRoot(r"/foo/bar/a", r"/foo/bar/a"): false
sameRoot(r"/foo/bar/a/b", r"/foo/bar/a/b"): false
sameRoot(r"\foo\bar/a/b", r"\foo\bar/a/b"): true
f:\temp>nim -v
Nim Compiler Version 1.0.99 [Windows: amd64]
Compiled at 2019-10-01
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 64d5e2582164a965e05934e9c9b5c371a04f2765
active boot switches: -d:release

On Windows 8.1 64bit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment