Flamenco/pkg/crosspath/crosspath.go
Sybren A. Stüvel 9f5e4cc0cc License: license all code under "GPL-3.0-or-later"
The add-on code was copy-pasted from other addons and used the GPL v2
license, whereas by accident the LICENSE text file had the GNU "Affero" GPL
license v3 (instead of regular GPL v3).

This is now all streamlined, and all code is licensed as "GPL v3 or later".

Furthermore, the code comments just show a SPDX License Identifier
instead of an entire license block.
2022-03-07 15:26:46 +01:00

78 lines
2.2 KiB
Go

// Package crosspath deals with file/directory paths in a cross-platform way.
//
// This package tries to understand Windows paths on UNIX and vice versa.
// Returned paths may be using forward slashes as separators.
package crosspath
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"fmt"
path_module "path" // import under other name so that parameters can be called 'path'
"path/filepath"
"strings"
)
// Base returns the last element of path. Trailing slashes are removed before
// extracting the last element. If the path is empty, Base returns ".". If the
// path consists entirely of slashes, Base returns "/".
func Base(path string) string {
slashed := ToSlash(path)
return path_module.Base(slashed)
}
// Dir returns all but the last element of path, typically the path's directory.
// If the path is empty, Dir returns ".".
func Dir(path string) string {
if path == "" {
return "."
}
slashed := ToSlash(path)
// Don't use path.Dir(), as that cleans up the path and removes double
// slashes. However, Windows UNC paths start with double blackslashes, which
// will translate to double slashes and should not be removed.
dir, _ := path_module.Split(slashed)
switch {
case dir == "":
return "."
case len(dir) > 1:
// Remove trailing slash.
return dir[:len(dir)-1]
default:
return dir
}
}
func Join(elem ...string) string {
return ToSlash(path_module.Join(elem...))
}
// Stem returns the filename without extension.
func Stem(path string) string {
base := Base(path)
ext := path_module.Ext(base)
return base[:len(base)-len(ext)]
}
// ToSlash replaces all backslashes with forward slashes.
// Contrary to filepath.ToSlash(), this also happens on Linux; it does not
// expect `path` to be in platform-native notation.
func ToSlash(path string) string {
return strings.ReplaceAll(path, "\\", "/")
}
// ToNative replaces all path separators (forward and backward slashes) with the
// platform-native separator.
func ToNative(path string) string {
switch filepath.Separator {
case '/':
return ToSlash(path)
case '\\':
return strings.ReplaceAll(path, "/", "\\")
default:
panic(fmt.Sprintf("this platform has an unknown path separator: %q", filepath.Separator))
}
}