summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2011-11-25 17:19:01 +0200
committerZahary Karadjov <zahary@gmail.com>2011-11-25 17:29:55 +0200
commitc617479c6848e07f25f92fd33b3397d65683812e (patch)
tree8d147e84c3f7e424c542bc444d8bb0d1d1bd0d78 /lib
parented9c7761c4e37ec22ebd81acf16e3137d064cfc9 (diff)
downloadNim-c617479c6848e07f25f92fd33b3397d65683812e.tar.gz
New algorithm for locating and loading nimrod config files.
Some new options added to the compiler (see news.txt for details)
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/os.nim32
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: