-
Notifications
You must be signed in to change notification settings - Fork 4
/
vector-stream.lisp
227 lines (195 loc) · 8.21 KB
/
vector-stream.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
;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: de.setf.utility.implementation; -*-
#|
<DOCUMENTATION>
<DESCRIPTION>
simple vector streams for use with cl-xml decoding
</DESCRIPTION>
<COPYRIGHT YEAR='2001' AUTHOR='james adam anderson' MARK='(C)'
href='file://xml/sysdcl.lisp' />
<CHRONOLOGY>
<DELTA DATE='20010605' AUTHOR='MS'>lispworks conformance</DELTA>
<DELTA DATE='20010702'>moved from xparser to xutils to support data url
</DELTA>
<DELTA DATE='20020118'>some CormanLisp</DELTA>
</CHRONOLOGY>
</DOCUMENTATION>
|#
(in-package :de.setf.utility.implementation)
(eval-when (:compile-toplevel :load-toplevel :execute)
(error "this is ibsolete. see codecs/vector-streamlisp"))
;;;
;;; abstract
(defClass vector-stream ()
((position :initform 0)
(vector :initarg :vector :reader vector-stream.vector)
#+(or CMU sbcl lispworks) (direction :initarg :direction)
)
#+CormanLisp
(:default-initargs :element-type 'character))
(defMethod stream-state ((stream vector-stream))
(with-slots (position) stream
(format nil "@~s" position)))
#+cmu
(let ((old-definition (fdefinition 'stream-element-type)))
(unless (typep old-definition 'generic-function)
(fmakunbound 'stream-element-type)
(defgeneric stream-element-type (stream))
(setf (documentation 'stream-element-type 'function)
(documentation old-definition 'function))
(defmethod stream-element-type (stream)
(funcall old-definition stream))))
(defMethod stream-element-type ((stream vector-stream))
'(unsigned-byte 8))
(defMethod stream-position
((stream vector-stream) &optional new)
(with-slots (position) stream
(if new
(setf position new)
position)))
(defMethod stream-eofp
((stream vector-stream))
(with-slots (position vector) stream
(>= position (length vector))))
(defMethod print-object
((vs vector-stream) (stream t)
&aux (*print-array* t) (*print-length* 32) (*print-base* 16))
;;nb. length and base likely render the vector unreadable
(print-unreadable-object (vs stream :type t)
(princ (vector-stream.vector vs) stream)))
;;;
;;; input
(defClass vector-input-stream (vector-stream
#+ALLEGRO excl::fundamental-binary-input-stream
#+LispWorks stream:fundamental-stream
#+(and MCL digitool) ccl::input-binary-stream
#+(and MCL openmcl) fundamental-binary-input-stream
#+CMU extensions:fundamental-binary-input-stream
#+sbcl sb-gray:fundamental-binary-input-stream
#+CormanLisp stream
)
()
(:default-initargs :direction :input))
(defMethod initialize-instance :after
((instance vector-input-stream) &key)
(with-slots (vector) instance
(etypecase vector
(string (setf vector (map 'vector #'char-code vector)))
(list (setf vector (map 'vector #'(lambda (datum)
(etypecase datum
(fixnum datum)
(character (char-code datum))))
vector)))
(vector t))))
(defMethod stream-tyi ((stream vector-input-stream))
(with-slots (position vector) stream
(when (< position (length vector))
(prog1 (svref vector position)
(incf position)))))
(defMethod stream-untyi ((stream vector-input-stream) (datum integer))
(with-slots (position vector) stream
(cond ((< position 0)
(decf position)
(setf (svref vector position) datum))
(t
(error 'end-of-file :stream stream)))))
(defMethod stream-reader ((stream vector-input-stream))
(with-slots (vector position) stream
(if (typep vector 'simple-vector)
#'(lambda (ignore) (declare (ignore ignore))
(when (< position (length vector))
(prog1 (svref vector position)
(incf position))))
#'(lambda (ignore) (declare (ignore ignore))
(when (< position (length vector))
(prog1 (aref vector position)
(incf position)))))))
;;;
;;; output
defMethod stream-tyo ((stream vector-output-stream) (datum integer) &aux next)
(with-slots (position vector) stream
(unless (< (setf next (1+ position)) (length vector))
(adjust-array vector (+ next (floor (/ next 4)))
:element-type '(unsigned-byte 8)))
(setf (aref vector position) datum)
(setf position next)))
(defmethod stream-write-byte ((stream vector-output-stream) (datum integer))
(stream-tyo stream datum))
(defMethod stream-writer ((stream vector-output-stream))
(with-slots (vector position) stream
(if (typep vector 'simple-vector)
#'(lambda (next datum)
(unless (< (setf next (1+ position)) (length vector))
(setf vector (adjust-array vector (+ next (floor (/ next 4)))
:element-type '(unsigned-byte 8))))
(setf (svref vector position) datum)
(setf position next))
#'(lambda (next datum)
(unless (< (setf next (1+ position)) (length vector))
(setf vector (adjust-array vector (+ next (floor (/ next 4)))
:element-type '(unsigned-byte 8))))
(setf (aref vector position) datum)
(setf position next)))))
(defmethod stream-write-sequence ((stream vector-output-stream) (sequence vector)
&optional (start 0) (end nil))
(unless end (setf end (length sequence)))
(assert (typep start '(integer 0)))
(assert (>= end start))
(with-slots (vector position) stream
(let* ((new-position (+ position (- end start))))
(when (> new-position position)
(unless (< new-position (length vector))
(adjust-array vector (floor (+ new-position (floor (/ new-position 4))))
:element-type '(unsigned-byte 8)))
(replace vector sequence
:start1 position :end1 new-position
:start2 start :end2 end)
(setf position new-position))
new-position)))
#+(or)
(progn
(stream-tyo (make-instance 'vector-output-stream) 1)
(let* ((data #(0 1 2 3 4 5 6 7 8 9 246 247 248 249 250 251 252 253 254 255))
(buffer (make-array 2 :element-type '(unsigned-byte 8) :adjustable t))
(outstream (make-instance 'vector-output-stream :vector buffer))
(instream (make-instance 'vector-input-stream :vector buffer)))
(write-sequence data outstream)
(map nil #'(lambda (c) (stream-write-byte outstream (char-code c))) "asdf")
(and (every #'eql (concatenate 'vector data (map 'vector #'char-code "asdf")) buffer)
(let ((data2 (make-array (length data)))
(data3 (make-array 4)))
(and (eql (stream-read-sequence instream data2) (length data2))
(equalp data2 data))
(eql (stream-read-sequence instream data3) 4)
(equal (map 'string #'code-char data3) "asdf"))))
)
#|
(inspect
(mapcar #'(lambda (vector)
(multiple-value-list
(decoding-stream-reader (make-instance 'vector-input-stream :vector vector) nil)))
'(#(#x00 #x00 #x00 #x3c)
#(#x3c #x00 #x00 #x00)
#(#x00 #x00 #x3c #x00)
#(#x00 #x3c #x00 #x00)
#(#xff #xfe #x00 #x3c)
#(#xfe #xff #x3c #x00)
#(#x00 #x3c #x00 #x3f)
#(#x3c #x00 #x3f #x00)
#(#x3c #x3f #x78 #x60)
#(#x12 #x12 #x3c #x3f))))
;("UCS-4-1234" "UCS-4-4321" "UTF-4-2143" "UCS-4-3412" "UTF-16-21" "UTF-16-12" "UTF-16-21" "UTF-16-12" "UTF-8" "UTF-8")
(defparameter *s* (encoded-stream "<?xml ?><x>asdfgh</x>"))
(inspect *s*)
(peek-char nil *s*)
(loop (unless (princ (encoded-stream-tyi *s*)) (return)))
(with-open-file (stream (choose-file-dialog) :direction :input
:element-type '(unsigned-byte 8))
(let ((c nil)
(w (make-instance 'fred-window))
(cs (make-instance 'decoding-stream :stream stream)))
(print (encoded-stream.encoding cs))
(loop (unless (setf c (stream-tyi cs)) (return))
(write-char c w))
(fred-update w)))
|#
:EOF