summaryrefslogtreecommitdiffhomepage
path: root/cmd/mfmt/main.go
blob: 263b258098163e24172fa306a9dd0380f9e75d7d (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"unsafe"

	"git.thomasvoss.com/euro-cash.eu/lib/mintage"
)

const cols = unsafe.Sizeof(mintage.Row{}.Cols) /
	unsafe.Sizeof(mintage.Row{}.Cols[0])

var rv int

func main() {
	if len(os.Args) == 1 {
		if err := mfmt("-", os.Stdin, os.Stdout); err != nil {
			warn(err)
		}
	}
	for _, arg := range os.Args[1:] {
		f, err := os.OpenFile(arg, os.O_RDWR, 0)
		if err != nil {
			warn(err)
			continue
		}
		defer f.Close()
		if err := mfmt(arg, f, f); err != nil {
			warn(err)
		}
	}
	os.Exit(rv)
}

func mfmt(path string, in io.Reader, out io.Writer) error {
	data, err := mintage.Parse(in, path)
	if err != nil {
		return err
	}

	f, outfile := out.(*os.File)
	if outfile {
		_, err := f.Seek(0, io.SeekStart)
		if err != nil {
			return err
		}
	}

	fmt.Fprintf(out, `BEGIN %d`, data.StartYear)
	for i, tbl := range data.Tables {
		if len(tbl) != 0 {
			var section string
			switch mintage.CoinType(i) {
			case mintage.TypeCirculated:
				section = "CIRC"
			case mintage.TypeNIFC:
				section = "BU"
			case mintage.TypeProof:
				section = "PROOF"
			}
			fmt.Fprintf(out, "\n\nBEGIN %s\n", section)
			formatSection(out, tbl)
		}
	}
	fmt.Fprintln(out, "")

	if outfile {
		if off, err := f.Seek(0, io.SeekCurrent); err != nil {
			return err
		} else if err = f.Truncate(off); err != nil {
			return err
		}
	}
	return nil
}

func formatSection(out io.Writer, rows []mintage.Row) {
	var (
		longestMM  int
		longestNum [cols]int
	)

	for i, row := range rows {
		n := len(row.Mintmark)
		if n != 0 && i != 0 && row.Year != rows[i-1].Year {
			n++
		}
		longestMM = max(longestMM, n)

		for j, col := range row.Cols {
			n := intlen(col)
			longestNum[j] = max(longestNum[j], n+(n-1)/3)
		}
	}

	extraSpace := 0
	if longestMM != 0 {
		extraSpace = 2
	}

	for i, row := range rows {
		var label string
		if row.Mintmark != "" {
			if i != 0 && row.Year != rows[i-1].Year {
				label = row.Mintmark + "*: "
			} else {
				label = row.Mintmark + ": "
			}
		}
		fmt.Fprintf(out, "%-*s", longestMM+extraSpace, label)
		for j, n := range row.Cols {
			if j != 0 {
				fmt.Fprintf(out, " ")
			}
			fmt.Fprintf(out, "%*s", longestNum[j], formatInt(n))
		}

		if i != len(rows)-1 {
			fmt.Fprintln(out, "")
		}
	}
}

func formatInt(n int) string {
	if n <= mintage.Invalid {
		panic(fmt.Sprintf("invalid input %d", n))
	} else if n == mintage.Unknown {
		return "?"
	}

	digits := intlen(n)
	dots := (digits - 1) / 3
	out := make([]byte, digits+dots)

	for i, j := len(out)-1, 0; ; i-- {
		out[i] = byte(n%10) + 48
		n /= 10
		if n == 0 {
			return string(out)
		}
		if j++; j == 3 {
			i, j = i-1, 0
			out[i] = '.'
		}
	}
}

func intlen(v int) int {
	switch {
	case v <= mintage.Invalid:
		panic("mintage count is negative and not -1")
	case v == 0, v == mintage.Unknown:
		return 1
	default:
		n := 0
		for x := v; x != 0; x /= 10 {
			n++
		}
		return n
	}
}

func warn(e error) {
	fmt.Fprintf(os.Stderr, "%s: %s\n", filepath.Base(os.Args[0]), e)
	rv = 1
}