2011-06-17 15:44:59 +00:00
|
|
|
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
|
|
// license. Its contents can be found at:
|
|
|
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2011-11-09 13:46:27 +00:00
|
|
|
"compress/gzip"
|
2011-06-17 15:44:59 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2011-11-09 13:46:27 +00:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2011-06-17 15:44:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// If gofmt exists on the system, run it over the target file to
|
|
|
|
// fix up the generated code. This is not necessary, just a convenience.
|
2011-11-02 15:40:26 +00:00
|
|
|
func gofmt(file string) (err error) {
|
2011-06-17 15:44:59 +00:00
|
|
|
var prog string
|
|
|
|
if prog = os.Getenv("GOBIN"); len(prog) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
prog = path.Join(prog, "gofmt")
|
|
|
|
cmd := exec.Command(prog, "-w", file)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate the input file.
|
|
|
|
// input -> gzip -> gowriter -> output.
|
2011-11-02 15:40:26 +00:00
|
|
|
func translate(input io.Reader, output io.Writer, pkgname, funcname string) (err error) {
|
2011-06-17 15:44:59 +00:00
|
|
|
var gz *gzip.Compressor
|
|
|
|
|
2011-06-17 16:52:40 +00:00
|
|
|
fmt.Fprintf(output, `package %s
|
2011-06-17 15:44:59 +00:00
|
|
|
import ( "io"; "os"; "bytes"; "compress/gzip" )
|
|
|
|
|
|
|
|
func %s() ([]byte, os.Error) {
|
|
|
|
var gz *gzip.Decompressor
|
|
|
|
var err os.Error
|
2011-06-17 16:52:40 +00:00
|
|
|
if gz, err = gzip.NewReader(bytes.NewBuffer([]byte{`, pkgname, funcname)
|
2011-06-17 15:44:59 +00:00
|
|
|
|
2011-06-17 16:52:40 +00:00
|
|
|
if gz, err = gzip.NewWriter(&GoWriter{Writer: output}); err != nil {
|
2011-06-17 15:44:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-17 16:52:40 +00:00
|
|
|
io.Copy(gz, input)
|
2011-06-17 15:44:59 +00:00
|
|
|
gz.Close()
|
|
|
|
|
2011-06-17 16:52:40 +00:00
|
|
|
fmt.Fprint(output, `
|
2011-06-17 15:44:59 +00:00
|
|
|
})); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
io.Copy(&b, gz)
|
|
|
|
gz.Close()
|
|
|
|
return b.Bytes(), nil}`)
|
|
|
|
return
|
|
|
|
}
|