~rbn/fit

ref: be89c47f35a510556bf10a989ba06db073c166f8 fit/cmd/fit/input.go -rw-r--r-- 659 bytes View raw
be89c47fRuben Schuller rename item field Path to Data and change type to []byte 1 year, 5 months ago
                                                                                
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
package main

import (
	"bufio"
	"fmt"
	"io"
	"strconv"
	"strings"

	"sr.ht/~rbn/fit"
)

func parseDu(r io.Reader) ([]fit.Item, error) {
	items := []fit.Item{}

	s := bufio.NewScanner(r)
	for s.Scan() {
		fs := strings.SplitN(s.Text(), "\t", 2)
		if len(fs) != 2 {
			return nil, fmt.Errorf("invalid line in input: %v", s.Text())
		}

		size, err := strconv.ParseInt(fs[0], 10, 64)
		if err != nil {
			return nil, fmt.Errorf("invalid size in input: %v", s.Text())
		}

		items = append(items, fit.Item{Size: size, Data: []byte(fs[1])})
	}

	if err := s.Err(); err != nil {
		return nil, fmt.Errorf("reading standard input: %v", err)
	}

	return items, nil
}