scheme shell
about
download
support
resources
docu
links
 
scsh.net
From: mmc@maruska.dyndns.org (Michal Maruška)
Newsgroups: comp.lang.scheme.scsh
Subject: Re: scsh script --- comment on path handling
Message-ID: <m2u1pdhaz1.fsf@linux11.maruska.tin.it>
Date: 12 May 2002 13:17:06 +0200


-*-scheme-*-

Andrew Tarr <arc@stuff.gen.nz> writes:

> You can find this script at http://arc.stuff.gen.nz/arcfilter.scsh I
> would appreciate any comments or feedback that anyone can give. None

my comment:

this approach is ugly imho:

(define (relativize-to-dir filename dirname)
    (let* ((ndirname (file-name-as-directory dirname))
	   (index (string-length ndirname)))
    (if (string= filename ndirname 0 index 0 index)
	(string-drop filename index)
	(filename))))


Better is to operate on the algebra of paths.
I use this:  (find it in http://maruska.dyndns.org/comp/scsh/scheme/fs/tree.scm
		   and http://maruska.dyndns.org/comp/scsh/scheme/packages.scm)


;; inverse of path-list->file-name:
(define (file-name->path-list filename)
  (split-file-name filename))


;; let's have 2 sets, 1 w/ an operation, and 2 mappings
;;
;; get the (inverse) image of op. on:
;;     args      result
;;trans|          ^
;;     V    proc  | inverse
;;     ARGS-------> RES
;;
(define (call-in-image proc trans inverse args)
  (let ((images (map trans args)))
    (inverse
     (apply proc images))))


;; fixme:  standard ?
(define (nthcdr list n)
  (cond ((= n 0) list)
	(#t (nthcdr (cdr list) (- n 1)))))

;;;  given 2 paths, the difference (not relation !!)
(define (minus path root) ; get the subpath from root -> path
  ;; only if the start  is equal !!
  (let ((l (length root))
	(lp (length path)))
    (if (and (>= lp l)
	     (list= string= root (take path l)))
	(nthcdr path (length root))
      (begin
	(error "cannot minus" path root)))))

(define (minus* path-string root-string) ; get the subpath from root -> path
  (call-in-image
   minus
   file-name->path-list
   path-list->file-name
   (list path-string root-string)))

Up