From c617479c6848e07f25f92fd33b3397d65683812e Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 25 Nov 2011 17:19:01 +0200 Subject: New algorithm for locating and loading nimrod config files. Some new options added to the compiler (see news.txt for details) --- lib/pure/os.nim | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'lib') 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: -- cgit 1.4.1-2-gfad0