-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
74 lines (65 loc) · 1.5 KB
/
generic.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
package asn2pf
import (
"bufio"
"compress/gzip"
"errors"
"io"
"os"
"github.com/klauspost/compress/zstd"
)
// const
const (
_app = "[asn2pf] "
_err = _app + "[error] "
_inf = _app + "[info] "
_empty = ""
_linefeed = "\n"
)
// getFileScanner ...
func getFileScanner(name string) (s *bufio.Scanner, err error) {
r, err := getReader(name)
if err != nil {
return s, errors.New("[compress] [scanner] unable to read file [" + name + "] [" + err.Error() + "]")
}
return bufio.NewScanner(r), nil
}
// getReader ...
func getReader(name string) (io.Reader, error) {
var r io.Reader
l := len(name)
if l < 3 {
return r, errors.New("[compress] [scanner] unable to read file [" + name + "] [unknown extension]")
}
f, err := os.Open(name)
if err != nil {
return r, errors.New("[compress] [scanner] unable to read file [" + name + "] [" + err.Error() + "]")
}
switch name[len(name)-3:] {
case "zst":
r, err = zstd.NewReader(f)
case ".gz":
r, err = gzip.NewReader(f)
case "tsv", "txt", "csv":
r = f
default:
return r, errors.New("[compress] [internal] unsupported format")
}
if err != nil {
return r, errors.New("[compress] [scanner] unable to read file [" + name + "] [" + err.Error() + "]")
}
return r, nil
}
//
// LITTLE GENERIC IO HELPER
//
// out ...
func out(msg string) { os.Stdout.Write([]byte(msg + _linefeed)) }
// info ...
func info(msg string) { out(_inf + msg) }
// pad ...
func pad(in string, l int) string {
for len(in) < l {
in = in + " "
}
return in
}