os: make read_lines() return ?[]string
parent
52e3586be3
commit
20d6492775
29
vlib/os/os.v
29
vlib/os/os.v
|
@ -154,16 +154,16 @@ pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
|
||||||
//single file copy
|
//single file copy
|
||||||
if !os.is_dir(source_path) {
|
if !os.is_dir(source_path) {
|
||||||
adjasted_path := if os.is_dir(dest_path) {
|
adjasted_path := if os.is_dir(dest_path) {
|
||||||
filepath.join(dest_path, os.filename(source_path))
|
filepath.join(dest_path, os.filename(source_path))
|
||||||
} else {
|
} else {
|
||||||
dest_path
|
dest_path
|
||||||
}
|
}
|
||||||
if os.file_exists(adjasted_path) {
|
if os.file_exists(adjasted_path) {
|
||||||
if overwrite {
|
if overwrite {
|
||||||
os.rm(adjasted_path)
|
os.rm(adjasted_path)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return error('Destination file path already exist')
|
return error('Destination file path already exist')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
os.cp(source_path, adjasted_path) or { return error(err) }
|
os.cp(source_path, adjasted_path) or { return error(err) }
|
||||||
|
@ -196,8 +196,7 @@ fn vfopen(path, mode string) *C.FILE {
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_lines reads the file in `path` into an array of lines.
|
// read_lines reads the file in `path` into an array of lines.
|
||||||
// TODO return `?[]string` TODO implement `?[]` support
|
pub fn read_lines(path string) ?[]string {
|
||||||
pub fn read_lines(path string) []string {
|
|
||||||
mut res := []string
|
mut res := []string
|
||||||
mut buf_len := 1024
|
mut buf_len := 1024
|
||||||
mut buf := malloc(buf_len)
|
mut buf := malloc(buf_len)
|
||||||
|
@ -205,9 +204,7 @@ pub fn read_lines(path string) []string {
|
||||||
mode := 'rb'
|
mode := 'rb'
|
||||||
mut fp := vfopen(path, mode)
|
mut fp := vfopen(path, mode)
|
||||||
if isnil(fp) {
|
if isnil(fp) {
|
||||||
// TODO
|
return error('read_lines() failed to open file "$path"')
|
||||||
// return error('failed to open file "$path"')
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mut buf_index := 0
|
mut buf_index := 0
|
||||||
|
@ -217,7 +214,7 @@ pub fn read_lines(path string) []string {
|
||||||
buf_len *= 2
|
buf_len *= 2
|
||||||
buf = C.realloc(buf, buf_len)
|
buf = C.realloc(buf, buf_len)
|
||||||
if isnil(buf) {
|
if isnil(buf) {
|
||||||
panic('Could not reallocate the read buffer')
|
return error('could not reallocate the read buffer')
|
||||||
}
|
}
|
||||||
buf_index = len
|
buf_index = len
|
||||||
continue
|
continue
|
||||||
|
@ -235,8 +232,10 @@ pub fn read_lines(path string) []string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_ulines(path string) []ustring {
|
fn read_ulines(path string) ?[]ustring {
|
||||||
lines := read_lines(path)
|
lines := read_lines(path) or {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
||||||
mut ulines := []ustring
|
mut ulines := []ustring
|
||||||
for myline in lines {
|
for myline in lines {
|
||||||
|
@ -983,7 +982,7 @@ pub fn tmpdir() string {
|
||||||
/*
|
/*
|
||||||
if path == '' {
|
if path == '' {
|
||||||
// TODO untested
|
// TODO untested
|
||||||
path = C.NSTemporaryDirectory()
|
path = C.NSTemporaryDirectory()
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if path == '' { path = '/tmp' }
|
if path == '' { path = '/tmp' }
|
||||||
|
|
Loading…
Reference in New Issue