-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiftext.go
88 lines (80 loc) · 1.47 KB
/
miftext.go
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
// ported from http://www.johnloomis.org/digitallab/rs232/miftext.html
package main
import (
"bufio"
"flag"
"fmt"
"os"
)
var (
status = 0
)
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
dump(bufio.NewReader(os.Stdin))
} else {
for _, name := range flag.Args() {
f, err := os.Open(name)
if ek(err) {
continue
}
dump(bufio.NewReader(f))
f.Close()
}
}
os.Exit(status)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: miftext [options] [file ...]")
flag.PrintDefaults()
os.Exit(2)
}
func ek(err error) bool {
if err != nil {
fmt.Fprintln(os.Stderr, "miftext:", err)
status = 1
return true
}
return false
}
func dump(r *bufio.Reader) {
const depth = 256
const width = 8
w := os.Stdout
fmt.Fprintf(w, "DEPTH = %d\n", depth)
fmt.Fprintf(w, "WIDTH = %d\n", width)
fmt.Fprintf(w, "ADDRESS_RADIX = HEX;\n")
fmt.Fprintf(w, "DATA_RADIX = HEX;\n")
fmt.Fprintf(w, "CONTENTS\n\nBEGIN\n")
fmt.Fprintf(w, "[0..%x] : 0;\n", depth-1)
addr := uint64(0)
off := uint64(0)
for {
b, err := r.ReadByte()
if err != nil {
break
}
if b == '\n' {
for off < 16 {
fmt.Fprintf(w, "%04x : %02x;\n", addr, ' ')
addr++
off++
}
off = 0
} else {
c := b
if c < 32 {
c = 32
}
fmt.Fprintf(w, "%04x : %02x; %% %c %%\n", addr, b, c)
addr++
off++
}
if addr >= depth {
break
}
}
fmt.Fprintf(w, "END;\n")
}