From 6b4421f1033ee72001c236b002ced106b0b3436d Mon Sep 17 00:00:00 2001 From: Kaoru HAYAMA Date: Wed, 4 Jul 2018 02:14:08 +0900 Subject: [PATCH] original-io.lines supports now io.lines(FILENAME) version for #323 --- mains/io.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/mains/io.go b/mains/io.go index a0f0b163..b574d70d 100644 --- a/mains/io.go +++ b/mains/io.go @@ -2,6 +2,7 @@ package mains import ( "bufio" + "fmt" "io" "os" @@ -39,7 +40,24 @@ func ioLinesIter(L *lua.LState) int { func ioLines(L *lua.LState) int { ud := L.NewUserData() _, sh := getRegInt(L) - if sh != nil { + if L.GetTop() >= 1 { + if filename, ok := L.Get(1).(lua.LString); ok { + if fd, err := os.Open(string(filename)); err == nil { + ud.Value = &fileHandleT{ + scanner: bufio.NewScanner(fd), + closer: fd, + } + } else { + L.Push(lua.LNil) + L.Push(lua.LString(fmt.Sprintf("%s: can not open", filename))) + return 2 + } + } else { + L.Push(lua.LNil) + L.Push(lua.LString(fmt.Sprintf("io.lines: not a string"))) + return 2 + } + } else if sh != nil { ud.Value = &fileHandleT{ scanner: bufio.NewScanner(sh.In()), closer: nil,