PageRenderTime 49ms CodeModel.GetById 13ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/core/io/io.factor

http://github.com/abeaumont/factor
Unknown | 259 lines | 189 code | 70 blank | 0 comment | 0 complexity | cb612bf5711e5533527a52362798c8ab MD5 | raw file
  1! Copyright (C) 2003, 2010 Slava Pestov.
  2! See http://factorcode.org/license.txt for BSD license.
  3USING: destructors kernel kernel.private math namespaces
  4sequences sequences.private ;
  5IN: io
  6
  7SYMBOLS: +byte+ +character+ ;
  8
  9GENERIC: stream-element-type ( stream -- type )
 10
 11GENERIC: stream-read1 ( stream -- elt )
 12GENERIC: stream-read-unsafe ( n buf stream -- count )
 13GENERIC: stream-read-until ( seps stream -- seq sep/f )
 14GENERIC: stream-read-partial-unsafe ( n buf stream -- count )
 15GENERIC: stream-readln ( stream -- str/f )
 16GENERIC: stream-contents* ( stream -- seq )
 17: stream-contents ( stream -- seq ) [ stream-contents* ] with-disposal ;
 18
 19GENERIC: stream-write1 ( elt stream -- )
 20GENERIC: stream-write ( data stream -- )
 21GENERIC: stream-flush ( stream -- )
 22GENERIC: stream-nl ( stream -- )
 23
 24ERROR: bad-seek-type type ;
 25
 26SINGLETONS: seek-absolute seek-relative seek-end ;
 27
 28GENERIC: stream-tell ( stream -- n )
 29GENERIC: stream-seek ( n seek-type stream -- )
 30GENERIC: stream-seekable? ( stream -- ? )
 31GENERIC: stream-length ( stream -- n/f )
 32
 33: stream-print ( str stream -- )
 34    [ stream-write ] [ stream-nl ] bi ; inline
 35
 36! Default streams
 37MIXIN: input-stream
 38MIXIN: output-stream
 39SYMBOL: error-stream
 40
 41: readln ( -- str/f ) input-stream get stream-readln ; inline
 42: read1 ( -- elt ) input-stream get stream-read1 ; inline
 43: read-until ( seps -- seq sep/f ) input-stream get stream-read-until ; inline
 44: tell-input ( -- n ) input-stream get stream-tell ; inline
 45: tell-output ( -- n ) output-stream get stream-tell ; inline
 46: seek-input ( n seek-type -- ) input-stream get stream-seek ; inline
 47: seek-output ( n seek-type -- ) output-stream get stream-seek ; inline
 48
 49: write1 ( elt -- ) output-stream get stream-write1 ; inline
 50: write ( seq -- ) output-stream get stream-write ; inline
 51: flush ( -- ) output-stream get stream-flush ; inline
 52
 53: nl ( -- ) output-stream get stream-nl ; inline
 54
 55: with-input-stream* ( stream quot -- )
 56    input-stream swap with-variable ; inline
 57
 58: with-input-stream ( stream quot -- )
 59    [ with-input-stream* ] curry with-disposal ; inline
 60
 61: with-output-stream* ( stream quot -- )
 62    output-stream swap with-variable ; inline
 63
 64: with-output-stream ( stream quot -- )
 65    [ with-output-stream* ] curry with-disposal ; inline
 66
 67: with-error-stream* ( stream quot -- )
 68    error-stream swap with-variable ; inline
 69
 70: with-error-stream ( stream quot -- )
 71    [ with-error-stream* ] curry with-disposal ; inline
 72
 73: with-output+error-stream* ( stream quot -- )
 74    [ dup ] dip [ with-error-stream* ] curry with-output-stream* ; inline
 75
 76: with-output+error-stream ( stream quot -- )
 77    [ with-output+error-stream* ] curry with-disposal ; inline
 78
 79: with-output>error ( quot -- )
 80    error-stream get swap with-output-stream* ; inline
 81
 82: with-error>output ( quot -- )
 83    output-stream get swap with-error-stream* ; inline
 84
 85: with-streams* ( input output quot -- )
 86    swapd [ with-output-stream* ] curry with-input-stream* ; inline
 87
 88: with-streams ( input output quot -- )
 89    #! We have to dispose of the output stream first, so that
 90    #! if both streams point to the same FD, we get to flush the
 91    #! buffer before closing the FD.
 92    swapd [ with-output-stream ] curry with-input-stream ; inline
 93
 94: with-input-output+error-streams* ( input output+error quot -- )
 95    swapd [ with-output+error-stream* ] curry with-input-stream* ; inline
 96
 97: with-input-output+error-streams ( input output+error quot -- )
 98    swapd [ with-output+error-stream ] curry with-input-stream ; inline
 99
