core/path/filepath
filepath
Types
2Relative_Error
Relative_Error :: enum int {
None = 0,
Cannot_Relate = 1,
}SourceWalker
Walker :: WalkerSourceConstants
6LIST_SEPARATOR
LIST_SEPARATOR :: ':'SourceSEPARATOR
SEPARATOR :: '/'SourceSEPARATOR_CHARS
SEPARATOR_CHARS :: `/\`SourceSEPARATOR_STRING
SEPARATOR_STRING :: `/`Sourcewalker_create
walker_create :: os.walker_createSourceCreates a walker, either using a path or a file pointer to a directory the walker will start at.
For an example on how to use the walker, see walker_walk.
walker_init
walker_init :: os.walker_initSourceInitializes a walker, either using a path or a file pointer to a directory the walker will start at.
You are allowed to repeatedly call this to reuse it for later walks.
For an example on how to use the walker, see walker_walk.
Procedures
27abs
abs :: proc(path: string, allocator: mem.Allocator = context.allocator) -> (absolute_path: string, error: os.Error)SourceGet the absolute path to path with respect to the process's current directory.
Allocates Using Provided Allocator
base
base :: proc(path: string) -> (string)SourceGets the file name and extension from a path.
e.g.
'path/to/name.tar.gz' -> 'name.tar.gz'
'path/to/name.txt' -> 'name.txt'
'path/to/name' -> 'name'
Returns "." if the path is an empty string.clean
clean :: proc(path: string, allocator: mem.Allocator = context.allocator) -> (cleaned: string, err: runtime.Allocator_Error)SourceReturns the shortest path name equivalent to path through solely lexical processing.
It applies the folliwng rules until none of them can be applied:
* Replace multiple separators with a single one
* Remove each current directory (`.`) path name element
* Remove each inner parent directory (`..`) path and the preceding paths
* Remove `..` that begin at the root of a path
* All possible separators are replaced with the OS specific separator
The return path ends in a slash only if it represents the root of a directory (`C:\` on Windows and `/` on *nix systems).
If the result of the path is an empty string, the returned path with be `"."`.dir
dir :: proc(path: string) -> (string)SourceReturns all but the last path element, usually the path's directory. Once the final element has been removed,
`dir` calls `clean` on the path and trailing separators are removed. If the path is empty or consists purely
of separators, then `"."` is returned.ext
ext :: proc(path: string) -> (string)SourceGets the file extension from a path, including the dot.
The file extension is such that stem(path) + ext(path) = base(path).
Only the last dot is considered when splitting the file extension.
See `long_ext`.
e.g.
'name.tar.gz' -> '.gz'
'name.txt' -> '.txt'
Returns an empty string if there is no dot.
Returns an empty string if there is a trailing path separator.glob
glob :: proc(pattern: string, allocator: mem.Allocator = context.allocator) -> (matches: []string, err: Error)Sourceglob returns the names of all files matching pattern or nil if there are no matching files The syntax of patterns is the same as "match". The pattern may describe hierarchical names such as /usr/*/bin (assuming '/' is a separator)
glob ignores file system errors
is_abs
is_abs :: proc(path: string) -> (bool)SourceReturn true if path is an absolute path as opposed to a relative one.
is_reserved_name
is_reserved_name :: proc(path: string) -> (bool)SourceIn Windows, returns true if path is one of the following:
"CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
On other platforms, returns `false`.is_separator
is_separator :: proc(c: u8) -> (bool)Sourceis_separator checks whether the byte is a valid separator character
join
join :: proc(elems: []string, allocator: mem.Allocator = context.allocator) -> (joined: string, err: runtime.Allocator_Error)SourceJoin all elems with the system's path separator and normalize the result.
Allocates Using Provided Allocator
For example, join_path({"/home", "foo", "bar.txt"}) will result in "/home/foo/bar.txt".
long_ext
long_ext :: proc(path: string) -> (string)SourceGets the file extension from a path, including the dot.
The long file extension is such that short_stem(path) + long_ext(path) = base(path).
The first dot is used to split off the file extension, unlike `ext` which uses the last dot.
e.g.
'name.tar.gz' -> '.tar.gz'
'name.txt' -> '.txt'
Returns an empty string if there is no dot.
Returns an empty string if there is a trailing path separator.match
match :: proc(pattern: string, name: string) -> (matched: bool, err: Error)Sourcematch states whether "name" matches the shell pattern Pattern syntax is:
pattern:
{term}
term:
'*' matches any sequence of non-/ characters
'?' matches any single non-/ character
'[' ['^'] { character-range } ']'
character classification (cannot be empty)
c matches character c (c != '*', '?', '\\', '[')
'\\' c matches character c
character-range
c matches character c (c != '\\', '-', ']')
'\\' c matches character c
lo '-' hi matches character c for lo <= c <= hi
match requires that the pattern matches the entirety of the name, not just a substring
The only possible error returned is .Syntax_Error
NOTE(bill): This is effectively the shell pattern matching system foundread_all_directory
read_all_directory :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error)SourceReads the file f (assuming it is a directory) and returns all of the unsorted directory entries.
read_all_directory_by_path
read_all_directory_by_path :: proc(path: string, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error)SourceReads the named directory by path (assuming it is a directory) and returns all of the unsorted directory entries.
read_directory
read_directory :: proc(f: ^File, n: int, allocator: runtime.Allocator) -> (files: []File_Info, err: Error)SourceReads the file f (assuming it is a directory) and returns the unsorted directory entries.
This returns up to `n` entries OR all of them if `n <= 0`.read_directory_by_path
read_directory_by_path :: proc(path: string, n: int, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error)SourceReads the named directory by path (assuming it is a directory) and returns the unsorted directory entries.
This returns up to `n` entries OR all of them if `n <= 0`.rel
rel :: proc(base_path: string, target_path: string, allocator: mem.Allocator = context.allocator) -> (Relative_Error, string)SourceReturns a relative path that is lexically equivalent to the target_path when joined with the base_path with an OS specific separator.
e.g. `join(base_path, rel(base_path, target_path))` is equivalent to `target_path`
On failure, the `Relative_Error` will be state it cannot compute the necessary relative path.replace_separators
replace_separators :: proc(path: string, new_sep: rune, allocator: mem.Allocator = context.allocator) -> (new_path: string, err: os.Error)SourceReturns the result of replacing each path separator character in the path with the specific character new_sep.
Allocates Using Provided Allocator
short_stem
short_stem :: proc(path: string) -> (string)SourceGets the name of a file from a path.
The short stem is such that short_stem(path) + long_ext(path) = base(path).
The first dot is used to split off the file extension, unlike `stem` which uses the last dot.
e.g.
'name.tar.gz' -> 'name'
'name.txt' -> 'name'
Returns an empty string if there is no stem. e.g: '.gitignore'.
Returns an empty string if there's a trailing path separator.split
split :: proc(path: string) -> (dir: string, filename: string)SourceSplits path immediate following the last separator; separating the path into a directory and file. If no separator is found, dir will be empty and path set to path.
split_list
split_list :: proc(path: string, allocator: runtime.Allocator) -> (list: []string, err: Error)SourceSplits the PATH-like path string, returning an array of its separated components (delete after use). For Windows the separator is ;, for Unix it's :. An empty string returns nil. A non-empty string with no separators returns a 1-element array. Any empty components will be included, e.g. a::b will return a 3-element array, as will ::. Separators within pairs of double-quotes will be ignored and stripped, e.g. "a:b"c:d will return []{a:bc, d}.
stem
stem :: proc(path: string) -> (string)SourceGets the name of a file from a path.
The stem of a file is such that stem(path) + ext(path) = base(path).
Only the last dot is considered when splitting the file extension.
See `short_stem`.
e.g.
'name.tar.gz' -> 'name.tar'
'name.txt' -> 'name'
Returns an empty string if there is no stem. e.g: '.gitignore'.
Returns an empty string if there's a trailing path separator.volume_name
volume_name :: proc(path: string) -> (string)SourceReturns leading volume name.
e.g.
"C:\foo\bar\baz" will return "C:" on Windows.
Everything else will be "".walker_destroy
walker_destroy :: proc(w: ^Walker)Sourcewalker_error
walker_error :: proc(w: ^Walker) -> (path: string, err: Error)SourceReturns the last error that occurred during the walker's operations.
Can be called while iterating, or only at the end to check if anything failed.
walker_skip_dir
walker_skip_dir :: proc(w: ^Walker)SourceMarks the current directory to be skipped (not entered into).
walker_walk
walker_walk :: proc(w: ^Walker) -> (fi: File_Info, ok: bool)SourceReturns the next file info in the iterator, files are iterated in breadth-first order.
If an error occurred opening a directory, you may get zero'd info struct and walker_error will return the error.
Example:
package main
import "core:fmt"
import "core:strings"
import "core:os"
main :: proc() {
w := os.walker_create("core")
defer os.walker_destroy(&w)
for info in os.walker_walk(&w) {
// Optionally break on the first error:
// _ = walker_error(&w) or_break
// Or, handle error as we go:
if path, err := os.walker_error(&w); err != nil {
fmt.eprintfln("failed walking %s: %s", path, err)
continue
}
// Or, do not handle errors during iteration, and just check the error at the end.
// Skip a directory:
if strings.has_suffix(info.fullpath, ".git") {
os.walker_skip_dir(&w)
continue
}
fmt.printfln("%#v", info)
}
// Handle error if one happened during iteration at the end:
if path, err := os.walker_error(&w); err != nil {
fmt.eprintfln("failed walking %s: %v", path, err)
}
}