blob: fd62f911350498a13899f458a59739c9905a2c50 (
plain)
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
|
(defun sys:get-buf-common (s bytes seek)
(let ((b (make-buf 0 0 (min bytes 4096)))
(o 0))
(when (plusp seek)
(unless (ignerr (seek-stream s seek :from-current))
(let ((b (make-buf (min seek 4096)))
(c 0))
(while (< c seek)
(let ((p (fill-buf b 0 s)))
(if (zerop p)
(return))
(inc c p))))))
(while (or (null bytes) (< (len b) bytes))
(let ((p (fill-buf-adjust b o s)))
(when (= p o)
(return))
(set o p)
(when (eql p (buf-alloc-size b))
(buf-set-length b (min (+ p p) bytes)))))
b))
(defun get-jsons (: (s *stdin*))
(when (stringp s)
(set s (make-string-byte-input-stream s)))
(build
(catch*
(while t
(add (get-json s)))
(syntax-error (type . args)
(if (parse-errors s)
(throw type . args))))))
(defun put-jsons (list : (s *stdout*) flat-p)
(each ((obj list))
(put-jsonl obj s flat-p))
t)
(defun file-get (name : mopt)
(with-stream (s (open-file name `r@mopt`))
(read s)))
(defun file-put (name obj : mopt)
(with-stream (s (open-file name `w@mopt`))
(prinl obj s)))
(defun file-append (name obj : mopt)
(with-stream (s (open-file name `a@mopt`))
(prinl obj s)))
(defun file-get-string (name : mopt)
(with-stream (s (open-file name `r@mopt`))
(get-string s)))
(defun file-put-string (name string : mopt)
(with-stream (s (open-file name `w@mopt`))
(put-string string s)))
(defun file-append-string (name string : mopt)
(with-stream (s (open-file name `a@mopt`))
(put-string string s)))
(defun file-get-lines (name : mopt)
(get-lines (open-file name `r@mopt`)))
(defun file-put-lines (name lines : mopt)
(with-stream (s (open-file name `w@mopt`))
(put-lines lines s)))
(defun file-append-lines (name lines : mopt)
(with-stream (s (open-file name `a@mopt`))
(put-lines lines s)))
(defun file-get-buf (name : bytes (seek 0) mopt)
(with-stream (s (open-file name `rb@(if bytes "u")@mopt`))
(sys:get-buf-common s bytes seek)))
(defun file-put-buf (name buf : (seek 0) mopt)
(with-stream (s (open-file name `wb@mopt`))
(unless (zerop seek)
(seek-stream s seek :from-current))
(put-buf buf 0 s)))
(defun file-place-buf (name buf : (seek 0) mopt)
(with-stream (s (open-file name `mb@mopt`))
(unless (zerop seek)
(seek-stream s seek :from-current))
(put-buf buf 0 s)))
(defun file-append-buf (name buf : mopt)
(with-stream (s (open-file name `ab@mopt`))
(put-buf buf 0 s)))
(defun file-get-json (name : mopt)
(with-stream (s (open-file name `r@mopt`))
(get-json s)))
(defun file-put-json (name obj : flat-p mopt)
(with-stream (s (open-file name `w@mopt`))
(put-jsonl obj s flat-p)))
(defun file-append-json (name obj : flat-p mopt)
(with-stream (s (open-file name `a@mopt`))
(put-jsonl obj s flat-p)))
(defun file-get-jsons (name : mopt)
(with-stream (s (open-file name `r@mopt`))
(get-jsons s)))
(defun file-put-jsons (name seq : flat-p mopt)
(with-stream (s (open-file name `w@mopt`))
(put-jsons seq s flat-p)))
(defun file-append-jsons (name seq : flat-p mopt)
(with-stream (s (open-file name `a@mopt`))
(put-jsons s seq flat-p)))
(defun command-get (cmd : mopt)
(with-stream (s (open-command cmd `r@mopt`))
(read s)))
(defun command-put (cmd obj : mopt)
(with-stream (s (open-command cmd `w@mopt`))
(prinl obj s)))
(defun command-get-string (cmd : mopt)
(with-stream (s (open-command cmd `r@mopt`))
(get-string s)))
(defun command-put-string (cmd string : mopt)
(with-stream (s (open-command cmd `w@mopt`))
(put-string string s)))
(defun command-get-lines (cmd : mopt)
(get-lines (open-command cmd `r@mopt`)))
(defun command-put-lines (cmd lines : mopt)
(with-stream (s (open-command cmd `w@mopt`))
(put-lines lines s)))
(defun command-get-buf (cmd : bytes (skip 0))
(with-stream (s (open-command cmd (if bytes "rbu" "rb")))
(sys:get-buf-common s bytes skip)))
(defun command-put-buf (cmd buf : mopt)
(with-stream (s (open-command cmd `wb@mopt`))
(put-buf buf 0 s)))
(defun command-get-json (cmd : mopt)
(with-stream (s (open-command cmd `r@mopt`))
(get-json s)))
(defun command-put-json (cmd obj : flat-p mopt)
(with-stream (s (open-command cmd `w@mopt`))
(put-jsonl obj s flat-p)))
(defun command-get-jsons (cmd : mopt)
(with-stream (s (open-command cmd `r@mopt`))
(get-jsons s)))
(defun command-put-jsons (cmd seq : flat-p mopt)
(with-stream (s (open-command cmd `w@mopt`))
(put-jsons seq s flat-p)))
(defmacro close-lazy-streams (. body)
^(let ((sys:*lazy-streams*))
(unwind-protect
(progn ,*body))
(mapdo (fun close-stream) sys:*lazy-streams*)))
|