diff options
author | Araq <rumpf_a@web.de> | 2011-11-26 10:49:48 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-11-26 10:49:48 +0100 |
commit | 840979b45caea47c97c3eaac145841464ab5e5b1 (patch) | |
tree | e362829bad55797efc0542993a8303b412022560 /lib/pure | |
parent | f7f0c90ffe11952472ef6246c90c32d0e65c6b5f (diff) | |
parent | c617479c6848e07f25f92fd33b3397d65683812e (diff) | |
download | Nim-840979b45caea47c97c3eaac145841464ab5e5b1.tar.gz |
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'lib/pure')
-rwxr-xr-x | lib/pure/os.nim | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index c7e3dccb4..626fbc7eb 100755 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -408,6 +408,36 @@ proc parentDir*(path: string): string {. else: result = path +proc isRootDir*(path: string): bool {. + noSideEffect, rtl, extern: "nos$1".} = + ## Checks whether a given `path` is a root directory + var p = parentDir(path) + result = p == path or p.len == 0 + +iterator parentDirs*(path: string, fromRoot = false, inclusive = true): string = + ## Walks over all parent directories of a given `path` + ## + ## If `fromRoot` is set, the traversal will start from the file system root + ## diretory. If `inclusive` is set, the original argument will be included + ## in the traversal. + ## + ## Relative paths won't be expanded by this proc. Instead, it will traverse + ## only the directories appearing in the relative path. + if not fromRoot: + var current = path + if inclusive: yield path + while true: + if current.isRootDir: break + current = current.parentDir + yield current + else: + for i in countup(0, path.len - 2): # ignore the last / + # deal with non-normalized paths such as /foo//bar//baz + if path[i] in {dirsep, altsep} and (i == 0 or path[i-1] notin {dirsep, altsep}): + yield path.substr(0, i) + + if inclusive: yield path + proc `/../` * (head, tail: string): string {.noSideEffect.} = ## The same as ``parentDir(head) / tail`` return parentDir(head) / tail @@ -522,7 +552,7 @@ proc cmpPaths*(pathA, pathB: string): int {. result = cmpIgnoreCase(pathA, pathB) proc isAbsolute*(path: string): bool {.rtl, noSideEffect, extern: "nos$1".} = - ## Checks whether a given path is absolute. + ## Checks whether a given `path` is absolute. ## ## on Windows, network paths are considered absolute too. when doslike: |