A modern, portable Fortran module for path manipulation and basic directory operations. This module provides a clean interface for working with file system paths in a platform-independent way. It correctly handles both Unix ('/') and Windows ('\') path separators through conditional compilation and offers deferred-length character results for maximum flexibility.
The module builds upon the fpx_string module for string type support and provides overloads of key procedures to accept either intrinsic character(*) or type(string) arguments.
Features include:
On Windows:
Data Types | |
| interface | join |
| Join path components using the platform separator. More... | |
| subroutine, public chdir | ( | character(*), intent(in) | path, |
| integer, intent(out), optional | err ) |
Changes the current working directory. This is a thin wrapper around the underlying C runtime chdir() implementation.
| [in] | path | Directory to change to |
| [out] | err | Optional integer error code (0 = success, non-zero = failure) integer :: ierr
call chdir('/tmp', ierr)
if (ierr /= 0) stop 'Failed to change directory'
|
| character(:) function, allocatable, public cwd |
Returns the current working directory as a deferred-length character string. Returns an empty string if the current directory cannot be determined.
| pure logical function, public is_absolute | ( | character(*), intent(in) | filepath | ) |
Returns .true. if the path is absolute. On Unix a path is absolute when it starts with '/'. On Windows a path is absolute when it starts with a drive letter followed by ':\C:\', 'd:/temp').
| [in] | filepath | Path to test |
| pure logical function, public is_rooted | ( | character(*), intent(in) | filepath | ) |
Returns .true. if the path is rooted (starts with a separator) or is absolute. A rooted path begins with the platform separator ('\' on Windows, '/' elsewhere) even if it is not a full absolute path (e.g. '/temp' on Linux).
| [in] | filepath | Path to test |
| pure character(:) function, allocatable join_character_character | ( | character(*), intent(in) | path1, |
| character(*), intent(in) | path2 ) |
Implementation of join for character arguments.
The generic interface accepts any combination of intrinsic character(*) and string arguments.
Supported overloads:
Examples
| pure character(:) function, allocatable join_character_string | ( | character(*), intent(in) | path1, |
| type(string), intent(in) | path2 ) |
Implementation of join for character arguments.
The generic interface accepts any combination of intrinsic character(*) and string arguments.
Supported overloads:
Examples
| pure character(:) function, allocatable join_string_character | ( | type(string), intent(in) | path1, |
| character(*), intent(in) | path2 ) |
Implementation of join for character arguments.
The generic interface accepts any combination of intrinsic character(*) and string arguments.
Supported overloads:
Examples
| pure character(:) function, allocatable join_string_string | ( | type(string), intent(in) | path1, |
| type(string), intent(in) | path2 ) |
Implementation of join for character arguments.
The generic interface accepts any combination of intrinsic character(*) and string arguments.
Supported overloads:
Examples
| pure subroutine, public split_path | ( | character(*), intent(in) | filepath, |
| character(:), intent(out), allocatable | head, | ||
| character(:), intent(out), allocatable | tail ) |
Splits a path into head (directory) and tail (basename) components. Special cases: