+Shell, +A LOT
[lineal.git] / src / save-restore.lisp
blobffe995166644bb293170baddd9c43c71b309ef6d
2 (defgeneric persist-to (v strm))
3 (defmethod persist-to ((k number) strm)
4 (format strm " ~S" k))
6 (defmethod persist-to ((u tuple) strm)
7 (format strm " (vcat~{ ~S~})" (tuple-elems u)))
9 (defmethod persist-to ((a mtrix) strm)
10 (format strm " (vcat~{ (cat~{ ~S~})~})"
11 (mtrix-elems a)))
13 (defun persist-var-to (k strm)
14 (format strm "(store ~S" k)
15 (persist-to
16 (symbol-value (find-symbol k :lineal.client-vars))
17 strm)
18 (princ ")" strm)
19 (terpri strm))
21 ;V Write all variables in *clent-vars* to the stream V
22 ;V in a special format which uses lineal's calculator V
23 ;V syntax. For example, if we have a V
24 ;V number a, vector b, and matrix c, where V
25 ; a = 17
26 ; b = (1, 2, 3)
27 ; [1 2]
28 ; c = [3 4]
29 ;V the data written to the stream is V
30 ; (store "a" 17)
31 ; (store "b" (vcat 1 2 3))
32 ; (store "c" (vcat (cat 1 2) (cat 3 4))))
34 (defun save-to-stream (strm)
35 (loop :for k :in
36 (append *saved-numbers*
37 *saved-tuples* *saved-matrices*)
38 :do (persist-var-to k strm)))
40 (defun restore-from-stream (strm)
41 (let ((result (process-input-from-stream strm nil)))
42 (if (stringp result)
43 (format nil "ERROR ~A" result)
44 "Variables successfully restored.")))
46 ;V Save a session capture file.V
47 (defun local-capture
48 (&optional
49 (file (make-pathname :name "captured_session")))
50 (with-open-file
51 (strm file :direction :output
52 :if-exists :supersede)
53 (save-to-stream strm)))
55 ;;; Restore from a capture file.
56 (defun local-restore
57 (&optional
58 (file (make-pathname :name "captured_session")))
59 (handler-case
60 (with-open-file (strm file :direction :input
61 :if-does-not-exist nil)
62 (if strm (restore-from-stream strm)
63 "File does not exist."))
64 (error (condit)
65 (declare (ignore condit))
66 "Something went horribly wrong.")))