-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileIO.go
97 lines (85 loc) · 2.58 KB
/
fileIO.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
)
func consumeIntFile(filepath string, sep string) ([]int, error) {
fileData, err := ioutil.ReadFile(filepath)
if err != nil {
fmt.Println("File reading error", err)
return nil, fmt.Errorf("FileParseError: Could not parse file: %v", filepath)
}
stringData := strings.Split(string(fileData), sep)
returnData := make([]int, len(stringData))
for i, val := range stringData {
returnData[i], err = strconv.Atoi(val)
if err != nil {
fmt.Println("Data parsing error", err)
if val != "" {
return nil, fmt.Errorf("FileParseError: Could not convert value to int: %v", val)
}
}
}
return returnData, nil
}
func consumeFile(filepath string) ([]string, error) {
fileData, err := ioutil.ReadFile(filepath)
if err != nil {
fmt.Println("File reading error", err)
return nil, fmt.Errorf("FileParseError: Could not parse file: %v", filepath)
}
stringData := strings.Split(string(fileData), "\n")
return stringData, nil
}
func readBingoFile(filepath string) ([]int, [][][]int, error) {
fileData, err := ioutil.ReadFile(filepath)
if err != nil {
fmt.Println("File reading error", err)
return nil, nil, fmt.Errorf("FileParseError: Could not parse file: %v", filepath)
}
stringData := strings.Split(string(fileData), "\n")
gameCallouts := strings.Split(stringData[0], ",")
gameCalloutsInt := make([]int, len(gameCallouts))
for ind, value := range gameCallouts {
callout, err := strconv.Atoi(value)
if err != nil {
fmt.Println("Game Callout Conversion Error", err)
return nil, nil, fmt.Errorf("FileParseError: Could not parse file: %v", filepath)
}
gameCalloutsInt[ind] = callout
}
log.Println(int((len(stringData) - 2)) / 6)
gameBoards := make([][][]int, int((len(stringData)-2))/6)
log.Println(len(gameBoards))
board := make([][]int, 5)
boardIndex := 0
currRow := 0
for currIndex := 2; currIndex < len(stringData); currIndex++ {
if stringData[currIndex] == "" {
gameBoards[boardIndex] = board
boardIndex++
board = make([][]int, 5)
currRow = 0
} else {
currBoardRow := strings.Split(stringData[currIndex], " ")
board[currRow] = make([]int, 5)
colIndex := 0
for _, rowValue := range currBoardRow {
if rowValue != "" {
boardRowColValue, err := strconv.Atoi(rowValue)
if err != nil {
fmt.Println("Game Board Conversion Error", err)
return nil, nil, fmt.Errorf("FileParseError: Could not parse file: %v", filepath)
}
board[currRow][colIndex] = boardRowColValue
colIndex++
}
}
currRow++
}
}
return gameCalloutsInt, gameBoards, nil
}