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
|
module os_js
pub struct File {
pub:
fd int
pub mut:
is_opened bool
}
#const $buffer = require('buffer');
// todo(playX): __as_cast is broken here
/*
pub struct ErrFileNotOpened {
msg string = 'os: file not opened'
code int
}
pub struct ErrSizeOfTypeIs0 {
msg string = 'os: size of type is 0'
code int
}
fn error_file_not_opened() IError {
return IError(&ErrFileNotOpened{})
}
fn error_size_of_type_0() IError {
return IError(&ErrSizeOfTypeIs0{})
}
*/
pub fn open_file(path string, mode string, options ...int) ?File {
mut res := File{}
$if js_node {
#if (!options) { options = new array([]); }
#let permissions = 0o666
#if (options.arr.length > 0) { permissions = options.arr[0]; }
#try {
#res.fd = new int($fs.openSync(''+path,''+mode,permissions))
#} catch (e) {
#return builtin.error('' + e);
#}
res.is_opened = true
} $else {
error('cannot open file on non NodeJS runtime')
}
return res
}
// open tries to open a file for reading and returns back a read-only `File` object.
pub fn open(path string) ?File {
f := open_file(path, 'r') ?
return f
}
pub fn create(path string) ?File {
f := open_file(path, 'w') ?
return f
}
pub fn stdin() File {
return File{
fd: 0
is_opened: true
}
}
pub fn stdout() File {
return File{
fd: 1
is_opened: true
}
}
pub fn stderr() File {
return File{
fd: 2
is_opened: true
}
}
pub fn (f &File) read(mut buf []byte) ?int {
if buf.len == 0 {
return 0
}
mut nbytes := 0
#try {
#let buffer = $fs.readFileSync(f.fd.valueOf());
#
#for (const val of buffer.values()) { buf.arr[nbytes++] = val; }
#}
#catch (e) { return builtin.error('' + e); }
return nbytes
}
pub fn (mut f File) write(buf []byte) ?int {
if !f.is_opened {
return error('file is not opened')
}
mut nbytes := 0
#const b = $buffer.Buffer.from(buf.arr.map((x) => x.valueOf()))
#try { $fs.writeSync(f.fd.valueOf(),b,0,buf.len.valueOf(),0); } catch (e) { return builtin.error('' + e); }
return nbytes
}
// writeln writes the string `s` into the file, and appends a \n character.
// It returns how many bytes were written, including the \n character.
pub fn (mut f File) writeln(s string) ?int {
mut nbytes := f.write(s.bytes()) ?
nbytes += f.write('\n'.bytes()) ?
return nbytes
}
pub fn (mut f File) write_to(pos u64, buf []byte) ?int {
if !f.is_opened {
return error('file is not opened')
}
mut nbytes := 0
#const b = $buffer.Buffer.from(buf.arr.map((x) => x.valueOf()))
#try { $fs.writeSync(f.fd.valueOf(),b,0,buf.len.valueOf(),pos.valueOf()); } catch (e) { return builtin.error('' + e); }
return nbytes
}
// write_string writes the string `s` into the file
// It returns how many bytes were actually written.
pub fn (mut f File) write_string(s string) ?int {
nbytes := f.write(s.bytes()) ?
return nbytes
}
|