This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
73 lines (59 loc) · 1.82 KB
/
writer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import "os"
import "io"
import "fmt"
import "strings"
import "github.com/codecat/go-libs/log"
func addSlashes(str string) string {
ret := str
ret = strings.Replace(ret, "\\", "\\\\", -1)
ret = strings.Replace(ret, "\"", "\\\"", -1)
return ret
}
func writeField(f io.Writer, class ClassInfo, field FieldInfo) {
fmt.Fprintf(f, " { ")
fmt.Fprintf(f, "ValueType::%s", translateTokenType(field.Type))
fmt.Fprintf(f, ", \"%s\"", field.Name)
if field.DefaultValue != "" {
fmt.Fprintf(f, ", \"%s\"", addSlashes(field.DefaultValue))
} else {
fmt.Fprintf(f, ", nullptr")
}
fmt.Fprintf(f, ", offsetof(%s, %s)", class.Name, field.Name)
fmt.Fprintf(f, " },\n")
}
func writeClass(f io.Writer, class ClassInfo) {
fmt.Fprintf(f, " class Tables_%s {\n", class.Name)
fmt.Fprintf(f, " public:\n")
fmt.Fprintf(f, " Tables_%s() {\n", class.Name)
fmt.Fprintf(f, " ClassInfo &ci = ncg::Classes.add();\n")
fmt.Fprintf(f, " ci.Name = \"%s\";\n", class.Name)
fmt.Fprintf(f, " ci.Fields = {\n")
for _, field := range class.Fields {
writeField(f, class, field)
}
fmt.Fprintf(f, " };\n")
fmt.Fprintf(f, " ci.Methods = {\n")
fmt.Fprintf(f, " //TODO\n")
fmt.Fprintf(f, " };\n")
fmt.Fprintf(f, " }\n")
fmt.Fprintf(f, " };\n")
fmt.Fprintf(f, " static Tables_%s _Tables_%s;\n", class.Name, class.Name)
}
func writeResults(fnmSource, fnm string) bool {
log.Info("Writing results to %s", fnm)
f, err := os.Create(fnm)
if err != nil {
log.Error("Failed to create output file %s: %s", fnm, err.Error())
return false
}
defer f.Close()
fmt.Fprintf(f, "#include <ncg.h>\n")
fmt.Fprintf(f, "#include \"%s\"\n", fnmSource)
fmt.Fprintf(f, "namespace ncg { namespace generated {\n")
for _, class := range results {
writeClass(f, class)
}
fmt.Fprintln(f, "} }")
return true
}