49 use,
intrinsic :: iso_c_binding
57 character,
parameter :: separator =
'\'
58 character(*),
parameter :: alphabet =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
60 character,
parameter :: separator =
'/'
65 function getcwd_c(buf, size) bind(C, name='_getcwd')
result(r)
70 character(kind=c_char),
intent(out) :: buf(*)
71 integer(kind=c_size_t),
value :: size
76 function getcwd_c(buf, size) bind(C, name='getcwd')
result(r)
81 character(kind=c_char),
intent(out) :: buf(*)
82 integer(kind=c_size_t),
value :: size
88 integer(c_int) function chdir_c(path) bind(C, name='chdir')
92 character(kind=c_char),
intent(in) :: path(*)
103 module procedure :: join_character_character
104 module procedure :: join_string_character
105 module procedure :: join_character_string
106 module procedure :: join_string_string
129 character(*),
intent(in) :: filepath
131 if (
len(filepath) < 2)
then
135 res = scan(filepath(1:1), alphabet) /= 0 .and. filepath(2:2) ==
':'
137 res = filepath(1:1) == separator
152 character(*),
intent(in) :: filepath
156 length =
len(filepath)
158 res = (length >= 1 .and. filepath(1:1) == separator) .or.
is_absolute(filepath)
160 res = (length > 0 .and. filepath(1:1) == separator)
180 pure function filename(filepath, keepext)
result(res)
181 character(*),
intent(in) :: filepath
182 character(:),
allocatable :: res
183 logical,
intent(in),
optional :: keepext
185 integer :: ipoint, islash
187 ipoint =
index(filepath,
'.', back=.true.)
188 islash =
index(filepath, separator, back=.true.)
189 if (ipoint < islash) ipoint =
len_trim(filepath) + 1
190 if (
present(keepext))
then
192 res = filepath(islash + 1:
len_trim(filepath))
194 res = filepath(islash + 1: ipoint - 1)
197 res = filepath(islash + 1: ipoint - 1)
209 pure function join_character_character(path1, path2)
result(res)
210 character(*),
intent(in) :: path1
211 character(*),
intent(in) :: path2
212 character(:),
allocatable :: res
214 character(:),
allocatable :: temp
216 temp =
trim(adjustl(path1))
217 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
219 res = temp // separator //
trim(adjustl(path2))
230 pure function join_character_string(path1, path2)
result(res)
231 character(*),
intent(in) :: path1
232 type(
string),
intent(in) :: path2
233 character(:),
allocatable :: res
235 character(:),
allocatable :: temp
237 temp =
trim(adjustl(path1))
238 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
240 res = temp // separator //
trim(adjustl(path2%chars))
251 pure function join_string_character(path1, path2)
result(res)
252 type(
string),
intent(in) :: path1
253 character(*),
intent(in) :: path2
254 character(:),
allocatable :: res
256 character(:),
allocatable :: temp
258 temp =
trim(adjustl(path1%chars))
259 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
261 res = temp // separator //
trim(adjustl(path2))
272 pure function join_string_string(path1, path2)
result(res)
273 type(
string),
intent(in) :: path1
274 type(
string),
intent(in) :: path2
275 character(:),
allocatable :: res
277 character(:),
allocatable :: temp
279 temp =
trim(adjustl(path1%chars))
280 if (temp(
len(temp):
len(temp)) == separator) temp =
trim(temp(:
len(temp) - 1))
282 res = temp // separator //
trim(adjustl(path2%chars))
296 character(*),
intent(in) :: filepath
297 character(:),
allocatable :: res
299 character(:),
allocatable :: temp
315 character(*),
intent(in) :: filepath
316 character(:),
allocatable :: res
318 character(:),
allocatable :: temp
331 character(*),
intent(in) :: filepath
332 character(:),
allocatable,
intent(out) ::
head
333 character(:),
allocatable,
intent(out) ::
tail
335 character(:),
allocatable :: temp
336 integer :: i, ipoint, isep
345 temp =
trim(adjustl(filepath))
346 if (temp(
len(temp):
len(temp)) == separator)
then
347 temp =
trim(temp(:
len(temp) - 1))
349 ipoint =
index(filepath,
'.', back=.true.)
350 isep =
index(filepath, separator, back=.true.)
351 if (ipoint > isep .and. isep > 0)
then
352 temp =
trim(temp(:isep - 1))
362 i =
len(temp) -
index(temp, separator, back=.true.) + 1
366 head = temp // separator
374 if (
index(temp, separator, back=.true.) == 0)
then
378 tail = temp(
len(temp) - i + 2:)
393 function cwd()
result(res)
394 character(:),
allocatable :: res
396 character(len=1, kind=c_char) :: buf(256)
398 integer(c_size_t) :: s
400 s =
size(buf, kind=c_size_t)
401 if (c_associated(getcwd_c(buf, s)))
then
402 n = findloc(buf, achar(0), 1)
403 allocate(
character(n - 1) :: res)
424 character(*),
intent(in) :: path
425 integer,
optional,
intent(out) :: err
428 loc_err = chdir_c(path // c_null_char)
430 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...