Skip to content

Commit

Permalink
original-io.lines supports now io.lines(FILENAME) version for #323
Browse files Browse the repository at this point in the history
  • Loading branch information
hymkor committed Jul 3, 2018
1 parent 3f09df8 commit 6b4421f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion mains/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mains

import (
"bufio"
"fmt"
"io"
"os"

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6b4421f

Please sign in to comment.