-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio.go
315 lines (289 loc) · 6.7 KB
/
io.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"bufio"
"compress/gzip"
"encoding/csv"
"errors"
"io"
"os"
"strings"
)
//load file content to buffer
func loadFileToBuffer(fn string, b *Buffer) error {
totalAddedLN := 0 //the number of lines has been added into buffer
scanner, err := getFileScanner(fn)
if err != nil {
return err
}
scanner.Split(bufio.ScanLines)
//set separator, if user does not provide it.
var detectLines []string //lines as detect separator data
if b.sep == 0 {
//read 10 lines to detect separator
lineNumber := 10
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
detectLines = append(detectLines, line)
if len(detectLines) >= lineNumber {
break
}
}
//if the suffix of file name is ".csv", set separator to ",".
//if the suffix of file name is "tsv", set separator to "\t".
if strings.HasSuffix(fn, ".csv") {
b.sep = ','
} else if strings.HasSuffix(fn, ".tsv") {
b.sep = '\t'
} else {
sd := sepDetecor{}
b.sep = sd.sepDetect(detectLines)
}
}
//check final separator
if b.sep == 0 {
fatalError(errors.New("tv can't identify separator, you need to set it manual"))
}
//add detectLines to buffer
for _, line := range detectLines {
//parse and add line to buffer
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
return err
}
totalAddedLN++
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 && args.NLine > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
//parse and add line to buffer
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
return err
}
totalAddedLN++
}
return nil
}
//load console pipe content to buffer
func loadPipeToBuffer(stdin io.Reader, b *Buffer) error {
totalAddedLN := 0 //the number of lines has been added into buffer
var err error
scanner := bufio.NewScanner(stdin)
//read 10 lines to detect separator
lineNumber := 10
var detectLines []string //lines as detect separator data
if b.sep == 0 {
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
detectLines = append(detectLines, line)
if len(detectLines) >= lineNumber {
break
}
}
sd := sepDetecor{}
b.sep = sd.sepDetect(detectLines)
}
//check final separator
if b.sep == 0 {
fatalError(errors.New("tv can't identify separator, you need to set it manual"))
}
//add detectLines to buffer
for _, line := range detectLines {
//parse and add line to buffer
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
return err
}
totalAddedLN++
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
}
for scanner.Scan() {
line := scanner.Text()
//skip empty line
if line == "\n" {
continue
}
//ignore first n lines
if args.SkipNum > 0 {
args.SkipNum--
continue
}
//ignore line with specified prefix
if skipLine(line, args.SkipSymbol) {
continue
}
//parse and add line to buffer
if totalAddedLN >= args.NLine && args.NLine > 0 {
break
}
err = addDRToBuffer(b, line, args.ShowNum, args.HideNum)
if err != nil {
return err
}
totalAddedLN++
}
return nil
}
//check a line whether should bu skip, according to prefix
func skipLine(line string, sy []string) bool {
for _, sy := range sy {
if strings.HasPrefix(line, sy) {
return true
}
}
return false
}
//get suitable scanner(compressed or not)
func getFileScanner(fn string) (*bufio.Scanner, error) {
info, err := os.Stat(fn)
if err != nil {
_, err := os.Open(fn)
return nil, err
}
//check if fn is a directory
if info.IsDir() {
return nil, errors.New(fn + " is a directory")
}
file, err := os.Open(fn)
if err != nil {
return nil, err
}
//if input is a gzip file
if strings.HasSuffix(fn, ".gz") {
gzCont, err := gzip.NewReader(file)
if err != nil {
return nil, err
}
return bufio.NewScanner(gzCont), nil
}
return bufio.NewScanner(file), nil
}
//check columns that should be displayed
func getVisCol(showNumL, hideNumL []int, colLen int) ([]int, error) {
for _, i := range showNumL {
if i > colLen || i <= 0 {
return nil, errors.New("Column number " + I2S(i) + " does not exist")
}
}
for _, i := range hideNumL {
if i > colLen || i <= 0 {
return nil, errors.New("Column number " + I2S(i) + " does not exist")
}
}
var visCol []int
for i := 0; i < colLen; i++ {
flag, err := checkVisible(showNumL, hideNumL, i)
if err != nil {
return nil, err
}
if flag {
visCol = append(visCol, i)
}
}
return visCol, nil
}
//check ith column should be displayed or not
func checkVisible(showNumL, hideNumL []int, col int) (bool, error) {
if len(showNumL) != 0 && len(hideNumL) != 0 {
return false, errors.New("you can only set visible column or hidden column")
}
if len(showNumL) != 0 {
for _, colTestS := range showNumL {
if col+1 == colTestS {
return true, nil
}
}
return false, nil
}
if len(hideNumL) != 0 {
for _, colTestH := range hideNumL {
if col+1 == colTestH {
return false, nil
}
}
}
return true, nil
}
//use go csv library to parse a string line into csv format
func lineCSVParse(s string, sep rune) ([]string, error) {
r := csv.NewReader(strings.NewReader(s))
r.Comma = sep
r.LazyQuotes = true
//r.TrimLeadingSpace = true //disable, because it will remove NULL item and cause issue.
record, err := r.Read()
return record, err
}
//add displayable(according to user's input argument) RowArray(covert line to array) To Buffer
func addDRToBuffer(b *Buffer, line string, showNum, hideNum []int) error {
var err error
lineCSVParts, err := lineCSVParse(line, b.sep)
if err != nil {
return err
}
if len(showNum) != 0 || len(hideNum) != 0 {
var lineSli []string
visCol, err := getVisCol(showNum, hideNum, len(lineCSVParts))
if err != nil {
return err
}
for _, i := range visCol {
lineSli = append(lineSli, lineCSVParts[i])
}
err = b.contAppendSli(lineSli, args.Strict)
if err != nil {
return err
}
} else {
err := b.contAppendSli(lineCSVParts, args.Strict)
if err != nil {
return err
}
}
return err
}