r4186: Auto commit for Debian build
[lml.git] / utils.lisp
1 ;;; $Id: utils.lisp,v 1.7 2003/03/12 17:01:48 kevin Exp $
2 ;;;;
3 ;;;; General purpose utilities
4
5 (in-package :lml)
6
7
8 (defmacro aif (test then &optional else)
9   `(let ((it ,test))
10      (if it ,then ,else)))
11
12 (defmacro awhen (test-form &body body)
13   `(aif ,test-form
14         (progn ,@body)))
15
16 (defun keyword-symbol? (x)
17   "Returns T if object is a symbol in the keyword package"
18   (and (symbolp x)
19        (string-equal "keyword" (package-name (symbol-package x)))))
20
21 (defun list-to-spaced-string (list)
22   (if (consp list) 
23       (format nil "~A~{ ~A~}" (first list) (rest list))
24       ""))
25
26 (defun indent-spaces (n &optional (stream *standard-output*))
27   "Indent n*2 spaces to output stream"
28   (let ((fmt (format nil "~~~DT" (+ n n))))
29     (format stream fmt)))
30
31 (defun print-file-contents (file &optional (strm *standard-output*))
32   "Opens a reads a file. Returns the contents as a single string"
33   (when (probe-file file)
34     (with-open-file (in file :direction :input)
35                     (do ((line (read-line in nil 'eof) 
36                                (read-line in nil 'eof)))
37                         ((eql line 'eof))
38                       (format strm "~A~%" line)))))
39
40 (defun date-string (ut)
41   (if (typep ut 'integer)
42       (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
43           (decode-universal-time ut)
44         (declare (ignore daylight-p zone))
45         (format nil "~[Mon~;Tue~;Wed~;Thu~;Fri~;Sat~;Sun~], ~d ~[Jan~;Feb~;Mar~;Apr~;May~;Jun~;Jul~;Aug~;Sep~;Oct~;Nov~;Dec~] ~d ~2,'0d:~2,'0d:~2,'0d" 
46                 dow
47                 day
48                 (1- mon)
49                 year
50                 hr min sec))))
51
52 (defun lml-quit (&optional (code 0))
53   "Function to exit the Lisp implementation. Copied from CLOCC's QUIT function."
54     #+allegro (excl:exit code)
55     #+clisp (#+lisp=cl ext:quit #-lisp=cl lisp:quit code)
56     #+(or cmu scl) (ext:quit code)
57     #+cormanlisp (win32:exitprocess code)
58     #+gcl (lisp:bye code)
59     #+lispworks (lw:quit :status code)
60     #+lucid (lcl:quit code)
61     #+sbcl (sb-ext:quit :unix-status (typecase code (number code) (null 0) (t 1)))
62     #+openmcl (ccl:quit code)
63     #+(and mcl (not openmcl)) (declare (ignore code))
64     #+(and mcl (not openmcl)) (ccl:quit)
65     #-(or allegro clisp cmu scl cormanlisp gcl lispworks lucid sbcl mcl)
66     (error 'not-implemented :proc (list 'quit code)))
67
68
69 (defun lml-cwd ()
70   "Returns the current working directory. Based on CLOCC's DEFAULT-DIRECTORY function."
71   #+allegro (excl:current-directory)
72   #+clisp (#+lisp=cl ext:default-directory #-lisp=cl lisp:default-directory)
73   #+(or cmu scl) (ext:default-directory)
74   #+cormanlisp (ccl:get-current-directory)
75   #+lispworks (hcl:get-working-directory)
76   #+lucid (lcl:working-directory)
77   #+sbcl (sb-unix:posix-getcwd/)
78   #+mcl (ccl:mac-default-directory)
79   #-(or allegro clisp cmu scl sbcl cormanlisp lispworks lucid mcl) (truename "."))
80
81