splitpath(3f) - [M_io:PATHNAMES] split a Unix pathname into components (LICENSE:PD)
Synopsis
Description
Options
Results
Example
Author
License
subroutine splitpath(path,dir,name,basename,ext)
integer,parameter :: maxlen=4096 character(len=maxlen),intent(in) :: path character(len=maxlen),intent(out),optional :: dir character(len=maxlen),intent(out),optional :: name character(len=maxlen),intent(out),optional :: basename character(len=maxlen),intent(out),optional :: ext
splitpath(3f) splits given pathname assuming a forward slash separates filename components and that the right-most period in the last leaf of the pathname is considered the beginning of an extension. If an extension is found it is left present in NAME but removed from BASENAME.This routine does not check the system for the existence or type of the filename components; it merely parses a string.
Assumes leaf separator is a slash or backslash as determined by separator(3f) and that filename does not contain trailing spaces.
Using these rules helps to reduce incorrect parsing, but the routine is only intended for simple parsing of names of the form "[dir/]name[.extension].
path Path to be broken into components. It is assumed
o Forward slashes (/) separate pathname components. o the name . means "current directory" o the name .. means "up one directory" o a pathname ending in a slash is a directory name o a slash starting the pathname represents the root directory. o trailing spaces are insignificant.
The path parameter can be a complete or partial file specification. The special name "." is assumed to mean the current directory, and the special name ".." is assumed to mean one directory above the current directory.
dir Path of directories, including the trailing slash. name Name of file leaf or, if no file is specified in path, name of the lowest directory. basename NAME with any extension removed ext File name extension, if any, including the leading period (.).
program demo_splitpath
use m_io, only : splitpath implicit none integer,parameter :: maxlen=4096 character(len=maxlen),parameter :: file(*)=[& & dirs/name.ext , & & xx/IO/zz/NN.FF , & & xx/IO/zz/NN , & & /xx/IO/zz/NN , & & /xx/IO/zz/ , & & /xx/IO/zz.A/ , & & /xx/IO/zz/. , & & , & & ./ , & & / , & & /.. , & & ./.. , & & name. , & & .name , & & .name. , & & . , & & .. , & & ... ]end program demo_splitpathcharacter(len=maxlen) :: dir character(len=maxlen) :: name character(len=maxlen) :: basename character(len=maxlen) :: ext integer :: i integer :: longest longest=maxval(len_trim(file)) ! find longest filename
do i=1,size(file) call splitpath(file(i), dir, name, basename, ext) write(*,(*("| ",a:))) & & file(i)(:longest), & & dir(:longest), & & name(:longest), & & basename(:longest), & & ext(:longest) enddo
Output
| dirs/name.ext | dirs | name.ext | name | .ext | xx/IO/zz/NN.FF| xx/IO/zz | NN.FF | NN | .FF | xx/IO/zz/NN | xx/IO/zz | NN | NN | | /xx/IO/zz/NN | /xx/IO/zz | NN | NN | | /xx/IO/zz/ | /xx/IO/zz | | | | /xx/IO/zz.A/ | /xx/IO/zz.A | | | | /xx/IO/zz/. | /xx/IO/zz/. | | | | | . | | | | ./ | . | | | | / | / | | | | /.. | / | | | | ./.. | ./.. | | | | name. | | name. | name | . | .name | | .name | .name | | .name. | | .name. | .name | . | . | . | | | | .. | | | | | ... | | ... | .. | .
John S. Urban
Public Domain
Nemo Release 3.1 | splitpath (3m_io) | October 25, 2024 |