Guest
Public paste!

DanielRM

By: a guest | Sep 28th, 2008 | Syntax: None | Size: 2.97 KB | Hits: 28 | Expires: Never
Copy text to clipboard
  1. ;;; rips.lisp
  2. ;;; A simple database for CD rips
  3. ;;; by Daniel Rhodes-Mumby (DanielRM)
  4. ;;;
  5. ;;; Shamelessly adapted from Peter Seibel's "Practical Common Lisp"
  6. ;;;
  7. ;;; September 2008
  8. ;;;
  9. ;;; Function definitions are preceded by ';;' and an explanation of their purpose.
  10. ;;; Inline comments are preceded by ';'
  11. ;;;
  12. ;; Define the startup function and dependencies.
  13. ;; Also define the the save database function for completeness.
  14. (defun startup ()
  15.   (format t "~a" "Welcome to the Rips-Lisp CD rips database program.")
  16.   ;say hello
  17.  
  18.   (defun db-load (filename)
  19.     ;define the load database function
  20.    
  21.     (with-open-file (in filename)
  22.       (with-standard-io-syntax
  23.         (setf *db-rips* (read in)))))
  24.         ;this clobbers the working database
  25.  
  26.   (defun db-save (filename)
  27.     ;define the save database function
  28.    
  29.     (with-open-file (out filename
  30.                          :direction :output
  31.                          :if-exists :supersede)
  32.       ;overwrite an existing file - if the user's smart they'll avoid doing this without thinking
  33.      
  34.       (with-standard-io-syntax
  35.         (print *db-rips* out))))
  36.         ;let the user know the contents of the database
  37.  
  38.   (defvar *db-rips* nil))
  39.   ;define the database variable
  40. ;;;
  41. ;; Define the function for making CD records.
  42. (defun cd-create (artist title priority ripped-mp3 ripped-ogg)
  43.   (list :artist artist :title title :priority priority :ripped-mp3 ripped-mp3 :ripped-ogg ripped-ogg))
  44. ;;;
  45. ;; Define the function for showing the database.
  46. (defun db-dump (&optional (db *db-rips*))
  47.   (dolist (cd db)
  48.     (format t "~{~a:~20t~a~%~}~%" cd)))
  49.     ;I quite honestly have no idea how most of this format call works.
  50. ;;;
  51. ;; Define record manipulation functions.
  52. (defun cd-add (cd)
  53.   (push cd *db-rips*))
  54.   ;push the record on to the database variable.
  55. ;;;
  56. ;; Define a prompt reader.
  57. (defun prompt-read (prompt)
  58.   (format *query-io* "~a: " prompt)
  59.   (force-output *query-io*)
  60.   (read-line *query-io*))
  61. ;;;
  62. ;; Prompt for the CD now.
  63. (defun cd-prompt ()
  64.   (cd-create
  65.     (prompt-read "Artist")
  66.     (prompt-read "Album name")
  67.     (or (parse-integer (prompt-read "Priority") :junk-allowed t) 0)
  68.     (y-or-n-p "Ripped to MP3 (you dirty capitalist) [y/n]: ")
  69.     (y-or-n-p "Ripped to OGG (freedom uber alles!) [y/n]: ")))
  70. ;;;
  71. ;; Continually prompt.
  72. (defun cd-prompter ()
  73.   (loop (cd-add (cd-prompt))
  74.         (if (not (y-or-n-p "Another? [y/n]: "))
  75.         ;loop around until the user doesn't want to add another CD
  76.           (return))))
  77. ;;;
  78. ;; Write a selector function for queries.
  79. (defun select (selector-function)
  80.   (db-dump (remove-if-not selector-function *db-rips*)))
  81. ;;;
  82. ;; Define a general selection category function.
  83. (defun where (&key artist title priority (ripped-mp3 nil ripped-mp3-p) (ripped-ogg nil ripped-ogg-p))
  84.   #'(lambda (cd)
  85.       (and
  86.         (if artist      (equal (getf cd :artist) artist) t)
  87.         (if title       (equal (getf cd :title) title) t)
  88.         (if priority    (equal (getf cd :priority) priority) t)
  89.         (if ripped-mp3-p (equal (getf cd :ripped-mp3) ripped-mp3) t)
  90.         (if ripped-ogg-p (equal (getf cd :ripped-ogg) ripped-ogg) t))))
  91. ;;; Actually run the startup function.
  92. (startup)