diff --git a/ackwork/ackwork_test.go b/ackwork/ackwork_test.go index b62c49474acd305c0d897232cf1273d35d557392..5e9a7afb960806dfa0614336ee7a59b4e06a4bcf 100644 --- a/ackwork/ackwork_test.go +++ b/ackwork/ackwork_test.go @@ -138,20 +138,40 @@ func TestPlot(t *testing.T) { } } -func TestMakepngName(t *testing.T) { +func TestSetSuffix (t *testing.T) { var tdata = []struct { - in string - out string + in, suffix, want string }{ - {"boo", "boo.png"}, - {"boo.", "boo.png"}, - {"a.png", "a.png"}, - {"/boo/foo/a.PNG", "/boo/foo/a.PNG"}, - {"a/b/c/d.svg", "a/b/c/d.png"}, + {"boo.", ".csv", "boo.csv"}, //xxx + {"boo", "csv", "boo.csv"}, + {"boo.", "csv", "boo.csv"}, + {"boo", ".csv", "boo.csv"}, + {"boo.csv", ".csv", "boo.csv"}, + {"/boo/blah/boo", ".csv", "/boo/blah/boo.csv"}, + {"/a/b/c.svg", ".png", "/a/b/c.png"}, + {"boo.", "", "boo"}, + {"boo", "", "boo"}, } + + for _, s := range tdata { + if tmp, err := SetSuffix (s.in, s.suffix); err != nil { + t.Fatal ("setSuffix error handling", s) + } else { + if tmp != s.want { + t.Fatal ("Wanted", s.want, "got", tmp, "working on", s) + } + } + } +} + +// Check that errors are generated +func TestSetSuffixErr (t *testing.T) { + var tdata = []struct {in, suffix string}{ + {"", ""}, + {".", ""} } for _, s := range tdata { - if tmp := MakepngName (s.in); tmp != s.out { - t.Fatal ("Wanted", s.out, "got", tmp) + if _, err := SetSuffix (s.in, s.suffix); err == nil { + t.Fatal ("setSuffix should return error on", s) } } } diff --git a/ackwork/dorun.go b/ackwork/dorun.go index 260746514802ad742bc16cb20d819e1c0c7aaff4..1412afc0ae4c422427e21419e153125b80c291c9 100644 --- a/ackwork/dorun.go +++ b/ackwork/dorun.go @@ -71,7 +71,10 @@ func setupRun(mcPrm *mcPrm, cprm *cprm) error { return err } if mcPrm.fPltName != "" { - mcPrm.fPltName = makepngName(mcPrm.fPltName) + var err error + if mcPrm.fPltName, err = setSuffix(mcPrm.fPltName, ".png"); err != nil { + return fmt.Errorf("plot filename: %w", err) + } if cprm.fPlt, err = os.Create(mcPrm.fPltName); err != nil { return err } diff --git a/ackwork/export_test.go b/ackwork/export_test.go index e8307e6682b5bb26a3e690cf8766425ee5138545..1fbc92595f99ffbb17b5d28158dc028baa82f0d7 100644 --- a/ackwork/export_test.go +++ b/ackwork/export_test.go @@ -1,3 +1,3 @@ package ackwork -var MakepngName = makepngName +var SetSuffix = setSuffix diff --git a/ackwork/plot.go b/ackwork/plot.go index 7382df5083c6c47feb4ed7d71c6742cbcf439ba1..276e9b26e5c4309ae6bea7e1edfdfdc51eb87a78 100644 --- a/ackwork/plot.go +++ b/ackwork/plot.go @@ -6,6 +6,7 @@ package ackwork import ( + "errors" "fmt" "io" "math" @@ -20,7 +21,7 @@ import ( // If it ends in something else, replace the ending with png. // We have stalinistically decided that we will only write png output. // svg is nicer, but the files can become silly big. -func makepngName(fname string) string { +func XmakepngName(fname string) string { const dotpng = ".png" ext := filepath.Ext(fname) switch ext { @@ -36,6 +37,36 @@ func makepngName(fname string) string { return "hello" } +// setSuffix takes a name and makes sure the desired suffix is at the end +// of the filename. +func setSuffix(fname, suffix string) (string, error) { + if len(fname) == 0 { + return "", errors.New("setSuffix given empty fname") + } + if suffix == "" { // no suffix might not be an error. Just + if fname[len(fname)-1] == '.' { // remove any trailing dot + fname = fname[0 : len(fname)-1] + } + if len(fname) > 0 { + return fname, nil + } + return "", errors.New("setSuffix got empty filename") + } + + if suffix[0] != '.' { + suffix = "." + suffix + } + oldExt := filepath.Ext(fname) + switch oldExt { + case suffix: + return fname, nil + case "": + return fname + suffix, nil + default: + return fname[0:len(fname)-len(oldExt)] + suffix, nil + } +} + // maketicks gives reasonable default tick locations func maketicks(axisDscrpt axticks.AxisDscrpt, prcsn int) []chart.Tick { xmin, delta, prcsn := axisDscrpt.Xmin, axisDscrpt.Delta, axisDscrpt.Prcsn @@ -68,7 +99,7 @@ func plotfWrt(xdata, ydata []float64, plotOut io.Writer, plotme bool) error { } xaxis := chart.XAxis{ - Name: "step", + Name: "step", Range: &chart.ContinuousRange{ Min: xAxisDscrpt.Xmin, Max: xAxisDscrpt.Xmax, @@ -116,6 +147,6 @@ func plotfWrt(xdata, ydata []float64, plotOut io.Writer, plotme bool) error { if err := graph.Render(chart.PNG, plotOut); err != nil { return fmt.Errorf("While plotting %w", err) } - + return nil }