diff options
Diffstat (limited to 'skate/xio/util.go')
-rw-r--r-- | skate/xio/util.go | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/skate/xio/util.go b/skate/xio/util.go index 49f38a3..90a1012 100644 --- a/skate/xio/util.go +++ b/skate/xio/util.go @@ -9,23 +9,24 @@ import ( "sync" ) -// SingleWriter makes any writer thread safe. -type SingleWriter struct { +// SyncWriter makes any writer thread safe. +type SyncWriter struct { sync.Mutex w io.Writer } -// NewSingleWriter returns an io.Writer that can be safely accessed by multiple +// NewSyncWriter returns an io.Writer that can be safely accessed by multiple // goroutines. -func NewSingleWriter(w io.Writer) *SingleWriter { - return &SingleWriter{w: w} +func NewSyncWriter(w io.Writer) *SyncWriter { + return &SyncWriter{w: w} } // Write wraps the underlying writer and gives exclusive access. -func (w *SingleWriter) Write(p []byte) (n int, err error) { +func (w *SyncWriter) Write(p []byte) (n int, err error) { w.Lock() - defer w.Unlock() - return w.w.Write(p) + n, err = w.w.Write(p) + w.Unlock() + return } // OpenTwo opens two files. The caller needs to check for a single error only. @@ -39,19 +40,9 @@ func OpenTwo(f0, f1 string) (g0, g1 *os.File, err error) { return g0, g1, nil } -// TabsToMapFile turns two columns from a tabular file into a map. -func TabsToMapFile(filename, sep string, kCol, vCol int) (map[string]string, error) { - f, err := os.Open(filename) - if err != nil { - return nil, err - } - defer f.Close() - return TabsToMap(f, sep, kCol, vCol) -} - -// TabsToMap read from a reader and turns values from kCol, vCol columns -// (1-indexed) into a map. -func TabsToMap(r io.Reader, sep string, kCol, vCol int) (map[string]string, error) { +// 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) @@ -68,12 +59,13 @@ func TabsToMap(r io.Reader, sep string, kCol, vCol int) (map[string]string, erro return nil, err } fields = strings.Split(line, sep) - if len(fields) > kCol-1 && len(fields) > vCol-1 { - k = strings.TrimSpace(fields[kCol-1]) - v = strings.TrimSpace(fields[vCol-1]) + 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), kCol, vCol) + return nil, fmt.Errorf("invalid line: %v (%v fields, %v, %v)", + line, len(fields), keyC, valueC) } } return m, nil |