Re: Portable directory listing function

Karsten Poeck <[email protected]> Sun, 29 Feb 2004 13:08:43 +0100
Newsgroups gmane.lisp.corman
Message-ID <[email protected]>
--=====================_73349951==_
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit



"Peter Seibel" <[email protected]> wrote in message 
news:<[email protected]>...
So I'm trying to write a function that returns a list of pathnames
representing files contained in a given subdirectory including
immediate subdirectories. Further, I want the subdirectories to be
represented by pathnames in "directory form", i.e. with all the name
elements in the directory component of the pathname as opposed to
"file form" where the last name element is split up into the name and
type components. Here's what I've got so far which I've tested on
current versions of SBCL, CMUCL, Lispworks, OpenMCL, Allegro, and
CLISP. If you have easy access to other implementations and care to
test this out, let me know what readtime conditionalization I need to
add.


In corman this is

#+cormanlisp
(multiple-value-bind
   (files dirs)
     (cl::directory-files-and-subdirs wildcard)
   (append files dirs))

but wild-pathname and file-namestring are not defined and :wild often not 
treated correctly

The attached patches fixes that

saludos

Karsten

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/cormanlisp/

<*> To unsubscribe from this group, send an email to:
     [email protected]

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 

--=====================_73349951==_
Content-Type: text/plain; charset="us-ascii"
Content-Disposition: attachment; filename="pathnames-pathnames.lisp"

;;; treat :wild a bit

(in-package :pathnames)

(defun convert-pathname-to-namestring (pathname)
	(let ((device (pathname-internal-device pathname))
		  (directory (pathname-internal-directory pathname))
		  (type (pathname-internal-type pathname))
		  (name (pathname-internal-name pathname)))
		(format nil "~{~A:~}~{~A~}~{~A\\~}~{~A~}~{.~A~}"
			(if device (list device) nil)
			(if (or (eq directory :wild)(eq (car directory) :absolute))
				'(#\\) nil)
			(if(eq directory :wild)
                 (list "*")
                (cdr directory))
			(if name 
                (if (eq name :wild) (list "*")
                    (list name))
                 nil)
			(if type 
                (if (eq type :wild) (list "*")
                    (list type))
                 nil))))
--=====================_73349951==_
Content-Type: text/plain; charset="us-ascii"
Content-Disposition: attachment; filename="wild-pathname-p.lisp"

(in-package :common-lisp)

(defun wild-pathname-p (path &optional field)
    ;;;;  field can be :host, :device  :directory, :name, :type, :version, or nil.
    ;;;   but use of wild in host or device seems not to be allowed
    ;;;   o well
    
    ;;; will that work on streams???
    (unless (pathnamep path)
        (setq path (pathname path)))
    
    (labels
        ((wild-name (name)
            (or (eq :wild name)
                (and (stringp name)
                    (string= "*" name))))
         (wild-dir (dir)
            (or (find :wild dir)
                    (find :wild-inferiors dir)
                    (find "*" dir :test #'string=)))
         )
    
    (if (null field)
            (or (wild-name (pathname-name path))
                (wild-name (pathname-type path))
                (wild-dir (pathname-directory path)))
        (case field
            (:name (wild-name (pathname-name path)))
            (:type (wild-name (pathname-type path)))
            (:directory (wild-dir (pathname-directory path)))
            (t nil)))))

#|
(wild-pathname-p "c:\\")
(wild-pathname-p "c:\\test.otto")
(wild-pathname-p "c:\\*")
(wild-pathname-p "c:\\datat\\*.*")
(wild-pathname-p "c:\\"test.*")
|#

(defun file-namestring (pathname)
    (unless (pathnamep pathname)
        (setq pathname (pathname pathname)))
    (let ((name (pathname-name pathname))
            (type (pathname-type pathname)))
    (concatenate 'string 
            (if name 
                (if (eq name :wild)
                    "*"
                    name)
                 "")
            "."
            (if type 
                (if (eq type :wild)
                    "*"
                    type)
                 ""))))
            
--=====================_73349951==_
Content-Type: text/plain; charset="us-ascii"; format=flowed


--=====================_73349951==_--