Reader -> Reader+Parser refactoring: moved the
reading of the lines to its own function
This commit is contained in:
parent
fcb2bc0d51
commit
221bcf2695
@ -12,18 +12,30 @@ type TextReader struct {
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
@ -31,8 +43,6 @@ func (r TextReader) Read() (*maze.RawMaze, error) {
|
||||
line := scanner.Text()
|
||||
lines = append(lines, line)
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
|
||||
return &maze.RawMaze{PathChar: r.PathChar, WallChar: r.WallChar, Data: lines}, nil
|
||||
return &lines, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user