os: make read_lines() return ?[]string

pull/2804/head
Alexander Medvednikov 2019-11-19 02:25:55 +03:00
parent 52e3586be3
commit 20d6492775
1 changed files with 14 additions and 15 deletions

View File

@ -196,8 +196,7 @@ fn vfopen(path, mode string) *C.FILE {
}
// 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 buf_len := 1024
mut buf := malloc(buf_len)
@ -205,9 +204,7 @@ pub fn read_lines(path string) []string {
mode := 'rb'
mut fp := vfopen(path, mode)
if isnil(fp) {
// TODO
// return error('failed to open file "$path"')
return res
return error('read_lines() failed to open file "$path"')
}
mut buf_index := 0
@ -217,7 +214,7 @@ pub fn read_lines(path string) []string {
buf_len *= 2
buf = C.realloc(buf, buf_len)
if isnil(buf) {
panic('Could not reallocate the read buffer')
return error('could not reallocate the read buffer')
}
buf_index = len
continue
@ -235,8 +232,10 @@ pub fn read_lines(path string) []string {
return res
}
fn read_ulines(path string) []ustring {
lines := read_lines(path)
fn read_ulines(path string) ?[]ustring {
lines := read_lines(path) or {
return err
}
// mut ulines := new_array(0, lines.len, sizeof(ustring))
mut ulines := []ustring
for myline in lines {