forked from zentures/sequence
-
Notifications
You must be signed in to change notification settings - Fork 3
/
filehandler.go
70 lines (58 loc) · 1.17 KB
/
filehandler.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
package sequence
import (
"bufio"
"compress/gzip"
"io/ioutil"
"os"
"strings"
)
func getDirOfFiles(path string) ([]string, error) {
filenames := make([]string, 0, 10)
files, err := ioutil.ReadDir(path)
if err != nil {
return filenames, err
}
for _, f := range files {
filenames = append(filenames, path+"/"+f.Name())
}
return filenames, err
}
//Opens an input file for reading.
func OpenInputFile(fname string) (*bufio.Scanner, *os.File, error) {
var s *bufio.Scanner
var f *os.File
var err error
//this determines the input is from the stdin
if fname == "-" {
f = os.Stdin
} else {
f, err = os.Open(fname)
if err != nil {
return s, f, err
}
}
if strings.HasSuffix(fname, ".gz") {
gunzip, err := gzip.NewReader(f)
if err != nil {
return s, f, err
}
s = bufio.NewScanner(gunzip)
} else {
s = bufio.NewScanner(f)
}
return s, f, err
}
//Opens and clears output file for writing.
func OpenOutputFile(fname string) (*os.File, error) {
var (
ofile *os.File
err error
)
if fname == "" {
ofile = os.Stdout
} else {
// Open output file
ofile, err = os.OpenFile(fname, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
}
return ofile, err
}