From 8c53711fbeeab5eab537a794b351c5839de43197 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 30 Jul 2016 11:36:13 -0400 Subject: [PATCH] check header again --- main.go | 10 +++------- parser.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 parser.go diff --git a/main.go b/main.go index fd6618b..fb533fb 100644 --- a/main.go +++ b/main.go @@ -68,12 +68,8 @@ func main() { bail(1, "input error: %v", err) } - buf := make([]byte, 8) - if _, err := r.Read(buf); err != nil { - bail(1, "error reading header: %v", err) + p := newParser(r) + if _, err := p.checkHeader(); err != nil { + bail(1, "parse error: %v", err) } - if string(buf) != replay_header { - bail(1, "unexpected replay header: %s", string(buf)) - } - fmt.Println(string(buf)) } diff --git a/parser.go b/parser.go new file mode 100644 index 0000000..7788b4e --- /dev/null +++ b/parser.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "io" +) + +type parser struct { + // the source of replay bytes. Must NOT be compressed. + source io.Reader +} + +func newParser(r io.Reader) *parser { + return &parser{source: r} +} + +// checks whether we have an acceptable header at the current reader position. +func (p *parser) checkHeader() (bool, error) { + buf := make([]byte, 8) + if _, err := p.source.Read(buf); err != nil { + return false, fmt.Errorf("unable to read header bytes: %v", err) + } + return string(buf) == replay_header, nil +}