1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: optimize os_windows.v

This commit is contained in:
yuyi 2020-02-24 03:46:13 +08:00 committed by GitHub
parent adb1d3f8c9
commit 9d2a60bb11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 12 deletions

View File

@ -51,6 +51,18 @@ fn test_open_file() {
os.rm(filename) 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() { fn test_write_and_read_string_to_file() {
filename := './test1.txt' filename := './test1.txt'
hello := 'hello world!' hello := 'hello world!'

View File

@ -135,27 +135,20 @@ pub fn is_dir(path string) bool {
*/ */
pub fn open(path string) ?File { pub fn open(path string) ?File {
mut file := File{} file := File {
wpath := path.to_wide() cfile: C._wfopen(path.to_wide(), 'rb'.to_wide())
mode := 'rb' opened: true
file = File{
cfile: C._wfopen(wpath, mode.to_wide())
} }
if isnil(file.cfile) { if isnil(file.cfile) {
return error('failed to open file "$path"') return error('failed to open file "$path"')
} }
file.opened = true
return file return file
} }
// create creates a file at a specified location and returns a writable `File` object. // create creates a file at a specified location and returns a writable `File` object.
pub fn create(path string) ?File { pub fn create(path string) ?File {
wpath := path.replace('/', '\\').to_wide() file := File {
mode := 'wb' cfile: C._wfopen(path.to_wide(), 'wb'.to_wide())
file := File{
cfile: C._wfopen(wpath, mode.to_wide())
opened: true opened: true
} }
if isnil(file.cfile) { if isnil(file.cfile) {