1
1
! SPDX-Identifier: MIT
2
2
3
- ! > Interaction with the filesystem.
3
+ !> Interaction with the filesystem.
4
4
module stdlib_io_filesystem
5
5
use stdlib_string_type, only: string_type
6
6
implicit none
7
7
private
8
8
9
9
public :: temp_dir, is_windows, exists, path_separator, list_dir, mkdir, rmdir, run
10
10
11
+ #: if OS == 'Windows'
12
+ logical, parameter :: is_windows = .true.
13
+ character, parameter :: path_separator = '\'
14
+ #: else
15
+ logical, parameter :: is_windows = .false.
16
+ character, parameter :: path_separator = '/'
17
+ #: endif
18
+
11
19
character(*), parameter :: temp_dir = 'temp'
12
20
13
21
contains
14
-
15
- ! > Version: experimental
16
- ! >
17
- ! > Whether the operating system is Windows.
18
- ! > [Specification](../page/specs/stdlib_io.html#is_windows)
19
- logical function is_windows ()
20
- character (len= 255 ) :: value
21
- integer :: length, stat
22
-
23
- call get_environment_variable(' OSTYPE' , value, length, stat)
24
- if (stat == 0 .and. length > 0 .and. (index (value, ' win' ) > 0 .or. index (value, ' msys' ) > 0 )) then
25
- is_windows = .true. ; return
26
- end if
27
-
28
- call get_environment_variable(' OS' , value, length, stat)
29
- if (stat == 0 .and. length > 0 .and. index (value, ' Windows_NT' ) > 0 ) then
30
- is_windows = .true. ; return
31
- end if
32
-
33
- is_windows = .false.
34
- end function
35
-
36
- ! > Version: experimental
37
- ! >
38
- ! > Returns the path separator for the current operating system.
39
- ! > [Specification](../page/specs/stdlib_io.html#path_separator)
40
- character function path_separator ()
41
- if (is_windows()) then
42
- path_separator = ' \'
43
- else
44
- path_separator = ' /'
45
- end if
46
- end function
47
-
48
22
!> Version: experimental
49
23
!>
50
24
!> Whether a file or directory exists at the given path.
@@ -89,9 +63,9 @@ subroutine list_dir(dir, files, iostat, iomsg)
89
63
end if
90
64
end if
91
65
92
- listed_contents = temp_dir// path_separator() // ' listed_contents.txt'
66
+ listed_contents = temp_dir//path_separator//'listed_contents.txt'
93
67
94
- if (is_windows() ) then
68
+ if (is_windows) then
95
69
call run('dir /b '//dir//' > '//listed_contents, stat)
96
70
else
97
71
call run('ls '//dir//' > '//listed_contents, stat)
@@ -127,7 +101,7 @@ subroutine mkdir(dir, iostat, iomsg)
127
101
integer, optional, intent(out) :: iostat
128
102
character(len=:), allocatable, optional, intent(out) :: iomsg
129
103
130
- if (is_windows() ) then
104
+ if (is_windows) then
131
105
call run('mkdir '//dir, iostat, iomsg)
132
106
else
133
107
call run('mkdir -p '//dir, iostat, iomsg)
@@ -141,7 +115,7 @@ subroutine mkdir(dir, iostat, iomsg)
141
115
subroutine rmdir(dir)
142
116
character(len=*), intent(in) :: dir
143
117
144
- if (is_windows() ) then
118
+ if (is_windows) then
145
119
call run('rmdir /s/q '//dir)
146
120
else
147
121
call run('rm -rf '//dir)
0 commit comments