Skip to content
Snippets Groups Projects
Commit 9cff8eaa authored by Andrew E. Torda's avatar Andrew E. Torda
Browse files

Used to have a function for appending png filenames. Now more general.

parent a925ff8b
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
}
}
......@@ -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
}
......
package ackwork
var MakepngName = makepngName
var SetSuffix = setSuffix
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment