core/path/filepath

filepath

Types

2

Constants

6

walker_init

walker_init :: os.walker_initSource

Initializes 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

27

abs

abs :: proc(path: string, allocator: mem.Allocator = context.allocator) -> (absolute_path: string, error: os.Error)Source

Get the absolute path to path with respect to the process's current directory.

Allocates Using Provided Allocator

base

base :: proc(path: string) -> (string)Source

Gets 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)Source

Returns 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)Source

Returns 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)Source

Gets 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)Source

glob 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_reserved_name

is_reserved_name :: proc(path: string) -> (bool)Source

In 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`.

join

join :: proc(elems: []string, allocator: mem.Allocator = context.allocator) -> (joined: string, err: runtime.Allocator_Error)Source

Join 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)Source

Gets 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)Source

match 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 found

read_all_directory

read_all_directory :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error)Source

Reads the file f (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)Source

Reads 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)Source

Reads 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)Source

Returns 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.

short_stem

short_stem :: proc(path: string) -> (string)Source

Gets 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)Source

Splits 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)Source

Splits 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)Source

Gets 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.

walker_error

walker_error :: proc(w: ^Walker) -> (path: string, err: Error)Source

Returns 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_walk

walker_walk :: proc(w: ^Walker) -> (fi: File_Info, ok: bool)Source

Returns 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)
	}
}

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.