File: reader.go

package info (click to toggle)
golang-golang-x-arch 0.0~git20201207.1e68675-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,724 kB
  • sloc: ansic: 1,832; makefile: 42
file content (69 lines) | stat: -rw-r--r-- 1,718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2017 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package x86csv

import (
	"encoding/csv"
	"io"
)

// A Reader reads entries from an "x86.csv" file.
type Reader struct {
	csv *csv.Reader
}

// NewReader returns a Reader reading from r, which should
// be of the content of the "x86.csv" (format version=0.2).
func NewReader(r io.Reader) *Reader {
	rcsv := csv.NewReader(r)
	rcsv.Comment = '#'
	return &Reader{csv: rcsv}
}

// ReadAll reads all remaining rows from r.
//
// If error is occured, still returns all rows
// that have been read during method execution.
//
// A successful call returns err == nil, not err == io.EOF.
// Because ReadAll is defined to read until EOF,
// it does not treat end of file as an error to be reported.
func (r *Reader) ReadAll() ([]*Inst, error) {
	var err error
	var insts []*Inst
	for inst, err := r.Read(); err == nil; inst, err = r.Read() {
		insts = append(insts, inst)
	}
	if err == io.EOF {
		return insts, nil
	}
	return insts, err
}

// Read reads and returns the next Row from the "x86.csv" file.
// If there is no data left to be read, Read returns {nil, io.EOF}.
func (r *Reader) Read() (*Inst, error) {
	cols, err := r.csv.Read()
	if err != nil {
		return nil, err
	}

	// This should be the only place where indexes
	// are used. Everything else should rely on Row records.
	inst := &Inst{
		Intel:     cols[0],
		Go:        cols[1],
		GNU:       cols[2],
		Encoding:  cols[3],
		Mode32:    cols[4],
		Mode64:    cols[5],
		CPUID:     cols[6],
		Tags:      cols[7],
		Action:    cols[8],
		Multisize: cols[9],
		DataSize:  cols[10],
	}
	return inst, nil
}