-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirectory_reader.go
157 lines (148 loc) · 2.97 KB
/
directory_reader.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// See RFC 2425 Mime Content-Type for Directory Information
package vcard
import (
"io"
"text/scanner"
)
type DirectoryInfoReader struct {
scan *scanner.Scanner
}
func NewDirectoryInfoReader(reader io.Reader) *DirectoryInfoReader {
var s scanner.Scanner
s.Init(reader)
return &DirectoryInfoReader{&s}
}
func (di *DirectoryInfoReader) ReadContentLine() *ContentLine {
if di.scan.Peek() == scanner.EOF {
return nil
}
group, name := di.readGroupName()
params := make(map[string]Value)
if di.scan.Peek() == ';' {
params = di.readParameters()
}
di.scan.Next()
value := di.readValues()
return &ContentLine{group, name, params, value}
}
func (di *DirectoryInfoReader) readGroupName() (group, name string) {
c := di.scan.Peek()
// Skip leading linebreaks
for c != scanner.EOF && (c == '\r' || c == '\n') {
di.scan.Next()
c = di.scan.Peek()
}
// Decode group.name
var buf []rune
for c != scanner.EOF {
if c == '.' {
group = string(buf)
buf = []rune{}
} else if c == ';' || c == ':' {
name = string(buf)
return
} else {
buf = append(buf, c)
}
di.scan.Next()
c = di.scan.Peek()
}
return
}
func (di *DirectoryInfoReader) readParameters() (params map[string]Value) {
lastChar := di.scan.Peek()
c := lastChar
var buf []rune
var name string
var value string
params = make(map[string]Value)
var values Value
for c != scanner.EOF {
if c == ',' {
values = append(values, string(buf))
buf = []rune{}
} else if c == ';' || c == ':' {
if name == "" {
name = string(buf)
} else {
value = string(buf)
}
if name != "" {
values = append(values, value)
if _, ok := params[name]; ok {
params[name] = append(params[name], values...)
} else {
params[name] = values
}
}
if c == ':' {
return
}
buf = []rune{}
values = Value{}
name = ""
value = ""
} else if c == '=' {
name = string(buf)
buf = []rune{}
} else {
buf = append(buf, c)
}
di.scan.Next()
c = di.scan.Peek()
}
return
}
func (di *DirectoryInfoReader) readValues() (value StructuredValue) {
lastChar := di.scan.Next()
c := lastChar
var buf []rune
escape := false
var val Value
for c != scanner.EOF {
if c == '\n' {
la := di.scan.Peek()
if la != 32 && la != 9 {
// return
if len(buf) > 0 {
val = append(val, string(buf))
}
value = append(value, val)
return
} else {
// unfold
lastChar = la
c = di.scan.Next()
for c == 32 || c == 9 {
c = di.scan.Next()
}
}
}
if c == '\\' {
escape = true
} else if escape {
if c == 'n' || c == 'N' {
c = '\n'
}
buf = append(buf, c)
escape = false
} else if c == ',' {
if len(buf) > 0 {
val = append(val, string(buf))
buf = []rune{}
}
} else if c == ';' {
if len(buf) > 0 {
val = append(val, string(buf))
buf = []rune{}
}
value = append(value, val)
val = Value{}
} else if c != '\n' && c != '\r' {
buf = append(buf, c)
}
lastChar = c
c = di.scan.Next()
}
return
}