package xio import ( "bufio" "fmt" "io" "os" "strings" "sync" ) type Encoder interface { Encode(interface{}) error } type SafeEncoder struct { sync.Mutex enc Encoder } func NewSafeEncoder(enc Encoder) *SafeEncoder { return &SafeEncoder{enc: enc} } func (s *SafeEncoder) Encode(v interface{}) error { s.Lock() defer s.Unlock() return s.enc.Encode(v) } // SingleWriter makes any writer thread safe. type SingleWriter struct { sync.Mutex w io.Writer } // NewSingleWriter returns an io.Writer that can be safely accessed by multiple // goroutines. func NewSingleWriter(w io.Writer) *SingleWriter { return &SingleWriter{w: w} } // Write wraps the underlying writer and gives exclusive access. func (w *SingleWriter) Write(p []byte) (n int, err error) { w.Lock() defer w.Unlock() return w.w.Write(p) } // 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 } // 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) { 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) > kCol-1 && len(fields) > vCol-1 { k = strings.TrimSpace(fields[kCol-1]) v = strings.TrimSpace(fields[vCol-1]) m[k] = v } else { return nil, fmt.Errorf("invalid line: %v (%v fields, %v, %v)", line, len(fields), kCol, vCol) } } return m, nil }