Loading...
Searching...
No Matches
Path

Definition

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:

  • Detection of absolute and rooted paths
  • Cross-platform path joining
  • Extraction of directory and filename components
  • Splitting paths into head/tail elements
  • Retrieval and modification of the current working directory
  • Support for both intrinsic CHARACTER and type(string) arguments
  • Changing the current working directory (chdir)
Note
All path-returning functions return allocatable deferred-length characters.
The public generic join interface works with any combination of character and string.

Examples

character(:), allocatable :: p1, p2, full
p1 = '/home/user/docs'
p2 = 'report.pdf'
full = join(p1, p2) ! => '/home/user/docs/report.pdf'
print *, is_absolute(full) ! .true. (on Unix)
print *, filename(full) ! 'report'
print *, filename(full,.true.) ! 'report.pdf'
print *, dirpath(full) ! '/home/user/docs'

On Windows:

character(:), allocatable :: p
p = join('C:\Users', 'Alice', 'Documents')
! p == 'C:\Users\Alice\Documents'
print *, is_absolute(p) ! .true.

Data Types

interface  join
 Join path components using the platform separator. More...

Methods

◆ chdir()

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.

Parameters
[in]pathDirectory to change to
[out]errOptional integer error code (0 = success, non-zero = failure)
integer :: ierr
call chdir('/tmp', ierr)
if (ierr /= 0) stop 'Failed to change directory'

Definition at line 456 of file path.f90.

◆ cwd()

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.

Returns
res Current working directory
character(:), allocatable :: here
here = cwd()
print *, 'We are in: ', here

Definition at line 425 of file path.f90.

◆ is_absolute()

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').

Parameters
[in]filepathPath to test
Returns
res .true. if filepath is absolute
print *, is_absolute('/home/user') ! .true. (Unix)
print *, is_absolute('C:\\Temp') ! .true. (Windows)
print *, is_absolute('docs/..') ! .false.

Definition at line 166 of file path.f90.

◆ is_rooted()

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

Parameters
[in]filepathPath to test
Returns
res .true. if filepath is rooted

Definition at line 188 of file path.f90.

◆ join_character_character()

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:

  • join(character, character)
  • join(character, string)
  • join(string, character)
  • join(string, string)

Examples

character(:), allocatable :: p
p = join('/usr','bin')
! '/usr/bin'
p = join(string('/usr'),'local')
! '/usr/local'

Definition at line 246 of file path.f90.

◆ join_character_string()

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:

  • join(character, character)
  • join(character, string)
  • join(string, character)
  • join(string, string)

Examples

character(:), allocatable :: p
p = join('/usr','bin')
! '/usr/bin'
p = join(string('/usr'),'local')
! '/usr/local'

Definition at line 264 of file path.f90.

◆ join_string_character()

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:

  • join(character, character)
  • join(character, string)
  • join(string, character)
  • join(string, string)

Examples

character(:), allocatable :: p
p = join('/usr','bin')
! '/usr/bin'
p = join(string('/usr'),'local')
! '/usr/local'

Definition at line 282 of file path.f90.

◆ join_string_string()

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:

  • join(character, character)
  • join(character, string)
  • join(string, character)
  • join(string, string)

Examples

character(:), allocatable :: p
p = join('/usr','bin')
! '/usr/bin'
p = join(string('/usr'),'local')
! '/usr/local'

Definition at line 300 of file path.f90.

◆ split_path()

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:

  • Empty paths return ('.','')
  • Root directories return ('/','')
  • Trailing separators are ignored
    Parameters
    [in]filepathInput path
    [out]headDirectory part (includes trailing separator when appropriate)
    [out]tailBase name part

Definition at line 363 of file path.f90.