-
Notifications
You must be signed in to change notification settings - Fork 8
/
common-pointer.lisp
252 lines (185 loc) · 8.69 KB
/
common-pointer.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(defpackage :pointer
(:nicknames :&)
(:use :closer-common-lisp :io)
(:export :deref :@))
(in-package :pointer)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Generic Reference/Dereference
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric deref (thing &optional type &rest args))
(defgeneric (setf deref) (value location &optional type &rest args))
(setf (fdefinition '@) (fdefinition 'deref))
(setf (fdefinition '(setf @)) (fdefinition '(setf deref)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; First class reference objects
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro get-place-accessors (place &environment env)
(multiple-value-bind (vars vals store-vars writer-form reader-form)
(get-setf-expansion place env)
(let ((writer `(let (,@(mapcar #'list vars vals))
(lambda (,@store-vars)
,writer-form)))
(reader `(let (,@(mapcar #'list vars vals))
(lambda () ,reader-form))))
`(values ,writer ,reader))))
(defmacro ref (place-form)
"This creates a closure which can write to and read from 'maps'"
(alexandria:with-gensyms (value value-supplied-p)
`(sb-int:named-lambda place (&optional (,value nil ,value-supplied-p))
(if ,value-supplied-p
(setf (,place-form) ,value)
(,place-form)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (defmethod deref :before (thing &optional type &rest args)
;; (declare (ignorable thing type args))
;; (log:debug "&(~S ~A~{ ~S~})" thing type args))
(defmethod deref ((thing t) &optional (type t) &rest args)
"if thing is not known to be a reference, return it."
(declare (ignore args))
(values thing type))
(defmethod deref ((thing cons) &optional (type 'car) &rest args)
"if thing is a cons cell, dereference as normal"
(declare (ignore args))
(ecase type
(car (car thing))
(cdr (cdr thing))))
(defmethod deref ((thing function) &optional type &rest args)
"return the result of applying function to args"
(apply thing type args))
(defmethod deref ((thing pathname) &optional (type 'string) &rest args)
"Returns the entire contents of the specified file."
(declare (ignore args))
(case type
(string (io:read-file-to-string thing))
(strings (io:read-file-to-string-list thing))
(cons (io:read-file-to-list thing))
(list (io:read-file-to-list thing))
(vector (io:read-file-to-byte-vector thing))
(t (cl-store:restore thing))))
(defmethod deref ((stream stream) &optional (type 'strings) &rest args)
(declare (ignore args))
(case type
(strings (io:read-stream-to-string-list stream))
(string (io:read-stream-to-string stream))
(t (cl-store:restore stream))))
(defmethod deref ((url ql-http:url) &optional (type 'vector) &rest args)
(declare (ignore args))
(let ((pathname (merge-pathnames (princ-to-string (unicly:make-v4-uuid)) #p"/tmp/")))
(unwind-protect (and
(ql-http:fetch url pathname)
(deref pathname type))
(delete-file pathname))))
;; this may or may not be reasonable behavior, but the idea is if one has puri loaded
;; they may likely prefer drakma as a default http client. Otherwise quicklisp is
;; assumed to be universally available
#+chunga
(defmethod deref ((uri puri:uri) &optional (type 'vector) &rest args)
(declare (ignore args))
(multiple-value-bind
(return-content return-code) (drakma:http-request uri :force-binary t :want-stream t)
(format *trace-output* "http-request [~A]: ~D" uri return-code)
(values
(case type
(stream return-content)
(vector (io:read-stream-to-byte-vector return-content))
(string (io:octets-to-string (io:read-stream-to-byte-vector return-content))))
return-code)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; setf-able assignment
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod (setf deref) ((thing t) (location pathname) &optional type &rest args)
(declare (ignore type args))
(prog1 thing
(cl-store:store thing location)))
(defmethod (setf deref) ((string string) (location pathname) &optional type &rest args)
(declare (ignore args))
(prog1 string
(with-open-file (out location :direction :output
:if-exists :overwrite :if-does-not-exist :create
:external-format (or type :default))
(write-sequence string out))))
(defmethod (setf deref) ((cons cons) (location pathname) &optional type &rest args)
(declare (ignore args))
(prog1 cons
(with-open-file (out location :direction :output
:if-exists :overwrite :if-does-not-exist :create
:external-format (or type :default))
(pprint cons out))))
(defmethod (setf deref) ((byte-vector vector) (location pathname) &optional (file-position 0)
&rest args)
(declare (ignorable args))
(prog1 byte-vector
(with-open-file (out location :direction :output :element-type '(unsigned-byte 8)
:if-exists :overwrite :if-does-not-exist :create
:external-format (getf args :external-format :default))
(file-position out (or file-position :end))
(write-sequence byte-vector out))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|
(assert (eq (deref :atom) :atom))
(assert (equal (deref #'list 0 1 2 3) '(0 1 2 3)))
(assert (equalp
(deref #P"~/.sbclrc" 'string)
(io:octets-to-string (deref #P"~/.sbclrc" 'vector))))
(assert (equalp
(io:string-to-octets (deref #P"~/.sbclrc" 'string))
(deref #P"~/.sbclrc" 'vector)))
(assert (equalp
(deref (ql-http:url "http://www.cliki.net/") 'string)
(sb-ext:octets-to-string (deref (ql-http:url "http://www.cliki.net/") 'vector))))
(assert (equalp
(io:string-to-octets (deref (puri:uri "http://www.cliki.net/") 'string))
(deref (puri:uri "http://www.cliki.net/") 'vector)))
(assert (equalp
(io:string-to-octets (deref (ql-http:url "http://www.cliki.net/") 'string))
(deref (ql-http:url "http://www.cliki.net/") 'vector)))
(assert (equalp
(setf (deref #p"/tmp/note.rss")
(deref (puri:uri "http://paste.lisp.org/list-full.rss") 'string))
(deref #p"/tmp/note.rss")))
(assert (equalp
(setf (deref #p"/tmp/note.rss")
(deref (ql-http:url "http://paste.lisp.org/list-full.rss") 'string))
(deref #p"/tmp/note.rss")))
(assert (equalp
(setf (deref #p"/tmp/dan.txt") (princ-to-string (unicly:make-v4-uuid)))
(deref #p"/tmp/dan.txt")))
(assert (equalp
(setf (deref #p"sexp.tmp") *features*)
(read-from-string (deref #p"sexp.tmp"))))
(assert (typep
(deref (puri:uri "http://paste.lisp.org/list.rss") 'stream)
'flex:flexi-io-stream))
(flet ((ok (return-type uri-string)
(equalp
(deref (puri:uri uri-string) return-type)
(deref (ql-http:url uri-string) return-type))))
(assert (ok 'vector "http://cliki.net/recent-changes.rdf"))
(assert (ok 'string "http://cliki.net/recent-changes.rdf"))
(assert (ok 'vector "http://cliki.net/"))
(assert (ok 'string "http://cliki.net/"))
(assert (ok 'vector "http://paste.lisp.org/list.rss"))
(assert (ok 'string "http://paste.lisp.org/list.rss")))
;;; extras
(deref #P"~/.sbclrc" 'string)
(deref #P"~/.sbclrc" 'strings)
(deref #P"~/.sbclrc" 'cons)
(deref #P"~/.sbclrc" 'vector)
(deref (ql-http:url "http://www.cliki.net/recent-changes.rdf"))
(deref (ql-http:url "http://www.cliki.net/recent-changes.rdf") 'vector)
(deref (ql-http:url "http://www.cliki.net/recent-changes.rdf") 'string)
(deref (ql-http:url "http://www.cliki.net/") 'strings)
(deref (ql-http:url "http://www.cliki.net/") 'string)
(deref (ql-http:url "http://www.cliki.net/") 'vector)
(dclx:printv (deref (puri:uri "http://paste.lisp.org/") 'string))
(dclx:printv (deref (puri:uri "http://paste.lisp.org/list.rss") 'string))
(dclx:printv (deref (ql-http:url "http://paste.lisp.org/list.rss") 'string))
(deref (ql-http:url "http://cliki.net/recent-changes.rdf") 'string)
(equalp
(deref (puri:uri "http://cliki.net/recent-changes.rdf") 'vector)
(deref (ql-http:url "http://cliki.net/recent-changes.rdf") 'vector))
|#