renamed paths package to resources package

ResourcePath() is now called JoinPath()

UniqueFilename() moved to a new package named unique and renamed to
Filename()
This commit is contained in:
JetSetIlly 2021-10-03 11:40:31 +01:00
parent 0025d2ee3a
commit e5bd938b41
36 changed files with 159 additions and 150 deletions

View file

@ -19,8 +19,8 @@ import (
"fmt"
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
type Preferences struct {
@ -50,7 +50,7 @@ func newPreferences(dsm *Disassembly) (*Preferences, error) {
p.mirrorOrigin = memorymap.OriginCartFxxxMirror
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}

View file

@ -37,11 +37,11 @@ import (
"github.com/jetsetilly/gopher2600/hiscore"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/modalflag"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/performance"
"github.com/jetsetilly/gopher2600/playmode"
"github.com/jetsetilly/gopher2600/recorder"
"github.com/jetsetilly/gopher2600/regression"
"github.com/jetsetilly/gopher2600/resources"
"github.com/jetsetilly/gopher2600/statsview"
"github.com/jetsetilly/gopher2600/wavwriter"
)
@ -414,7 +414,7 @@ func play(md *modalflag.Modes, sync *mainSync) error {
func debug(md *modalflag.Modes, sync *mainSync) error {
md.NewMode()
defInitScript, err := paths.ResourcePath(defaultInitScript)
defInitScript, err := resources.JoinPath(defaultInitScript)
if err != nil {
return err
}

View file

@ -16,8 +16,8 @@
package crt
import (
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
type Preferences struct {
@ -84,7 +84,7 @@ func NewPreferences() (*Preferences, error) {
p.SetDefaults()
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}

View file

@ -16,8 +16,8 @@
package sdlaudio
import (
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
type Preferences struct {
@ -36,7 +36,7 @@ func NewPreferences() (*Preferences, error) {
p.SetDefaults()
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}

View file

@ -20,7 +20,7 @@ import (
"github.com/jetsetilly/gopher2600/gui/sdlimgui/framebuffer"
"github.com/jetsetilly/gopher2600/hardware/television/specification"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources/unique"
)
type screenshotMode int
@ -95,9 +95,9 @@ func (sh *screenshotSequencer) startProcess(mode screenshotMode) {
sh.crtProcessing = sh.img.crtPrefs.Enabled.Get().(bool)
if sh.crtProcessing {
sh.baseFilename = paths.UniqueFilename("crt", sh.img.vcs.Mem.Cart.ShortName)
sh.baseFilename = unique.Filename("crt", sh.img.vcs.Mem.Cart.ShortName)
} else {
sh.baseFilename = paths.UniqueFilename("pix", sh.img.vcs.Mem.Cart.ShortName)
sh.baseFilename = unique.Filename("pix", sh.img.vcs.Mem.Cart.ShortName)
}
}

View file

@ -17,8 +17,8 @@ package sdlimgui
import (
"fmt"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
// unified preferences for both modes (debugger and playmode). preferences
@ -67,7 +67,7 @@ func newPreferences(img *SdlImgui) (*preferences, error) {
p.superchargerNotifications.Set(true)
// setup preferences
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}
@ -139,7 +139,7 @@ func (p *preferences) setWindowPreferences(isPlayMode bool) error {
}
// setup preferences
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return err
}

View file

@ -29,8 +29,8 @@ import (
"github.com/jetsetilly/gopher2600/hardware"
"github.com/jetsetilly/gopher2600/hardware/television"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/reflection"
"github.com/jetsetilly/gopher2600/resources"
"github.com/jetsetilly/gopher2600/userinput"
"github.com/veandco/go-sdl2/sdl"
@ -137,7 +137,7 @@ func NewSdlImgui(tv *television.Television) (*SdlImgui, error) {
return nil, curated.Errorf("sdlimgui: %v", err)
}
iniPath, err := paths.ResourcePath(imguiIniFile)
iniPath, err := resources.JoinPath(imguiIniFile)
if err != nil {
return nil, curated.Errorf("sdlimgui: %v", err)
}

View file

@ -23,7 +23,7 @@ import (
"github.com/jetsetilly/gopher2600/disassembly/coprocessor"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/harmony/arm7tdmi"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources/unique"
)
const winCoProcLastExecutionID = "Last Execution"
@ -270,10 +270,10 @@ func (win *winCoProcLastExecution) save() {
var fn string
if win.showLastExecution {
itr = win.img.dbg.Disasm.Coprocessor.NewIteration(coprocessor.LastExecution)
fn = paths.UniqueFilename("coproc_lastexecution", "")
fn = unique.Filename("coproc_lastexecution", "")
} else {
itr = win.img.dbg.Disasm.Coprocessor.NewIteration(coprocessor.Disassembly)
fn = paths.UniqueFilename("coproc_disasm", "")
fn = unique.Filename("coproc_disasm", "")
}
f, err := os.Create(fmt.Sprintf("%s.csv", fn))

View file

@ -24,7 +24,7 @@ import (
"github.com/jetsetilly/gopher2600/emulation"
"github.com/jetsetilly/gopher2600/gui/fonts"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources/unique"
"github.com/inkyblackness/imgui-go/v4"
)
@ -237,7 +237,7 @@ func (win *winTerm) draw() {
}
func (win *winTerm) saveOutput() {
fn := paths.UniqueFilename("terminal", "")
fn := unique.Filename("terminal", "")
f, err := os.Create(fn)
if err != nil {
win.output = append(win.output, terminalOutput{

View file

@ -19,8 +19,8 @@ import (
"fmt"
"math/rand"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
const (
@ -45,7 +45,7 @@ func newPreferences() (*Preferences, error) {
p.ID.SetMaxLen(MaxIDLength)
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}

View file

@ -21,7 +21,7 @@ import (
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
)
// list of allowed filenames for the supercharger BIOS.
@ -75,7 +75,7 @@ func loadBIOS(path string) ([]uint8, error) {
// the emulator's resource path
for _, b := range biosFile {
p, err := paths.ResourcePath(b)
p, err := resources.JoinPath(b)
if err != nil {
return nil, err
}

View file

@ -19,8 +19,8 @@ import (
"math/rand"
"time"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
// Preferences defines and collates all the preference values used by the debugger.
@ -83,7 +83,7 @@ func NewPreferences() (*Preferences, error) {
p.SetDefaults()
// setup preferences and load from disk
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}

View file

@ -19,7 +19,7 @@ import (
"os"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
)
const saveKeyPath = "savekey"
@ -60,7 +60,7 @@ func newEeprom() *EEPROM {
// Read EEPROM data from disk.
func (ee *EEPROM) Read() {
fn, err := paths.ResourcePath(saveKeyPath)
fn, err := resources.JoinPath(saveKeyPath)
if err != nil {
logger.Logf("savekey", "could not load eeprom file (%s)", err)
return
@ -98,7 +98,7 @@ func (ee *EEPROM) Read() {
// Write EEPROM data to disk.
func (ee *EEPROM) Write() {
fn, err := paths.ResourcePath(saveKeyPath)
fn, err := resources.JoinPath(saveKeyPath)
if err != nil {
logger.Logf("savekey", "could not write eeprom file (%s)", err)
return

View file

@ -17,8 +17,8 @@ package revision
import (
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
// Preferences for TIA revisins. Use the "live" values inside the emulation
@ -57,7 +57,7 @@ func newPreferences() (*Preferences, error) {
p := &Preferences{}
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, curated.Errorf("revision: %v", err)
}

View file

@ -17,8 +17,8 @@ package hiscore
import (
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
type Preferences struct {
@ -37,7 +37,7 @@ func newPreferences() (*Preferences, error) {
p := &Preferences{}
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, curated.Errorf("hiscore: %v", err)
}

View file

@ -25,7 +25,7 @@ import (
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
)
const patchPath = "patches"
@ -39,7 +39,7 @@ const neoSeparator = ":"
func CartridgeMemory(mem *cartridge.Cartridge, patchFile string) (bool, error) {
var err error
p, err := paths.ResourcePath(patchPath, patchFile)
p, err := resources.JoinPath(patchPath, patchFile)
if err != nil {
return false, curated.Errorf("patch: %v", err)
}

View file

@ -1,36 +0,0 @@
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
// Package paths contains functions to prepare paths for gopher2600 resources.
//
// The ResourcePath() function returns the correct path to the resource
// directory/file specified in the arguments. The result of ResourcePath
// depends on the build tag used to compile the program.
//
// For "release" tagged builds, the correct path is one rooted in the user's
// configuration directory. On modern Linux systems the full path would be
// something like:
//
// /home/user/.config/gopher2600/
//
// For "non-release" tagged builds, the correct path is rooted in the current
// working directory:
//
// .gopher2600
//
// The reason for this is simple. During development, it is more convenient to
// have the config directory close to hand. For release binaries meanwhile, the
// config directory should be somewhere the user expects.
package paths

View file

@ -36,8 +36,8 @@ import (
"github.com/jetsetilly/gopher2600/hiscore"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/patch"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/recorder"
"github.com/jetsetilly/gopher2600/resources/unique"
"github.com/jetsetilly/gopher2600/setup"
"github.com/jetsetilly/gopher2600/userinput"
)
@ -202,7 +202,7 @@ func Play(tv *television.Television, scr gui.GUI, newRecording bool, cartload ca
// new recording requested
// create a unique filename
recording = paths.UniqueFilename("recording", cartload.ShortName())
recording = unique.Filename("recording", cartload.ShortName())
// prepare new recording
rec, err := recorder.NewRecorder(recording, vcs)

View file

@ -24,7 +24,7 @@ import (
"unicode"
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/paths/fs"
"github.com/jetsetilly/gopher2600/resources/fs"
)
// DefaultPrefsFile is the default filename of the global preferences file.

View file

@ -21,9 +21,9 @@
//
// The Disk type is the key resource in the package. First, create new Disk
// instance with NewDisk(), specifying the location of the preferences file
// (using paths.ResourcePath() as necessary).
// (using resources.ResourcePath() as necessary).
//
// fn, _ := paths.ResourcePath(prefs.DefaultPrefsFile)
// fn, _ := resources.ResourcePath(prefs.DefaultPrefsFile)
// prf := prefs.NewDisk(fn)
//
// Preference value can then be added with the Add() function, specifying the

View file

@ -190,7 +190,7 @@ func (reg *PlaybackRegression) regress(newRegression bool, output io.Writer, msg
// regressionScripts directory
if newRegression {
// create a unique filename
newScript, err := uniqueFilename("playback", plb.CartLoad)
newScript, err := uniqueFilename("playback", plb.CartLoad.ShortName())
if err != nil {
return false, "", curated.Errorf("playback: %v", err)
}

View file

@ -25,14 +25,14 @@ import (
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/database"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
)
// ansi code for clear line.
const ansiClearLine = "\033[2K"
// the location of the regressionDB file and the location of any regression
// scripts. these should be wrapped by paths.ResourcePath().
// scripts. these should be wrapped by resources.ResourcePath().
const regressionPath = "regression"
const regressionDBFile = "db"
const regressionScripts = "scripts"
@ -76,7 +76,7 @@ func RegressList(output io.Writer) error {
return curated.Errorf("regression: list: io.Writer should not be nil (use a nopWriter)")
}
dbPth, err := paths.ResourcePath(regressionPath, regressionDBFile)
dbPth, err := resources.JoinPath(regressionPath, regressionDBFile)
if err != nil {
return curated.Errorf("regression: list: %v", err)
}
@ -99,7 +99,7 @@ func RegressAdd(output io.Writer, reg Regressor) error {
return curated.Errorf("regression: add: io.Writer should not be nil (use a nopWriter)")
}
dbPth, err := paths.ResourcePath(regressionPath, regressionDBFile)
dbPth, err := resources.JoinPath(regressionPath, regressionDBFile)
if err != nil {
return curated.Errorf("regression: add: %v", err)
}
@ -144,7 +144,7 @@ func RegressRedux(output io.Writer, confirmation io.Reader) error {
return nil
}
dbPth, err := paths.ResourcePath(regressionPath, regressionDBFile)
dbPth, err := resources.JoinPath(regressionPath, regressionDBFile)
if err != nil {
return curated.Errorf("regression: redux: %v", err)
}
@ -219,7 +219,7 @@ func RegressCleanup(output io.Writer, confirmation io.Reader) error {
return nil
}
dbPth, err := paths.ResourcePath(regressionPath, regressionDBFile)
dbPth, err := resources.JoinPath(regressionPath, regressionDBFile)
if err != nil {
return curated.Errorf("regression: cleanup: %v", err)
}
@ -257,7 +257,7 @@ func RegressCleanup(output io.Writer, confirmation io.Reader) error {
}
// gather list of files on disk in path
scriptPth, err := paths.ResourcePath(regressionPath, regressionScripts)
scriptPth, err := resources.JoinPath(regressionPath, regressionScripts)
if err != nil {
return curated.Errorf("regression: list: %v", err)
}
@ -281,7 +281,7 @@ func RegressCleanup(output io.Writer, confirmation io.Reader) error {
for _, e := range filesOnDisk {
found := false
n, err := paths.ResourcePath(regressionPath, regressionScripts, e.Name())
n, err := resources.JoinPath(regressionPath, regressionScripts, e.Name())
if err != nil {
return curated.Errorf("regression: cleanup: %v", err)
}
@ -333,7 +333,7 @@ func RegressDelete(output io.Writer, confirmation io.Reader, key string) error {
return curated.Errorf("regression: delete: invalid key [%s]", key)
}
dbPth, err := paths.ResourcePath(regressionPath, regressionDBFile)
dbPth, err := resources.JoinPath(regressionPath, regressionDBFile)
if err != nil {
return curated.Errorf("regression: delete: %v", err)
}
@ -369,7 +369,7 @@ func RegressRun(output io.Writer, verbose bool, filterKeys []string) error {
return curated.Errorf("regression: run: io.Writer should not be nil (use a nopWriter)")
}
dbPth, err := paths.ResourcePath(regressionPath, regressionDBFile)
dbPth, err := resources.JoinPath(regressionPath, regressionDBFile)
if err != nil {
return curated.Errorf("regression: run: %v", err)
}

View file

@ -16,25 +16,27 @@
package regression
import (
"github.com/jetsetilly/gopher2600/cartridgeloader"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
"github.com/jetsetilly/gopher2600/resources/unique"
)
// create a unique filename from a CatridgeLoader instance. used when saving
// uniqueFilename is a shim function for unique.Filename().
//
// It create a unique filename from a CatridgeLoader instance. used when saving
// scripts into regressionScripts directory. calls paths.UniqueFilename() to
// maintain common formatting used in the project.
func uniqueFilename(prepend string, cartload cartridgeloader.Loader) (string, error) {
f := paths.UniqueFilename(prepend, cartload.ShortName())
func uniqueFilename(filetype string, cartname string) (string, error) {
f := unique.Filename(filetype, cartname)
scriptsPath, err := paths.ResourcePath(regressionPath, regressionScripts)
scriptsPath, err := resources.JoinPath(regressionPath, regressionScripts)
if err != nil {
return "", err
}
scrPth, err := paths.ResourcePath(scriptsPath, f)
p, err := resources.JoinPath(scriptsPath, f)
if err != nil {
return "", err
}
return scrPth, nil
return p, nil
}

View file

@ -269,7 +269,7 @@ func (reg *VideoRegression) regress(newRegression bool, output io.Writer, msg st
if reg.State != StateNone {
// create a unique filename
reg.stateFile, err = uniqueFilename("state", reg.CartLoad)
reg.stateFile, err = uniqueFilename("state", reg.CartLoad.ShortName())
if err != nil {
return false, "", curated.Errorf("video: %v", err)
}

View file

@ -16,7 +16,7 @@
//go:build !release
// +build !release
package paths
package resources
const gopherConfigDir = ".gopher2600"

40
resources/doc.go Normal file
View file

@ -0,0 +1,40 @@
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
// Package resources contains functions to prepare paths for gopher2600
// resources.
//
// The JoinPath() function returns the correct path to the resource
// directory/file specified in the arguments. It handles the creation of
// directories as required but does not otherwise touch or create files.
//
// JoinPath() handles the inclusion of the correct base path. The base path
// depends on how the binary was built.
//
// For builds with the "releas" build tag, the path returned by JoinPath() is
// rooted in the user's configuration directory. On modern Linux systems the
// full path would be something like:
//
// /home/user/.config/gopher2600/
//
// For non-"release" builds, the correct path is rooted in the current working
// directory:
//
// .gopher2600
//
// The package does this because during development it is more convenient to
// have the config directory close to hand. For release binaries however, the
// config directory should be somewhere the end-user expects.
package resources

View file

@ -13,21 +13,21 @@
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package paths
package resources
import (
"os"
"path/filepath"
"github.com/jetsetilly/gopher2600/paths/fs"
"github.com/jetsetilly/gopher2600/resources/fs"
)
// ResourcePath prepends the supplied path with a with OS/build specific base
// JoinPath prepends the supplied path with a with OS/build specific base
// paths
//
// The function creates all folders necessary to reach the end of sub-path. It
// does not otherwise touch or create the file.
func ResourcePath(path ...string) (string, error) {
func JoinPath(path ...string) (string, error) {
b, err := baseResourcePath()
if err != nil {
return "", err

View file

@ -16,7 +16,7 @@
//go:build release
// +build release
package paths
package resources
import (
"os"

View file

@ -13,51 +13,33 @@
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package paths_test
package resources_test
import (
"testing"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
"github.com/jetsetilly/gopher2600/test"
)
func TestResourcePath(t *testing.T) {
pth, err := paths.ResourcePath("foo/bar", "baz")
func TestJoinPath(t *testing.T) {
pth, err := resources.JoinPath("foo/bar", "baz")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/foo/bar/baz")
pth, err = paths.ResourcePath("foo/bar", "")
pth, err = resources.JoinPath("foo", "bar", "baz")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/foo/bar/baz")
pth, err = resources.JoinPath("foo/bar", "")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/foo/bar")
pth, err = paths.ResourcePath("", "baz")
pth, err = resources.JoinPath("", "baz")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/baz")
pth, err = paths.ResourcePath("", "")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600")
}
func TestResourceFile(t *testing.T) {
pth, err := paths.ResourcePath("foo/bar", "baz")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/foo/bar/baz")
pth, err = paths.ResourcePath("foo", "bar", "baz")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/foo/bar/baz")
pth, err = paths.ResourcePath("foo/bar", "")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/foo/bar")
pth, err = paths.ResourcePath("", "baz")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600/baz")
pth, err = paths.ResourcePath("", "")
pth, err = resources.JoinPath("", "")
test.Equate(t, err, nil)
test.Equate(t, pth, ".gopher2600")
}

22
resources/unique/doc.go Normal file
View file

@ -0,0 +1,22 @@
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
// Package unique contains functions that compose filenames that should be
// unique on the filesystem.
//
// This is useful in other Gopher2600 packages. For example, it used to
// generate filenames for: playback recordings, regression scripts and many
// other file types.
package unique

View file

@ -13,7 +13,7 @@
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package paths
package unique
import (
"fmt"
@ -21,34 +21,33 @@ import (
"time"
)
// UniqueFilename creates a filename that (assuming a functioning clock) should
// not collide with any existing file. Note that the function does not test for
// Filename creates a filename that (assuming a functioning clock) should not
// collide with any existing file. Note that the function does not test for
// this.
//
// Used to generate filenames for:
// - playback recordings
// - regression scripts
// - terminal output (sdlimgui GUI)
//
// Format of returned string is:
//
// prepend_cartname_YYYYMMDD_HHMMSS
// filetype_cartname_YYYYMMDD_HHMMSS
//
// Where cartname is the string returned by cartload.ShortName(). If there is
// no cartridge name the returned string will be of the format:
//
// prepend_YYYYMMDD_HHMMSS
func UniqueFilename(prepend string, shortCartName string) string {
// filetype_YYYYMMDD_HHMMSS
//
// The filetype argument is simply another way of identifying the file
// uniquely. For example, if saving a screenshot the filetype might simply be
// "screenshot" or "photo".
func Filename(filetype string, cartName string) string {
n := time.Now()
timestamp := fmt.Sprintf("%04d%02d%02d_%02d%02d%02d", n.Year(), n.Month(), n.Day(), n.Hour(), n.Minute(), n.Second())
var fn string
c := strings.TrimSpace(shortCartName)
c := strings.TrimSpace(cartName)
if len(c) > 0 {
fn = fmt.Sprintf("%s_%s_%s", prepend, c, timestamp)
fn = fmt.Sprintf("%s_%s_%s", filetype, c, timestamp)
} else {
fn = fmt.Sprintf("%s_%s", prepend, timestamp)
fn = fmt.Sprintf("%s_%s", filetype, timestamp)
}
return fn

View file

@ -16,8 +16,8 @@
package rewind
import (
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/prefs"
"github.com/jetsetilly/gopher2600/resources"
)
type Preferences struct {
@ -50,7 +50,7 @@ func newPreferences(r *Rewind) (*Preferences, error) {
p.Freq.Set(snapshotFreq)
// save server using the prefs package
pth, err := paths.ResourcePath(prefs.DefaultPrefsFile)
pth, err := resources.JoinPath(prefs.DefaultPrefsFile)
if err != nil {
return nil, err
}

View file

@ -20,7 +20,7 @@ import (
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/database"
"github.com/jetsetilly/gopher2600/hardware"
"github.com/jetsetilly/gopher2600/paths"
"github.com/jetsetilly/gopher2600/resources"
)
// the location of the setupDB file.
@ -65,7 +65,7 @@ func AttachCartridge(vcs *hardware.VCS, cartload cartridgeloader.Loader) error {
return curated.Errorf("setup: %v", err)
}
dbPth, err := paths.ResourcePath(setupDBFile)
dbPth, err := resources.JoinPath(setupDBFile)
if err != nil {
return curated.Errorf("setup: %v", err)
}