maze-solver-go/io/reader/text.go
Karma Riuk 0e42c0f15d Reader -> Reader+Parser refactoring: COMPLETE
Added a string reader too so that one can create a maze just with a slice of stings and RawMaze now has chunks of bytes to limit memory usage with big mazes (hopefully)
2023-08-05 16:21:56 +02:00

51 lines
810 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
}
strings_reader := StringsReader{
PathChar: r.PathChar,
WallChar: r.WallChar,
Lines: lines,
}
return strings_reader.Read()
}
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
}