Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit b4d735d0bf7e15d74f00bce527a4add710b59f56 @nsf committed Nov 5, 2010
Showing with 137 additions and 0 deletions.
  1. +6 −0 Makefile
  2. +131 −0 bin2go.go
6 Makefile
@@ -0,0 +1,6 @@
+include $(GOROOT)/src/Make.inc
+
+TARG=bin2go
+GOFILES=bin2go.go
+
+include $(GOROOT)/src/Make.cmd
131 bin2go.go
@@ -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)
+}

0 comments on commit b4d735d

Please sign in to comment.