Reader -> Reader+Parser refactoring: moved the

reading of the lines to its own function
This commit is contained in:
Karma Riuk 2023-08-05 10:48:41 +02:00
parent ab6f85b7b6
commit 929c5b58a0

View File

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