mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: change optional to result of io (#16075)
This commit is contained in:
@ -52,7 +52,7 @@ fn fix_windows_path(path string) string {
|
||||
}
|
||||
|
||||
// open_file can be used to open or create a file with custom flags and permissions and returns a `File` object.
|
||||
pub fn open_file(path string, mode string, options ...int) ?File {
|
||||
pub fn open_file(path string, mode string, options ...int) !File {
|
||||
mut flags := 0
|
||||
mut seek_to_end := false
|
||||
for m in mode {
|
||||
@ -126,7 +126,7 @@ pub fn open_file(path string, mode string, options ...int) ?File {
|
||||
}
|
||||
|
||||
// open tries to open a file for reading and returns back a read-only `File` object.
|
||||
pub fn open(path string) ?File {
|
||||
pub fn open(path string) !File {
|
||||
/*
|
||||
$if linux {
|
||||
$if !android {
|
||||
@ -141,7 +141,7 @@ pub fn open(path string) ?File {
|
||||
}
|
||||
}
|
||||
*/
|
||||
cfile := vfopen(path, 'rb')?
|
||||
cfile := vfopen(path, 'rb')!
|
||||
fd := fileno(cfile)
|
||||
return File{
|
||||
cfile: cfile
|
||||
@ -151,7 +151,7 @@ pub fn open(path string) ?File {
|
||||
}
|
||||
|
||||
// create creates or opens a file at a specified location and returns a write-only `File` object.
|
||||
pub fn create(path string) ?File {
|
||||
pub fn create(path string) !File {
|
||||
/*
|
||||
// Note: android/termux/bionic is also a kind of linux,
|
||||
// but linux syscalls there sometimes fail,
|
||||
@ -175,7 +175,7 @@ pub fn create(path string) ?File {
|
||||
}
|
||||
}
|
||||
*/
|
||||
cfile := vfopen(path, 'wb')?
|
||||
cfile := vfopen(path, 'wb')!
|
||||
fd := fileno(cfile)
|
||||
return File{
|
||||
cfile: cfile
|
||||
@ -218,7 +218,7 @@ pub fn (f &File) eof() bool {
|
||||
}
|
||||
|
||||
// reopen allows a `File` to be reused. It is mostly useful for reopening standard input and output.
|
||||
pub fn (mut f File) reopen(path string, mode string) ? {
|
||||
pub fn (mut f File) reopen(path string, mode string) ! {
|
||||
p := fix_windows_path(path)
|
||||
mut cfile := &C.FILE(0)
|
||||
$if windows {
|
||||
@ -257,7 +257,7 @@ pub fn (f &File) read(mut buf []u8) !int {
|
||||
// **************************** Write ops ***************************
|
||||
// write implements the Writer interface.
|
||||
// It returns how many bytes were actually written.
|
||||
pub fn (mut f File) write(buf []u8) ?int {
|
||||
pub fn (mut f File) write(buf []u8) !int {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -278,7 +278,7 @@ pub fn (mut f File) write(buf []u8) ?int {
|
||||
|
||||
// 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 {
|
||||
pub fn (mut f File) writeln(s string) !int {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -305,15 +305,15 @@ pub fn (mut f File) writeln(s string) ?int {
|
||||
|
||||
// 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 {
|
||||
unsafe { f.write_full_buffer(s.str, usize(s.len))? }
|
||||
pub fn (mut f File) write_string(s string) !int {
|
||||
unsafe { f.write_full_buffer(s.str, usize(s.len))! }
|
||||
return s.len
|
||||
}
|
||||
|
||||
// write_to implements the RandomWriter interface.
|
||||
// It returns how many bytes were actually written.
|
||||
// It resets the seek position to the end of the file.
|
||||
pub fn (mut f File) write_to(pos u64, buf []u8) ?int {
|
||||
pub fn (mut f File) write_to(pos u64, buf []u8) !int {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -359,7 +359,7 @@ pub fn (mut f File) write_ptr(data voidptr, size int) int {
|
||||
// write_full_buffer writes a whole buffer of data to the file, starting from the
|
||||
// address in `buffer`, no matter how many tries/partial writes it would take.
|
||||
[unsafe]
|
||||
pub fn (mut f File) write_full_buffer(buffer voidptr, buffer_len usize) ? {
|
||||
pub fn (mut f File) write_full_buffer(buffer voidptr, buffer_len usize) ! {
|
||||
if buffer_len <= usize(0) {
|
||||
return
|
||||
}
|
||||
@ -411,7 +411,7 @@ pub fn (mut f File) write_ptr_at(data voidptr, size int, pos u64) int {
|
||||
// **************************** Read ops ***************************
|
||||
|
||||
// fread wraps C.fread and handles error and end-of-file detection.
|
||||
fn fread(ptr voidptr, item_size int, items int, stream &C.FILE) ?int {
|
||||
fn fread(ptr voidptr, item_size int, items int, stream &C.FILE) !int {
|
||||
nbytes := int(C.fread(ptr, item_size, items, stream))
|
||||
// If no bytes were read, check for errors and end-of-file.
|
||||
if nbytes <= 0 {
|
||||
@ -421,7 +421,7 @@ fn fread(ptr voidptr, item_size int, items int, stream &C.FILE) ?int {
|
||||
// read. The caller will get none on their next call because there will be
|
||||
// no data available and the end-of-file will be encountered again.
|
||||
if C.feof(stream) != 0 {
|
||||
return none
|
||||
return IError(Eof{})
|
||||
}
|
||||
// If fread encountered an error, return it. Note that fread and ferror do
|
||||
// not tell us what the error was, so we can't return anything more specific
|
||||
@ -453,7 +453,7 @@ pub fn (f &File) read_bytes_at(size int, pos u64) []u8 {
|
||||
// read_bytes_into_newline reads from the beginning of the file into the provided buffer.
|
||||
// Each consecutive call on the same file continues reading where it previously ended.
|
||||
// A read call is either stopped, if the buffer is full, a newline was read or EOF.
|
||||
pub fn (f &File) read_bytes_into_newline(mut buf []u8) ?int {
|
||||
pub fn (f &File) read_bytes_into_newline(mut buf []u8) !int {
|
||||
if buf.len == 0 {
|
||||
return error(@FN + ': `buf.len` == 0')
|
||||
}
|
||||
@ -492,7 +492,7 @@ pub fn (f &File) read_bytes_into_newline(mut buf []u8) ?int {
|
||||
// read_bytes_into fills `buf` with bytes at the given position in the file.
|
||||
// `buf` *must* have length greater than zero.
|
||||
// Returns the number of read bytes, or an error.
|
||||
pub fn (f &File) read_bytes_into(pos u64, mut buf []u8) ?int {
|
||||
pub fn (f &File) read_bytes_into(pos u64, mut buf []u8) !int {
|
||||
if buf.len == 0 {
|
||||
return error(@FN + ': `buf.len` == 0')
|
||||
}
|
||||
@ -500,14 +500,14 @@ pub fn (f &File) read_bytes_into(pos u64, mut buf []u8) ?int {
|
||||
$if windows {
|
||||
// Note: fseek errors if pos == os.file_size, which we accept
|
||||
C._fseeki64(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)?
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)!
|
||||
$if debug {
|
||||
C._fseeki64(f.cfile, 0, C.SEEK_SET)
|
||||
}
|
||||
return nbytes
|
||||
} $else {
|
||||
C.fseeko(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)?
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)!
|
||||
$if debug {
|
||||
C.fseeko(f.cfile, 0, C.SEEK_SET)
|
||||
}
|
||||
@ -516,7 +516,7 @@ pub fn (f &File) read_bytes_into(pos u64, mut buf []u8) ?int {
|
||||
}
|
||||
$if x32 {
|
||||
C.fseek(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)?
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)!
|
||||
$if debug {
|
||||
C.fseek(f.cfile, 0, C.SEEK_SET)
|
||||
}
|
||||
@ -526,7 +526,7 @@ pub fn (f &File) read_bytes_into(pos u64, mut buf []u8) ?int {
|
||||
}
|
||||
|
||||
// read_from implements the RandomReader interface.
|
||||
pub fn (f &File) read_from(pos u64, mut buf []u8) ?int {
|
||||
pub fn (f &File) read_from(pos u64, mut buf []u8) !int {
|
||||
if buf.len == 0 {
|
||||
return 0
|
||||
}
|
||||
@ -537,12 +537,12 @@ pub fn (f &File) read_from(pos u64, mut buf []u8) ?int {
|
||||
C.fseeko(f.cfile, pos, C.SEEK_SET)
|
||||
}
|
||||
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)?
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)!
|
||||
return nbytes
|
||||
}
|
||||
$if x32 {
|
||||
C.fseek(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)?
|
||||
nbytes := fread(buf.data, 1, buf.len, f.cfile)!
|
||||
return nbytes
|
||||
}
|
||||
return error('Could not read file')
|
||||
@ -550,7 +550,7 @@ pub fn (f &File) read_from(pos u64, mut buf []u8) ?int {
|
||||
|
||||
// read_into_ptr reads at most max_size bytes from the file and writes it into ptr.
|
||||
// Returns the amount of bytes read or an error.
|
||||
pub fn (f &File) read_into_ptr(ptr &u8, max_size int) ?int {
|
||||
pub fn (f &File) read_into_ptr(ptr &u8, max_size int) !int {
|
||||
return fread(ptr, 1, max_size, f.cfile)
|
||||
}
|
||||
|
||||
@ -588,7 +588,7 @@ fn error_size_of_type_0() IError {
|
||||
}
|
||||
|
||||
// read_struct reads a single struct of type `T`
|
||||
pub fn (mut f File) read_struct<T>(mut t T) ? {
|
||||
pub fn (mut f File) read_struct<T>(mut t T) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -596,14 +596,14 @@ pub fn (mut f File) read_struct<T>(mut t T) ? {
|
||||
if tsize == 0 {
|
||||
return error_size_of_type_0()
|
||||
}
|
||||
nbytes := fread(t, 1, tsize, f.cfile)?
|
||||
nbytes := fread(t, 1, tsize, f.cfile)!
|
||||
if nbytes != tsize {
|
||||
return error_with_code('incomplete struct read', nbytes)
|
||||
}
|
||||
}
|
||||
|
||||
// read_struct_at reads a single struct of type `T` at position specified in file
|
||||
pub fn (mut f File) read_struct_at<T>(mut t T, pos u64) ? {
|
||||
pub fn (mut f File) read_struct_at<T>(mut t T, pos u64) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -615,17 +615,17 @@ pub fn (mut f File) read_struct_at<T>(mut t T, pos u64) ? {
|
||||
$if x64 {
|
||||
$if windows {
|
||||
C._fseeki64(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes = fread(t, 1, tsize, f.cfile)?
|
||||
nbytes = fread(t, 1, tsize, f.cfile)!
|
||||
C._fseeki64(f.cfile, 0, C.SEEK_END)
|
||||
} $else {
|
||||
C.fseeko(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes = fread(t, 1, tsize, f.cfile)?
|
||||
nbytes = fread(t, 1, tsize, f.cfile)!
|
||||
C.fseeko(f.cfile, 0, C.SEEK_END)
|
||||
}
|
||||
}
|
||||
$if x32 {
|
||||
C.fseek(f.cfile, pos, C.SEEK_SET)
|
||||
nbytes = fread(t, 1, tsize, f.cfile)?
|
||||
nbytes = fread(t, 1, tsize, f.cfile)!
|
||||
C.fseek(f.cfile, 0, C.SEEK_END)
|
||||
}
|
||||
if nbytes != tsize {
|
||||
@ -634,7 +634,7 @@ pub fn (mut f File) read_struct_at<T>(mut t T, pos u64) ? {
|
||||
}
|
||||
|
||||
// read_raw reads and returns a single instance of type `T`
|
||||
pub fn (mut f File) read_raw<T>() ?T {
|
||||
pub fn (mut f File) read_raw<T>() !T {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -643,7 +643,7 @@ pub fn (mut f File) read_raw<T>() ?T {
|
||||
return error_size_of_type_0()
|
||||
}
|
||||
mut t := T{}
|
||||
nbytes := fread(&t, 1, tsize, f.cfile)?
|
||||
nbytes := fread(&t, 1, tsize, f.cfile)!
|
||||
if nbytes != tsize {
|
||||
return error_with_code('incomplete struct read', nbytes)
|
||||
}
|
||||
@ -651,7 +651,7 @@ pub fn (mut f File) read_raw<T>() ?T {
|
||||
}
|
||||
|
||||
// read_raw_at reads and returns a single instance of type `T` starting at file byte offset `pos`
|
||||
pub fn (mut f File) read_raw_at<T>(pos u64) ?T {
|
||||
pub fn (mut f File) read_raw_at<T>(pos u64) !T {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -666,7 +666,7 @@ pub fn (mut f File) read_raw_at<T>(pos u64) ?T {
|
||||
if C._fseeki64(f.cfile, pos, C.SEEK_SET) != 0 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
nbytes = fread(&t, 1, tsize, f.cfile)?
|
||||
nbytes = fread(&t, 1, tsize, f.cfile)!
|
||||
if C._fseeki64(f.cfile, 0, C.SEEK_END) != 0 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
@ -674,7 +674,7 @@ pub fn (mut f File) read_raw_at<T>(pos u64) ?T {
|
||||
if C.fseeko(f.cfile, pos, C.SEEK_SET) != 0 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
nbytes = fread(&t, 1, tsize, f.cfile)?
|
||||
nbytes = fread(&t, 1, tsize, f.cfile)!
|
||||
if C.fseeko(f.cfile, 0, C.SEEK_END) != 0 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
@ -684,7 +684,7 @@ pub fn (mut f File) read_raw_at<T>(pos u64) ?T {
|
||||
if C.fseek(f.cfile, pos, C.SEEK_SET) != 0 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
nbytes = fread(&t, 1, tsize, f.cfile)?
|
||||
nbytes = fread(&t, 1, tsize, f.cfile)!
|
||||
if C.fseek(f.cfile, 0, C.SEEK_END) != 0 {
|
||||
return error(posix_get_error_msg(C.errno))
|
||||
}
|
||||
@ -697,7 +697,7 @@ pub fn (mut f File) read_raw_at<T>(pos u64) ?T {
|
||||
}
|
||||
|
||||
// write_struct writes a single struct of type `T`
|
||||
pub fn (mut f File) write_struct<T>(t &T) ? {
|
||||
pub fn (mut f File) write_struct<T>(t &T) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -716,7 +716,7 @@ pub fn (mut f File) write_struct<T>(t &T) ? {
|
||||
}
|
||||
|
||||
// write_struct_at writes a single struct of type `T` at position specified in file
|
||||
pub fn (mut f File) write_struct_at<T>(t &T, pos u64) ? {
|
||||
pub fn (mut f File) write_struct_at<T>(t &T, pos u64) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -753,7 +753,7 @@ pub fn (mut f File) write_struct_at<T>(t &T, pos u64) ? {
|
||||
// TODO `write_raw[_at]` implementations are copy-pasted from `write_struct[_at]`
|
||||
|
||||
// write_raw writes a single instance of type `T`
|
||||
pub fn (mut f File) write_raw<T>(t &T) ? {
|
||||
pub fn (mut f File) write_raw<T>(t &T) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -772,7 +772,7 @@ pub fn (mut f File) write_raw<T>(t &T) ? {
|
||||
}
|
||||
|
||||
// write_raw_at writes a single instance of type `T` starting at file byte offset `pos`
|
||||
pub fn (mut f File) write_raw_at<T>(t &T, pos u64) ? {
|
||||
pub fn (mut f File) write_raw_at<T>(t &T, pos u64) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -841,7 +841,7 @@ pub enum SeekMode {
|
||||
// be returned to the caller.
|
||||
// A successful call to the fseek() function clears the end-of-file
|
||||
// indicator for the file.
|
||||
pub fn (mut f File) seek(pos i64, mode SeekMode) ? {
|
||||
pub fn (mut f File) seek(pos i64, mode SeekMode) ! {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
@ -866,7 +866,7 @@ pub fn (mut f File) seek(pos i64, mode SeekMode) ? {
|
||||
// the start of the file, in bytes. It is complementary to seek, i.e.
|
||||
// you can use the return value as the `pos` parameter to .seek( pos, .start ),
|
||||
// so that your next read will happen from the same place.
|
||||
pub fn (f &File) tell() ?i64 {
|
||||
pub fn (f &File) tell() !i64 {
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
|
Reference in New Issue
Block a user