-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
69 lines (54 loc) · 1.09 KB
/
util.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
package main
import (
"bufio"
"compress/bzip2"
"compress/gzip"
"errors"
"io"
"log"
"os"
"strconv"
"strings"
)
const GZIP_SUFFIX = ".gz"
const BZIP2_SUFFIX = ".bz2"
var ErrZeroLengthFile = errors.New("File has zero length")
func genericReader(fileName string) (*bufio.Reader, error) {
var reader io.Reader
var err error = nil
var f *os.File = nil
if fileName == "" {
logv("Reading from stdin")
return bufio.NewReader(os.Stdin), nil
} else {
logv("Opening: " + fileName)
f, err = os.Open(fileName)
if err != nil {
logg(err)
return nil, err
}
fileInfo, err := f.Stat()
if err != nil {
logg(err)
return nil, err
}
if fileInfo.Size() == 0 {
return nil, ErrZeroLengthFile
}
logv(" File size: " + strconv.FormatInt(fileInfo.Size(), 10))
if strings.HasSuffix(fileName, GZIP_SUFFIX) {
reader, err = gzip.NewReader(f)
if err != nil {
log.Println(err)
return nil, err
}
} else {
if strings.HasSuffix(fileName, BZIP2_SUFFIX) {
reader = bzip2.NewReader(f)
} else {
reader = f
}
}
return bufio.NewReader(reader), nil
}
}