Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b4d735d
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
include $(GOROOT)/src/Make.inc | |||
|
|||
TARG=bin2go | |||
GOFILES=bin2go.go | |||
|
|||
include $(GOROOT)/src/Make.cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,131 @@ | |||
package main | |||
|
|||
import ( | |||
"os" | |||
"io" | |||
"fmt" | |||
"flag" | |||
"bufio" | |||
"io/ioutil" | |||
) | |||
|
|||
var ( | |||
in = flag.String("in", "", "use this file instead of the stdin for input") | |||
out = flag.String("out", "", "use this file instead of the stdout for output") | |||
pkg = flag.String("pkg", "", "prepend package clause specifing this package") | |||
) | |||
|
|||
// Exit error codes | |||
const ( | |||
NO_ERROR = iota | |||
WRONG_ARGS | |||
INPUT_FAIL | |||
OUTPUT_FAIL | |||
) | |||
|
|||
func printUsage() { | |||
fmt.Printf("usage: %s [-in=<path>] [-out=<path>] <varname>\n", os.Args[0]) | |||
flag.PrintDefaults() | |||
} | |||
|
|||
func printUsageAndExit() { | |||
flag.Usage() | |||
os.Exit(WRONG_ARGS) | |||
} | |||
|
|||
func readInput() []byte { | |||
var data []byte | |||
var err os.Error | |||
|
|||
if *in != "" { | |||
data, err = ioutil.ReadFile(*in) | |||
} else { | |||
data, err = ioutil.ReadAll(os.Stdin) | |||
} | |||
|
|||
if err != nil { | |||
fmt.Fprintf(os.Stderr, "Failed to read input:\n%s\n", err.String()) | |||
os.Exit(INPUT_FAIL) | |||
} | |||
return data | |||
} | |||
|
|||
func checkOutputFailure(err os.Error) { | |||
if err != nil { | |||
fmt.Fprintf(os.Stderr, "Failed to write output:\n%s\n", err.String()) | |||
os.Exit(OUTPUT_FAIL) | |||
} | |||
} | |||
|
|||
func writeData(data []byte, out io.Writer) { | |||
varname := flag.Arg(0) | |||
|
|||
// write header | |||
_, err := fmt.Fprintf(out, "var %s = []byte{\n\t", varname) | |||
checkOutputFailure(err) | |||
|
|||
lastbytei := len(data) - 1 | |||
n := 8 | |||
for i, b := range data { | |||
// write single byte | |||
_, err = fmt.Fprintf(out, "0x%.2x,", b) | |||
checkOutputFailure(err) | |||
|
|||
n += 6 | |||
|
|||
// be readable, break line after 78 characters | |||
if n >= 78 { | |||
_, err = fmt.Fprint(out, "\n\t") | |||
checkOutputFailure(err) | |||
|
|||
n = 8 | |||
} else if i != lastbytei { | |||
// if this is not the last byte and we're not | |||
// breaking the line, insert space after ',' | |||
_, err = fmt.Fprint(out, " ") | |||
checkOutputFailure(err) | |||
} | |||
} | |||
_, err = fmt.Fprint(out, "\n}\n") | |||
checkOutputFailure(err) | |||
} | |||
|
|||
func writeOutput(data []byte) { | |||
var output *bufio.Writer | |||
|
|||
// prepare "output" | |||
if *out != "" { | |||
file, err := os.Open(*out, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0644) | |||
checkOutputFailure(err) | |||
defer file.Close() | |||
|
|||
output = bufio.NewWriter(file) | |||
} else { | |||
output = bufio.NewWriter(os.Stdout) | |||
} | |||
|
|||
// write package clause if any | |||
if *pkg != "" { | |||
_, err := fmt.Fprintf(output, "package %s\n\n", *pkg) | |||
checkOutputFailure(err) | |||
} | |||
|
|||
// write data | |||
writeData(data, output) | |||
|
|||
// flush | |||
err := output.Flush() | |||
checkOutputFailure(err) | |||
} | |||
|
|||
func main() { | |||
flag.Usage = printUsage | |||
flag.Parse() | |||
|
|||
if (flag.NArg() < 1) { | |||
printUsageAndExit() | |||
} | |||
|
|||
data := readInput() | |||
writeOutput(data) | |||
} |