@@ -4,56 +4,50 @@ module pathlib
44
55implicit none
66private
7- public :: mkdir, copyfile, expanduser, home
7+ public :: mkdir, copyfile, expanduser, home, get_suffix, filesep_swap, assert_directory_exists, assert_file_exists
8+
9+ interface ! pathlib_{unix,windows}.f90
10+ module integer function copyfile(source, dest) result(istat)
11+ character (* ), intent (in ) :: source, dest
12+ end function copyfile
13+
14+ module integer function mkdir(path) result(istat)
15+ character (* ), intent (in ) :: path
16+ end function mkdir
17+ end interface
18+
19+ interface ! path_exists*.f90
20+ module subroutine assert_directory_exists (path )
21+ character (* ), intent (in ) :: path
22+ end subroutine assert_directory_exists
23+ end interface
824
925contains
1026
11- integer function copyfile (source , dest ) result(istat)
12- ! ! overwrites existing file in destination
13- character (* ), intent (in ) :: source, dest
14- character (len (source)) :: src
15- character (len (dest)) :: dst
16- logical :: exists
17- integer :: icstat
18-
19- #if defined(_WIN32)
20- ! ! https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy
21- character (* ), parameter :: CMD= ' copy /y '
22- src = filesep_swap(source)
23- dst = filesep_swap(dest)
24- #else
25- character (* ), parameter :: CMD= ' cp -r '
26- src = source
27- dst = dest
28- #endif
29-
30- call execute_command_line(CMD// src// ' ' // dst, exitstat= istat, cmdstat= icstat)
31- if (istat == 0 .and. icstat /= 0 ) istat = icstat
32- if (istat /= 0 ) write (stderr,* ) ' ERROR: ' // CMD// src// ' ' // dst
27+ pure function get_suffix (filename )
28+ character (* ), intent (in ) :: filename
29+ character (:), allocatable :: get_suffix
30+
31+ get_suffix = filename(index (filename, ' .' , back= .true. ) : len (filename))
32+
33+ end function get_suffix
3334
34- end function copyfile
3535
36+ subroutine assert_file_exists (path )
37+ ! ! throw error if file does not exist
38+ ! ! this accomodates non-Fortran 2018 error stop with variable character
3639
37- integer function mkdir (path ) result(istat)
38- ! ! create a directory, with parents if needed
3940character (* ), intent (in ) :: path
40- character (len (path)) :: p
41- integer :: icstat
42-
43- #if defined(_WIN32)
44- ! ! https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/md
45- character (* ), parameter :: CMD= ' mkdir '
46- p = filesep_swap(path)
47- #else
48- character (* ), parameter :: CMD= ' mkdir -p '
49- p = path
50- #endif
51-
52- call execute_command_line(CMD // p, exitstat= istat, cmdstat= icstat)
53- if (istat == 0 .and. icstat /= 0 ) istat = icstat
54- if (istat /= 0 ) write (stderr,* ) ' ERROR: ' // CMD// p
41+ logical :: exists
5542
56- end function mkdir
43+ inquire (file= path, exist= exists)
44+
45+ if (exists) return
46+
47+ write (stderr,' (A)' ) ' ERROR: file does not exist ' // path
48+ error stop
49+
50+ end subroutine assert_file_exists
5751
5852
5953function filesep_swap (path ) result(swapped)
@@ -105,20 +99,18 @@ end function expanduser
10599
106100function home ()
107101! ! https://en.wikipedia.org/wiki/Home_directory#Default_home_directory_per_operating_system
108- character (:), allocatable :: home, var
102+ character (:), allocatable :: home
109103character (256 ) :: buf
110104integer :: L, istat
111105
112- #if defined(_WIN32)
113- var = " USERPROFILE"
114- #else
115- var = " HOME"
116- #endif
106+ call get_environment_variable(" HOME" , buf, length= L, status= istat)
107+ if (L== 0 .or. istat /= 0 ) then
108+ call get_environment_variable(" USERPROFILE" , buf, length= L, status= istat)
109+ endif
117110
118- call get_environment_variable(var, buf, length= L, status= istat)
119111if (L== 0 .or. istat /= 0 ) then
120- write (stderr,* ) ' ERROR: could not determine home directory from env var ' ,var
121- if (istat== 1 ) write (stderr,* ) ' env var ' ,var, ' does not exist.'
112+ write (stderr,* ) ' ERROR: could not determine home directory from env variable '
113+ if (istat== 1 ) write (stderr,* ) ' env variable does not exist.'
122114 home = " "
123115else
124116 home = trim (buf) // ' /'
0 commit comments