2020-08-01 23:07:37 +02:00
|
|
|
module os
|
|
|
|
|
|
|
|
pub struct File {
|
2021-01-12 04:38:43 +01:00
|
|
|
cfile voidptr // Using void* instead of FILE*
|
2020-08-01 23:07:37 +02:00
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
fd int
|
2020-08-01 23:07:37 +02:00
|
|
|
pub mut:
|
|
|
|
is_opened bool
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileInfo {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
// **************************** Write ops ***************************
|
2020-11-15 21:54:47 +01:00
|
|
|
// write implements the Writer interface
|
|
|
|
pub fn (mut f File) write(buf []byte) ?int {
|
2020-08-01 23:07:37 +02:00
|
|
|
if !f.is_opened {
|
2020-09-12 07:31:15 +02:00
|
|
|
return error('file is not opened')
|
2020-08-01 23:07:37 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
$if linux {
|
|
|
|
$if !android {
|
|
|
|
C.syscall(sys_write, f.fd, s.str, s.len)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2021-01-15 11:15:22 +01:00
|
|
|
written := int(C.fwrite(buf.data, buf.len, 1, f.cfile))
|
2020-11-15 21:54:47 +01:00
|
|
|
if written == 0 && buf.len != 0 {
|
2020-09-12 07:31:15 +02:00
|
|
|
return error('0 bytes written')
|
|
|
|
}
|
|
|
|
return written
|
2020-08-01 23:07:37 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 07:31:15 +02:00
|
|
|
pub fn (mut f File) writeln(s string) ?int {
|
2020-08-01 23:07:37 +02:00
|
|
|
if !f.is_opened {
|
2020-09-12 07:31:15 +02:00
|
|
|
return error('file is not opened')
|
2020-08-01 23:07:37 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
$if linux {
|
|
|
|
$if !android {
|
|
|
|
snl := s + '\n'
|
|
|
|
C.syscall(sys_write, f.fd, snl.str, snl.len)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// TODO perf
|
2021-01-15 11:15:22 +01:00
|
|
|
written := int(C.fwrite(s.str, s.len, 1, f.cfile))
|
2020-09-12 07:31:15 +02:00
|
|
|
if written == 0 && s.len != 0 {
|
|
|
|
return error('0 bytes written')
|
|
|
|
}
|
|
|
|
x := C.fputs('\n', f.cfile)
|
|
|
|
if x < 0 {
|
|
|
|
return error('could not add newline')
|
|
|
|
}
|
|
|
|
return (written + 1)
|
2020-08-01 23:07:37 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 19:37:26 +01:00
|
|
|
pub fn (mut f File) write_string(s string) ?int {
|
|
|
|
if !f.is_opened {
|
|
|
|
return error('file is not opened')
|
|
|
|
}
|
|
|
|
// TODO perf
|
2021-01-15 11:15:22 +01:00
|
|
|
written := int(C.fwrite(s.str, s.len, 1, f.cfile))
|
2020-12-08 19:37:26 +01:00
|
|
|
if written == 0 && s.len != 0 {
|
|
|
|
return error('0 bytes written')
|
|
|
|
}
|
|
|
|
return written
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
// write_to implements the RandomWriter interface
|
|
|
|
pub fn (mut f File) write_to(pos int, buf []byte) ?int {
|
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
2021-01-15 11:15:22 +01:00
|
|
|
res := int(C.fwrite(buf.data, 1, buf.len, f.cfile))
|
2020-11-15 21:54:47 +01:00
|
|
|
C.fseek(f.cfile, 0, C.SEEK_END)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-02-27 09:11:17 +01:00
|
|
|
[unsafe]
|
2020-08-01 23:07:37 +02:00
|
|
|
pub fn (mut f File) write_bytes(data voidptr, size int) int {
|
2021-01-15 11:15:22 +01:00
|
|
|
return int(C.fwrite(data, 1, size, f.cfile))
|
2020-08-01 23:07:37 +02:00
|
|
|
}
|
|
|
|
|
2021-02-27 09:11:17 +01:00
|
|
|
[unsafe]
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int {
|
2020-08-01 23:07:37 +02:00
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
2021-01-15 11:15:22 +01:00
|
|
|
res := int(C.fwrite(data, 1, size, f.cfile))
|
2020-08-01 23:07:37 +02:00
|
|
|
C.fseek(f.cfile, 0, C.SEEK_END)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// **************************** Read ops ***************************
|
2020-09-14 15:34:34 +02:00
|
|
|
// read_bytes reads bytes from the beginning of the file
|
2020-08-01 23:07:37 +02:00
|
|
|
pub fn (f &File) read_bytes(size int) []byte {
|
|
|
|
return f.read_bytes_at(size, 0)
|
|
|
|
}
|
|
|
|
|
2020-09-14 15:34:34 +02:00
|
|
|
// read_bytes_at reads bytes at the given position in the file
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (f &File) read_bytes_at(size int, pos int) []byte {
|
2020-09-12 07:31:15 +02:00
|
|
|
mut arr := []byte{len: size}
|
2020-09-27 03:14:24 +02:00
|
|
|
nreadbytes := f.read_bytes_into(pos, mut arr) or {
|
2020-09-14 15:34:34 +02:00
|
|
|
// return err
|
|
|
|
return []
|
|
|
|
}
|
2020-08-01 23:07:37 +02:00
|
|
|
return arr[0..nreadbytes]
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:57:51 +01:00
|
|
|
// read_bytes_into fills `buf` with bytes at the given position in the file.
|
2020-09-14 15:34:34 +02:00
|
|
|
// `buf` must have length greater than zero.
|
|
|
|
// Returns number of bytes read or an error.
|
|
|
|
pub fn (f &File) read_bytes_into(pos int, mut buf []byte) ?int {
|
|
|
|
if buf.len == 0 {
|
|
|
|
panic(@FN + ': `buf.len` == 0')
|
|
|
|
}
|
|
|
|
// Note: fseek errors if pos == os.file_size, which we accept
|
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
|
|
|
// errno is only set if fread fails, so clear it first to tell
|
|
|
|
C.errno = 0
|
2021-01-15 11:15:22 +01:00
|
|
|
nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile))
|
2020-09-14 15:34:34 +02:00
|
|
|
if C.errno != 0 {
|
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
$if debug {
|
|
|
|
C.fseek(f.cfile, 0, C.SEEK_SET)
|
|
|
|
}
|
|
|
|
return nbytes
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
// read implements the Reader interface
|
|
|
|
pub fn (f &File) read(mut buf []byte) ?int {
|
|
|
|
if buf.len == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
C.errno = 0
|
2021-01-15 11:15:22 +01:00
|
|
|
nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile))
|
2020-11-15 21:54:47 +01:00
|
|
|
if C.errno != 0 {
|
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
return nbytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// read_at reads buf.len bytes from pos in the file
|
|
|
|
pub fn (f &File) read_at(pos int, mut buf []byte) ?int {
|
|
|
|
if buf.len == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
|
|
|
C.errno = 0
|
2021-01-15 11:15:22 +01:00
|
|
|
nbytes := int(C.fread(buf.data, 1, buf.len, f.cfile))
|
2020-11-15 21:54:47 +01:00
|
|
|
if C.errno != 0 {
|
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
return nbytes
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:07:37 +02:00
|
|
|
// **************************** Utility ops ***********************
|
2020-11-15 21:54:47 +01:00
|
|
|
// flush writes any unwritten data in stream's buffer
|
2020-08-01 23:07:37 +02:00
|
|
|
pub fn (mut f File) flush() {
|
|
|
|
if !f.is_opened {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
C.fflush(f.cfile)
|
|
|
|
}
|
2020-09-09 18:37:38 +02:00
|
|
|
|
|
|
|
// open_stdin - return an os.File for stdin, so that you can use .get_line on it too.
|
|
|
|
pub fn open_stdin() File {
|
|
|
|
return File{
|
|
|
|
fd: 0
|
|
|
|
cfile: C.stdin
|
|
|
|
is_opened: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
pub fn (mut f File) write_str(s string) ? {
|
|
|
|
if !f.is_opened {
|
|
|
|
return error('file is closed')
|
2020-09-09 18:37:38 +02:00
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
f.write(s.bytes()) ?
|
2020-09-09 18:37:38 +02:00
|
|
|
}
|