golang.org/x/arch@v0.17.0/x86/x86csv/reader.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package x86csv 6 7 import ( 8 "encoding/csv" 9 "io" 10 ) 11 12 // A Reader reads entries from an "x86.csv" file. 13 type Reader struct { 14 csv *csv.Reader 15 } 16 17 // NewReader returns a Reader reading from r, which should 18 // be of the content of the "x86.csv" (format version=0.2). 19 func NewReader(r io.Reader) *Reader { 20 rcsv := csv.NewReader(r) 21 rcsv.Comment = '#' 22 return &Reader{csv: rcsv} 23 } 24 25 // ReadAll reads all remaining rows from r. 26 // 27 // If error has occurred, still returns all rows 28 // that have been read during method execution. 29 // 30 // A successful call returns err == nil, not err == io.EOF. 31 // Because ReadAll is defined to read until EOF, 32 // it does not treat end of file as an error to be reported. 33 func (r *Reader) ReadAll() ([]*Inst, error) { 34 var err error 35 var insts []*Inst 36 for inst, err := r.Read(); err == nil; inst, err = r.Read() { 37 insts = append(insts, inst) 38 } 39 if err == io.EOF { 40 return insts, nil 41 } 42 return insts, err 43 } 44 45 // Read reads and returns the next Row from the "x86.csv" file. 46 // If there is no data left to be read, Read returns {nil, io.EOF}. 47 func (r *Reader) Read() (*Inst, error) { 48 cols, err := r.csv.Read() 49 if err != nil { 50 return nil, err 51 } 52 53 // This should be the only place where indexes 54 // are used. Everything else should rely on Row records. 55 inst := &Inst{ 56 Intel: cols[0], 57 Go: cols[1], 58 GNU: cols[2], 59 Encoding: cols[3], 60 Mode32: cols[4], 61 Mode64: cols[5], 62 CPUID: cols[6], 63 Tags: cols[7], 64 Action: cols[8], 65 Multisize: cols[9], 66 DataSize: cols[10], 67 } 68 return inst, nil 69 }