os: change f.write and f.writeln to return options too
parent
332f3a924c
commit
60ecb7e4b6
|
@ -925,7 +925,7 @@ A defer statement defers the execution of a block of statements until the surrou
|
|||
|
||||
```v
|
||||
fn read_log() {
|
||||
f := os.open('log.txt')
|
||||
f := os.open('log.txt') or { panic(err) }
|
||||
defer { f.close() }
|
||||
...
|
||||
if !ok {
|
||||
|
|
|
@ -22,9 +22,9 @@ pub fn (f File) is_opened() bool {
|
|||
}
|
||||
|
||||
// **************************** Write ops ***************************
|
||||
pub fn (mut f File) write(s string) {
|
||||
pub fn (mut f File) write(s string) ?int {
|
||||
if !f.is_opened {
|
||||
return
|
||||
return error('file is not opened')
|
||||
}
|
||||
/*
|
||||
$if linux {
|
||||
|
@ -34,12 +34,16 @@ pub fn (mut f File) write(s string) {
|
|||
}
|
||||
}
|
||||
*/
|
||||
C.fwrite(s.str, s.len, 1, f.cfile)
|
||||
written := C.fwrite(s.str, s.len, 1, f.cfile)
|
||||
if written == 0 && s.len != 0 {
|
||||
return error('0 bytes written')
|
||||
}
|
||||
return written
|
||||
}
|
||||
|
||||
pub fn (mut f File) writeln(s string) {
|
||||
pub fn (mut f File) writeln(s string) ?int {
|
||||
if !f.is_opened {
|
||||
return
|
||||
return error('file is not opened')
|
||||
}
|
||||
/*
|
||||
$if linux {
|
||||
|
@ -51,8 +55,15 @@ pub fn (mut f File) writeln(s string) {
|
|||
}
|
||||
*/
|
||||
// TODO perf
|
||||
C.fwrite(s.str, s.len, 1, f.cfile)
|
||||
C.fputs('\n', f.cfile)
|
||||
written := C.fwrite(s.str, s.len, 1, f.cfile)
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn (mut f File) write_bytes(data voidptr, size int) int {
|
||||
|
|
Loading…
Reference in New Issue