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
|
package xio
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"sync"
)
// SyncWriter makes any writer thread safe.
type SyncWriter struct {
sync.Mutex
w io.Writer
}
// NewSyncWriter returns an io.Writer that can be safely accessed by multiple
// goroutines.
func NewSyncWriter(w io.Writer) *SyncWriter {
return &SyncWriter{w: w}
}
// Write wraps the underlying writer and gives exclusive access.
func (w *SyncWriter) Write(p []byte) (n int, err error) {
w.Lock()
n, err = w.w.Write(p)
w.Unlock()
return
}
// OpenTwo opens two files. The caller needs to check for a single error only.
func OpenTwo(f0, f1 string) (g0, g1 *os.File, err error) {
if g0, err = os.Open(f0); err != nil {
return nil, nil, err
}
if g1, err = os.Open(f1); err != nil {
return nil, nil, err
}
return g0, g1, nil
}
// MapFromTabular reads from a reader and turns values from keyC, valueC
// columns, both 1-indexed, into a map.
func MapFromTabular(r io.Reader, sep string, keyC, valueC int) (map[string]string, error) {
var (
br = bufio.NewReader(r)
m = make(map[string]string)
line, k, v string
fields []string
err error
)
for {
line, err = br.ReadString('\n')
if err == io.EOF {
return m, nil
}
if err != nil {
return nil, err
}
fields = strings.Split(line, sep)
if len(fields) > keyC-1 && len(fields) > valueC-1 {
k = strings.TrimSpace(fields[keyC-1])
v = strings.TrimSpace(fields[valueC-1])
m[k] = v
} else {
return nil, fmt.Errorf("invalid line: %v (%v fields, %v, %v)",
line, len(fields), keyC, valueC)
}
}
return m, nil
}
|