home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / defs1.lsp < prev    next >
Encoding:
Text File  |  1994-12-12  |  26.2 KB  |  690 lines

  1. ;;;; Einige Definitionen von Standard-Funktionen in LISP
  2. ;;;; 1.8.1989, 2.9.1989, 8.10.1989
  3.  
  4. (in-package "LISP")
  5. (export '(doseq dohash #-UNIX *default-time-zone* default-directory dir))
  6. (in-package "SYSTEM")
  7.  
  8.  
  9. ;;; Funktionen fⁿr Symbole (Kapitel 10)
  10.  
  11. (defun copy-symbol (symbol &optional flag)
  12.                    ;; Common LISP, S. 169
  13.   (let ((sym (make-symbol (symbol-name symbol))))
  14.     (when flag
  15.       (when (boundp symbol) (set sym (symbol-value symbol)))
  16.       (when (fboundp symbol) (sys::%putd sym (symbol-function symbol)))
  17.       (sys::%putplist sym (copy-list (symbol-plist symbol)))
  18.     )
  19.     sym
  20. ) )
  21.  
  22. (let ((gentemp-count 0))
  23.   (defun gentemp (&optional (prefix "T") (package *package*))
  24.                  ;; Common LISP, S. 170
  25.     (loop
  26.       (setq gentemp-count (1+ gentemp-count))
  27.       (multiple-value-bind (sym flag)
  28.         (intern
  29.           (string-concat prefix
  30.             (write-to-string gentemp-count :base 10 :radix nil :readably nil)
  31.           )
  32.           package
  33.         )
  34.         (unless flag (return sym))
  35. ) ) ) )
  36.  
  37.  
  38. ;;; Macros fⁿr Packages (Kapitel 11), S. 187-188
  39.  
  40. (defmacro do-symbols ((var &optional (packageform '*package*) (resultform nil))
  41.                       &body body &environment env)
  42.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  43.     (let ((packvar (gensym)))
  44.       `(BLOCK NIL
  45.          (LET ((,packvar ,packageform))
  46.            (LET ((,var NIL))
  47.              (DECLARE (IGNORABLE ,var) ,@declarations)
  48.              (SYSTEM::MAP-SYMBOLS
  49.                #'(LAMBDA (,var)
  50.                    ,@(if declarations `((DECLARE ,@declarations)) '())
  51.                    ,@body-rest
  52.                  )
  53.                ,packvar
  54.              )
  55.              ,resultform
  56.        ) ) )
  57. ) ) )
  58.  
  59. (defmacro do-external-symbols ((var &optional (packageform '*package*) (resultform nil))
  60.                                &body body &environment env)
  61.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  62.     (let ((packvar (gensym)))
  63.       `(BLOCK NIL
  64.          (LET ((,packvar ,packageform))
  65.            (LET ((,var NIL))
  66.              (DECLARE (IGNORABLE ,var) ,@declarations)
  67.              (SYSTEM::MAP-EXTERNAL-SYMBOLS
  68.                #'(LAMBDA (,var)
  69.                    ,@(if declarations `((DECLARE ,@declarations)) '())
  70.                    ,@body-rest
  71.                  )
  72.                ,packvar
  73.              )
  74.              ,resultform
  75.        ) ) )
  76. ) ) )
  77.  
  78. (defmacro do-all-symbols ((var &optional (resultform nil))
  79.                           &body body &environment env)
  80.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  81.     `(BLOCK NIL
  82.        (LET ((,var NIL))
  83.          (DECLARE (IGNORABLE ,var) ,@declarations)
  84.          (SYSTEM::MAP-ALL-SYMBOLS
  85.            #'(LAMBDA (,var)
  86.                ,@(if declarations `((DECLARE ,@declarations)) '())
  87.                ,@body-rest
  88.              )
  89.          )
  90.          ,resultform
  91.      ) )
  92. ) )
  93.  
  94. ;;; Modulverwaltung (Kapitel 11.8), CLTL S. 188
  95.  
  96. (defvar *modules* nil)
  97.  
  98. (defun provide (module-name)
  99.   (setq *modules* (adjoin (string module-name) *modules* :test #'string=))
  100. )
  101.  
  102. (defun require (module-name &optional (pathname nil p-given))
  103.   (unless (member (string module-name) *modules* :test #'string-equal)
  104.     (unless p-given (setq pathname (pathname module-name)))
  105.     (let (#-CLISP(*default-pathname-defaults* '#""))
  106.       (if (atom pathname) (load pathname) (mapcar #'load pathname))
  107.     )
  108. ) )
  109.  
  110.  
  111. ;;; Konstanten fⁿr Zahlen (Kapitel 12)
  112.  
  113. ; vgl. File INTLOG.TXT
  114. (defconstant boole-clr 0)
  115. (defconstant boole-set 15)
  116. (defconstant boole-1 10)
  117. (defconstant boole-2 12)
  118. (defconstant boole-c1 5)
  119. (defconstant boole-c2 3)
  120. (defconstant boole-and 8)
  121. (defconstant boole-ior 14)
  122. (defconstant boole-xor 6)
  123. (defconstant boole-eqv 9)
  124. (defconstant boole-nand 7)
  125. (defconstant boole-nor 1)
  126. (defconstant boole-andc1 4)
  127. (defconstant boole-andc2 2)
  128. (defconstant boole-orc1 13)
  129. (defconstant boole-orc2 11)
  130.  
  131. ; Zum Wiedereinlesen von BYTEs:
  132. (defun make-byte (&key size position) (byte size position))
  133.  
  134. ; X3J13 vote <79>
  135. (defconstant least-positive-normalized-short-float least-positive-short-float)
  136. (defconstant least-negative-normalized-short-float least-negative-short-float)
  137. (defconstant least-positive-normalized-single-float least-positive-single-float)
  138. (defconstant least-negative-normalized-single-float least-negative-single-float)
  139. (defconstant least-positive-normalized-double-float least-positive-double-float)
  140. (defconstant least-negative-normalized-double-float least-negative-double-float)
  141. (defconstant least-positive-normalized-long-float least-positive-long-float)
  142. (defconstant least-negative-normalized-long-float least-negative-long-float)
  143.  
  144.  
  145. ;;; Konstanten fⁿr Zeichen (Kapitel 13)
  146.  
  147. (defconstant char-code-limit 256)
  148. (defconstant char-font-limit 16)
  149. (defconstant char-bits-limit 16)
  150.                    ;; Common LISP, S. 233, 234
  151.  
  152. (defconstant char-control-bit 1)
  153. (defconstant char-meta-bit 2)
  154. (defconstant char-super-bit 4)
  155. (defconstant char-hyper-bit 8)
  156.                    ;; Common LISP, S. 243
  157.  
  158.  
  159. ;;; Funktionen fⁿr Sequences (Kapitel 14)
  160.  
  161. (defmacro doseq ((var seqform &optional resultform) &body body &environment env)
  162.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  163.     (let ((seqvar (gensym)))
  164.       `(BLOCK NIL
  165.          (LET ((,seqvar ,seqform))
  166.            (LET ((,var NIL))
  167.              (DECLARE (IGNORABLE ,var) ,@declarations)
  168.              (MAP NIL
  169.                   #'(LAMBDA (,var)
  170.                       ,@(if declarations `((DECLARE ,@declarations)) '())
  171.                       (TAGBODY ,@body-rest)
  172.                     )
  173.                   ,seqvar
  174.              )
  175.              ,resultform
  176.        ) ) )
  177. ) ) )
  178.  
  179.  
  180. ;;; Funktionen fⁿr Listen (Kapitel 15)
  181.  
  182. ; Hilfsversion von MEMBER, die das :KEY-Argument auch auf item anwendet:
  183. (defun sys::member1 (item list &rest rest &key test test-not key)
  184.   (declare (ignore test test-not))
  185.   (unless key (setq key #'identity))
  186.   (apply #'member (funcall key item) list rest)
  187. )
  188.  
  189. (defun union (list1 list2 &rest rest &key test test-not key)
  190.   (declare (ignore test test-not key))
  191.   (cond ((endp list1) list2)
  192.         ((apply #'sys::member1 (car list1) list2 rest)
  193.          (apply #'union (cdr list1) list2 rest))
  194.         (t (cons (car list1) (apply #'union (cdr list1) list2 rest)))
  195. ) )
  196.  
  197. (defun nunion (list1 list2 &rest rest &key test test-not key)
  198.   (declare (ignore test test-not key))
  199.   (cond ((endp list1) list2)
  200.         ((apply #'sys::member1 (car list1) list2 rest)
  201.          (apply #'nunion (cdr list1) list2 rest))
  202.         (t (rplacd list1 (apply #'nunion (cdr list1) list2 rest)))
  203. ) )
  204.  
  205. (defun intersection (list1 list2 &rest rest &key test test-not key)
  206.   (declare (ignore test test-not key))
  207.   (cond ((endp list1) nil)
  208.         ((apply #'sys::member1 (car list1) list2 rest)
  209.          (cons (car list1)
  210.                (apply #'intersection (cdr list1) list2 rest)))
  211.         (t (apply #'intersection (cdr list1) list2 rest))
  212. ) )
  213.  
  214. (defun nintersection (list1 list2 &rest rest &key test test-not key)
  215.   (declare (ignore test test-not key))
  216.   (cond ((endp list1) nil)
  217.         ((apply #'sys::member1 (car list1) list2 rest)
  218.          (rplacd list1 (apply #'nintersection (cdr list1) list2 rest)) )
  219.         (t (apply #'nintersection (cdr list1) list2 rest))
  220. ) )
  221.  
  222. (defun set-difference (list1 list2 &rest rest &key test test-not key)
  223.   (declare (ignore test test-not key))
  224.   (cond ((endp list1) nil)
  225.         ((not (apply #'sys::member1 (car list1) list2 rest))
  226.          (cons (car list1)
  227.                (apply #'set-difference (cdr list1) list2 rest)
  228.         ))
  229.         (t (apply #'set-difference (cdr list1) list2 rest))
  230. ) )
  231.  
  232. (defun nset-difference (list1 list2 &rest rest &key test test-not key)
  233.   (declare (ignore test test-not key))
  234.   (cond ((endp list1) nil)
  235.         ((not (apply #'sys::member1 (car list1) list2 rest))
  236.          (rplacd list1 (apply #'nset-difference (cdr list1) list2 rest)) )
  237.         (t (apply #'nset-difference (cdr list1) list2 rest))
  238. ) )
  239.  
  240. (defun set-exclusive-or (list1 list2 &rest rest &key test test-not key)
  241.   (declare (ignore test test-not key))
  242.   (append (apply #'set-difference list1 list2 rest)
  243.           (apply #'set-difference list2 list1 rest)
  244. ) )
  245.  
  246. (defun nset-exclusive-or (list1 list2 &rest rest &key test test-not key)
  247.   (declare (ignore test test-not key))
  248.   (nconc (apply #'set-difference list1 list2 rest)
  249.          (apply #'nset-difference list2 list1 rest)
  250. ) )
  251.  
  252. (defun subsetp (list1 list2 &rest rest &key test test-not key)
  253.   (declare (ignore test test-not key))
  254.   (do ((l list1 (cdr l)))
  255.       ((endp l) t)
  256.     (if (not (apply #'sys::member1 (car l) list2 rest)) (return nil))
  257. ) )
  258.  
  259.  
  260. ;;; Funktionen fⁿr Hash-Tabellen (Kapitel 16)
  261.  
  262. (defmacro dohash ((keyvar valuevar HTform &optional resultform) &body body &environment env)
  263.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  264.     (let ((HTvar (gensym)))
  265.       `(BLOCK NIL
  266.          (LET ((,HTvar ,HTform))
  267.            (LET ((,keyvar NIL) (,valuevar NIL))
  268.              (DECLARE (IGNORABLE ,keyvar ,valuevar) ,@declarations)
  269.              (MAPHASH
  270.                #'(LAMBDA (,keyvar ,valuevar)
  271.                    ,@(if declarations `((DECLARE ,@declarations)) '())
  272.                    (TAGBODY ,@body-rest)
  273.                  )
  274.                ,HTvar
  275.              )
  276.              ,resultform
  277.        ) ) )
  278. ) ) )
  279.  
  280.  
  281. ;;; Funktionen fⁿr Strings (Kapitel 18)
  282.  
  283. (defun string-trim (character-bag string)
  284.   (sys::string-both-trim character-bag character-bag string)
  285. )
  286.  
  287. (defun string-left-trim (character-bag string)
  288.   (sys::string-both-trim character-bag nil string)
  289. )
  290.  
  291. (defun string-right-trim (character-bag string)
  292.   (sys::string-both-trim nil character-bag string)
  293. )
  294.  
  295.  
  296. ;;; Funktionen fⁿr Pathnames (Kapitel 23.1.5)
  297. #+LOGICAL-PATHNAMES
  298. (progn
  299.   (defun logical-pathname-translations (host)
  300.     (setq host (string-upcase host))
  301.     (or (gethash host sys::*logical-pathname-translations*) ; :test #'equal !
  302.         (error (DEUTSCH "~S: ~S benennt keinen Logical Host."
  303.                 ENGLISH "~S: ~S does not name a logical host"
  304.                 FRANCAIS "~S : ~S n'est pas le nom d'un ½host logique╗.")
  305.                'logical-pathname-translations host
  306.   ) )   )
  307.   (defun set-logical-pathname-translations (host translations)
  308.     (setq host (string-upcase host))
  309.     (puthash host sys::*logical-pathname-translations* ; :test #'equal !
  310.              (let ;((host-pathname (logical-pathname (string-concat host ":;"))))
  311.                   ((host-pathname (sys::make-logical-pathname :host host)))
  312.                (mapcar #'(lambda (rule)
  313.                            (cons (merge-pathnames (logical-pathname (first rule))
  314.                                                   host-pathname 'NIL
  315.                                  )
  316.                                  (rest rule)
  317.                          ) )
  318.                        translations
  319.   ) )        ) )
  320.   (defun load-logical-pathname-translations (host)
  321.     (setq host (string-upcase host))
  322.     (unless (gethash host sys::*logical-pathname-translations*) ; :test #'equal !
  323.       (set-logical-pathname-translations host
  324.         ; Woher bekommt man die Liste der Umsetzungen??
  325.         #| ; Marcus versucht es so:
  326.            (read
  327.              (open
  328.                (merge-pathnames
  329.                  (merge-pathnames host
  330.                    (string-concat (sys::getenv "CLISP_LOGICAL_PATHNAME_TRANSLATIONS"))
  331.                  )
  332.                  (make-pathname :type '#".lsp")
  333.            ) ) )
  334.         |#
  335.         (if (and (fboundp 'sys::getenv)
  336.                  (sys::getenv (string-concat "LOGICAL_HOST_" host "_FROM"))
  337.                  (sys::getenv (string-concat "LOGICAL_HOST_" host "_TO"))
  338.             )
  339.           (list (list (sys::getenv (string-concat "LOGICAL_HOST_" host "_FROM"))
  340.                       (sys::getenv (string-concat "LOGICAL_HOST_" host "_TO"))
  341.           )     )
  342.           '()
  343.   ) ) ) )
  344.   (set-logical-pathname-translations "SYS"
  345.     '#+(or ATARI DOS)       (("**;*.LISP" "\\**\\*.LSP") (";**;*.LISP" "**\\*.LSP")
  346.                              ("**;*.*" "\\**\\*.*") (";**;*.*" "**\\*.*")
  347.                             )
  348.      #+(or AMIGA UNIX OS/2) (("**;*.LISP" "/**/*.lsp") (";**;*.LISP" "**/*.lsp")
  349.                              ("**;*.*" "/**/*.*") (";**;*.*" "**/*.*")
  350.                             )
  351.      #+ACORN-RISCOS         (("**;*.LISP" "$.**.*.lsp") (";**;*.LISP" "**.*.lsp")
  352.                              ("**;*.*" "$.**.*.*") (";**;*.*" "**.*.*")
  353.                             )
  354.   )
  355. )
  356.  
  357.  
  358. ;;; Funktionen fⁿr Zeit (Kapitel 25.4.1)
  359.  
  360. ; Hilfsfunktion fⁿr Macro TIME
  361. (defun %time (new-real1 new-real2 new-run1 new-run2 new-gc1 new-gc2
  362.               new-space1 new-space2 new-gccount
  363.               old-real1 old-real2 old-run1 old-run2 old-gc1 old-gc2
  364.               old-space1 old-space2 old-gccount)
  365.   (macrolet ((merge-2-values (val1 val2)
  366.                (if (< internal-time-units-per-second 1000000)
  367.                  `(dpb ,val1 (byte 16 16) ,val2) ; TIME_1: ATARI, AMIGA, DOS, OS/2, UNIX_TIMES
  368.                  `(+ (* ,val1 internal-time-units-per-second) ,val2) ; TIME_2: UNIX sonst
  369.             )) )
  370.     (let ((Real-Time (- (merge-2-values new-real1 new-real2)
  371.                         (merge-2-values old-real1 old-real2)
  372.           )          )
  373.           (Run-Time (- (merge-2-values new-run1 new-run2)
  374.                        (merge-2-values old-run1 old-run2)
  375.           )         )
  376.           (GC-Time (- (merge-2-values new-gc1 new-gc2)
  377.                       (merge-2-values old-gc1 old-gc2)
  378.           )        )
  379.           (Space (- (dpb new-space1 (byte 24 24) new-space2)
  380.                     (dpb old-space1 (byte 24 24) old-space2)
  381.           )      )
  382.           (GC-Count (- new-gccount old-gccount))
  383.          )
  384.       (terpri)
  385.       (write-string "Real time: ")
  386.       (write (float (/ Real-Time internal-time-units-per-second)))
  387.       (write-string " sec.")
  388.       (terpri)
  389.       (write-string "Run time: ")
  390.       (write (float (/ Run-Time internal-time-units-per-second)))
  391.       (write-string " sec.")
  392.       (terpri)
  393.       (write-string "Space: ") (write Space) (write-string " Bytes")
  394.       (when (or (plusp GC-Count) (plusp GC-Time))
  395.         (terpri)
  396.         (write-string "GC: ") (write GC-Count)
  397.         (write-string ", GC time: ")
  398.         (write (float (/ GC-Time internal-time-units-per-second)))
  399.         (write-string " sec.")
  400.       )
  401. ) ) )
  402.  
  403. ; (sleep seconds) macht seconds Sekunden Pause. CLTL S. 447
  404. (defun sleep (time)
  405.   (if (and (numberp time) (not (complexp time)) (not (minusp time)))
  406.     (progn
  407.       #+(or ATARI AMIGA DOS OS/2 ACORN-RISCOS)
  408.       (if (> time '#,(floor (expt 2 31) internal-time-units-per-second))
  409.         ; Mehr als 248 bzw. 994 bzw. 497 Tage? (Denn sys::%sleep akzeptiert nur
  410.         ; Argumente < 2^32, bei #+(or DOS OS/2) sogar nur Argumente < 2^31.)
  411.         (loop ; ja -> Endlosschleife
  412.           #+(or AMIGA OS/2 ACORN-RISCOS)
  413.           (sys::%sleep '#,(* 86400 internal-time-units-per-second))
  414.         )
  415.         (sys::%sleep (round (* time internal-time-units-per-second)))
  416.       )
  417.       #+UNIX
  418.       (if (> time 16700000) ; mehr als 193 Tage?
  419.         (loop (sys::%sleep 86400 0)) ; ja -> Endlosschleife
  420.         (multiple-value-bind (seconds rest) (floor time)
  421.           (sys::%sleep seconds (round (* rest 1000000)))
  422.       ) )
  423.     )
  424.     (error-of-type 'type-error
  425.       :datum time :expected-type '(REAL 0 *)
  426.       (DEUTSCH "~S: Argument mu▀ eine Zahl >=0 sein, nicht ~S"
  427.        ENGLISH "~S: argument ~S should be a nonnegative number"
  428.        FRANCAIS "~S : L'argument doit Ωtre un nombre positif ou zΘro et non ~S")
  429.       'sleep time
  430. ) ) )
  431.  
  432.  
  433. ;; Funktionen fⁿr Zeit-Umrechnung und Zeitzonen (CLTL Kapitel 25.4.1)
  434. ;; Version 2, beinhaltet mehr Mathematik und basiert auf MΣrz-Jahren
  435.  
  436. ; Ein MΣrz-Jahr sei die Periode vom 1.3. bis 28/29.2.
  437. ; Vorteil: Umrechnung Monat/Tag <--> Jahrtag wird einfacher.
  438. ; Skizze:
  439. ;   1.1.1900            1.1.1901            1.1.1902
  440. ;                                         
  441. ;   |-------------------|-------------------|-------------------|
  442. ;   |     Jahr 1900     |     Jahr 1901     |     Jahr 1902     |
  443. ;   |--|----------------|--|----------------|--|----------------|--|
  444. ;      |  MΣrz-Jahr 1900   |  MΣrz-Jahr 1901   |  MΣrz-Jahr 1902   |
  445. ;      |-------------------|-------------------|-------------------|
  446. ;                                            
  447. ;      1.3.1900            1.3.1901            1.3.1902
  448.  
  449. ; (UTag Jahr) = Nummer des Tages 1.3.Jahr (gegenⁿber 1.1.1900)
  450. ; UTag(J) = 365*J + floor(J/4) - floor(J/100) + floor(J/400) - 693901
  451. ; damit  UTag(J) - UTag(J-1) = 365 + [1 falls J Schaltjahr]
  452. ; und    UTag(1899) = -306
  453. ; gelten.
  454. (defun UTag (Jahr)
  455.   (+ (* 365 Jahr) (floor Jahr 4) (- (floor Jahr 100)) (floor Jahr 400) -693901)
  456. )
  457.  
  458. ; NΣherungwert:
  459. ; 365+1/4-1/100+1/400 = 365.2425 = 146097/400 .
  460. ; Durch Betrachtung einer Wertetabelle der 400-periodischen Funktion
  461. ; (J -> UTag(J)-146097/400*J) sieht man:
  462. ;   146097/400*J - 693902.4775 <= UTag(J) <= 146097/400*J - 693900.28
  463.  
  464. ; Bestimmt zu einem Tag (0 = 1.1.1900) das MΣrz-Jahr und den Tag im MΣrz-Jahr.
  465. ; (Jahr&Tag UTTag) ==> Jahr, Jahrtag
  466. ; mit (= UTTag (+ (UTag Jahr) Jahrtag))
  467. (defun Jahr&Tag (UTTag)
  468.   ; Gesucht ist das gr÷▀te Jahr mit UTag(Jahr) <= UTTag.
  469.   ; Fⁿr dieses Jahr J gilt
  470.   ; 146097/400*J - 693902.4775 <= UTag(J) <= UTTag < UTag(J+1) <= 146097/400*J - 693535.0375,
  471.   ; also 146097*J - 277560991 <= 400*UTTag < 146097*J - 277414015,
  472.   ; also 146097*(J-1900) + 23309 <= 400*UTTag < 146097*(J-1900) + 170285,
  473.   ; also J + 0.159544... <= 1900 + UTTag/(146097/400) < J + 1.165561... .
  474.   (let* ((Jahr (+ 1900 (floor (- UTTag 58) 146097/400)))
  475.          (Jahresanfang (UTag Jahr)))
  476.     ; Wegen 146097*(J-1900) + 109 <= 400*(UTTag-58) < 146097*(J-1900) + 147084,
  477.     ; also J <= 1900 + (UTTag-58)/(146097/400) < J+1.006755...,
  478.     ; ist die SchΣtzung  Jahr := floor(1900 + (UTTag-58)/(146097/400))
  479.     ; meist richtig und jedenfalls nicht zu klein und um h÷chstens 1 zu gro▀.
  480.     (when (< UTTag Jahresanfang) ; zu gro▀?
  481.       (decf Jahr)
  482.       (setq Jahresanfang (UTag Jahr))
  483.     )
  484.     (values Jahr (- UTTag Jahresanfang))
  485. ) )
  486.  
  487. ; Bei vielen Betriebssystemen (nicht bei UNIX) mu▀ die Zeitzone beim
  488. ; Installieren in timezone.lsp eingetragen werden. Hier stehen nur
  489. ; Defaultwerte.
  490.  
  491. #-UNIX
  492. ; lokale Zeitzone
  493. (defvar *default-time-zone* -1) ; Default: 1 h ÷stlich GMT = MEZ
  494. ; NB: Zeitzone mu▀ nicht ganzzahlig sein, sollte aber Vielfaches
  495. ; einer Sekunde sein.
  496.  
  497. #-UNIX
  498. ; Funktion, die feststellt, ob bei gegebenem MΣrz-Jahr und Tag und Stunde
  499. ; Sommerzeit gilt.
  500. (defvar *default-dst-check* ; Default: Sommerzeit nicht explizit bekannt
  501.   #'(lambda (Jahr Jahrtag Stunde) (declare (ignore Jahr Jahrtag Stunde)) nil)
  502. )
  503.  
  504. ; andere Abbildung  Jahrtag -> Monat  fⁿr decode-universal-time:
  505. ; Seien Monat und Jahrtag auf den 1. MΣrz bezogen
  506. ; (d.h. Jahrtag = 0 am 1. MΣrz, = 364 am 28. Februar, usw.,
  507. ;  und MΣrz=0,...,Dezember=9,Januar=10,Februar=11).
  508. ; Dann ist
  509. ;                Monat = floor(a*Jahrtag+b)
  510. ; sofern a und b so gewΣhlt sind, da▀ die Ungleichungen
  511. ;   122*a+b >= 4, 275*a+b >= 9, 30*a+b < 1, 336*a+b < 11
  512. ; gelten. Dies ist ein Viereck im Bereich
  513. ; 0.032653... = 8/245 <= a <= 7/214 = 0.032710...,
  514. ; 0.009345... = 1/107 <= b <= 1/49 = 0.020408...,
  515. ; in dem z.B. der Punkt (a=5/153,b=2/153) liegt:
  516. ;                Monat = floor((5*Jahrtag+2)/153).
  517.  
  518. ; andere Abbildung  Monat -> Jahrtag
  519. ; fⁿr encode-universal-time und decode-universal-time:
  520. ; Seien Monat und Jahrtag auf den 1. MΣrz bezogen
  521. ; (d.h. Jahrtag = 0 am 1. MΣrz, = 364 am 28. Februar, usw.,
  522. ;  und MΣrz=0,...,Dezember=9,Januar=10,Februar=11).
  523. ; Die Abbildung
  524. ;      Monat   0  1  2  3  4   5   6   7   8   9   10  11
  525. ;      Jahrtag 0 31 61 92 122 153 184 214 245 275 306 337
  526. ; kann man schreiben
  527. ;                Jahrtag = floor(a*Monat+b)
  528. ; sofern a und b so gewΣhlt sind, da▀ die Ungleichungen
  529. ;   a+b >= 31, 11*a+b >= 337, 4*a+b < 123, 9*a+b < 276
  530. ; gelten. Dies ist ein Viereck im Bereich
  531. ; 30.5714... = 214/7 <= a <= 245/8 = 30.625,
  532. ; 0.375      = 3/8   <= b <= 5/7   = 0.7142...,
  533. ; in dem z.B. der Punkt (a=153/5,b=2/5) liegt:
  534. ;                Jahrtag = floor((153*Monat+2)/5).
  535. ; Dies ist allerdings langsamer als ein Tabellenzugriff.
  536.  
  537. (macrolet ((Monat->Jahrtag (Monat) ; 0 <= Monat < 12, 0=MΣrz,...,11=Februar
  538.              `(svref '#(0 31 61 92 122 153 184 214 245 275 306 337) ,Monat)
  539.           ))
  540.  
  541. ; (encode-universal-time second minute hour date month year [time-zone]),
  542. ; CLTL S. 446
  543. (defun encode-universal-time
  544.               (Sekunde Minute Stunde Tag Monat Jahr &optional (Zeitzone nil)
  545.                &aux Monat3 Jahr3 Jahrtag UTTag)
  546.   (unless (and (and (integerp Jahr)
  547.                     (progn
  548.                       (when (<= 0 Jahr 99)
  549.                         (multiple-value-bind (i1 i2 i3 i4 i5 Jahrjetzt) (get-decoded-time)
  550.                           (declare (ignore i1 i2 i3 i4 i5))
  551.                           (setq Jahr
  552.                             (+ Jahr (* 100 (ceiling (- Jahrjetzt Jahr 50) 100)))
  553.                       ) ) )
  554.                       (<= 1900 Jahr)
  555.                )    )
  556.                (and (integerp Monat) (<= 1 Monat 12))
  557.                (progn
  558.                  (if (< Monat 3)
  559.                    (setq Jahr3 (1- Jahr)  Monat3 (+ Monat 9)) ; Monat3 10..11
  560.                    (setq Jahr3 Jahr       Monat3 (- Monat 3)) ; Monat3 0..9
  561.                  )
  562.                  (and (and (integerp Tag) (<= 1 Tag))
  563.                       (progn
  564.                         (setq Jahrtag (+ (1- Tag) (Monat->Jahrtag Monat3)))
  565.                         (setq UTTag (+ Jahrtag (UTag Jahr3)))
  566.                         (and (if (not (eql Monat3 11))
  567.                                (< Jahrtag (Monat->Jahrtag (1+ Monat3)))
  568.                                (< UTTag (UTag (1+ Jahr3)))
  569.                              )
  570.                              (and (integerp Stunde) (<= 0 Stunde 23))
  571.                              (and (integerp Minute) (<= 0 Minute 59))
  572.                              (and (integerp Sekunde) (<= 0 Sekunde 59))
  573.                              (and (progn
  574.                                     (unless Zeitzone
  575.                                       (setq Zeitzone
  576.                                         #-UNIX (- *default-time-zone*
  577.                                                   (if (funcall *default-dst-check* Jahr3 Jahrtag Stunde) 1 0)
  578.                                                )
  579.                                         #+UNIX (default-time-zone (+ (* 24 UTTag) Stunde))
  580.                                     ) )
  581.                                     (when (floatp Zeitzone) (setq Zeitzone (rational Zeitzone)))
  582.                                     (or (integerp Zeitzone)
  583.                                         (and (rationalp Zeitzone) (integerp (* 3600 Zeitzone)))
  584.                                   ) )
  585.                                   (<= -13 Zeitzone 12)
  586.           )    ) )    ) )    )
  587.     (error-of-type 'error
  588.       (DEUTSCH "Inkorrektes Datum: ~S.~S.~S, ~Sh~Sm~Ss, Zeitzone ~S"
  589.        ENGLISH "incorrect date: ~S.~S.~S, ~Sh~Sm~Ss, time zone ~S"
  590.        FRANCAIS "Date incorrecte : ~S/~S/~S, ~Sh~Sm~Ss, heure ~S")
  591.       Tag Monat Jahr Stunde Minute Sekunde Zeitzone
  592.   ) )
  593.   (+ Sekunde
  594.      (* 60 (+ Minute
  595.               (* 60 (+ Stunde Zeitzone
  596.                        (* 24 UTTag)
  597.   )  )     )  )     )
  598. )
  599.  
  600. ; (decode-universal-time universal-time [time-zone]), CLTL S. 445
  601. (defun decode-universal-time (UT &optional (time-zone nil)
  602.                               &aux Sommerzeit Zeitzone)
  603.   (if time-zone
  604.     (setq Sommerzeit nil Zeitzone time-zone)
  605.     #-UNIX
  606.     (setq time-zone *default-time-zone*
  607.           Sommerzeit (let ((UT (- UT (round (* 3600 time-zone)))))
  608.                        (multiple-value-bind (UTTag Stunde) (floor (floor UT 3600) 24)
  609.                          (multiple-value-bind (Jahr Jahrtag) (Jahr&Tag UTTag)
  610.                            (funcall *default-dst-check* Jahr Jahrtag Stunde)
  611.                      ) ) )
  612.           Zeitzone (- time-zone (if Sommerzeit 1 0))
  613.     )
  614.     #+UNIX
  615.     (progn
  616.       (multiple-value-setq (Zeitzone Sommerzeit) (default-time-zone (floor UT 3600)))
  617.       (setq time-zone (+ Zeitzone (if Sommerzeit 1 0)))
  618.     )
  619.   )
  620.   ; time-zone = Zeitzone ohne Sommerzeitberⁿcksichtigung,
  621.   ; Zeitzone = Zeitzone mit Sommerzeitberⁿcksichtigung.
  622.   (let ((UTSekunden (- UT (round (* 3600 Zeitzone)))))
  623.     (multiple-value-bind (UTMinuten Sekunde) (floor UTSekunden 60)
  624.       (multiple-value-bind (UTStunden Minute) (floor UTMinuten 60)
  625.         (multiple-value-bind (UTTage Stunde) (floor UTStunden 24)
  626.           (multiple-value-bind (Jahr Jahrtag) (Jahr&Tag UTTage)
  627.             (let* ((Monat (floor (+ (* 5 Jahrtag) 2) 153))
  628.                    (Tag (1+ (- Jahrtag (Monat->Jahrtag Monat)))))
  629.               (if (< Monat 10) ; Monat MΣrz..Dezember?
  630.                 (setq Monat (+ Monat 3)) ; Monat 3..12
  631.                 (setq Monat (- Monat 9) Jahr (+ Jahr 1)) ; Monat 1..2
  632.               )
  633.               (values Sekunde Minute Stunde Tag Monat Jahr (mod UTTage 7)
  634.                       Sommerzeit time-zone
  635. ) ) ) ) ) ) ) )
  636.  
  637. ) ; Ende von macrolet
  638.  
  639. ; (get-decoded-time), CLTL S. 445
  640. (defun get-decoded-time ()
  641.   (decode-universal-time (get-universal-time))
  642. )
  643.  
  644.  
  645. ;;; Verschiedenes
  646.  
  647. ; (concat-pnames obj1 obj2) liefert zu zwei Objekten (Symbolen oder Strings)
  648. ;  ein Symbol, dessen Printname sich aus den beiden Objekten zusammensetzt.
  649. (defun concat-pnames (obj1 obj2)
  650.   (let ((str (string-concat (string obj1) (string obj2))))
  651.     (if (and (plusp (length str)) (eql (char str 0) #\:))
  652.       (intern (subseq str 1) *keyword-package*)
  653.       (intern str)
  654. ) ) )
  655.  
  656. ; (default-directory) ist ein Synonym fⁿr (cd).
  657. (defun default-directory () (cd))
  658.  
  659. ; FORMAT-Control-String zur Datumsausgabe,
  660. ; anwendbar auf eine Liste (sec min hour day month year ...),
  661. ; belegt 17-19 Zeichen
  662. (defun date-format ()
  663.   (DEUTSCH "~1{~3@*~D.~4@*~D.~5@*~D ~2@*~2,'0D:~1@*~2,'0D:~0@*~2,'0D~:}"
  664.    ENGLISH "~1{~5@*~D/~4@*~D/~3@*~D ~2@*~2,'0D.~1@*~2,'0D.~0@*~2,'0D~:}"
  665.    FRANCAIS "~1{~3@*~D/~4@*~D/~5@*~D ~2@*~2,'0D:~1@*~2,'0D:~0@*~2,'0D~:}")
  666. )
  667.  
  668. ; zeigt ein Directory an.
  669. (defun dir (&optional (pathnames #+(or ATARI DOS) '("*.*\\" "*.*")
  670.                                  #+(or AMIGA UNIX OS/2) '("*/" "*")
  671.                                  #+ACORN-RISCOS '("*." "*" "*.*")
  672.            )          )
  673.   (flet ((onedir (pathname)
  674.            (let ((pathname-list (directory pathname :full t :circle t)))
  675.              (if (every #'atom pathname-list)
  676.                (format t "~{~%~A~}"
  677.                  (sort pathname-list #'string< :key #'namestring)
  678.                )
  679.                (format t
  680.                  (concatenate 'string
  681.                    "~:{~%~0@*~A~40T~3@*~7D~52T~2@*~21<" (date-format) "~>~}"
  682.                  )
  683.                  (sort pathname-list #'string< :key #'(lambda (l) (namestring (first l))))
  684.         )) ) ) )
  685.     (if (listp pathnames) (mapc #'onedir pathnames) (onedir pathnames))
  686.   )
  687.   (values)
  688. )
  689.  
  690.