You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.2 KiB
Go

8 years ago
package main
import (
8 years ago
"compress/bzip2"
8 years ago
"flag"
8 years ago
"fmt"
8 years ago
"io"
"os"
"strings"
8 years ago
)
const (
replay_header = "PBDEMS2\000"
)
8 years ago
func ensureNewline(t string) string {
if strings.HasSuffix(t, "\n") {
return t
}
return t + "\n"
}
func bail(status int, t string, args ...interface{}) {
var out io.Writer
if status == 0 {
out = os.Stdout
} else {
out = os.Stderr
}
fmt.Fprintf(out, ensureNewline(t), args...)
os.Exit(status)
}
8 years ago
type options struct {
b bool // bzip compression flag
v bool // verbose flag
f string // input file
}
func (o options) input() (io.Reader, error) {
var r io.Reader
if o.f == "--" {
r = os.Stdin
} else {
fi, err := os.Open(o.f)
if err != nil {
return nil, fmt.Errorf("unable to open file %s: %v", o.f, err)
}
r = fi
8 years ago
}
8 years ago
if o.b {
r = bzip2.NewReader(r)
8 years ago
}
8 years ago
return r, nil
8 years ago
}
8 years ago
func main() {
8 years ago
var opts options
flag.BoolVar(&opts.b, "b", false, "input is expected to be bzip-compressed")
flag.BoolVar(&opts.v, "v", false, "verbose mode")
flag.StringVar(&opts.f, "f", "--", "input file to be used. -- means stdin")
8 years ago
flag.Parse()
8 years ago
r, err := opts.input()
8 years ago
if err != nil {
8 years ago
bail(1, "input error: %v", err)
8 years ago
}
p := newParser(r)
if _, err := p.checkHeader(); err != nil {
bail(1, "parse error: %v", err)
}
8 years ago
}