Converted zwinconstgen.go to use a template to generate its output.
This commit is contained in:
parent
1f883c5b40
commit
3596ae7b96
|
@ -13,6 +13,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"text/template"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -85,7 +86,13 @@ func writeLine(f *os.File, line string) {
|
||||||
fmt.Fprintf(f, "%s\n", line)
|
fmt.Fprintf(f, "%s\n", line)
|
||||||
}
|
}
|
||||||
|
|
||||||
const cgopreamble = `
|
const outTemplate = `package main
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"bytes"
|
||||||
|
"reflect"
|
||||||
|
"go/format"
|
||||||
|
)
|
||||||
// #define UNICODE
|
// #define UNICODE
|
||||||
// #define _UNICODE
|
// #define _UNICODE
|
||||||
// #define STRICT
|
// #define STRICT
|
||||||
|
@ -98,25 +105,37 @@ const cgopreamble = `
|
||||||
// #define NTDDI_VERSION 0x05010000 /* according to Microsoft's sdkddkver.h */
|
// #define NTDDI_VERSION 0x05010000 /* according to Microsoft's sdkddkver.h */
|
||||||
// #include <windows.h>
|
// #include <windows.h>
|
||||||
// #include <commctrl.h>
|
// #include <commctrl.h>
|
||||||
// #include <stdint.h>` // no closing newline; added by writeLine()
|
// #include <stdint.h>
|
||||||
|
{{range .Consts}}// uintptr_t {{.}} = (uintptr_t) ({{noprefix .}});
|
||||||
|
{{end}}import "C"
|
||||||
|
func main() {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
fmt.Fprintln(buf, "package ui")
|
||||||
|
{{range .Consts}} fmt.Fprintf(buf, "const %s = %d\n", {{printf "%q" .}}, C.{{.}})
|
||||||
|
{{end}}
|
||||||
|
var t reflect.Type
|
||||||
|
{{range .Structs}} t = reflect.TypeOf(C.{{noprefix .}}{})
|
||||||
|
fmt.Fprintf(buf, "type %s struct {\n", {{printf "%q" .}})
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
fmt.Fprintf(buf, "\t%s %s\n", t.Field(i).Name, t.Field(i).Type.Kind())
|
||||||
|
}
|
||||||
|
fmt.Fprintf(buf, "}")
|
||||||
|
{{end}}
|
||||||
|
res, err := format.Source(buf.Bytes())
|
||||||
|
if err != nil { panic(err) }
|
||||||
|
fmt.Printf("%s", res)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
func writeConstCast(f *os.File, c string) {
|
type templateArgs struct {
|
||||||
cc := c[2:] // strip leading c_
|
Consts []string
|
||||||
fmt.Fprintf(f, "// uintptr_t %s = (uintptr_t) (%s);\n", c, cc)
|
Structs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeConstPrint(f *os.File, c string) {
|
var templateFuncs = template.FuncMap{
|
||||||
fmt.Fprintf(f, "\tfmt.Fprintf(buf, \"const %%s = %%d\\n\", %q, C.%s)\n", c, c)
|
"noprefix": func(s string) string {
|
||||||
}
|
return s[2:]
|
||||||
|
},
|
||||||
func writeStructPrint(f *os.File, s string) {
|
|
||||||
cs := s[2:] // strip leading s_
|
|
||||||
fmt.Fprintf(f, "\tt = reflect.TypeOf(C.%s{})\n", cs)
|
|
||||||
fmt.Fprintf(f, "\tfmt.Fprintf(buf, \"type %%s struct {\\n\", %q)\n", s)
|
|
||||||
fmt.Fprintf(f, "\tfor i := 0; i < t.NumField(); i++ {\n")
|
|
||||||
fmt.Fprintf(f, "\t\tfmt.Fprintf(buf, \"\\t%%s %%s\\n\", t.Field(i).Name, t.Field(i).Type.Kind())\n")
|
|
||||||
fmt.Fprintf(f, "\t}\n")
|
|
||||||
fmt.Fprintf(f, "\tfmt.Fprintf(buf, \"}\\n\")\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -169,30 +188,14 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
writeLine(f, "package main")
|
t := template.Must(template.New("winconstgenout").Funcs(templateFuncs).Parse(outTemplate))
|
||||||
writeLine(f, "import \"fmt\"")
|
err = t.Execute(f, &templateArgs{
|
||||||
writeLine(f, "import \"bytes\"")
|
Consts: consts,
|
||||||
writeLine(f, "import \"reflect\"")
|
Structs: structs,
|
||||||
writeLine(f, "import \"go/format\"")
|
})
|
||||||
writeLine(f, cgopreamble)
|
if err != nil {
|
||||||
for _, c := range consts {
|
panic(err)
|
||||||
writeConstCast(f, c)
|
|
||||||
}
|
}
|
||||||
writeLine(f, "import \"C\"")
|
|
||||||
writeLine(f, "func main() {")
|
|
||||||
writeLine(f, "\tbuf := new(bytes.Buffer)")
|
|
||||||
writeLine(f, "\tfmt.Fprintln(buf, \"package ui\")")
|
|
||||||
for _, c := range consts {
|
|
||||||
writeConstPrint(f, c)
|
|
||||||
}
|
|
||||||
writeLine(f, "\tvar t reflect.Type")
|
|
||||||
for _, s := range structs {
|
|
||||||
writeStructPrint(f, s)
|
|
||||||
}
|
|
||||||
writeLine(f, "\tres, err := format.Source(buf.Bytes())")
|
|
||||||
writeLine(f, "\tif err != nil { panic(err) }")
|
|
||||||
writeLine(f, "\tfmt.Printf(\"%s\", res)")
|
|
||||||
writeLine(f, "}")
|
|
||||||
|
|
||||||
cmd := exec.Command("go", "run")
|
cmd := exec.Command("go", "run")
|
||||||
cmd.Args = append(cmd.Args, goopts...) // valid if len(goopts) == 0; in that case this will just be a no-op
|
cmd.Args = append(cmd.Args, goopts...) // valid if len(goopts) == 0; in that case this will just be a no-op
|
||||||
|
|
Loading…
Reference in New Issue