This repository has been archived by the owner on May 14, 2024. It is now read-only.
/
main.go
253 lines (218 loc) · 4.45 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
)
var osStdout *os.File = os.Stdout
const (
ifdefToken = "#ifdef"
kvToken = "#kv"
endToken = "#endif"
)
var (
listFlag *bool
inputFile *string
outputFile *string
pres *string
)
func init() {
{
var b bool
listFlag = &b
}
{
var s string
inputFile = &s
}
{
var s string
outputFile = &s
}
{
var s string
pres = &s
}
}
func main() {
// flags
listFlag = flag.Bool("l", false, "show list of preprocessor names")
inputFile = flag.String("i", "", "name of input Go source")
outputFile = flag.String("o", "", "name of output Go source")
pres = flag.String("p", "", "allowable preprocessors #ifdef...#endif")
flag.Parse()
if *inputFile == "" {
fmt.Fprintf(os.Stderr, "Name of input file is empty")
return
}
// show list of preprocessors names
if *listFlag {
if err := list(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
}
return
}
if *outputFile == "" {
fmt.Fprintf(os.Stderr, "Name of output file is empty")
return
}
if *pres == "" {
fmt.Fprintf(os.Stderr, "List of allowable preprocessor names is empty")
return
}
if err := change(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
return
}
}
// list of preprocessors
func list() error {
b, err := ioutil.ReadFile(*inputFile)
if err != nil {
return fmt.Errorf("cannot read file `%s` : %v", *inputFile, err)
}
lines := bytes.Split(b, []byte("\n"))
var names []string
for i := range lines {
// filter
if !bytes.Contains(lines[i], []byte(ifdefToken)) {
continue
}
// find name
index := bytes.Index(lines[i], []byte(ifdefToken))
if index < 0 {
continue
}
// get name
name := string(lines[i][index+len(ifdefToken):])
ls := strings.Split(name, " ")
for j := range ls {
ls[j] = strings.TrimSpace(ls[j])
if ls[j] == "" {
continue
}
names = append(names, ls[j])
}
}
// show names
fmt.Fprintf(osStdout, "Preprocessor names :\n")
sort.Strings(names)
for i := 0; i < len(names); i++ {
isUniq := true
for j := i + 1; j < len(names); j++ {
if names[i] == names[j] {
isUniq = false
}
}
if !isUniq {
continue
}
fmt.Fprintf(osStdout, "* %s\n", names[i])
}
return nil
}
func change() error {
b, err := ioutil.ReadFile(*inputFile)
if err != nil {
return fmt.Errorf("cannot read file `%s` : %v", *inputFile, err)
}
ps := string(*pres)
lines := bytes.Split(b, []byte("\n"))
var buf bytes.Buffer
addLine := true
kv := map[string]string{}
for i := range lines {
line := string(lines[i])
// ifdefToken
has, found := hasIfdef(line, ps)
if has {
addLine = false
if found {
addLine = true
}
continue
}
// kvToken
has, found, key, value := hasKv(line, ps)
if has {
if found {
kv[key] = value
}
continue
}
// endToken
index := strings.Index(line, endToken)
if index >= 0 {
// get name
addLine = true
continue
}
if !addLine {
continue
}
// key-value changing
for k, v := range kv {
line = strings.Replace(line, "#"+k, v, -1)
}
buf.WriteString(line)
buf.Write([]byte("\n"))
}
err = ioutil.WriteFile(*outputFile, buf.Bytes(), 0644)
if err != nil {
return err
}
// gofmt
_, _ = exec.Command("gofmt", "-s", "-w", *outputFile).CombinedOutput()
// goimports
_, _ = exec.Command("goimports", "-w", *outputFile).CombinedOutput()
return nil
}
func hasIfdef(line, ps string) (has, found bool) {
index := strings.Index(line, ifdefToken)
if index >= 0 {
has = true
preprocessors := strings.Split(line[index+len(ifdefToken):], " ")
for i := range preprocessors {
preprocessors[i] = strings.TrimSpace(preprocessors[i])
if preprocessors[i] == "" {
continue
}
if preprocessors[i] == ps {
found = true
break
}
}
}
return
}
func hasKv(line, ps string) (has, found bool, key, value string) {
index := strings.Index(line, kvToken)
if index >= 0 {
has = true
// example : preprocessors = "Float32 keys:value"
preprocessors := strings.TrimSpace(line[index+len(kvToken):])
index = strings.Index(preprocessors, " ")
if index < 0 {
return
}
if preprocessors[:index] != ps {
return
}
found = true
preprocessors = preprocessors[index:]
// example : preprocessors = "keys:value"
preprocessors = strings.TrimSpace(preprocessors)
index = strings.Index(preprocessors, ":")
if index < 0 {
return
}
key = preprocessors[:index]
value = preprocessors[index+1:]
}
return
}