-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreader.go
131 lines (117 loc) · 2.93 KB
/
reader.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"strings"
)
type Reader interface {
getSumAllIntValues(filename string, key string) ([]int64, error)
getTextValue(filename string, key string) (string, error)
getFloatValue(filename string, key string) (float64, error)
getIntValue(filename string, key string) (int64, error)
getIntWhole(filename string) (int64, error)
getFloatKeyValuePairs(filename string) (result map[string]float64, err error)
}
type FileReader struct {
}
func trimLastSemicolon(text string) string {
if last := len(text) - 1; last >= 0 && text[last] == ':' {
text = text[:last]
}
return text
}
func (o FileReader) getFloatKeyValuePairs(filename string) (result map[string]float64, err error) {
result = make(map[string]float64)
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
data := strings.Fields(scanner.Text())
key := trimLastSemicolon(data[0])
value, err := strconv.ParseFloat(data[1], 64)
if err != nil {
log.Print(fmt.Sprintf("Error '%s' while parsing '%s' in '%s'", err, key, filename))
} else {
result[key] = value
}
}
err = scanner.Err()
return
}
func (o FileReader) getSumAllIntValues(filename string, key string) ([]int64, error) {
var result []int64
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
data := strings.Fields(scanner.Text())
if data[0] == key {
i, err := strconv.ParseInt(data[1], 10, 64)
if err == nil {
result = append(result, i)
}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return result, nil
}
func (o FileReader) getTextValue(filename string, key string) (string, error) {
var result string
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
data := strings.Fields(scanner.Text())
if trimLastSemicolon(data[0]) == key {
result = data[1]
break
}
}
if err := scanner.Err(); err != nil {
return "", err
}
if len(result) == 0 {
err = fmt.Errorf("Key '%s' was not found in '%s'", key, filename)
return "", err
}
return result, nil
}
func (o FileReader) getFloatValue(filename string, key string) (float64, error) {
var result float64 = math.NaN()
text, err := o.getTextValue(filename, key)
if err == nil {
result, err = strconv.ParseFloat(text, 64)
}
return result, err
}
func (o FileReader) getIntValue(filename string, key string) (int64, error) {
var result int64
text, err := o.getTextValue(filename, key)
if err == nil {
result, err = strconv.ParseInt(text, 10, 64)
}
return result, err
}
func (o FileReader) getIntWhole(filename string) (int64, error) {
text, err := ioutil.ReadFile(filename)
if err != nil {
return 0, err
}
return strconv.ParseInt(strings.TrimSpace(string(text)), 10, 64)
}