493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
0
|
package main |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
1
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
2
|
import ( |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
3
|
"errors" |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
4
|
"flag" |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
5
|
"fmt" |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
6
|
"io" |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
7
|
"os" |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
8
|
"strings" |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
9
|
) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
10
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
11
|
var spaces, magiscule bool |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
12
|
var tabCount, tabWidth, width int |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
13
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
14
|
func main() { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
15
|
flag.BoolVar(&spaces, "spaces", false, "Use spaces instead of tabs for indentation") |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
16
|
flag.IntVar(&tabCount, "tabs", 1, "Number of tabs to indent per line") |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
17
|
flag.IntVar(&tabWidth, "tab-width", 4, "Number of spaces per tab") |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
18
|
flag.IntVar(&width, "width", 120, "Number of characters per line, including indentation") |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
19
|
flag.BoolVar(&magiscule, "magiscule", false, "Use uppercase hex digits") |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
20
|
flag.Parse() |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
21
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
22
|
if len(flag.Args()) < 1 { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
23
|
fmt.Println("Usage: binclude file...") |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
24
|
os.Exit(-1) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
25
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
26
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
27
|
for _, a := range flag.Args() { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
28
|
if err := binclude(a); err != nil { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
29
|
fmt.Println(err) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
30
|
os.Exit(-1) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
31
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
32
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
33
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
34
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
35
|
func binclude(name string) error { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
36
|
file, err := os.Open(name) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
37
|
if err != nil { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
38
|
return err |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
39
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
40
|
defer file.Close() |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
41
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
42
|
hexWidth := width - (tabCount * tabWidth) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
43
|
hexCount := ((hexWidth - 5) / 6) + 1 |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
44
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
45
|
buf := make([]byte, hexCount) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
46
|
for { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
47
|
n, err := file.Read(buf) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
48
|
if err != nil { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
49
|
if errors.Is(err, io.EOF) { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
50
|
break |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
51
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
52
|
return err |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
53
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
54
|
if n == 0 { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
55
|
break |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
56
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
57
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
58
|
if spaces { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
59
|
fmt.Print(strings.Repeat(" ", tabCount*tabWidth)) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
60
|
} else { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
61
|
fmt.Print(strings.Repeat("\t", tabCount)) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
62
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
63
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
64
|
for i := range n { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
65
|
if magiscule { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
66
|
fmt.Printf("0x%02X, ", buf[i]) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
67
|
} else { |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
68
|
fmt.Printf("0x%02x, ", buf[i]) |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
69
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
70
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
71
|
fmt.Println() |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
72
|
} |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
73
|
|
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
74
|
return nil |
493878e |
Jakob Wakeling |
2024-12-07 16:39:00 |
75
|
} |
|
|
|
76
|
|