Skip to content

implement the read function #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/io.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A banana contains 75% water.
The most consumed fruit in America is the banana
Bananas are a good source of vitamin C, potassium and fiber.
Fresh apples float because they contain 25% air.
Bananas contain no fat, cholesterol or sodium.
1 change: 1 addition & 0 deletions fixtures/number.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345
3 changes: 3 additions & 0 deletions fixtures/read_all.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file = io.open("fixtures/io.txt", "r")
print(file:read("*a"))
file:close()
3 changes: 3 additions & 0 deletions fixtures/read_bytes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file = io.open("fixtures/io.txt", "r")
print(file:read(20))
file:close()
4 changes: 4 additions & 0 deletions fixtures/read_lines.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
file = io.open("fixtures/io.txt", "r")
print(file:read("*l"))
print(file:read("*l"))
file:close()
3 changes: 3 additions & 0 deletions fixtures/read_number.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file = io.open("fixtures/number.txt", "r")
print(file:read("*n"))
file:close()
59 changes: 56 additions & 3 deletions io.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package lua

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
)

const fileHandle = "FILE*"
Expand Down Expand Up @@ -114,14 +116,65 @@ func readNumber(l *State, f *os.File) (err error) {
return
}

func readAll(l *State, f *os.File) error {
bytes, err := ioutil.ReadAll(f)
if err == nil {
l.PushString(string(bytes))
}
return err
}

func readLineHelper(l *State, f *os.File) error {
originalFileOffset, err := f.Seek(0, 1)
if err != nil {
return err
}

reader := bufio.NewReader(f)
bytes, err := reader.ReadBytes('\n')
if err == nil {
l.PushString(string(bytes))
length := int64(len(bytes))
f.Seek(originalFileOffset+length, 0) // bufio loads the entire file. This is a lazy hack to get around the problem.
}
return err
}

func readBytes(l *State, f *os.File, i int) error {
buf := make([]byte, i)
_, err := f.Read(buf)
if err == nil {
l.PushString(string(buf))
}
return err
}

func read(l *State, f *os.File, argIndex int) int {
resultCount := 0
var err error
if argCount := l.Top() - 1; argCount == 0 {
// err = readLineHelper(l, f, true)
err = readLineHelper(l, f)
resultCount = argIndex + 1
} else {
// TODO
if !l.CheckStack(l.Top() - 1) {
Errorf(l, "too many arguments")
}

p := CheckString(l, l.Top())
switch p[0:2] {
case "*a":
err = readAll(l, f)
case "*n":
err = readNumber(l, f)
case "*l":
err = readLineHelper(l, f)
default:
i, err := strconv.Atoi(p)
if err != nil {
Errorf(l, "invalid format")
}
err = readBytes(l, f, i)
}
}
if err != nil {
return FileResult(l, err, "")
Expand Down Expand Up @@ -256,7 +309,7 @@ var fileHandleMethods = []RegistryFunction{
{"close", close},
{"flush", func(l *State) int { return FileResult(l, toFile(l).Sync(), "") }},
{"lines", func(l *State) int { toFile(l); lines(l, false); return 1 }},
{"read", func(l *State) int { return read(l, toFile(l), 2) }},
{"read", func(l *State) int { read(l, toFile(l), 2); return 1 }},
{"seek", func(l *State) int {
whence := []int{os.SEEK_SET, os.SEEK_CUR, os.SEEK_END}
f := toFile(l)
Expand Down
85 changes: 85 additions & 0 deletions io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package lua

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)

// TODO: add missing tests

func TestReadAll(t *testing.T) {
l := NewState()
OpenLibraries(l)
output := captureOutput(func() {
DoFile(l, "fixtures/read_all.lua")
})

expected := `A banana contains 75% water.
The most consumed fruit in America is the banana
Bananas are a good source of vitamin C, potassium and fiber.
Fresh apples float because they contain 25% air.
Bananas contain no fat, cholesterol or sodium.`

if strings.Trim(output, "\n") != expected {
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
}
}

func TestReadLines(t *testing.T) {
l := NewState()
OpenLibraries(l)
output := captureOutput(func() {
DoFile(l, "fixtures/read_lines.lua")
})

expected := `A banana contains 75% water.

The most consumed fruit in America is the banana`

if strings.Trim(output, "\n") != expected {
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
}
}

func TestReadNumber(t *testing.T) {
l := NewState()
OpenLibraries(l)
output := captureOutput(func() {
DoFile(l, "fixtures/read_number.lua")
})

expected := "12345"
if strings.Trim(output, "\n") != expected {
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
}
}

func TestReadBytes(t *testing.T) {
l := NewState()
OpenLibraries(l)
output := captureOutput(func() {
DoFile(l, "fixtures/read_bytes.lua")
})

expected := "A banana contains 75"
if strings.Trim(output, "\n") != expected {
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
}
}

func captureOutput(f func()) string {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

f()

w.Close()
out, _ := ioutil.ReadAll(r)
fmt.Println(string(out))
os.Stdout = rescueStdout
return string(out)
}