From ed0dc6f57036c8a1cbbeb6fb36915ee198321b49 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Fri, 7 Nov 2014 23:47:19 -0500 Subject: [PATCH] hey --- errors.go | 31 ++++++++++++++++++++++++++ main.go | 35 +++++++++++++++++++++++++++++ speck.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 errors.go create mode 100644 main.go create mode 100644 speck.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..31e22e7 --- /dev/null +++ b/errors.go @@ -0,0 +1,31 @@ +package main + +import ( + "strings" +) + +const ( + E_Ok int = iota + E_No_Data +) + +type errorGroup []error + +func (e errorGroup) Error() string { + messages := make([]string, 0, len(e)) + for i, _ := range e { + messages[i] = e[i].Error() + } + return strings.Join(messages, " && ") +} + +func (g *errorGroup) AddError(err error) { + if err == nil { + return + } + if g == nil { + panic("fart") + *g = make([]error, 0, 4) + } + *g = append(*g, err) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..af4ce3c --- /dev/null +++ b/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "os" +) + +var dataPath = "/projects/exo/expl.speck" + +func log_error(template string, args ...interface{}) { + fmt.Fprint(os.Stderr, "ERROR ") + fmt.Fprintf(os.Stderr, template+"\n", args...) +} + +func log_info(template string, args ...interface{}) { + fmt.Fprint(os.Stdout, "INFO ") + fmt.Fprintf(os.Stdout, template+"\n", args...) +} + +func bail(status int, template string, args ...interface{}) { + if status == 0 { + fmt.Fprintf(os.Stdout, template, args...) + } else { + fmt.Fprintf(os.Stderr, template, args...) + } + os.Exit(status) +} + +func main() { + fi, err := os.Open(dataPath) + if err != nil { + bail(E_No_Data, "unable to open data path: %v", err) + } + speckStream(fi) +} diff --git a/speck.go b/speck.go new file mode 100644 index 0000000..b612423 --- /dev/null +++ b/speck.go @@ -0,0 +1,66 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "regexp" + "strconv" + "strings" +) + +func speckStream(r io.ReadCloser) { + defer r.Close() + keep := regexp.MustCompile(`^\s*[\d-]`) + + br := bufio.NewReader(r) + for { + line, err := br.ReadBytes('\n') + switch err { + case io.EOF: + return + case nil: + break + default: + log_error("unable to stream speck file: %v", err) + return + } + if !keep.Match(line) { + continue + } + parseSpeckLine(line) + } +} + +type exoSystem struct { + x, y, z float64 + planets int + name string +} + +func (e exoSystem) String() string { + return fmt.Sprintf("", e.name, e.x, e.y, e.z, e.planets) +} + +func parseSpeckLine(line []byte) { + parts := strings.Split(string(line), " ") + var err error + var g errorGroup + s := new(exoSystem) + + s.x, err = strconv.ParseFloat(parts[0], 64) + g.AddError(err) + s.y, err = strconv.ParseFloat(parts[1], 64) + g.AddError(err) + s.z, err = strconv.ParseFloat(parts[2], 64) + g.AddError(err) + s.planets, err = strconv.Atoi(parts[3]) + g.AddError(err) + + s.name = strings.TrimSpace(strings.Join(parts[7:], " ")) + + if g != nil { + log_error("%v", g) + } + log_info("%v", s) +}