list.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package brlyt
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "io"
  6. )
  7. func (r *Root) ParseChildren() ([]Children, error) {
  8. var children []Children
  9. isOver := false
  10. for {
  11. var sectionHeader SectionHeader
  12. err := binary.Read(r.reader, binary.BigEndian, &sectionHeader)
  13. if err != nil {
  14. return nil, err
  15. }
  16. // Subtract the header size
  17. sectionSize := int(sectionHeader.Size) - 8
  18. // We could end off at a pane end, meaning it would try to read EOF
  19. if sectionSize == 0 {
  20. r.count--
  21. break
  22. }
  23. temp := make([]byte, sectionSize)
  24. _, err = r.reader.Read(temp)
  25. if err != nil {
  26. return nil, err
  27. }
  28. switch sectionHeader.Type {
  29. case SectionTypeBND:
  30. bnd, err := r.ParseBND(temp)
  31. if err != nil {
  32. return nil, err
  33. }
  34. children = append(children, Children{BND: bnd})
  35. case SectionTypePIC:
  36. pic, err := r.ParsePIC(temp)
  37. if err != nil {
  38. return nil, err
  39. }
  40. children = append(children, Children{PIC: pic})
  41. case SectionTypeTXT:
  42. txt, err := r.ParseTXT(temp, sectionHeader.Size)
  43. if err != nil {
  44. return nil, err
  45. }
  46. children = append(children, Children{TXT: txt})
  47. case SectionTypeWND:
  48. wnd, err := r.ParseWND(temp)
  49. if err != nil {
  50. return nil, err
  51. }
  52. children = append(children, Children{WND: wnd})
  53. case SectionTypePAN:
  54. pan, err := r.ParsePAN(temp)
  55. if err != nil {
  56. return nil, err
  57. }
  58. children = append(children, Children{Pane: pan})
  59. case SectionTypeGRP:
  60. grp, err := r.ParseGRP(temp)
  61. if err != nil {
  62. return nil, err
  63. }
  64. children = append(children, Children{GRP: grp})
  65. case SectionTypePAE:
  66. isOver = true
  67. case SectionTypeGRE:
  68. isOver = true
  69. }
  70. // Deincrement the amount of sections left to read.
  71. r.count--
  72. if isOver {
  73. break
  74. }
  75. }
  76. return children, nil
  77. }
  78. func (r *Root) HasChildren() bool {
  79. var sectionHeader SectionHeader
  80. err := binary.Read(r.reader, binary.BigEndian, &sectionHeader)
  81. if err != nil {
  82. if errors.Is(err, io.EOF) {
  83. return false
  84. }
  85. panic(err)
  86. }
  87. if sectionHeader.Type == SectionTypePAS || sectionHeader.Type == SectionTypeGRS {
  88. // Read the pane start
  89. r.count--
  90. return true
  91. }
  92. _, err = r.reader.Seek(-8, io.SeekCurrent)
  93. if err != nil {
  94. panic(err)
  95. }
  96. return false
  97. }