-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
118 lines (107 loc) · 2.67 KB
/
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
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
package tbln
import (
"fmt"
"io"
"sort"
)
// Writer writes records to a TBLN encoded file.
type Writer struct {
Writer io.Writer
}
// NewWriter returns a new Writer that writes to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{
Writer: w,
}
}
// WriteRow writes a single tbln record to w along with any necessary escaping.
// A record is a slice of strings with each string being one field.
func (w *Writer) WriteRow(row []string) error {
_, err := io.WriteString(w.Writer, JoinRow(row)+"\n")
return err
}
// WriteAll writes multiple tbln records to w using Write.
func WriteAll(writer io.Writer, tbln *TBLN) error {
w := NewWriter(writer)
if err := w.WriteDefinition(tbln.Definition); err != nil {
return err
}
for _, row := range tbln.Rows {
if err := w.WriteRow(row); err != nil {
return err
}
}
return nil
}
// WriteDefinition writes Definition (comment and extra) to w.
func (w *Writer) WriteDefinition(d *Definition) error {
if err := w.writeComment(d); err != nil {
return err
}
return w.writeExtra(d)
}
// writeComment writes comment to w.
func (w *Writer) writeComment(d *Definition) error {
for _, comment := range d.Comments {
if _, err := io.WriteString(w.Writer, fmt.Sprintf("# %s\n", comment)); err != nil {
return err
}
}
return nil
}
// writeExtra writes extra to w.
func (w *Writer) writeExtra(d *Definition) error {
if err := w.writeExtraTarget(d, false); err != nil {
return err
}
if len(d.Signs) > 0 {
if err := w.writeSigns(d); err != nil {
return err
}
}
if len(d.Hashes) > 0 {
if err := w.writeHashes(d); err != nil {
return err
}
}
return w.writeExtraTarget(d, true)
}
func (w *Writer) writeExtraTarget(d *Definition, targetFlag bool) error {
keys := make([]string, 0, len(d.Extras))
for k := range d.Extras {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if d.Extras[k].hashTarget != targetFlag {
continue
}
if _, err := fmt.Fprintf(w.Writer, "; %s: %s\n", k, d.Extras[k].value); err != nil {
return err
}
}
return nil
}
func (w *Writer) writeSigns(d *Definition) error {
for k, v := range d.Signs {
signs := make([]string, 0, 3)
signs = append(signs, k)
signs = append(signs, v.algorithm)
signs = append(signs, fmt.Sprintf("%x", v.sign))
if _, err := fmt.Fprintf(w.Writer, "; Signature: %s\n", JoinRow(signs)); err != nil {
return err
}
}
return nil
}
func (w *Writer) writeHashes(d *Definition) error {
for k, v := range d.Hashes {
hashes := make([]string, 0, 2)
hashes = append(hashes, k)
hashes = append(hashes, fmt.Sprintf("%x", v))
if _, err := fmt.Fprintf(w.Writer, "; Hash: %s\n", JoinRow(hashes)); err != nil {
return err
}
}
return nil
}