diff options
Diffstat (limited to 'skate/xio/util.go')
-rw-r--r-- | skate/xio/util.go | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/skate/xio/util.go b/skate/xio/util.go index 554317b..c0439e5 100644 --- a/skate/xio/util.go +++ b/skate/xio/util.go @@ -1,9 +1,14 @@ package xio -import "os" +import ( + "bufio" + "io" + "os" + "strings" +) -// OpenTwo opens two files, and the caller needs to check for a single error only. -func OpenTwo(f1, f2 string) (g1 *os.File, g2 *os.File, err error) { +// OpenTwo opens two files. The caller needs to check for a single error only. +func OpenTwo(f1, f2 string) (g1, g2 *os.File, err error) { if g1, err = os.Open(f1); err != nil { return nil, nil, err } @@ -12,3 +17,28 @@ func OpenTwo(f1, f2 string) (g1 *os.File, g2 *os.File, err error) { } return g1, g2, nil } + +// TabsToMap read from a reader and turns values from kCol, vCol columns into a +// mapping. +func TabsToMap(r io.Reader, sep string, kCol, vCol int) (map[string]string, error) { + var ( + br = bufio.NewReader(r) + m = make(map[string]string) + ) + 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) <= kCol && len(fields) <= vCol { + k := strings.TrimSpace(fields[kCol-1]) + v := strings.TrimSpace(fields[vCol-1]) + m[k] = v + } + } + return m, nil +} |