2012-04-30 22:18:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
func (s *Struct) Define(c *Context) {
|
|
|
|
c.Putln("type %s struct {", s.SrcName())
|
|
|
|
for _, field := range s.Fields {
|
|
|
|
field.Define(c)
|
|
|
|
}
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
|
|
|
|
// Write function that reads bytes and produces this struct.
|
|
|
|
s.Read(c)
|
|
|
|
|
2012-05-01 07:08:03 +02:00
|
|
|
// Write function that reads bytes and produces a list of this struct.
|
2012-04-30 22:18:17 +02:00
|
|
|
s.ReadList(c)
|
|
|
|
|
|
|
|
// Write function that writes bytes given this struct.
|
|
|
|
s.Write(c)
|
|
|
|
|
|
|
|
// Write function that writes a list of this struct.
|
|
|
|
s.WriteList(c)
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
// Write function that computes the size of a list of these structs,
|
|
|
|
// IF there is a list field in this struct.
|
|
|
|
if s.HasList() {
|
|
|
|
s.WriteListSize(c)
|
|
|
|
}
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
2013-01-26 18:51:48 +01:00
|
|
|
// Read for a struct creates a function 'ReadStructName' that takes a source
|
2012-04-30 22:18:17 +02:00
|
|
|
// byte slice (i.e., the buffer) and a destination struct, and returns
|
|
|
|
// the number of bytes read off the buffer.
|
|
|
|
// 'ReadStructName' should only be used to read raw reply data from the wire.
|
|
|
|
func (s *Struct) Read(c *Context) {
|
2012-05-11 05:57:34 +02:00
|
|
|
c.Putln("// %sRead reads a byte slice into a %s value.",
|
|
|
|
s.SrcName(), s.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("func %sRead(buf []byte, v *%s) int {", s.SrcName(), s.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
|
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("")
|
|
|
|
for _, field := range s.Fields {
|
2012-05-03 07:00:01 +02:00
|
|
|
field.Read(c, "v.")
|
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
c.Putln("return b")
|
|
|
|
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadList for a struct creates a function 'ReadStructNameList' that takes
|
2013-01-26 18:51:48 +01:00
|
|
|
// a source (i.e., the buffer) byte slice, and a destination slice and returns
|
2012-04-30 22:18:17 +02:00
|
|
|
// the number of bytes read from the byte slice.
|
|
|
|
func (s *Struct) ReadList(c *Context) {
|
2012-05-11 05:57:34 +02:00
|
|
|
c.Putln("// %sReadList reads a byte slice into a list of %s values.",
|
|
|
|
s.SrcName(), s.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("func %sReadList(buf []byte, dest []%s) int {",
|
2012-04-30 22:18:17 +02:00
|
|
|
s.SrcName(), s.SrcName())
|
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("for i := 0; i < len(dest); i++ {")
|
|
|
|
c.Putln("dest[i] = %s{}", s.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("b += %sRead(buf[b:], &dest[i])", s.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("return xgb.Pad(b)")
|
2012-04-30 22:18:17 +02:00
|
|
|
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Struct) Write(c *Context) {
|
2012-05-11 05:57:34 +02:00
|
|
|
c.Putln("// Bytes writes a %s value to a byte slice.", s.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("func (v %s) Bytes() []byte {", s.SrcName())
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("buf := make([]byte, %s)", s.Size().Reduce("v."))
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("")
|
|
|
|
for _, field := range s.Fields {
|
2012-05-03 07:00:01 +02:00
|
|
|
field.Write(c, "v.")
|
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
c.Putln("return buf")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Struct) WriteList(c *Context) {
|
2012-05-11 05:57:34 +02:00
|
|
|
c.Putln("// %sListBytes writes a list of %s values to a byte slice.",
|
2012-05-27 00:24:52 +02:00
|
|
|
s.SrcName(), s.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("func %sListBytes(buf []byte, list []%s) int {",
|
|
|
|
s.SrcName(), s.SrcName())
|
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("var structBytes []byte")
|
|
|
|
c.Putln("for _, item := range list {")
|
|
|
|
c.Putln("structBytes = item.Bytes()")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("copy(buf[b:], structBytes)")
|
2013-08-12 02:54:15 +02:00
|
|
|
c.Putln("b += len(structBytes)")
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
2013-08-12 02:54:15 +02:00
|
|
|
c.Putln("return xgb.Pad(b)")
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Struct) WriteListSize(c *Context) {
|
2012-05-11 05:57:34 +02:00
|
|
|
c.Putln("// %sListSize computes the size (bytes) of a list of %s values.",
|
|
|
|
s.SrcName(), s.SrcName())
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("func %sListSize(list []%s) int {", s.SrcName(), s.SrcName())
|
|
|
|
c.Putln("size := 0")
|
2012-05-06 09:06:02 +02:00
|
|
|
if s.Size().Expression.Concrete() {
|
|
|
|
c.Putln("for _ = range list {")
|
|
|
|
} else {
|
|
|
|
c.Putln("for _, item := range list {")
|
|
|
|
}
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("size += %s", s.Size().Reduce("item."))
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("return size")
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|