-
Notifications
You must be signed in to change notification settings - Fork 6
/
adifwriter.go
52 lines (44 loc) · 969 Bytes
/
adifwriter.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
package adifparser
import (
"bufio"
"errors"
"fmt"
"io"
)
var OutputStarted = errors.New("Output already started.")
// Basic writer type
type ADIFWriter interface {
WriteRecord(ADIFRecord) error
Flush() error
SetComment(string) error
}
type baseADIFWriter struct {
writer *bufio.Writer
started bool
}
// Construct a new writer
func NewADIFWriter(w io.Writer) *baseADIFWriter {
writer := &baseADIFWriter{}
writer.writer = bufio.NewWriter(w)
writer.started = false
return writer
}
func (writer *baseADIFWriter) WriteRecord(r ADIFRecord) error {
writer.started = true
_, err := fmt.Fprintf(writer.writer, "%s<eor>\n", r.ToString())
if err != nil {
// TODO: log
return err
}
return nil
}
func (writer *baseADIFWriter) Flush() error {
return writer.writer.Flush()
}
func (writer *baseADIFWriter) SetComment(comment string) error {
if writer.started {
return OutputStarted
}
fmt.Fprintf(writer.writer, "%s<eoh>\n", comment)
return nil
}