-
Notifications
You must be signed in to change notification settings - Fork 3
/
streamx.m
74 lines (64 loc) · 2.02 KB
/
streamx.m
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
streamx ; Stream Class
; Copyright (C) 2008 Etienne Cherdlu <[email protected]>
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU Affero General Public License as
; published by the Free Software Foundation, either version 3 of the
; License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU Affero General Public License for more details.
;
; You should have received a copy of the GNU Affero General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
openFile(stream,fileName) ; Public ; Create a stream object for a file and open the stream
;
o fileName:READ
;
s stream("current")=""
s stream("fileName")=fileName
s stream("recordCount")=0
s stream("buffer")=""
;
q
openPipe(stream,fileName) ; Public ; Create a stream object for a pipe and open the stream
;
o fileName:FIFO
;
s stream("current")=""
s stream("fileName")=fileName
s stream("recordCount")=0
s stream("buffer")=""
;
q
read(stream) ; Public ; Read a record from a stream
;
n line
;
u stream("fileName") r line
s stream("current")=line
s stream("recordCount")=stream("recordCount")+1
;
q line
readx(stream) ; Public ; Read buffered data from a stream
;
n line,data
;
; Keep the buffer filled (between 10,000 and 20,000 bytes)
i $l(stream("buffer"))<10000 d
. u stream("fileName") r line#10000 i $l(line)=0 q
. s stream("buffer")=stream("buffer")_line
;
; Read up to the next >< delimited point
i stream("buffer")'["><" s data=stream("buffer") ; Should be end of file (I hope)
e s data=$p(stream("buffer"),"><",1)_">",stream("buffer")=$e(stream("buffer"),$l(data)+1,30000)
s stream("recordCount")=stream("recordCount")+1
;
q data
close(stream) ; Public ; Close the stream and destroy the stream object
;
c stream("fileName")
k stream
q