100: print ( str -- ) output-stream get stream-print ; inline
101
102: bl ( -- ) " " write ;
103
104: each-morsel ( ..a handler: ( ..a data -- ..b ) reader: ( ..b -- ..a data ) -- ..a )
105    [ dup ] compose swap while drop ; inline
106
107<PRIVATE
108
109: stream-exemplar ( stream -- exemplar )
110    stream-element-type +byte+ = B{ } "" ? ; inline
111
112: stream-exemplar-growable ( stream -- exemplar )
113    stream-element-type +byte+ = BV{ } SBUF" " ? ; inline
114
115: (new-sequence-for-stream) ( n stream -- seq )
116    stream-exemplar new-sequence ; inline
117
118: resize-if-necessary ( wanted-n got-n seq -- seq' )
119    2over = [ [ 2drop ] dip ] [ resize nip ] if ; inline
120
121: (read-into-new) ( n stream quot -- seq/f )
122    [ dup ] 2dip
123    [ 2dup (new-sequence-for-stream) swap ] dip curry keep
124    over 0 = [ 3drop f ] [ resize-if-necessary ] if ; inline
125
126: (read-into) ( buf stream quot -- buf-slice/f )
127    [ dup length over ] 2dip call
128    [ (head) <slice-unsafe> ] [ zero? not ] bi ; inline
129
130PRIVATE>
131
132: stream-read ( n stream -- seq/f )
133    [ stream-read-unsafe ] (read-into-new) ; inline
134
135: stream-read-partial ( n stream -- seq/f )
136    [ stream-read-partial-unsafe ] (read-into-new) ; inline
137
138ERROR: invalid-read-buffer buf stream ;
139
140: stream-read-into ( buf stream -- buf-slice more? )
141    [ stream-read-unsafe { fixnum } declare ] (read-into) ; inline
142
143: stream-read-partial-into ( buf stream -- buf-slice more? )
144    [ stream-read-partial-unsafe { fixnum } declare ] (read-into) ; inline
145
146: read ( n -- seq ) input-stream get stream-read ; inline
147
148: read-partial ( n -- seq ) input-stream get stream-read-partial ; inline
149
150: read-into ( buf -- buf-slice more? )
151    input-stream get stream-read-into ; inline
152
153: read-partial-into ( buf -- buf-slice more? )
154    input-stream get stream-read-partial-into ; inline
155
156: each-stream-line ( ... stream quot: ( ... line -- ... ) -- ... )
157    swap [ stream-readln ] curry each-morsel ; inline
158
159: each-line ( ... quot: ( ... line -- ... ) -- ... )
160    input-stream get swap each-stream-line ; inline
161
162: stream-lines ( stream -- seq )
163    [
164        [ ] collector [ each-stream-line ] dip { } like
165    ] with-disposal ; inline
166
167: lines ( -- seq )
168    input-stream get stream-lines ; inline
169
170CONSTANT: each-block-size 65536
171
172: each-stream-block-slice ( ... stream quot: ( ... block-slice -- ... ) -- ... )
173    [ drop ] prepose
174    swap [ each-block-size swap (new-sequence-for-stream) ] keep
175    [ stream-read-partial-into ] 2curry each-morsel drop ; inline
176
177: each-stream-block ( ... stream quot: ( ... block -- ... ) -- ... )
178    swap [ each-block-size swap stream-read-partial ] curry each-morsel ; inline
179
180: each-block-slice ( ... quot: ( ... block -- ... ) -- ... )
181    input-stream get swap each-stream-block-slice ; inline
182
183: each-block ( ... quot: ( ... block -- ... ) -- ... )
184    input-stream get swap each-stream-block ; inline
185
186: (stream-contents-by-length) ( stream len -- seq )
187    dup rot
188    [ (new-sequence-for-stream) ]
189    [ [ stream-read-unsafe ] curry keep resize ] bi ; inline
190
191: (stream-contents-by-block) ( stream -- seq )
192    [ [ ] collector [ each-stream-block ] dip { } like ]
193    [ stream-exemplar concat-as ] bi ; inline
194
195: (stream-contents-by-length-or-block) ( stream -- seq )
196    dup stream-length
197    [ (stream-contents-by-length) ]
198    [ (stream-contents-by-block)  ] if* ; inline
199
200: (stream-contents-by-element) ( stream -- seq )
201    [
202        [ [ stream-read1 dup ] curry [ ] ]
203        [ stream-exemplar produce-as nip ] bi
204    ] with-disposal ; inline
205
206: contents ( -- seq )
207    input-stream get stream-contents ; inline
208
209: stream-copy* ( in out -- )
210    [ stream-write ] curry each-stream-block ; inline
211
212: stream-copy ( in out -- )
213    [ [ stream-copy* ] with-disposal ] curry with-disposal ; inline
214
215! Default implementations of stream operations in terms of read1/write1
216
217<PRIVATE
218: read-loop ( buf stream n i -- count )
219     2dup = [ nip nip nip ] [
220        pick stream-read1 [
221            over [ pick set-nth-unsafe ] 2curry 3dip
222            1 + read-loop
223        ] [ nip nip nip ] if*
224     ] if ; inline recursive
225
226: finalize-read-until ( seq sep/f -- seq/f sep/f )
227    2dup [ empty? ] [ not ] bi* and [ 2drop f f ] when ; inline
228
229: read-until-loop ( seps stream -- seq sep/f )
230    [ [ stream-read1 dup [ rot member? not ] [ nip f ] if* ] 2curry [ ] ]
231    [ stream-exemplar ] bi produce-as swap finalize-read-until ; inline
232PRIVATE>
233
234M: input-stream stream-read-unsafe rot 0 read-loop ; inline
235M: input-stream stream-read-partial-unsafe stream-read-unsafe ; inline
236M: input-stream stream-read-until read-until-loop ; inline
237M: input-stream stream-readln
238    "\n" swap stream-read-until drop ; inline
239M: input-stream stream-contents* (stream-contents-by-length-or-block) ; inline
240M: input-stream stream-seekable? drop f ; inline
241M: input-stream stream-length drop f ; inline
242
243M: output-stream stream-write [ stream-write1 ] curry each ; inline
244M: output-stream stream-flush drop ; inline
245M: output-stream stream-nl CHAR: \n swap stream-write1 ; inline
246M: output-stream stream-seekable? drop f ; inline
247M: output-stream stream-length drop f ; inline
248
249M: f stream-read1 drop f ; inline
250M: f stream-read-unsafe 3drop 0 ; inline
251M: f stream-read-until 2drop f f ; inline
252M: f stream-read-partial-unsafe 3drop 0 ; inline
253M: f stream-readln drop f ; inline
254M: f stream-contents* drop f ; inline
255
256M: f stream-write1 2drop ; inline
257M: f stream-write 2drop ; inline
258M: f stream-flush drop ; inline
259M: f stream-nl drop ; inline