50 use,
intrinsic :: iso_c_binding
71 character,
parameter :: separator =
'\'
72 character(*),
parameter :: alphabet =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
80 character,
parameter :: separator =
'/'
85 function getcwd_c(buf, size) bind(C, name='_getcwd')
result(r)
90 character(kind=c_char),
intent(out) :: buf(*)
91 integer(kind=c_size_t),
value :: size
96 function getcwd_c(buf, size) bind(C, name='getcwd')
result(r)
101 character(kind=c_char),
intent(out) :: buf(*)
102 integer(kind=c_size_t),
value :: size
108 integer(c_int) function chdir_c(path) bind(C, name='chdir')
112 character(kind=c_char),
intent(in) :: path(*)
167 character(*),
intent(in) :: filepath
169 if (
len(filepath) < 2)
then
173 res = scan(filepath(1:1), alphabet) /= 0 .and. filepath(2:2) ==
':'
175 res = filepath(1:1) == separator
189 character(*),
intent(in) :: filepath
193 length =
len(filepath)
195 res = (length >= 1 .and. filepath(1:1) == separator) .or.
is_absolute(filepath)
197 res = (length > 0 .and. filepath(1:1) == separator)
220 pure function filename(filepath, keepext)
result(res)
221 character(*),
intent(in) :: filepath
222 character(:),
allocatable :: res
223 logical,
intent(in),
optional :: keepext
225 integer :: ipoint, islash
227 ipoint =
index(filepath,
'.', back=.true.)
228 islash =
index(filepath, separator, back=.true.)
229 if (ipoint < islash) ipoint =
len_trim(filepath) + 1
230 if (
present(keepext))
then
232 res = filepath(islash + 1:
len_trim(filepath))
234 res = filepath(islash + 1: ipoint - 1)
237 res = filepath(islash + 1: ipoint - 1)
247 character(*),
intent(in) :: path1
248 character(*),
intent(in) :: path2
249 character(:),
allocatable :: res
251 character(:),
allocatable :: temp
253 temp =
trim(adjustl(path1))
254 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
256 res = temp // separator //
trim(adjustl(path2))
265 character(*),
intent(in) :: path1
266 type(
string),
intent(in) :: path2
267 character(:),
allocatable :: res
269 character(:),
allocatable :: temp
271 temp =
trim(adjustl(path1))
272 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
274 res = temp // separator //
trim(adjustl(path2%chars))
283 type(
string),
intent(in) :: path1
284 character(*),
intent(in) :: path2
285 character(:),
allocatable :: res
287 character(:),
allocatable :: temp
289 temp =
trim(adjustl(path1%chars))
290 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
292 res = temp // separator //
trim(adjustl(path2))
301 type(
string),
intent(in) :: path1
302 type(
string),
intent(in) :: path2
303 character(:),
allocatable :: res
305 character(:),
allocatable :: temp
307 temp =
trim(adjustl(path1%chars))
308 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
310 res = temp // separator //
trim(adjustl(path2%chars))
324 pure function dirpath(filepath)
result(res)
325 character(*),
intent(in) :: filepath
326 character(:),
allocatable :: res
328 character(:),
allocatable :: temp
344 pure function dirname(filepath)
result(res)
345 character(*),
intent(in) :: filepath
346 character(:),
allocatable :: res
348 character(:),
allocatable :: temp
364 character(*),
intent(in) :: filepath
365 character(:),
allocatable,
intent(out) ::
head
366 character(:),
allocatable,
intent(out) ::
tail
368 character(:),
allocatable :: temp
369 integer :: i, ipoint, isep
378 temp =
trim(adjustl(filepath))
379 if (temp(
len(temp):
len(temp)) == separator)
then
380 temp =
trim(temp(:
len(temp) - 1))
382 ipoint =
index(filepath,
'.', back=.true.)
383 isep =
index(filepath, separator, back=.true.)
384 if (ipoint > isep .and. isep > 0)
then
385 temp =
trim(temp(:isep - 1))
395 i =
len(temp) -
index(temp, separator, back=.true.) + 1
399 head = temp // separator
407 if (
index(temp, separator, back=.true.) == 0)
then
411 tail = temp(
len(temp) - i + 2:)
425 function cwd()
result(res)
426 character(:),
allocatable :: res
428 character(len=1, kind=c_char) :: buf(256)
430 integer(c_size_t) :: s
432 s =
size(buf, kind=c_size_t)
433 if (c_associated(getcwd_c(buf, s)))
then
434 n = findloc(buf, achar(0), 1)
435 allocate(
character(n - 1) :: res)
457 character(*),
intent(in) :: path
458 integer,
optional,
intent(out) :: err
461 loc_err = chdir_c(path // c_null_char)
463 if (
present(err)) err = loc_err
pure character(:) function, allocatable join_string_string(path1, path2)
Implementation of join for character arguments.
pure logical function, public is_rooted(filepath)
Returns .true. if the path is rooted (starts with a separator) or is absolute. A rooted path begins w...
character(:) function, allocatable, public cwd()
Returns the current working directory as a deferred-length character string. Returns an empty string ...
pure logical function, public is_absolute(filepath)
Returns .true. if the path is absolute. On Unix a path is absolute when it starts with '/'....
subroutine, public chdir(path, err)
Changes the current working directory. This is a thin wrapper around the underlying C runtime chdir()...
pure character(:) function, allocatable join_character_string(path1, path2)
Implementation of join for character arguments.
pure character(:) function, allocatable join_string_character(path1, path2)
Implementation of join for character arguments.
pure subroutine, public split_path(filepath, head, tail)
Splits a path into head (directory) and tail (basename) components. Special cases:
pure character(:) function, allocatable join_character_character(path1, path2)
Implementation of join for character arguments.
character function, public tail(str)
Returns the last non-blank character of a string.
character function, public head(str)
Returns the first character of the trimmed string.
Join path components using the platform separator.
Locate the position of a substring.
Return the trimmed length of a string object.
Return the length of a string object.
Remove trailing blanks from a string object.
Represents text as a sequence of ASCII code units. The derived type wraps an allocatable character ar...