-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_dumper.go
122 lines (92 loc) · 1.95 KB
/
csv_dumper.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
119
120
121
122
//go:build csv_dumper
package main
import (
"flag"
"fmt"
"io"
"os"
"strconv"
"tlog.app/go/eazy"
)
type Buf []byte
var (
infile = flag.String("i", "", "eazy compressed input file")
outfile = flag.String("o", "", "output csv file")
header = flag.Bool("h", true, "add header row")
base = flag.Int("base", 10, "offset and length base")
data = flag.Int("data", 0, "data max len")
)
func main() {
flag.Parse()
err := run()
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
}
func run() (err error) {
var fr io.Reader
if q := *infile; q != "" && q != "-" {
f, err := os.Open(q)
if err != nil {
return fmt.Errorf("open input file: %w", err)
}
defer func() {
e := f.Close()
if err == nil {
err = fmt.Errorf("close input file: %w", e)
}
}()
fr = f
} else {
fr = os.Stdin
}
var fw io.Writer
if q := *outfile; q != "" && q != "-" {
f, err := os.Create(q)
if err != nil {
return fmt.Errorf("create output file: %w", err)
}
defer func() {
e := f.Close()
if err == nil {
err = fmt.Errorf("close output file: %w", e)
}
}()
fw = f
} else {
fw = os.Stdout
}
if *header {
_, err = fw.Write([]byte("ioff,iend,ooff,tag,l,off\n"))
if err != nil {
return fmt.Errorf("write header")
}
}
var b Buf
d := eazy.NewDumper(nil)
d.Debug = func(ioff, iend, ooff int64, tag byte, l, off int) {
b = b[:0]
b = strconv.AppendInt(b, ioff, *base)
b = append(b, ',')
b = strconv.AppendInt(b, iend, *base)
b = append(b, ',')
b = strconv.AppendInt(b, ooff, *base)
b = append(b, ',')
b = fmt.Appendf(b, "%c,", tag)
b = strconv.AppendInt(b, int64(l), *base)
b = append(b, ',')
if tag == 'm' {
off >>= 3
}
b = strconv.AppendInt(b, int64(off), *base)
b = append(b, '\n')
_, _ = fw.Write(b)
// fmt.Fprintf(fw, "%d,%d,%c,%d,%d\n", ioff, ooff, tag, l, off)
}
_, err = io.Copy(d, fr)
if err != nil {
return fmt.Errorf("copy: %w", err)
}
return nil
}