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