-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconf.go
101 lines (87 loc) · 1.73 KB
/
conf.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
package main
import (
"bufio"
"os"
"strconv"
"strings"
)
// read in conf file
type Conf struct {
Bandwidth float64
Read bool
TraceFileName string
GenerateOnly bool
Generate bool
Load float64
NumFlows uint
CDFFileName string
}
func (c Conf) assert_generation() {
if !c.Generate {
panic("Conflicting options")
}
}
func (c Conf) assert_readtrace() {
if !c.Read {
panic("Conflicting options")
}
}
func readConf(fn string) Conf {
file, ok := os.Open(fn)
check(ok)
defer file.Close()
c := Conf{}
defer func() {
if c.Bandwidth == 0 {
panic("Invalid configuration")
} else if c.Read == c.Generate {
panic("Conflicting options")
}
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
l := strings.Split(scanner.Text(), " ")
switch {
case l[0][0] == '#':
continue
case l[0] == "Bandwidth":
b, ok := strconv.ParseFloat(l[1], 64)
check(ok)
c.Bandwidth = b
case l[0] == "Read":
c.Read = true
defer func() {
if c.TraceFileName == "" {
panic("Invalid configuration")
}
}()
case l[0] == "TraceFile":
c.TraceFileName = l[1]
defer c.assert_readtrace()
case l[0] == "GenerateOnly":
c.GenerateOnly = true
fallthrough
case l[0] == "Generate":
c.Generate = true
defer func() {
if c.CDFFileName == "" || c.Load == 0 || c.NumFlows == 0 {
panic("Invalid configuration")
}
}()
case l[0] == "Load":
l, ok := strconv.ParseFloat(l[1], 64)
check(ok)
c.Load = l
defer c.assert_generation()
case l[0] == "NumFlows":
n, ok := strconv.ParseUint(l[1], 10, 32)
check(ok)
c.NumFlows = uint(n)
defer c.assert_generation()
case l[0] == "CDF":
c.CDFFileName = l[1]
defer c.assert_generation()
}
}
return c
}