summaryrefslogtreecommitdiff
path: root/xgbgen/go_list.go
diff options
context:
space:
mode:
authorAndrew Gallant (Ocelot) <[email protected]>2012-04-30 16:18:17 -0400
committerAndrew Gallant (Ocelot) <[email protected]>2012-04-30 16:18:17 -0400
commit5d79dc324de0a23fd0543951f0fd1f0352018654 (patch)
treef924ee2fba3def91bec831e4a6577204470d412d /xgbgen/go_list.go
parentf7be3db65e559e77355537a11a88c8657ec4c059 (diff)
splitting up go specific code. too much for one file IMO. more progress. almost done with structs.
Diffstat (limited to 'xgbgen/go_list.go')
-rw-r--r--xgbgen/go_list.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/xgbgen/go_list.go b/xgbgen/go_list.go
new file mode 100644
index 0000000..03da22e
--- /dev/null
+++ b/xgbgen/go_list.go
@@ -0,0 +1,75 @@
+package main
+
+import (
+ "fmt"
+ "log"
+)
+
+// List fields
+func (f *ListField) Define(c *Context) {
+ c.Putln("%s []%s // size: %s",
+ f.SrcName(), f.Type.SrcName(), f.Size())
+}
+
+func (f *ListField) Read(c *Context) {
+ c.Putln("")
+
+ switch t := f.Type.(type) {
+ case *Resource:
+ length := f.LengthExpr.Reduce("v.", "")
+ c.Putln("v.%s = make([]Id, %s)", f.SrcName(), length)
+ c.Putln("for i := 0; i < %s; i++ {", length)
+ ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t)
+ c.Putln("}")
+ c.Putln("b = pad(b)")
+ case *Base:
+ length := f.LengthExpr.Reduce("v.", "")
+ c.Putln("v.%s = make([]%s, %s)", f.SrcName(), t.SrcName(), length)
+ if t.SrcName() == "byte" {
+ c.Putln("copy(v.%s[:%s], buf[b:])", f.SrcName(), length)
+ c.Putln("b += pad(%s)", length)
+ } else {
+ c.Putln("for i := 0; i < %s; i++ {", length)
+ ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t)
+ c.Putln("}")
+ c.Putln("b = pad(b)")
+ }
+ case *Struct:
+ c.Putln("v.%s = make([]%s, %s)",
+ f.SrcName(), t.SrcName(), f.LengthExpr.Reduce("v.", ""))
+ c.Putln("b += Read%sList(buf[b:], v.%s)", t.SrcName(), f.SrcName())
+ default:
+ log.Fatalf("Cannot read list field '%s' with %T type.",
+ f.XmlName(), f.Type)
+ }
+}
+
+func (f *ListField) Write(c *Context) {
+ c.Putln("")
+
+ switch t := f.Type.(type) {
+ case *Resource:
+ length := f.LengthExpr.Reduce("v.", "")
+ c.Putln("for i := 0; i < %s; i++", length)
+ WriteSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t)
+ c.Putln("}")
+ c.Putln("b = pad(b)")
+ case *Base:
+ length := f.LengthExpr.Reduce("v.", "")
+ if t.SrcName() == "byte" {
+ c.Putln("copy(buf[b:], v.%s[:%s])", f.SrcName(), length)
+ c.Putln("b += pad(%s)", length)
+ } else {
+ c.Putln("for i := 0; i < %s; i++ {", length)
+ WriteSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t)
+ c.Putln("}")
+ c.Putln("b = pad(b)")
+ }
+ case *Struct:
+ c.Putln("b += %sListBytes(buf[b:], v.%s)", t.SrcName(), f.SrcName())
+ default:
+ log.Fatalf("Cannot read list field '%s' with %T type.",
+ f.XmlName(), f.Type)
+ }
+}
+