-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfg.go
103 lines (96 loc) · 2.85 KB
/
cfg.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
// cfg - Yet another config file reader
// License: MIT / X11
// Copyright (c) 2013 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package cfg
import (
"errors"
"os"
"regexp"
"strings"
"fmt"
)
var re *regexp.Regexp
var pat = "[#].*\\n|\\s+\\n|\\S+[=]|.*\n"
func init() {
re, _ = regexp.Compile(pat)
}
// Load adds or updates entries in an existing map with string keys
// and string values using a configuration file.
//
// The filename paramter indicates the configuration file to load ...
// the dest parameter is the map that will be updated.
//
// The configuration file entries should be constructed in key=value
// syntax. A # symbol at the beginning of a line indicates a comment.
// Blank lines are ignored.
func Load(filename string, dest map[string]string) error {
fi, err := os.Stat(filename)
if err != nil {
return err
}
f, err := os.Open(filename)
if err != nil {
return err
}
buff := make([]byte, fi.Size())
f.Read(buff)
f.Close()
str := string(buff)
if !strings.HasSuffix(str, "\n") {
str += "\n";
}
s2 := re.FindAllString(str, -1)
for i := 0; i < len(s2); {
if strings.HasPrefix(s2[i], "#") {
i++
} else if strings.HasSuffix(s2[i], "=") {
key := strings.ToLower(s2[i])[0 : len(s2[i])-1]
i++
if strings.HasSuffix(s2[i], "\n") {
val := s2[i][0 : len(s2[i])-1]
if strings.HasSuffix(val, "\r") {
val = val[0 : len(val)-1]
}
i++
dest[key] = val
}
} else if strings.Index(" \t\r\n", s2[i][0:1]) > -1 {
i++
} else {
return errors.New("Unable to process line in cfg file containing " + s2[i])
}
}
return nil
}
func main() {
mymap := make(map[string]string)
err := Load("test.cfg", mymap)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%v\n", mymap)
}