forked from datatogether/warc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
83 lines (74 loc) · 1.84 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
package warc
import (
"fmt"
"io"
"strconv"
)
// WriteRecords calls Write on each record to w
func WriteRecords(w io.Writer, records []Record) error {
for _, rec := range records {
if err := rec.Write(w); err != nil {
return err
}
}
return nil
}
// WriteHeader writes a fully formed header with version to w
func writeHeader(w io.Writer, t RecordType, fields map[string]string) error {
if err := writeWarcVersion(w); err != nil {
return err
}
if err := writeField(w, warc_type, t.String()); err != nil {
return err
}
if err := writeFields(w, fields); err != nil {
return err
}
if _, err := io.WriteString(w, "\r\n"); err != nil {
return err
}
return nil
}
// WriteBlock writes all of reader (record content) to w, followed by 2 CRLF's
func writeBlock(w io.Writer, r []byte) error {
// fmt.Println(string(r))
// fmt.Println("------")
if _, err := w.Write(r); err != nil {
return err
}
// write 2xCRLF
_, err := io.WriteString(w, "\r\n\r\n")
return err
}
// writeWarcVersion writes the warc version header
func writeWarcVersion(w io.Writer) error {
_, err := io.WriteString(w, "WARC/1.0\r\n")
return err
}
// writeDefinedFields takes a map of token constants to values, and writes them to w
// it skips fields who's value is ""
func writeFields(w io.Writer, fields map[string]string) error {
for field, value := range fields {
if err := writeField(w, field, value); err != nil {
return err
}
}
return nil
}
func writeField(w io.Writer, key, value string) error {
// don't write empty fields
if value == "" {
return nil
}
// format entry
ln := fmt.Sprintf("%s: %s\r\n", key, value)
_, err := io.WriteString(w, ln)
return err
}
// convenience func to convert int64s to a string
func int64String(i int64) string {
return strconv.FormatInt(i, 10)
}
func intString(i int) string {
return strconv.FormatInt(int64(i), 10)
}