From a1c0bb35857e6b8ce8c16d913ef0c01b95c317ed Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 8 Dec 2020 19:37:26 +0100 Subject: [PATCH] os: File.write_string() --- vlib/os/file.v | 13 +++++++++++++ vlib/v/gen/cgen.v | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/vlib/os/file.v b/vlib/os/file.v index 069ffb234a..2317424ff3 100644 --- a/vlib/os/file.v +++ b/vlib/os/file.v @@ -65,6 +65,19 @@ pub fn (mut f File) writeln(s string) ?int { 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 pub fn (mut f File) write_to(pos int, buf []byte) ?int { C.fseek(f.cfile, pos, C.SEEK_SET) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 02209f40b5..cfa70b8707 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -114,7 +114,8 @@ mut: // autofree_pregen_buf strings.Builder // autofree_tmp_vars []string // to avoid redefining the same tmp vars in a single function 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, ...)` // nr_vars_to_free int // 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 in ['builtin', 'os', 'strconv', 'strings', 'gg'] g.cur_mod = node.name + g.cur_mod_node = node } ast.Return { g.write_defer_stmts_when_needed()