2012-04-30 22:18:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f *SingleField) Define(c *Context) {
|
|
|
|
c.Putln("%s %s", f.SrcName(), f.Type.SrcName())
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadSimpleSingleField(c *Context, name string, typ Type) {
|
|
|
|
switch t := typ.(type) {
|
|
|
|
case *Resource:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("%s = %s(xgb.Get32(buf[b:]))", name, t.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
case *TypeDef:
|
|
|
|
switch t.Size().Eval() {
|
|
|
|
case 1:
|
|
|
|
c.Putln("%s = %s(buf[b])", name, t.SrcName())
|
|
|
|
case 2:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("%s = %s(xgb.Get16(buf[b:]))", name, t.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
case 4:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("%s = %s(xgb.Get32(buf[b:]))", name, t.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
case 8:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("%s = %s(xgb.Get64(buf[b:]))", name, t.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
case *Base:
|
2012-05-01 07:08:03 +02:00
|
|
|
// If this is a bool, stop short and do something special.
|
|
|
|
if t.SrcName() == "bool" {
|
|
|
|
c.Putln("if buf[b] == 1 {")
|
|
|
|
c.Putln("%s = true", name)
|
|
|
|
c.Putln("} else {")
|
|
|
|
c.Putln("%s = false", name)
|
|
|
|
c.Putln("}")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2012-04-30 22:18:17 +02:00
|
|
|
var val string
|
|
|
|
switch t.Size().Eval() {
|
|
|
|
case 1:
|
|
|
|
val = fmt.Sprintf("buf[b]")
|
|
|
|
case 2:
|
2012-05-10 23:01:42 +02:00
|
|
|
val = fmt.Sprintf("xgb.Get16(buf[b:])")
|
2012-04-30 22:18:17 +02:00
|
|
|
case 4:
|
2012-05-10 23:01:42 +02:00
|
|
|
val = fmt.Sprintf("xgb.Get32(buf[b:])")
|
2012-04-30 22:18:17 +02:00
|
|
|
case 8:
|
2012-05-10 23:01:42 +02:00
|
|
|
val = fmt.Sprintf("xgb.Get64(buf[b:])")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to convert base types if they aren't uintXX or byte
|
|
|
|
ty := t.SrcName()
|
|
|
|
if ty != "byte" && ty != "uint16" && ty != "uint32" && ty != "uint64" {
|
|
|
|
val = fmt.Sprintf("%s(%s)", ty, val)
|
|
|
|
}
|
|
|
|
c.Putln("%s = %s", name, val)
|
|
|
|
default:
|
2012-05-01 07:08:03 +02:00
|
|
|
log.Panicf("Cannot read field '%s' as a simple field with %T type.",
|
2012-04-30 22:18:17 +02:00
|
|
|
name, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Putln("b += %s", typ.Size())
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (f *SingleField) 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
|
|
|
ReadSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
case *TypeDef:
|
2012-05-03 07:00:01 +02:00
|
|
|
ReadSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
case *Base:
|
2012-05-03 07:00:01 +02:00
|
|
|
ReadSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
case *Struct:
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("%s%s = %s{}", prefix, f.SrcName(), t.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("b += %sRead(buf[b:], &%s%s)", t.SrcName(), prefix, f.SrcName())
|
2012-05-01 07:08:03 +02:00
|
|
|
case *Union:
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("%s%s = %s{}", prefix, f.SrcName(), t.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("b += %sRead(buf[b:], &%s%s)", t.SrcName(), prefix, f.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
default:
|
2012-05-01 07:08:03 +02:00
|
|
|
log.Panicf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteSimpleSingleField(c *Context, name string, typ Type) {
|
|
|
|
switch t := typ.(type) {
|
|
|
|
case *Resource:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put32(buf[b:], uint32(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
case *TypeDef:
|
|
|
|
switch t.Size().Eval() {
|
|
|
|
case 1:
|
|
|
|
c.Putln("buf[b] = byte(%s)", name)
|
|
|
|
case 2:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put16(buf[b:], uint16(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
case 4:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put32(buf[b:], uint32(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
case 8:
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put64(buf[b:], uint64(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
case *Base:
|
2012-05-01 07:08:03 +02:00
|
|
|
// If this is a bool, stop short and do something special.
|
|
|
|
if t.SrcName() == "bool" {
|
|
|
|
c.Putln("if %s {", name)
|
|
|
|
c.Putln("buf[b] = 1")
|
|
|
|
c.Putln("} else {")
|
|
|
|
c.Putln("buf[b] = 0")
|
|
|
|
c.Putln("}")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2012-04-30 22:18:17 +02:00
|
|
|
switch t.Size().Eval() {
|
|
|
|
case 1:
|
|
|
|
if t.SrcName() != "byte" {
|
|
|
|
c.Putln("buf[b] = byte(%s)", name)
|
|
|
|
} else {
|
|
|
|
c.Putln("buf[b] = %s", name)
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if t.SrcName() != "uint16" {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put16(buf[b:], uint16(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
} else {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put16(buf[b:], %s)", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
if t.SrcName() != "uint32" {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put32(buf[b:], uint32(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
} else {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put32(buf[b:], %s)", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
case 8:
|
|
|
|
if t.SrcName() != "uint64" {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put64(buf[b:], uint64(%s))", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
} else {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.Put64(buf[b:], %s)", name)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Fatalf("Cannot read field '%s' as a simple field with %T type.",
|
|
|
|
name, typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Putln("b += %s", typ.Size())
|
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
func (f *SingleField) 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
|
|
|
WriteSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
case *TypeDef:
|
2012-05-03 07:00:01 +02:00
|
|
|
WriteSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
|
2012-04-30 22:18:17 +02:00
|
|
|
case *Base:
|
2012-05-03 07:00:01 +02:00
|
|
|
WriteSimpleSingleField(c, fmt.Sprintf("%s%s", prefix, f.SrcName()), t)
|
2012-05-01 07:08:03 +02:00
|
|
|
case *Union:
|
|
|
|
c.Putln("{")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("unionBytes := %s%s.Bytes()", prefix, f.SrcName())
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("copy(buf[b:], unionBytes)")
|
2013-12-28 15:33:09 +01:00
|
|
|
c.Putln("b += len(unionBytes)")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("}")
|
2012-04-30 22:18:17 +02:00
|
|
|
case *Struct:
|
|
|
|
c.Putln("{")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("structBytes := %s%s.Bytes()", prefix, f.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("copy(buf[b:], structBytes)")
|
2013-12-28 15:33:09 +01:00
|
|
|
c.Putln("b += len(structBytes)")
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
default:
|
|
|
|
log.Fatalf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
|
|
|
|
}
|
|
|
|
}
|