Debian Codegraph @

keyfile/keyfile.go

raw
1 // Package keyfile implements the ini file format that is used in many
2 // of the xdg specs.
3 //
4 // WARNING: This package is meant for internal use and the API may
5 // change without warning.
6 package keyfile
7
8 import (
9 "bufio"
10 "bytes"
11 "io"
12 "strings"
13 )
14
15 // KeyFile is an implementation of the keyfile format used by several
16 // FreeDesktop.org (xdg) specs. The key values without a header section
17 // can be accessed using an empty string as the group.
18 type KeyFile struct {
19 m map[string]map[string]string
20 }
21
22 // New creates a new KeyFile and returns it.
23 func New(r io.Reader) (*KeyFile, error) {
24 kf := new(KeyFile)
25 kf.m = make(map[string]map[string]string)
26 hdr := ""
27 kf.m[hdr] = make(map[string]string)
28
29 sc := bufio.NewScanner(r)
30 for sc.Scan() {
31 line := strings.TrimSpace(sc.Text())
32 switch {
33 case len(line) == 0:
34 // Empty line.
35 case line[0] == '#':
36 // Comment.
37 case line[0] == '[' && line[len(line)-1] == ']':
38 // Group header.
39 hdr = line[1 : len(line)-1]
40 kf.m[hdr] = make(map[string]string)
41 case strings.Contains(line, "="):
42 // Entry.
43 p := strings.SplitN(line, "=", 2)
44 p[0] = strings.TrimSpace(p[0])
45 p[1] = strings.TrimSpace(p[1])
46 kf.m[hdr][p[0]] = p[1]
47 default:
48 return nil, ErrInvalid
49 }
50 }
51 return kf, nil
52 }
53
54 // Groups returns a slice of groups that exist for the KeyFile.
55 func (kf *KeyFile) Groups() []string {
56 groups := make([]string, 0, len(kf.m))
57 for k := range kf.m {
58 groups = append(groups, k)
59 }
60 return groups
61 }
62
63 // GroupExists returns a bool indicating whether the given group 'g'
64 // exists.
65 func (kf *KeyFile) GroupExists(g string) bool {
66 _, exists := kf.m[g]
67 return exists
68 }
69
70 // Keys returns a slice of keys that exist for the given group 'g'.
71 func (kf *KeyFile) Keys(g string) []string {
72 keys := make([]string, 0, len(kf.m[g]))
73 for k := range kf.m[g] {
74 keys = append(keys, k)
75 }
76 return keys
77 }
78
79 // KeyExists returns a bool indicating whether the given group 'g' and
80 // key 'k' exists.
81 func (kf *KeyFile) KeyExists(g, k string) bool {
82 _, exists := kf.m[g][k]
83 return exists
84 }
85
86 // Value returns the raw string for group 'g' and key 'k'. Value will
87 // return a blank string if the key doesn't exist; use GroupExists or
88 // KeyExists to if you need to treat a missing value differently then a
89 // blank value.
90 func (kf *KeyFile) Value(g, k string) string {
91 return kf.m[g][k]
92 }
93
94 // ValueList returns a slice of raw strings for group 'g' and key 'k'.
95 // ValueList will return an empty slice if the key doesn't exist; use
96 // GroupExists or KeyExists to if you need to treat a missing value
97 // differently then a blank value.
98 func (kf *KeyFile) ValueList(g, k string) ([]string, error) {
99 var buf bytes.Buffer
100 var isEscaped bool
101 var list []string
102
103 for _, r := range kf.Value(g, k) {
104 if isEscaped {
105 if r == ';' {
106 buf.WriteRune(';')
107 } else {
108 // The escape sequence isn't '\;', so we
109 // want to copy it over as is.
110 buf.WriteRune('\\')
111 buf.WriteRune(r)
112 }
113 isEscaped = false
114 } else {
115 switch r {
116 case '\\':
117 isEscaped = true
118 case ';':
119 list = append(list, buf.String())
120 buf.Reset()
121 default:
122 buf.WriteRune(r)
123 }
124 }
125 }
126 if isEscaped {
127 return nil, ErrUnexpectedEndOfString
128 }
129
130 last := buf.String()
131 if last != "" {
132 list = append(list, last)
133 }
134
135 return list, nil
136 }