os: vfopen returns option now (#6244)

pull/6247/head
Maciej Obarski 2020-08-28 14:24:00 +02:00 committed by GitHub
parent 789674bd5a
commit 2ce5797ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 27 deletions

View File

@ -10,10 +10,7 @@ pub const (
// read_bytes returns all bytes read from file in `path`. // read_bytes returns all bytes read from file in `path`.
pub fn read_bytes(path string) ?[]byte { pub fn read_bytes(path string) ?[]byte {
mut fp := vfopen(path, 'rb') mut fp := vfopen(path, 'rb')?
if isnil(fp) {
return error('failed to open file "$path"')
}
C.fseek(fp, 0, C.SEEK_END) C.fseek(fp, 0, C.SEEK_END)
fsize := C.ftell(fp) fsize := C.ftell(fp)
C.rewind(fp) C.rewind(fp)
@ -27,10 +24,7 @@ pub fn read_bytes(path string) ?[]byte {
// read_file reads the file in `path` and returns the contents. // read_file reads the file in `path` and returns the contents.
pub fn read_file(path string) ?string { pub fn read_file(path string) ?string {
mode := 'rb' mode := 'rb'
mut fp := vfopen(path, mode) mut fp := vfopen(path, mode)?
if isnil(fp) {
return error('failed to open file "$path"')
}
defer { C.fclose(fp) } defer { C.fclose(fp) }
C.fseek(fp, 0, C.SEEK_END) C.fseek(fp, 0, C.SEEK_END)
fsize := C.ftell(fp) fsize := C.ftell(fp)
@ -181,11 +175,17 @@ pub fn mv_by_cp(source string, target string) ? {
// vfopen returns an opened C file, given its path and open mode. // vfopen returns an opened C file, given its path and open mode.
// NB: os.vfopen is useful for compatibility with C libraries, that expect `FILE *`. // NB: os.vfopen is useful for compatibility with C libraries, that expect `FILE *`.
// If you write pure V code, os.create or os.open are more convenient. // If you write pure V code, os.create or os.open are more convenient.
pub fn vfopen(path, mode string) &C.FILE { pub fn vfopen(path, mode string) ?&C.FILE {
mut fp := voidptr(0)
$if windows { $if windows {
return C._wfopen(path.to_wide(), mode.to_wide()) fp = C._wfopen(path.to_wide(), mode.to_wide())
} $else { } $else {
return C.fopen(charptr(path.str), charptr(mode.str)) fp = C.fopen(charptr(path.str), charptr(mode.str))
}
if isnil(fp) {
return error('failed to open file "$path"')
} else {
return fp
} }
} }
@ -843,10 +843,7 @@ pub fn read_file_array<T>(path string) []T {
a := T{} a := T{}
tsize := int(sizeof(a)) tsize := int(sizeof(a))
// prepare for reading, get current file size // prepare for reading, get current file size
mut fp := vfopen(path, 'rb') mut fp := vfopen(path, 'rb') or { return array{} }
if isnil(fp) {
return array{}
}
C.fseek(fp, 0, C.SEEK_END) C.fseek(fp, 0, C.SEEK_END)
fsize := C.ftell(fp) fsize := C.ftell(fp)
C.rewind(fp) C.rewind(fp)
@ -1342,10 +1339,7 @@ pub fn open(path string) ?File {
} }
} }
*/ */
cfile := vfopen(path, 'rb') cfile := vfopen(path, 'rb')?
if cfile == voidptr(0) {
return error('failed to open file "$path"')
}
fd := fileno(cfile) fd := fileno(cfile)
return File { return File {
cfile: cfile cfile: cfile
@ -1379,10 +1373,7 @@ pub fn create(path string) ?File {
} }
} }
*/ */
cfile := vfopen(path, 'wb') cfile := vfopen(path, 'wb')?
if cfile == voidptr(0) {
return error('failed to create file "$path"')
}
fd := fileno(cfile) fd := fileno(cfile)
return File { return File {
cfile: cfile cfile: cfile

View File

@ -146,10 +146,7 @@ pub fn mkdir(path string) ?bool {
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019 // Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
// get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor. // get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
pub fn get_file_handle(path string) HANDLE { pub fn get_file_handle(path string) HANDLE {
cfile := vfopen(path, 'rb') cfile := vfopen(path, 'rb') or { return HANDLE(invalid_handle_value) }
if cfile == voidptr(0) {
return HANDLE(invalid_handle_value)
}
handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_- handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_-
return handle return handle
} }