-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsection.go
58 lines (52 loc) · 1.11 KB
/
section.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
package darknet
import (
"bufio"
"os"
"strings"
)
// Section represents a section in the configuration file.
type Section struct {
Type string
Options []string
}
// readCfg reads a configuration file and returns a list of sections.
func readCfg(filename string) ([]Section, error) {
// Open the configuration file.
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
// Create a list of sections.
var sections []Section
var current *Section
// Read each line in the file.
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
switch line[0] {
case '[':
if current != nil {
sections = append(sections, *current)
}
current = &Section{Type: line}
case '#', ';', '\x00':
// Ignore comments and empty lines.
default:
if current == nil {
current = &Section{}
}
current.Options = append(current.Options, line)
}
}
if current != nil {
sections = append(sections, *current)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return sections, nil
}