r3056: *** empty log message ***
[lml.git] / utils.lisp
1 ;;; $Id: utils.lisp,v 1.3 2002/10/16 11:25:28 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   (format nil "~{ ~A~}" list))
23
24 (defun indent-spaces (n &optional (stream *standard-output*))
25   "Indent n*2 spaces to output stream"
26   (let ((fmt (format nil "~~~DT" (+ n n))))
27     (format stream fmt)))
28
29 (defun print-file-contents (file &optional (strm *standard-output*))
30   "Opens a reads a file. Returns the contents as a single string"
31   (when (probe-file file)
32     (with-open-file (in file :direction :input)
33                     (do ((line (read-line in nil 'eof) 
34                                (read-line in nil 'eof)))
35                         ((eql line 'eof))
36                       (format strm "~A~%" line)))))
37
38 (defun date-string (ut)
39   (if (typep ut 'integer)
40       (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
41           (decode-universal-time ut)
42         (declare (ignore daylight-p zone))
43         (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" 
44                 dow
45                 day
46                 (1- mon)
47                 year
48                 hr min sec))))
49
50 (defun lml-quit (&optional (code 0))
51   "Function to exit the Lisp implementation. Copied from CLOCC's QUIT function."
52     #+allegro (excl:exit code)
53     #+clisp (#+lisp=cl ext:quit #-lisp=cl lisp:quit code)
54     #+(or cmu scl) (ext:quit code)
55     #+cormanlisp (win32:exitprocess code)
56     #+gcl (lisp:bye code)
57     #+lispworks (lw:quit :status code)
58     #+lucid (lcl:quit code)
59     #+sbcl (sb-ext:quit :unix-status (typecase code (number code) (null 0) (t 1)))
60     #-(or allegro clisp cmu scl cormanlisp gcl lispworks lucid sbcl)
61     (error 'not-implemented :proc (list 'quit code)))
62
63
64 (defun lml-cwd ()
65   "Returns the current working directory. Based on CLOCC's DEFAULT-DIRECTORY function."
66   #+allegro (excl:current-directory)
67   #+clisp (#+lisp=cl ext:default-directory #-lisp=cl lisp:default-directory)
68   #+(or cmu scl) (ext:default-directory)
69   #+cormanlisp (ccl:get-current-directory)
70   #+lispworks (hcl:get-working-directory)
71   #+lucid (lcl:working-directory)
72   #+sbcl (sb-unix:posix-getcwd/)
73   #-(or allegro clisp cmu scl sbcl cormanlisp lispworks lucid) (truename "."))
74
75