maze-solver-go/io/reader/text.go
Karma Riuk fcb2bc0d51 Reader -> Reader+Parser refacfotring: create the
parser package, moved the parsing aspect of reader
to parser (still have some naughty stuff like
WallChar and PathChar in parser but it'll be fixed
in next commit)
2023-08-05 10:38:46 +02:00

39 lines
621 B
Go

package reader
import (
"bufio"
"maze-solver/maze"
"os"
)
type TextReader struct {
Filename string
PathChar, WallChar byte
}
func (r TextReader) Read() (*maze.RawMaze, error) {
var lines []string
if _, err := os.Stat(r.Filename); err != nil {
return nil, err
}
file, err := os.Open(r.Filename)
if err != nil {
return nil, err
}
{
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
file.Close()
}
return &maze.RawMaze{PathChar: r.PathChar, WallChar: r.WallChar, Data: lines}, nil
}