-
Notifications
You must be signed in to change notification settings - Fork 26
/
2goarray.go
52 lines (45 loc) · 1.08 KB
/
2goarray.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
// Simple utility to convert a file into a Go byte array
// Clint Caywood
// http://github.com/cratonica/2goarray
package main
import (
"fmt"
"io"
"os"
)
const (
NAME = "2goarray"
VERSION = "0.1.0"
URL = "http://github.com/cratonica/2goarray"
GENERATED_BY = "// File generated by " + NAME + " v" + VERSION + " (" + URL + ")"
)
func main() {
if len(os.Args) != 3 {
fmt.Print(NAME + " v" + VERSION + "\n\n")
fmt.Println("Usage: " + NAME + " array_name package_name")
return
}
if isTerminal() {
fmt.Println("\nPlease pipe the file you wish to encode into stdin\n")
return
}
fmt.Println(GENERATED_BY + "\n")
fmt.Printf("package %s\n\n", os.Args[2])
fmt.Printf("var %s []byte = []byte{", os.Args[1])
buf := make([]byte, 1)
var err error
var totalBytes uint64
var n int
for n, err = os.Stdin.Read(buf); n > 0 && err == nil; {
if totalBytes%12 == 0 {
fmt.Printf("\n\t")
}
fmt.Printf("0x%02x, ", buf[0])
totalBytes++
n, err = os.Stdin.Read(buf)
}
if err != nil && err != io.EOF {
fmt.Errorf("Error: %v", err)
}
fmt.Print("\n}\n\n")
}