diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-08-02 02:55:18 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-09-18 22:53:57 +0200 |
commit | 23cf6047ae320a1d666ddf6418bade67dfa95ada (patch) | |
tree | 1f5897d93efa3ed8de49ce4ebd199b40106f29ea /module/hnh/util/path.scm | |
parent | Extend module-imports to work on non-module scheme files. (diff) | |
download | calp-23cf6047ae320a1d666ddf6418bade67dfa95ada.tar.gz calp-23cf6047ae320a1d666ddf6418bade67dfa95ada.tar.xz |
Add procedure relative-to.
Diffstat (limited to 'module/hnh/util/path.scm')
-rw-r--r-- | module/hnh/util/path.scm | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/module/hnh/util/path.scm b/module/hnh/util/path.scm index 0c8af48a..ed2d9f24 100644 --- a/module/hnh/util/path.scm +++ b/module/hnh/util/path.scm @@ -8,7 +8,8 @@ path-split file-hidden? filename-extension - realpath)) + realpath + relative-to)) (define // file-name-separator-string) (define /? file-name-separator?) @@ -90,3 +91,31 @@ (if (absolute-file-name? filename) filename (path-append (getcwd) filename))) + + +(define (relative-to base path) + ;; (typecheck base string?) + ;; (typecheck path string?) + + (when (string-null? base) + (error "Base can't be empty" )) + + (let ((base (if (absolute-file-name? base) + base + (path-append (getcwd) base)))) + + (cond ((equal? '("") base) path) + ((not (absolute-file-name? path)) + (path-append base path)) + (else + (let loop ((a (path-split base)) + (b (path-split path))) + (cond + ((null? a) (path-join b)) + ((null? b) path) + ((string=? (car a) (car b)) (loop (cdr a) (cdr b))) + (else + (path-join + (append + (make-list (length a) "..") + (drop b (length a))))))))))) |