forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.pxi
More file actions
140 lines (120 loc) · 3.74 KB
/
io.pxi
File metadata and controls
140 lines (120 loc) · 3.74 KB
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
(ns pixie.io)
(def fopen (ffi-fn libc "fopen" [CCharP CCharP] CVoidP))
(def fread (ffi-fn libc "fread" [CVoidP CInt CInt CVoidP] CInt))
(def fgetc (ffi-fn libc "fgetc" [CVoidP] CInt))
(def fputc (ffi-fn libc "fputc" [CInt CVoidP] CInt))
(def fwrite (ffi-fn libc "fwrite" [CVoidP CInt CInt CVoidP] CInt))
(def fclose (ffi-fn libc "fclose" [CVoidP] CInt))
(def popen (ffi-fn libc "popen" [CCharP CCharP] CVoidP))
(def pclose (ffi-fn libc "pclose" [CVoidP] CInt))
(defprotocol IInputStream
(read-byte [this] "Read a single character")
(read [this buffer len] "Reads multiple bytes into a buffer, returns the number of bytes read"))
(defprotocol IOutputStream
(write-byte [this byte])
(write [this buffer]))
(defprotocol IClosable
(close [this] "Closes the stream"))
(def DEFAULT-BUFFER-SIZE 1024)
(deftype FileStream [fp]
IInputStream
(read [this buffer len]
(assert (<= (buffer-capacity buffer) len)
"Not enough capacity in the buffer")
(let [read-count (fread buffer 1 len fp)]
(set-buffer-count! buffer read-count)
read-count))
(read-byte [this]
(fgetc buffer))
IClosable
(close [this]
(fclose fp))
IReduce
(-reduce [this f init]
(let [buf (buffer DEFAULT-BUFFER-SIZE)
rrf (preserving-reduced f)]
(loop [acc init]
(let [read-count (read this buf DEFAULT-BUFFER-SIZE)]
(if (> read-count 0)
(let [result (reduce rrf acc buf)]
(if (not (reduced? result))
(recur result)
@result))
acc))))))
(defn open-read
{:doc "Open a file for reading, returning a IInputStream"
:added "0.1"}
[filename]
(assert (string? filename) "Filename must be a string")
(->FileStream (fopen filename "r")))
(deftype FileOutputStream [fp]
IOutputStream
(write-byte [this val]
(assert (integer? val) "Value must be a int")
(fputc val fp))
(write [this buffer]
(fwrite buffer 1 (count buffer) fp))
IClosable
(close [this]
(fclose fp)))
(defn file-output-rf [filename]
(let [fp (->FileOutputStream (fopen filename "w"))]
(fn ([] 0)
([cnt] (close fp) cnt)
([cnt chr]
(assert (integer? chr))
(let [written (write-byte fp chr)]
(if (= written 0)
(reduced cnt)
(+ cnt written)))))))
(defn spit [filename val]
(transduce (map int)
(file-output-rf filename)
(str val)))
(defn slurp [filename]
(let [c (->FileStream (fopen filename "r"))
result (transduce
(map char)
string-builder
c)]
(close c)
result))
(deftype ProcessInputStream [fp]
IInputStream
(read [this buffer len]
(assert (<= (buffer-capacity buffer) len)
"Not enough capacity in the buffer")
(let [read-count (fread buffer 1 len fp)]
(set-buffer-count! buffer read-count)
read-count))
(read-byte [this]
(fgetc fp))
IClosable
(close [this]
(pclose fp))
IReduce
(-reduce [this f init]
(let [buf (buffer DEFAULT-BUFFER-SIZE)
rrf (preserving-reduced f)]
(loop [acc init]
(let [read-count (read this buf DEFAULT-BUFFER-SIZE)]
(if (> read-count 0)
(let [result (reduce rrf acc buf)]
(if (not (reduced? result))
(recur result)
@result))
acc))))))
(defn popen-read
{:doc "Open a file for reading, returning a IInputStream"
:added "0.1"}
[command]
(assert (string? command) "Command must be a string")
(->ProcessInputStream (popen command "r")))
(defn run-command [command]
(let [c (->ProcessInputStream (popen command "r"))
result (transduce
(map char)
string-builder
c)]
(close c)
result))