os: cleanup of old deprecated functions. Add File.write_full_buffer/2; use it in os.write_file/2
parent
e797547d6d
commit
6890756cd2
|
@ -133,12 +133,6 @@ pub fn create(path string) ?File {
|
|||
}
|
||||
}
|
||||
|
||||
[deprecated: 'use os.stdin() instead']
|
||||
[deprecated_after: '2021-05-17']
|
||||
pub fn open_stdin() File {
|
||||
return stdin()
|
||||
}
|
||||
|
||||
// stdin - return an os.File for stdin, so that you can use .get_line on it too.
|
||||
pub fn stdin() File {
|
||||
return File{
|
||||
|
@ -180,7 +174,7 @@ pub fn (f &File) read(mut buf []byte) ?int {
|
|||
// It returns how many bytes were actually written.
|
||||
pub fn (mut f File) write(buf []byte) ?int {
|
||||
if !f.is_opened {
|
||||
return error('file is not opened')
|
||||
return error_file_not_opened()
|
||||
}
|
||||
/*
|
||||
$if linux {
|
||||
|
@ -201,7 +195,7 @@ pub fn (mut f File) write(buf []byte) ?int {
|
|||
// It returns how many bytes were written, including the \n character.
|
||||
pub fn (mut f File) writeln(s string) ?int {
|
||||
if !f.is_opened {
|
||||
return error('file is not opened')
|
||||
return error_file_not_opened()
|
||||
}
|
||||
/*
|
||||
$if linux {
|
||||
|
@ -227,15 +221,8 @@ 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 {
|
||||
if !f.is_opened {
|
||||
return error('file is not opened')
|
||||
}
|
||||
// TODO perf
|
||||
written := int(C.fwrite(s.str, 1, s.len, f.cfile))
|
||||
if written == 0 && s.len != 0 {
|
||||
return error('0 bytes written')
|
||||
}
|
||||
return written
|
||||
unsafe { f.write_full_buffer(s.str, size_t(s.len)) ? }
|
||||
return s.len
|
||||
}
|
||||
|
||||
// write_to implements the RandomWriter interface.
|
||||
|
@ -243,7 +230,7 @@ pub fn (mut f File) write_string(s string) ?int {
|
|||
// It resets the seek position to the end of the file.
|
||||
pub fn (mut f File) write_to(pos u64, buf []byte) ?int {
|
||||
if !f.is_opened {
|
||||
return error('file is not opened')
|
||||
return error_file_not_opened()
|
||||
}
|
||||
$if x64 {
|
||||
$if windows {
|
||||
|
@ -276,25 +263,6 @@ pub fn (mut f File) write_to(pos u64, buf []byte) ?int {
|
|||
return error('Could not write to file')
|
||||
}
|
||||
|
||||
// write_bytes writes `size` bytes to the file, starting from the address in `data`.
|
||||
// NB: write_bytes is unsafe and should be used carefully, since if you pass invalid
|
||||
// pointers to it, it will cause your programs to segfault.
|
||||
[deprecated: 'use File.write_ptr()']
|
||||
[unsafe]
|
||||
pub fn (mut f File) write_bytes(data voidptr, size int) int {
|
||||
return unsafe { f.write_ptr(data, size) }
|
||||
}
|
||||
|
||||
// write_bytes_at writes `size` bytes to the file, starting from the address in `data`,
|
||||
// at byte offset `pos`, counting from the start of the file (pos 0).
|
||||
// NB: write_bytes_at is unsafe and should be used carefully, since if you pass invalid
|
||||
// pointers to it, it will cause your programs to segfault.
|
||||
[deprecated: 'use File.write_ptr_at() instead']
|
||||
[unsafe]
|
||||
pub fn (mut f File) write_bytes_at(data voidptr, size int, pos u64) int {
|
||||
return unsafe { f.write_ptr_at(data, size, pos) }
|
||||
}
|
||||
|
||||
// write_ptr writes `size` bytes to the file, starting from the address in `data`.
|
||||
// NB: write_ptr is unsafe and should be used carefully, since if you pass invalid
|
||||
// pointers to it, it will cause your programs to segfault.
|
||||
|
@ -303,6 +271,30 @@ pub fn (mut f File) write_ptr(data voidptr, size int) int {
|
|||
return int(C.fwrite(data, 1, size, f.cfile))
|
||||
}
|
||||
|
||||
// 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 size_t) ? {
|
||||
if buffer_len <= size_t(0) {
|
||||
return
|
||||
}
|
||||
if !f.is_opened {
|
||||
return error_file_not_opened()
|
||||
}
|
||||
mut ptr := &byte(buffer)
|
||||
mut remaining_bytes := i64(buffer_len)
|
||||
for remaining_bytes > 0 {
|
||||
unsafe {
|
||||
x := i64(C.fwrite(ptr, 1, remaining_bytes, f.cfile))
|
||||
ptr += x
|
||||
remaining_bytes -= x
|
||||
if x <= 0 {
|
||||
return error('C.fwrite returned 0')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write_ptr_at writes `size` bytes to the file, starting from the address in `data`,
|
||||
// at byte offset `pos`, counting from the start of the file (pos 0).
|
||||
// NB: write_ptr_at is unsafe and should be used carefully, since if you pass invalid
|
||||
|
@ -448,12 +440,6 @@ pub fn (f &File) read_bytes_into(pos u64, mut buf []byte) ?int {
|
|||
return error('Could not read file')
|
||||
}
|
||||
|
||||
// read_at reads `buf.len` bytes starting at file byte offset `pos`, in `buf`.
|
||||
[deprecated: 'use File.read_from() instead']
|
||||
pub fn (f &File) read_at(pos u64, mut buf []byte) ?int {
|
||||
return f.read_from(pos, mut buf)
|
||||
}
|
||||
|
||||
// read_from implements the RandomReader interface.
|
||||
pub fn (f &File) read_from(pos u64, mut buf []byte) ?int {
|
||||
if buf.len == 0 {
|
||||
|
@ -486,13 +472,6 @@ pub fn (mut f File) flush() {
|
|||
C.fflush(f.cfile)
|
||||
}
|
||||
|
||||
// write_str writes the bytes of a string into a file,
|
||||
// *including* the terminating 0 byte.
|
||||
[deprecated: 'use File.write_string() instead']
|
||||
pub fn (mut f File) write_str(s string) ? {
|
||||
f.write_string(s) or { return err }
|
||||
}
|
||||
|
||||
pub struct ErrFileNotOpened {
|
||||
msg string = 'os: file not opened'
|
||||
code int
|
||||
|
|
13
vlib/os/os.v
13
vlib/os/os.v
|
@ -340,14 +340,14 @@ pub fn home_dir() string {
|
|||
// write_file writes `text` data to a file in `path`.
|
||||
pub fn write_file(path string, text string) ? {
|
||||
mut f := create(path) ?
|
||||
f.write_string(text) ?
|
||||
unsafe { f.write_full_buffer(text.str, size_t(text.len)) ? }
|
||||
f.close()
|
||||
}
|
||||
|
||||
// write_file_array writes the data in `buffer` to a file in `path`.
|
||||
pub fn write_file_array(path string, buffer array) ? {
|
||||
mut f := create(path) ?
|
||||
unsafe { f.write_ptr_at(buffer.data, (buffer.len * buffer.element_size), 0) }
|
||||
unsafe { f.write_full_buffer(buffer.data, size_t(buffer.len * buffer.element_size)) ? }
|
||||
f.close()
|
||||
}
|
||||
|
||||
|
@ -618,15 +618,6 @@ pub mut:
|
|||
machine string
|
||||
}
|
||||
|
||||
[deprecated: 'use os.execute or os.execute_or_panic instead']
|
||||
pub fn exec(cmd string) ?Result {
|
||||
res := execute(cmd)
|
||||
if res.exit_code < 0 {
|
||||
return error_with_code(res.output, -1)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn execute_or_panic(cmd string) Result {
|
||||
res := execute(cmd)
|
||||
if res.exit_code != 0 {
|
||||
|
|
|
@ -46,12 +46,6 @@ type SignalHandler = fn (Signal)
|
|||
|
||||
fn C.signal(signal int, handlercb SignalHandler) voidptr
|
||||
|
||||
[deprecated: 'use os.signal_opt() instead']
|
||||
[deprecated_after: '2021-06-18']
|
||||
pub fn signal(signum int, handler voidptr) voidptr {
|
||||
return voidptr(signal_opt(Signal(signum), handler) or { C.SIG_ERR })
|
||||
}
|
||||
|
||||
// signal will assign `handler` callback to be called when `signum` signal is received.
|
||||
pub fn signal_opt(signum Signal, handler SignalHandler) ?SignalHandler {
|
||||
C.errno = 0
|
||||
|
|
Loading…
Reference in New Issue