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

Simplify file creation. Remove all the funny cases for windows and just print...

Simplify file creation. Remove all the funny cases for windows and just print out a long error message.
parent 37950e34
No related branches found
No related tags found
No related merge requests found
......@@ -40,11 +40,12 @@ This is in csv format. For gnuplot, do not forget to `set datafile separator ','
# BUGS
## Real Bugs
Only under windows: I use os.Create() to open a file for writing. In my "Documents" folder, the function call returns "The system cannot find the file specified". Note, this is when creating a file, not opening an existing one. I do not have any error when I run the program from a different directory. I did experiment with chmod() on the directory, but this (a) did not do anything and (b) only the bits set by 0200 are actually affected according to go's documentation.
### Only under windows
I use `os.Create()` to open a file for writing (plots, `.csv` files. In my "Documents" folder, the function call returns "The system cannot find the file specified". On my test machine, this was a result of trying to write to a protected folder with an application (ackley_mc) that windows did not know about. If you fight your way through the windows menus, you will find a place that shows "blocked software". You could add `ackley_mc.exe` to the list of blessed executables, but why not just run the code from another directory?
## Non bugs
The UI toolkit is "fyne". It does not do charts, so we use go-chart which makes a png, which we can store in memory and feed to the fyne toolkit. Unfortunately, an svg file from go-chart causes fyne to choke. This means we are stuck with png. One day, one should try out the charting library which is based on fyne.
The UI toolkit is "fyne". It does not do charts, so we use go-chart which makes a png, which we can store in memory and feed to the fyne toolkit. We can ask go-chart to make an svg file, but this causes fyne to choke. This means we are stuck with png. One day, one should try out the charting library which is based on fyne.
The graphical interface follows no known style convention and looks like it was designed by a three-year old.
......
......@@ -4,10 +4,8 @@ package mcwork
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"math"
"math/rand"
"os"
......@@ -65,45 +63,27 @@ func isNotSane(mcPrm *McPrm) error {
return nil
}
// fExists returns true if the file exists.
// It is part of the exercise, working around file permissions on windows
func fExists(name string) bool {
var err error
if _, err = os.Stat(name); err == nil {
return true
}
if errors.Is(err, fs.ErrNotExist) {
return false
}
return true
}
// createFix is a wrapper around os.Create() which I seem to need for windows.
// On windows, this fails if I am in my "Documents" directory.
// createFix is a wrapper around os.Create() with a long error message.
// On windows, this fails if I am in my "Documents" directory due to
// its ransomware protection.
func createFix(name string) (*os.File, error) {
longErr := `%s does not exist, but could not create it:
emsg := `Could not create "%s". The error was
%w
Please run the program from somewhere other than %s`
existErr := `File exists, but unable to write to it:
%w`
This is probably a permission error. Specify a directory other than
%s
and try again. It might be your protected folder settings.`
var t string
var err, e2 error
var f *os.File
var err error
if f, err = os.Create(name); err == nil {
return f, nil // Most common path (no problem)
}
if !fExists(name) { // does not exist, but we have a problem.
var d string
if t, e := filepath.Abs(name); e != nil {
return nil, fmt.Errorf("cannot work out path %w", e)
} else {
d = filepath.Dir(t)
}
if _, e := os.Stat(d); e != nil {
return nil, fmt.Errorf("parent directory %s is broken %w", d, err)
}
return nil, fmt.Errorf(longErr, name, err, d)
if t, e2 = filepath.Abs(name); e2 != nil {
return nil, fmt.Errorf("The path for %s is broken: \"%w\"", name, e2)
}
return nil, fmt.Errorf(existErr, err) // The file exists..
return nil, fmt.Errorf(emsg, name, err, filepath.Dir(t))
}
// setupRun does things like get the cooling rate, seed the random numbers,
......
......@@ -7,3 +7,17 @@ func alt_newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float32) {
xT[i] = x + t
}
}
// fExists returns true if the file exists.
// It is part of the exercise, working around file permissions on windows
func fExists(name string) bool {
var err error
if _, err = os.Stat(name); err == nil {
return true
}
if errors.Is(err, fs.ErrNotExist) {
return false
}
return true
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment