2012-04-30 22:18:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2012-05-03 07:00:01 +02:00
|
|
|
"strings"
|
2012-04-30 22:18:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// List fields
|
|
|
|
func (f *ListField) Define(c *Context) {
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("%s %s // size: %s",
|
|
|
|
f.SrcName(), f.SrcType(), f.Size())
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (f *ListField) Read(c *Context, prefix string) {
|
2012-04-30 22:18:17 +02:00
|
|
|
switch t := f.Type.(type) {
|
|
|
|
case *Resource:
|
2012-05-03 07:00:01 +02:00
|
|
|
length := f.LengthExpr.Reduce(prefix)
|
2012-05-10 18:47:19 +02:00
|
|
|
c.Putln("%s%s = make([]%s, %s)",
|
|
|
|
prefix, f.SrcName(), t.SrcName(), length)
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("for i := 0; i < int(%s); i++ {", length)
|
|
|
|
ReadSimpleSingleField(c, fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("b = pad(b)")
|
|
|
|
case *Base:
|
2012-05-03 07:00:01 +02:00
|
|
|
length := f.LengthExpr.Reduce(prefix)
|
|
|
|
if strings.ToLower(t.XmlName()) == "char" {
|
|
|
|
c.Putln("{")
|
|
|
|
c.Putln("byteString := make([]%s, %s)", t.SrcName(), length)
|
|
|
|
c.Putln("copy(byteString[:%s], buf[b:])", length)
|
|
|
|
c.Putln("%s%s = string(byteString)", prefix, f.SrcName())
|
|
|
|
c.Putln("b += pad(int(%s))", length)
|
|
|
|
c.Putln("}")
|
|
|
|
} else if t.SrcName() == "byte" {
|
|
|
|
c.Putln("%s%s = make([]%s, %s)",
|
|
|
|
prefix, f.SrcName(), t.SrcName(), length)
|
|
|
|
c.Putln("copy(%s%s[:%s], buf[b:])", prefix, f.SrcName(), length)
|
|
|
|
c.Putln("b += pad(int(%s))", length)
|
2012-04-30 22:18:17 +02:00
|
|
|
} else {
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("%s%s = make([]%s, %s)",
|
|
|
|
prefix, f.SrcName(), t.SrcName(), length)
|
|
|
|
c.Putln("for i := 0; i < int(%s); i++ {", length)
|
|
|
|
ReadSimpleSingleField(c,
|
|
|
|
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("b = pad(b)")
|
|
|
|
}
|
2012-05-03 07:00:01 +02:00
|
|
|
case *TypeDef:
|
|
|
|
length := f.LengthExpr.Reduce(prefix)
|
|
|
|
c.Putln("%s%s = make([]%s, %s)",
|
|
|
|
prefix, f.SrcName(), t.SrcName(), length)
|
|
|
|
c.Putln("for i := 0; i < int(%s); i++ {", length)
|
|
|
|
ReadSimpleSingleField(c, fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("b = pad(b)")
|
2012-05-01 07:08:03 +02:00
|
|
|
case *Union:
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("%s%s = make([]%s, %s)",
|
|
|
|
prefix, f.SrcName(), t.SrcName(), f.LengthExpr.Reduce(prefix))
|
|
|
|
c.Putln("b += Read%sList(buf[b:], %s%s)",
|
|
|
|
t.SrcName(), prefix, f.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
case *Struct:
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("%s%s = make([]%s, %s)",
|
|
|
|
prefix, f.SrcName(), t.SrcName(), f.LengthExpr.Reduce(prefix))
|
|
|
|
c.Putln("b += Read%sList(buf[b:], %s%s)",
|
|
|
|
t.SrcName(), prefix, f.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
default:
|
2012-05-02 07:46:30 +02:00
|
|
|
log.Panicf("Cannot read list field '%s' with %T type.",
|
2012-04-30 22:18:17 +02:00
|
|
|
f.XmlName(), f.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (f *ListField) Write(c *Context, prefix string) {
|
2012-04-30 22:18:17 +02:00
|
|
|
switch t := f.Type.(type) {
|
|
|
|
case *Resource:
|
2012-05-03 07:00:01 +02:00
|
|
|
length := f.Length().Reduce(prefix)
|
|
|
|
c.Putln("for i := 0; i < int(%s); i++ {", length)
|
|
|
|
WriteSimpleSingleField(c,
|
|
|
|
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("b = pad(b)")
|
|
|
|
case *Base:
|
2012-05-03 07:00:01 +02:00
|
|
|
length := f.Length().Reduce(prefix)
|
2012-04-30 22:18:17 +02:00
|
|
|
if t.SrcName() == "byte" {
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("copy(buf[b:], %s%s[:%s])", prefix, f.SrcName(), length)
|
|
|
|
c.Putln("b += pad(int(%s))", length)
|
2012-04-30 22:18:17 +02:00
|
|
|
} else {
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("for i := 0; i < int(%s); i++ {", length)
|
|
|
|
WriteSimpleSingleField(c,
|
|
|
|
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("b = pad(b)")
|
|
|
|
}
|
2012-05-03 07:00:01 +02:00
|
|
|
case *TypeDef:
|
|
|
|
length := f.Length().Reduce(prefix)
|
|
|
|
c.Putln("for i := 0; i < int(%s); i++ {", length)
|
|
|
|
WriteSimpleSingleField(c,
|
|
|
|
fmt.Sprintf("%s%s[i]", prefix, f.SrcName()), t)
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("b = pad(b)")
|
2012-05-01 07:08:03 +02:00
|
|
|
case *Union:
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("b += %sListBytes(buf[b:], %s%s)",
|
|
|
|
t.SrcName(), prefix, f.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
case *Struct:
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("b += %sListBytes(buf[b:], %s%s)",
|
|
|
|
t.SrcName(), prefix, f.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
default:
|
2012-05-03 07:00:01 +02:00
|
|
|
log.Panicf("Cannot write list field '%s' with %T type.",
|
2012-04-30 22:18:17 +02:00
|
|
|
f.XmlName(), f.Type)
|
|
|
|
}
|
|
|
|
}
|