Finish the Set/ implementation

This commit is contained in:
2018-03-18 17:27:32 +00:00
parent 961a213752
commit 738abfc4a8
5 changed files with 78 additions and 55 deletions

View File

@@ -2,7 +2,6 @@ package sets
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strconv"
@@ -13,10 +12,17 @@ import (
type MapSet struct {
Description string
Defs [4]byte // TODO: work out what these are for
SurfaceCount int
LeftCount int
RightCount int
CenterCount int
// TODO: is there any more structure than this? Should I preserve empty lines?
Palette []string
SurfacePalette []string
LeftPalette []string
RightPalette []string
CenterPalette []string
}
func LoadSets(dir string) (map[string]*MapSet, error) {
@@ -63,57 +69,77 @@ func LoadSet(filename string) (*MapSet, error) {
return nil, err
}
out.Defs, err = consumeDefs(s)
if err != nil {
if err := consumeDefs(s, out); err != nil {
return nil, err
}
for {
str, err := s.ConsumeString()
if err != nil {
if err == io.EOF {
return out, nil
}
return nil, err
}
out.Palette = append(out.Palette, str)
if err := consumePalette(s, out.SurfaceCount, &out.SurfacePalette); err != nil {
return nil, fmt.Errorf("Failed to read surface palette: %v", err)
}
if err := consumePalette(s, out.LeftCount, &out.LeftPalette); err != nil {
return nil, fmt.Errorf("Failed to read left palette: %v", err)
}
if err := consumePalette(s, out.RightCount, &out.RightPalette); err != nil {
return nil, fmt.Errorf("Failed to read right palette: %v", err)
}
if err := consumePalette(s, out.CenterCount, &out.CenterPalette); err != nil {
return nil, fmt.Errorf("Failed to read center palette: %v", err)
}
return out, nil
}
func consumeDefs(scanner *asciiscan.Scanner) ([4]byte, error) {
var out [4]byte
func consumeDefs(scanner *asciiscan.Scanner, in *MapSet) error {
expectDefs, err := scanner.ConsumeString()
if err != nil {
return out, err
return err
}
if expectDefs != "Defs" {
return out, fmt.Errorf("Couldn't find Defs section")
return fmt.Errorf("Couldn't find Defs section")
}
defs, err := scanner.ConsumeString()
if err != nil {
return out, err
return err
}
parts := strings.SplitN(defs, " ", -1)
if len(parts) != 4 {
return out, fmt.Errorf("Defs section did not have 4 components")
return fmt.Errorf("Defs section did not have 4 components")
}
ints := make([]int, 4)
for i, part := range parts {
n, err := strconv.ParseInt(part, 10, 8)
if err != nil {
return out, err
return err
}
out[i] = byte(n) // safe as we specify 8 bits to ParseInt
ints[i] = int(n) // safe as we specify 8 bits to ParseInt
}
return out, nil
in.SurfaceCount = ints[0]
in.LeftCount = ints[1]
in.RightCount = ints[2]
in.CenterCount = ints[3]
return nil
}
func consumePalette(scanner *asciiscan.Scanner, n int, in *[]string) error {
out := make([]string, 0, n)
for i := 0; i < n; i++ {
str, err := scanner.ConsumeString()
if err != nil {
return err // EOF is bad: the number of entries is explicit
}
out = append(out, str)
}
*in = out
return nil
}