ospaths2
nimony/lib/std/private/ospaths2.nim
func normalizePathEnd(path: var string; trailingSep: bool)Ensures
pathhas exactly 0 or 1 trailingDirSep, depending ontrailingSep, and taking care of edge cases: it preservers whether a path is absolute or relative, and makes sure trailing sep isDirSep, notAltSep. Trailing/.are compressed, see examples.proc joinPath(head: string; tail: string): stringJoins two directory names to one.
returns normalized path concatenation of
headandtail, preserving whether or nottailhas a trailing slash (or, if tail if empty, whether head has one).See also:
joinPath(parts: varargs[string]) proc_/ proc_splitPath proc_- uri.combine proc
- uri./ proc
proc /(head: string; tail: string): stringThe same as
joinPath(head, tail) proc_.See also:
/../ proc_joinPath(head, tail) proc_joinPath(parts: varargs[string]) proc_splitPath proc_- uri.combine proc
- uri./ proc
proc splitPath(path: string): tuple[head: string, tail: string]Splits a directory into
(head, tail)tuple, so thathead / tail == path(except for edge cases like "/usr").See also:
joinPath(head, tail) proc_joinPath(parts: varargs[string]) proc_/ proc_/../ proc_relativePath proc_
proc isAbsolute(path: string): boolChecks whether a given
pathis absolute.On Windows, network paths are considered absolute too.
proc relativePath(path: string; base: string; sep: char): stringConverts
pathto a path relative tobase.The
sep(default: DirSep_) is used for the path normalizations, this can be useful to ensure the relative path only contains'/'so that it can be used for URL constructions.On Windows, if a root of
pathand a root ofbaseare different, returnspathas is because it is impossible to make a relative path. That means an absolute path can be returned.See also:
splitPath proc_parentDir proc_tailDir proc_
proc isRelativeTo(path: string; base: string): boolReturns true if
pathis relative tobase.proc parentDir(path: string): stringReturns the parent directory of
path.This is similar to
splitPath(path).headwhenpathdoesn't end in a dir separator, but also takes care of path normalizations. The remainder can be obtained withlastPathPart(path) proc_.See also:
relativePath proc_splitPath proc_tailDir proc_parentDirs iterator_
proc tailDir(path: string): stringReturns the tail part of
path.See also:
relativePath proc_splitPath proc_parentDir proc_
proc isRootDir(path: string): boolChecks whether a given
pathis a root directory.iterator parentDirs(path: string; fromRoot: bool; inclusive: bool): stringWalks over all parent directories of a given
path.If
fromRootis true (default: false), the traversal will start from the file system root directory. Ifinclusiveis true (default), the original argument will be included in the traversal.Relative paths won't be expanded by this iterator. Instead, it will traverse only the directories appearing in the relative path.
See also:
parentDir proc_
proc /../(head: string; tail: string): stringThe same as
parentDir(head) / tail, unless there is no parent directory. Thenhead / tailis performed instead.See also:
/ proc_parentDir proc_
func searchExtPos(path: string): int64Returns index of the
'.'char inpathif it signifies the beginning of the file extension. Returns -1 otherwise.See also:
splitFile proc_extractFilename proc_lastPathPart proc_changeFileExt proc_addFileExt proc_
proc splitFile(path: string): tuple[dir: string, name: string, ext: string]Splits a filename into
(dir, name, extension)tuple.dirdoes not end in DirSep_ unless it's/.extensionincludes the leading dot.If
pathhas no extension,extis the empty string. Ifpathhas no directory component,diris the empty string. Ifpathhas no filename component,nameandextare empty strings.See also:
searchExtPos proc_extractFilename proc_lastPathPart proc_changeFileExt proc_addFileExt proc_
proc extractFilename(path: string): stringExtracts the filename of a given
path.This is the same as
name & extfromsplitFile(path) proc_.See also:
searchExtPos proc_splitFile proc_lastPathPart proc_changeFileExt proc_addFileExt proc_
proc lastPathPart(path: string): stringLike
extractFilename proc_, but ignores trailing dir separator; aka:baseName:idx: in some other languages.See also:
searchExtPos proc_splitFile proc_extractFilename proc_changeFileExt proc_addFileExt proc_
proc changeFileExt(filename: string; ext: string): stringChanges the file extension to
ext.If the
filenamehas no extension,extwill be added. Ifext== "" then any extension is removed.Extshould be given without the leading'.', because some filesystems may use a different character. (Although I know of none such beast.)See also:
searchExtPos proc_splitFile proc_extractFilename proc_lastPathPart proc_addFileExt proc_
proc addFileExt(filename: string; ext: string): stringAdds the file extension
exttofilename, unlessfilenamealready has an extension.Extshould be given without the leading'.', because some filesystems may use a different character. (Although I know of none such beast.)See also:
searchExtPos proc_splitFile proc_extractFilename proc_lastPathPart proc_changeFileExt proc_
proc cmpPaths(pathA: string; pathB: string): int64Compares two paths.
On a case-sensitive filesystem this is done case-sensitively otherwise case-insensitively. Returns:
|
0if pathA == pathB|
< 0if pathA < pathB|
> 0if pathA > pathBproc unixToNativePath(path: string; drive: string): stringConverts an UNIX-like path to a native one.
On an UNIX system this does nothing. Else it converts
'/','.','..'to the appropriate things.On systems with a concept of "drives",
driveis used to determine which drive label to use during absolute path conversion.drivedefaults to the drive of the current working directory, and is ignored on systems that do not have a concept of "drives".proc getCurrentDir(): stringReturns the
current working directory:idx: i.e. where the built binary is run.So the path returned by this proc is determined at run time.
See also:
getHomeDir proc_getConfigDir proc_getTempDir proc_setCurrentDir proc_- currentSourcePath template
- getProjectPath proc
proc absolutePath(path: string; root: string): stringReturns the absolute path of
path, rooted atroot(which must be absolute; default: current directory). Ifpathis absolute, return it, ignoringroot.See also:
normalizedPath proc_normalizePath proc_
proc normalizePath(path: var string)Normalize a path.
Consecutive directory separators are collapsed, including an initial double slash.
On relative paths, double dot (
..) sequences are collapsed if possible. On absolute paths they are always collapsed... warning:: URL-encoded and Unicode attempts at directory traversal are not detected. Triple dot is not handled.
See also:
absolutePath proc_normalizedPath proc_ for outplace versionnormalizeExe proc_
proc normalizedPath(path: string): stringReturns a normalized path for the current OS.
See also:
absolutePath proc_normalizePath proc_ for the in-place version
proc normalizeExe(file: var string)on posix, prepends
./iffiledoesn't contain/and is not"", ".", "..".proc sameFile(path1: string; path2: string): boolReturns true if both pathname arguments refer to the same physical file or directory.
Raises
OSErrorif any of the files does not exist or information about it can not be obtained.This proc will return true if given two alternative hard-linked or sym-linked paths to the same file or directory.
See also:
sameFileContent proc_