r7061: initial property settings
[lml2.git] / utils.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          utils.lisp
6 ;;;; Purpose:       General purpose utilities
7 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  June 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of LML2, is copyrighted and open-source software.
13 ;;;; Rights of modification and redistribution are in the LICENSE file.
14 ;;;;
15 ;;;; *************************************************************************
16
17 (in-package #:lml2)
18
19 (defmacro aif (test then &optional else)
20   `(let ((it ,test))
21      (if it ,then ,else)))
22
23 (defmacro awhen (test-form &body body)
24   `(aif ,test-form
25         (progn ,@body)))
26
27 (defun print-file-contents (file &optional (strm *standard-output*))
28   "Opens a reads a file. Returns the contents as a single string"
29   (when (probe-file file)
30     (let ((eof (cons 'eof nil)))
31       (with-open-file (in file :direction :input)
32         (do ((line (read-line in nil eof) 
33                    (read-line in nil eof)))
34             ((eq line eof))
35           (write-string line strm)
36           (write-char #\newline strm))))))
37
38 (defun date-string (ut)
39   (check-type 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 day (1- mon) year hr min sec)))
45
46 (defun lml-quit (&optional (code 0))
47   "Function to exit the Lisp implementation."
48     #+allegro (excl:exit code)
49     #+clisp (#+lisp=cl ext:quit #-lisp=cl lisp:quit code)
50     #+(or cmu scl) (ext:quit code)
51     #+cormanlisp (win32:exitprocess code)
52     #+gcl (lisp:bye code)
53     #+lispworks (lw:quit :status code)
54     #+lucid (lcl:quit code)
55     #+sbcl (sb-ext:quit :unix-status (typecase code (number code) (null 0) (t 1)))
56     #+openmcl (ccl:quit code)
57     #+(and mcl (not openmcl)) (declare (ignore code))
58     #+(and mcl (not openmcl)) (ccl:quit)
59     #-(or allegro clisp cmu scl cormanlisp gcl lispworks lucid sbcl mcl)
60     (error 'not-implemented :proc (list 'quit code)))
61
62
63 (defun lml-cwd ()
64   "Returns the current working directory. Based on CLOCC's DEFAULT-DIRECTORY function."
65   #+allegro (excl:current-directory)
66   #+clisp (#+lisp=cl ext:default-directory #-lisp=cl lisp:default-directory)
67   #+(or cmu scl) (ext:default-directory)
68   #+cormanlisp (ccl:get-current-directory)
69   #+lispworks (hcl:get-working-directory)
70   #+lucid (lcl:working-directory)
71   #+sbcl (sb-unix:posix-getcwd/)
72   #+mcl (ccl:mac-default-directory)
73   #-(or allegro clisp cmu scl sbcl cormanlisp lispworks lucid mcl) (truename "."))
74
75
76 #+ignore
77 (defun fformat (&rest args)
78   (declare (dynamic-extent args))
79   (apply (if (find-package 'kmrcl)
80              (symbol-function (intern (symbol-name #:fformat)
81                                       (symbol-name #:kmrcl)))
82              #'format)
83          args))
84
85 (defmacro fformat (stream control-string &rest args)
86   (if stream
87       `(funcall (formatter ,control-string) ,stream ,@args)
88       `(format nil ,control-string ,@args)))
89