Files
HackBrowserData/internal/export/csv.go
T
Roger 0ed920f846 docs: trim code comments to the why (#676)
Comments that restated names or narrated the code are gone, and multi-line ones are cut to a line of why.
Only comments change: each edited Go file has the same token stream as before. winapi and abe come later.
2026-11-02 00:03:03 +08:00

41 lines
777 B
Go

package export
import (
"encoding/csv"
"fmt"
"io"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
// Excel reads a CSV file as UTF-8 only when it starts with a BOM.
var utf8BOM = []byte{0xEF, 0xBB, 0xBF}
type csvEncoder struct {
w io.Writer
rows *csv.Writer
header bool
}
func newCSVEncoder(w io.Writer) *csvEncoder {
return &csvEncoder{w: w, rows: csv.NewWriter(w)}
}
func (e *csvEncoder) encode(r record.Record) error {
if !e.header {
if _, err := e.w.Write(utf8BOM); err != nil {
return fmt.Errorf("write BOM: %w", err)
}
if err := e.rows.Write(structCSVHeader(r)); err != nil {
return err
}
e.header = true
}
return e.rows.Write(structCSVRow(r))
}
func (e *csvEncoder) end() error {
e.rows.Flush()
return e.rows.Error()
}