From 9d2a60bb11801bace87e7cd368828066424d217f Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 24 Feb 2020 03:46:13 +0800 Subject: [PATCH] os: optimize os_windows.v --- vlib/os/os_test.v | 12 ++++++++++++ vlib/os/os_windows.v | 17 +++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index ce36409195..b3cec0fa9d 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -51,6 +51,18 @@ fn test_open_file() { os.rm(filename) } +fn test_create_file() { + filename := './test1.txt' + hello := 'hello world!' + mut f := os.create(filename) or { panic(err)} + f.write(hello) + f.close() + + assert hello.len == os.file_size(filename) + + os.rm(filename) +} + fn test_write_and_read_string_to_file() { filename := './test1.txt' hello := 'hello world!' diff --git a/vlib/os/os_windows.v b/vlib/os/os_windows.v index 976b7c4e20..e0efb66767 100644 --- a/vlib/os/os_windows.v +++ b/vlib/os/os_windows.v @@ -135,27 +135,20 @@ pub fn is_dir(path string) bool { */ pub fn open(path string) ?File { - mut file := File{} - wpath := path.to_wide() - mode := 'rb' - file = File{ - cfile: C._wfopen(wpath, mode.to_wide()) + file := File { + cfile: C._wfopen(path.to_wide(), 'rb'.to_wide()) + opened: true } if isnil(file.cfile) { return error('failed to open file "$path"') } - file.opened = true return file } - - // create creates a file at a specified location and returns a writable `File` object. pub fn create(path string) ?File { - wpath := path.replace('/', '\\').to_wide() - mode := 'wb' - file := File{ - cfile: C._wfopen(wpath, mode.to_wide()) + file := File { + cfile: C._wfopen(path.to_wide(), 'wb'.to_wide()) opened: true } if isnil(file.cfile) {