forked from schachmat/gods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuevent.go
54 lines (41 loc) · 819 Bytes
/
uevent.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
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
type Hash struct {
values map[string]string
}
func parseFile(path string) *Hash {
file, err := os.Open(path)
hash := &Hash{values: make(map[string]string)}
if err != nil {
return hash
}
defer file.Close()
var buffer []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
buffer = strings.Split(scanner.Text(), "=")
if len(buffer) == 2 {
hash.values[buffer[0]] = buffer[1]
}
}
return hash
}
func (h *Hash) SearchForInt(fields []string) int {
for _, field := range fields {
if _, exists := h.values[field]; exists {
return h.GetInt(field)
}
}
return 0
}
func (h *Hash) GetInt(field string) int {
if convertedValue, err := strconv.Atoi(h.values[field]); err == nil {
return convertedValue
}
return 0
}