2023-08-04 21:05:28 +02:00
|
|
|
package reader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-08-10 19:38:43 +02:00
|
|
|
"maze-solver/utils"
|
2023-08-04 21:05:28 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TextReader struct {
|
2023-08-05 10:38:46 +02:00
|
|
|
Filename string
|
2023-08-04 21:05:28 +02:00
|
|
|
PathChar, WallChar byte
|
|
|
|
}
|
|
|
|
|
2023-08-11 12:30:37 +02:00
|
|
|
func (r *TextReader) Read() (*RawMaze, error) {
|
2023-08-10 19:38:43 +02:00
|
|
|
defer utils.Timer("Text Reader", 3)()
|
2023-08-05 10:48:41 +02:00
|
|
|
lines, err := getLines(r.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-04 21:05:28 +02:00
|
|
|
|
2023-08-05 16:18:03 +02:00
|
|
|
strings_reader := StringsReader{
|
2023-08-05 10:48:41 +02:00
|
|
|
PathChar: r.PathChar,
|
|
|
|
WallChar: r.WallChar,
|
2023-08-05 16:18:03 +02:00
|
|
|
Lines: lines,
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings_reader.Read()
|
2023-08-05 10:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getLines(filename string) (*[]string, error) {
|
|
|
|
var lines []string
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
2023-08-04 21:05:28 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-05 10:48:41 +02:00
|
|
|
file, err := os.Open(filename)
|
2023-08-04 21:05:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-05 10:48:41 +02:00
|
|
|
defer file.Close()
|
2023-08-04 21:05:28 +02:00
|
|
|
|
2023-08-05 10:48:41 +02:00
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
2023-08-04 21:05:28 +02:00
|
|
|
|
2023-08-05 10:48:41 +02:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
lines = append(lines, line)
|
2023-08-05 10:01:34 +02:00
|
|
|
}
|
2023-08-04 21:05:28 +02:00
|
|
|
|
2023-08-05 10:48:41 +02:00
|
|
|
return &lines, nil
|
2023-08-04 23:53:34 +02:00
|
|
|
}
|