49 use,
intrinsic :: iso_c_binding
56 character,
parameter :: separator =
'\'
57 character(*),
parameter :: alphabet =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
59 character,
parameter :: separator =
'/'
64 function getcwd_c(buf, size) bind(C, name='_getcwd')
result(r)
69 character(kind=c_char),
intent(out) :: buf(*)
70 integer(kind=c_size_t),
value :: size
75 function getcwd_c(buf, size) bind(C, name='getcwd')
result(r)
80 character(kind=c_char),
intent(out) :: buf(*)
81 integer(kind=c_size_t),
value :: size
87 integer(c_int) function chdir_c(path) bind(C, name='chdir')
91 character(kind=c_char),
intent(in) :: path(*)
102 module procedure :: join_character_character
103 module procedure :: join_string_character
104 module procedure :: join_character_string
105 module procedure :: join_string_string
128 character(*),
intent(in) :: filepath
130 if (
len(filepath) < 2)
then
134 res = scan(filepath(1:1), alphabet) /= 0 .and. filepath(2:2) ==
':'
136 res = filepath(1:1) == separator
151 character(*),
intent(in) :: filepath
155 length =
len(filepath)
157 res = (length >= 1 .and. filepath(1:1) == separator) .or.
is_absolute(filepath)
159 res = (length > 0 .and. filepath(1:1) == separator)
179 pure function filename(filepath, keepext)
result(res)
180 character(*),
intent(in) :: filepath
181 character(:),
allocatable :: res
182 logical,
intent(in),
optional :: keepext
184 integer :: ipoint, islash
186 ipoint = index(filepath,
'.', back=.true.)
187 islash = index(filepath, separator, back=.true.)
188 if (ipoint < islash) ipoint =
len_trim(filepath) + 1
189 if (
present(keepext))
then
191 res = filepath(islash + 1:
len_trim(filepath))
193 res = filepath(islash + 1: ipoint - 1)
196 res = filepath(islash + 1: ipoint - 1)
208 pure function join_character_character(path1, path2)
result(res)
209 character(*),
intent(in) :: path1
210 character(*),
intent(in) :: path2
211 character(:),
allocatable :: res
213 character(:),
allocatable :: temp
215 temp =
trim(adjustl(path1))
216 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
218 res = temp // separator //
trim(adjustl(path2))
229 pure function join_character_string(path1, path2)
result(res)
230 character(*),
intent(in) :: path1
231 type(
string),
intent(in) :: path2
232 character(:),
allocatable :: res
234 character(:),
allocatable :: temp
236 temp =
trim(adjustl(path1))
237 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
239 res = temp // separator //
trim(adjustl(path2%chars))
250 pure function join_string_character(path1, path2)
result(res)
251 type(
string),
intent(in) :: path1
252 character(*),
intent(in) :: path2
253 character(:),
allocatable :: res
255 character(:),
allocatable :: temp
257 temp =
trim(adjustl(path1%chars))
258 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
260 res = temp // separator //
trim(adjustl(path2))
271 pure function join_string_string(path1, path2)
result(res)
272 type(
string),
intent(in) :: path1
273 type(
string),
intent(in) :: path2
274 character(:),
allocatable :: res
276 character(:),
allocatable :: temp
278 temp =
trim(adjustl(path1%chars))
279 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
281 res = temp // separator //
trim(adjustl(path2%chars))
295 character(*),
intent(in) :: filepath
296 character(:),
allocatable :: res
298 character(:),
allocatable :: temp
314 character(*),
intent(in) :: filepath
315 character(:),
allocatable :: res
317 character(:),
allocatable :: temp
330 character(*),
intent(in) :: filepath
331 character(:),
allocatable,
intent(out) ::
head
332 character(:),
allocatable,
intent(out) ::
tail
334 character(:),
allocatable :: temp
335 integer :: i, ipoint, isep
344 temp =
trim(adjustl(filepath))
345 if (temp(
len(temp):
len(temp)) == separator)
then
346 temp =
trim(temp(:
len(temp) - 1))
348 ipoint = index(filepath,
'.', back=.true.)
349 isep = index(filepath, separator, back=.true.)
350 if (ipoint > isep .and. isep > 0)
then
351 temp =
trim(temp(:isep - 1))
361 i =
len(temp) - index(temp, separator, back=.true.) + 1
365 head = temp // separator
373 if (index(temp, separator, back=.true.) == 0)
then
377 tail = temp(
len(temp) - i + 2:)
392 function cwd()
result(res)
393 character(:),
allocatable :: res
395 character(len=1, kind=c_char) :: buf(256)
397 integer(c_size_t) :: s
399 s =
size(buf, kind=c_size_t)
400 if (c_associated(getcwd_c(buf, s)))
then
401 n = findloc(buf, achar(0), 1)
402 allocate(
character(n - 1) :: res)
423 character(*),
intent(in) :: path
424 integer,
optional,
intent(out) :: err
427 loc_err = chdir_c(path // c_null_char)
429 if (
present(err)) err = loc_err
subroutine chdir(path, err)
Changes the current working directory.
pure character(:) function, allocatable dirname(filepath)
Returns the base name (filename) part of a path.
pure subroutine split_path(filepath, head, tail)
Splits a path into head (directory) and tail (basename) components.
pure logical function is_rooted(filepath)
Returns .true. if the path is rooted (starts with a separator) or is absolute. A rooted path begins w...
pure character(:) function, allocatable filename(filepath, keepext)
Extracts the filename part of a path. By default the extension is stripped. If keepext=....
pure character(:) function, allocatable dirpath(filepath)
Returns the directory part of a path (everything before the last separator).
character(:) function, allocatable cwd()
Returns the current working directory as a deferred-length character string. Returns empty string on ...
pure logical function is_absolute(filepath)
Returns .true. if the path is absolute. On Unix a path is absolute when it starts with '/'....
character function, public tail(str)
Returns the last non-blank character of a string.
character function, public head(str)
Returns the first non-blank character of a string.
Generic interface for joining two path components Supports all combinations of character and string a...
Return the trimmed length of a string.
Return the length of a string.
Return the trimmed string.
Represents text as a sequence of ASCII code units. The derived type wraps an allocatable character ar...