os: File.write_string()

pull/7203/head
Alexander Medvednikov 2020-12-08 19:37:26 +01:00
parent 036e762446
commit a1c0bb3585
2 changed files with 16 additions and 1 deletions

View File

@ -65,6 +65,19 @@ pub fn (mut f File) writeln(s string) ?int {
return (written + 1) return (written + 1)
} }
pub fn (mut f File) write_string(s string) ?int {
if !f.is_opened {
return error('file is not opened')
}
// TODO perf
written := C.fwrite(s.str, s.len, 1, f.cfile)
if written == 0 && s.len != 0 {
return error('0 bytes written')
}
return written
}
// write_to implements the RandomWriter interface // write_to implements the RandomWriter interface
pub fn (mut f File) write_to(pos int, buf []byte) ?int { pub fn (mut f File) write_to(pos int, buf []byte) ?int {
C.fseek(f.cfile, pos, C.SEEK_SET) C.fseek(f.cfile, pos, C.SEEK_SET)

View File

@ -114,7 +114,8 @@ mut:
// autofree_pregen_buf strings.Builder // autofree_pregen_buf strings.Builder
// autofree_tmp_vars []string // to avoid redefining the same tmp vars in a single function // autofree_tmp_vars []string // to avoid redefining the same tmp vars in a single function
called_fn_name string called_fn_name string
cur_mod string cur_mod string // TODO remove
cur_mod_node ast.Module
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)` is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`
// nr_vars_to_free int // nr_vars_to_free int
// doing_autofree_tmp bool // doing_autofree_tmp bool
@ -1090,6 +1091,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
// g.is_builtin_mod = node.name == 'builtin' // g.is_builtin_mod = node.name == 'builtin'
g.is_builtin_mod = node.name in ['builtin', 'os', 'strconv', 'strings', 'gg'] g.is_builtin_mod = node.name in ['builtin', 'os', 'strconv', 'strings', 'gg']
g.cur_mod = node.name g.cur_mod = node.name
g.cur_mod_node = node
} }
ast.Return { ast.Return {
g.write_defer_stmts_when_needed() g.write_defer_stmts_when_needed()