home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / unxdir.scm < prev    next >
Text File  |  2001-05-12  |  3KB  |  87 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: unxdir.scm,v 14.12 2001/05/12 20:03:03 cph Exp $
  4.  
  5. Copyright (c) 1988-2001 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  20. USA.
  21. |#
  22.  
  23. ;;;; Directory Operations -- unix
  24. ;;; package: (runtime directory)
  25.  
  26. (declare (usual-integrations))
  27.  
  28. (define *expand-directory-prefixes?* true)
  29.  
  30. (define (directory-read pattern #!optional sort?)
  31.   (if (if (default-object? sort?) true sort?)
  32.       (sort (directory-read-nosort pattern) pathname<?)
  33.       (directory-read-nosort pattern)))
  34.  
  35. (define (directory-read-nosort pattern)
  36.   (let ((pattern
  37.      (let ((pattern (merge-pathnames pattern)))
  38.        (if (directory-pathname? pattern)
  39.            (make-pathname (pathname-host pattern)
  40.                   (pathname-device pattern)
  41.                   (pathname-directory pattern)
  42.                   'WILD
  43.                   'WILD
  44.                   (pathname-version pattern))
  45.            pattern))))
  46.     (let ((directory-path (directory-pathname pattern)))
  47.       (map (lambda (pathname)
  48.          (merge-pathnames pathname directory-path))
  49.        (let ((pathnames
  50.           (let ((fnames (generate-directory-pathnames directory-path)))
  51.             (fluid-let ((*expand-directory-prefixes?* false))
  52.               (map ->pathname fnames)))))
  53.          (if (and (eq? (pathname-name pattern) 'WILD)
  54.               (eq? (pathname-type pattern) 'WILD))
  55.          pathnames
  56.          (list-transform-positive pathnames
  57.            (lambda (instance)
  58.              (and (match-component (pathname-name pattern)
  59.                        (pathname-name instance))
  60.               (match-component (pathname-type pattern)
  61.                        (pathname-type instance)))))))))))
  62.  
  63. (define (generate-directory-pathnames pathname)
  64.   (let ((channel (directory-channel-open (->namestring pathname))))
  65.     (let loop ((result '()))
  66.       (let ((name (directory-channel-read channel)))
  67.     (if name
  68.         (loop (cons name result))
  69.         (begin
  70.           (directory-channel-close channel)
  71.           result))))))
  72.  
  73. (define (match-component pattern instance)
  74.   (or (eq? pattern 'WILD)
  75.       (equal? pattern instance)))
  76.  
  77. (define (pathname<? x y)
  78.   (or (component<? (pathname-name x) (pathname-name y))
  79.       (and (equal? (pathname-name x) (pathname-name y))
  80.        (component<? (pathname-type x) (pathname-type y)))))
  81.  
  82. (define (component<? x y)
  83.   (and y
  84.        (or (not x)
  85.        (and (string? y)
  86.         (or (not (string? x))
  87.             (string<? x y))))))