diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-05-26 22:47:05 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-05-26 22:47:05 +0200 |
commit | fff30b71abf23222f1dbc7e45591cdd093bf85d1 (patch) | |
tree | a38e8856afc0e46fab0a3067ab3141969b13a516 /skate/xio/util.go | |
parent | 57826605209de687e0b6e6cb151021b7bcf034ca (diff) | |
download | refcat-fff30b71abf23222f1dbc7e45591cdd093bf85d1.tar.gz refcat-fff30b71abf23222f1dbc7e45591cdd093bf85d1.zip |
add author key mapping
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 +} |