-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirectory_writer.go
82 lines (77 loc) · 1.88 KB
/
directory_writer.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
package vcard
import (
"io"
)
// Permit to serialize Directory Information data as defined by RFC 2425
type DirectoryInfoWriter struct {
writer io.Writer
}
//create a new DirectoryInfoWriter
func NewDirectoryInfoWriter(writer io.Writer) *DirectoryInfoWriter {
return &DirectoryInfoWriter{writer}
}
func (di *DirectoryInfoWriter) WriteContentLine(contentLine *ContentLine) {
if contentLine.Group != "" {
io.WriteString(di.writer, contentLine.Group)
io.WriteString(di.writer, ".")
}
io.WriteString(di.writer, contentLine.Name)
if contentLine.Params != nil {
for key, values := range contentLine.Params {
io.WriteString(di.writer, ";")
io.WriteString(di.writer, key)
if len(values) > 0 {
io.WriteString(di.writer, "=")
for vi := 0; vi < len(values); vi++ {
io.WriteString(di.writer, values[vi])
if vi+1 < len(values) {
io.WriteString(di.writer, ",")
}
}
}
}
}
io.WriteString(di.writer, ":")
for si := 0; si < len(contentLine.Value); si++ {
for vi := 0; vi < len(contentLine.Value[si]); vi++ {
di.WriteValue(contentLine.Value[si][vi])
if vi+1 < len(contentLine.Value[si]) {
io.WriteString(di.writer, ",")
}
}
if si+1 < len(contentLine.Value) {
io.WriteString(di.writer, ";")
}
}
io.WriteString(di.writer, "\r\n")
}
// this function escape '\n' '\r' ';' ',' character with the '\\' character
func (di *DirectoryInfoWriter) WriteValue(value string) {
i := 0
for _, c := range value {
if i == 76 {
// if line to long fold value on multiple line
io.WriteString(di.writer, "\n ")
i = 0
}
var e string
switch c {
case '\r':
e = `\r`
case '\n':
e = `\n`
case ';':
e = `\;`
case ':':
e = `\:`
case ',':
e = `\,`
default:
// c is an int representing a Unicode code point.
// convert it to string (UTF-8 encoded character)
e = string(c)
}
io.WriteString(di.writer, e)
i++
}
}