maze-solver-go/io/reader/text.go
Karma Riuk 221bcf2695 Reader -> Reader+Parser refactoring: moved the
reading of the lines to its own function
2023-08-05 11:03:42 +02:00

49 lines
774 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) {
lines, err := getLines(r.Filename)
if err != nil {
return nil, err
}
return &maze.RawMaze{
PathChar: r.PathChar,
WallChar: r.WallChar,
Data: *lines,
}, nil
}
func getLines(filename string) (*[]string, error) {
var lines []string
if _, err := os.Stat(filename); err != nil {
return nil, err
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
return &lines, nil
}