utils.lisp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. (in-package #:photo-store)
  2. (defun aget (key alist)
  3. (cdr (assoc key alist :test #'equal)))
  4. (defun agetter (key)
  5. (lambda (x) (aget key x)))
  6. ;; From 'Practical Common Lisp' by Peter Seibel
  7. (defun component-present-p (value)
  8. (and value (not (eql value :unspecific))))
  9. (defun directory-pathname-p (p)
  10. (and
  11. (not (component-present-p (pathname-name p)))
  12. (not (component-present-p (pathname-type p)))
  13. p))
  14. (defun pathname-as-directory (name)
  15. (let ((pathname (pathname name)))
  16. (when (wild-pathname-p pathname)
  17. (error "Can't reliably convert wild pathnames."))
  18. (if (not (directory-pathname-p name))
  19. (make-pathname
  20. :directory (append (or (pathname-directory pathname) (list :relative))
  21. (list (file-namestring pathname)))
  22. :name nil
  23. :type nil
  24. :defaults pathname)
  25. pathname)))
  26. (defun directory-wildcard (dirname)
  27. (make-pathname
  28. :name :wild
  29. :type #-clisp :wild #+clisp nil
  30. :defaults (pathname-as-directory dirname)))
  31. (defun list-directory (dirname)
  32. (when (wild-pathname-p dirname)
  33. (error "Can only list concrete directory names."))
  34. (let ((wildcard (directory-wildcard dirname)))
  35. #+(or sbcl cmu lispworks)
  36. (directory wildcard)
  37. #+openmcl
  38. (directory wildcard :directories t)
  39. #+allegro
  40. (directory wildcard :directories-are-files nil)
  41. #+clisp
  42. (nconc
  43. (directory wildcard)
  44. (directory (clisp-subdirectories-wildcard wildcard)))
  45. #-(or sbcl cmu lispworks openmcl allegro clisp)
  46. (error "list-directory not implemented")))
  47. #+clisp
  48. (defun clisp-subdirectories-wildcard (wildcard)
  49. (make-pathname
  50. :directory (append (pathname-directory wildcard) (list :wild))
  51. :name nil
  52. :type nil
  53. :defaults wildcard))
  54. (defun file-exists-p (pathname)
  55. #+(or sbcl lispworks openmcl)
  56. (probe-file pathname)
  57. #+(or allegro cmu)
  58. (or (probe-file (pathname-as-directory pathname))
  59. (probe-file pathname))
  60. #+clisp
  61. (or (ignore-errors
  62. (probe-file (pathname-as-file pathname)))
  63. (ignore-errors
  64. (let ((directory-form (pathname-as-directory pathname)))
  65. (when (ext:probe-directory directory-form)
  66. directory-form))))
  67. #-(or sbcl cmu lispworks openmcl allegro clisp)
  68. (error "file-exists-p not implemented"))
  69. (defun pathname-as-file (name)
  70. (let ((pathname (pathname name)))
  71. (when (wild-pathname-p pathname)
  72. (error "Can't reliably convert wild pathnames."))
  73. (if (directory-pathname-p name)
  74. (let* ((directory (pathname-directory pathname))
  75. (name-and-type (pathname (first (last directory)))))
  76. (make-pathname
  77. :directory (butlast directory)
  78. :name (pathname-name name-and-type)
  79. :type (pathname-type name-and-type)
  80. :defaults pathname))
  81. pathname)))
  82. (defun walk-directory (dirname fn &key directories (test (constantly t)))
  83. (labels
  84. ((walk (name)
  85. (cond
  86. ((directory-pathname-p name)
  87. (when (and directories (funcall test name))
  88. (funcall fn name))
  89. (when (funcall test name)
  90. (dolist (x (list-directory name)) (walk x))))
  91. ((funcall test name) (funcall fn name)))))
  92. (walk (pathname-as-directory dirname))))