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
|
module os
// file descriptor based operations:
// close filedescriptor
pub fn fd_close(fd int) int {
if fd == -1 {
return 0
}
return C.close(fd)
}
pub fn fd_write(fd int, s string) {
if fd == -1 {
return
}
mut sp := s.str
mut remaining := s.len
for remaining > 0 {
written := C.write(fd, sp, remaining)
if written < 0 {
return
}
remaining = remaining - written
sp = unsafe { sp + written }
}
}
// read from filedescriptor, block until data
pub fn fd_slurp(fd int) []string {
mut res := []string{}
if fd == -1 {
return res
}
for {
s, b := fd_read(fd, 4096)
if b <= 0 {
break
}
res << s
}
return res
}
// read from filedescriptor, don't block
// return [bytestring,nrbytes]
pub fn fd_read(fd int, maxbytes int) (string, int) {
if fd == -1 {
return '', 0
}
unsafe {
mut buf := malloc_noscan(maxbytes + 1)
nbytes := C.read(fd, buf, maxbytes)
if nbytes < 0 {
free(buf)
return '', nbytes
}
buf[nbytes] = 0
return tos(buf, nbytes), nbytes
}
}
|