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
|
```v
|
||||||
fn read_log() {
|
fn read_log() {
|
||||||
f := os.open('log.txt')
|
f := os.open('log.txt') or { panic(err) }
|
||||||
defer { f.close() }
|
defer { f.close() }
|
||||||
...
|
...
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -22,9 +22,9 @@ pub fn (f File) is_opened() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// **************************** Write ops ***************************
|
// **************************** Write ops ***************************
|
||||||
pub fn (mut f File) write(s string) {
|
pub fn (mut f File) write(s string) ?int {
|
||||||
if !f.is_opened {
|
if !f.is_opened {
|
||||||
return
|
return error('file is not opened')
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
$if linux {
|
$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 {
|
if !f.is_opened {
|
||||||
return
|
return error('file is not opened')
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
$if linux {
|
$if linux {
|
||||||
|
@ -51,8 +55,15 @@ pub fn (mut f File) writeln(s string) {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
// TODO perf
|
// TODO perf
|
||||||
C.fwrite(s.str, s.len, 1, f.cfile)
|
written := C.fwrite(s.str, s.len, 1, f.cfile)
|
||||||
C.fputs('\n', 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 {
|
pub fn (mut f File) write_bytes(data voidptr, size int) int {
|
||||||
|
|
Loading…
Reference in New Issue