-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (128 loc) · 3.11 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
)
type actionFlags uint32
const (
unknown actionFlags = iota
sph2json
meta2json
json2
)
func getAction(inf string) (actionFlags, string, error) {
ln := len(inf)
if ln < 3 {
return unknown, "", errors.New(fmt.Sprintf("wrong file provided (%s)", inf))
}
var outfile string
if inf[len(inf)-3:] == "sph" {
outfile = inf + ".json"
return sph2json, outfile, nil
}
if inf[len(inf)-4:] == "meta" {
outfile = inf + ".json"
return meta2json, outfile, nil
}
if ln < 5 || inf[len(inf)-5:] != ".json" {
return unknown, "", errors.New(fmt.Sprintf("wrong file provided (%s)", inf))
}
outfile = inf[0 : len(inf)-5]
return json2, outfile, nil
}
func check(e error) {
if e != nil {
fmt.Printf("%v", e)
os.Exit(1)
}
}
func main() {
inFile := flag.String("i", "", "path to sph, meta, sph.json or meta.json file")
poutf := flag.String("o", "", "path to target file (optional)")
flag.Parse()
version := "$Format:%h$"
if inFile == nil || *inFile == "" {
fmt.Printf("SPHtool v %s - sph and meta files converter\n", version)
fmt.Printf("Supports sph v. %d, meta v. %d, pq meta v. %d\n", SPHVersion, MetaVersion, MetaVersionPq)
flag.Usage()
os.Exit(1)
}
action, outf, err := getAction(*inFile)
check(err)
if poutf != nil && *poutf != "" {
outf = *poutf
}
r, err := os.Open(*inFile)
check(err)
defer func() { _ = r.Close() }()
o, err := os.Create(outf)
check(err)
defer func() { _ = o.Close() }()
switch action {
case sph2json:
fmt.Printf("Input sph file is %v\n", *inFile)
var header sph
header.load(r)
header.Checkvalid() // will abort inside if this fails
enc := json.NewEncoder(o)
err = enc.Encode(&header)
check(err)
fmt.Printf("File serialized to json %v\n", outf)
case meta2json:
fmt.Printf("Input meta file is %v\n", *inFile)
enc := json.NewEncoder(o)
var hdr metahdr
var err error
hdr.load(r)
if hdr.isPq() {
var pqmeta metapq
pqmeta.metahdr = hdr
pqmeta.load(r)
pqmeta.Checkvalid() // will abort inside if this fails
err = enc.Encode(&pqmeta)
} else if hdr.isRt() {
var rtmeta meta
rtmeta.metahdr = hdr
rtmeta.load(r)
rtmeta.Checkvalid() // will abort inside if this fails
err = enc.Encode(&rtmeta)
}
check(err)
fmt.Printf("File serialized to json %v\n", outf)
case json2:
fmt.Printf("Input json file is %v\n", *inFile)
var hdr metahdr
{
dec := json.NewDecoder(r)
err = dec.Decode(&hdr)
check(err)
}
_, _ = r.Seek(0, 0)
if hdr.isSph() {
var header sph
dec := json.NewDecoder(r)
err = dec.Decode(&header)
check(err)
header.save(o)
fmt.Printf("Json encoded to sph %v\n", outf)
} else if hdr.isRt() {
var header meta
dec := json.NewDecoder(r)
err = dec.Decode(&header)
check(err)
header.save(o)
fmt.Printf("Json encoded to meta %v\n", outf)
} else if hdr.isPq() {
var header metapq
dec := json.NewDecoder(r)
err = dec.Decode(&header)
check(err)
header.save(o)
fmt.Printf("Json encoded to pq meta %v\n", outf)
}
}
}