@@ -40,6 +40,25 @@ function basename(path,suffix) result (base)
40
40
end function basename
41
41
42
42
43
+ logical function is_dir (dir )
44
+ character (* ), intent (in ) :: dir
45
+ integer :: stat
46
+
47
+ select case (get_os_type())
48
+
49
+ case (OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_CYGWIN, OS_SOLARIS, OS_FREEBSD)
50
+ call execute_command_line(" test -d " // dir , exitstat= stat)
51
+
52
+ case (OS_WINDOWS)
53
+ call execute_command_line(' cmd /c "if not exist ' // windows_path(dir) // ' \ exit /B 1"' , exitstat= stat)
54
+
55
+ end select
56
+
57
+ is_dir = (stat == 0 )
58
+
59
+ end function is_dir
60
+
61
+
43
62
function join_path (a1 ,a2 ,a3 ,a4 ,a5 ) result(path)
44
63
! Construct path by joining strings with os file separator
45
64
!
@@ -130,11 +149,15 @@ subroutine mkdir(dir)
130
149
end subroutine mkdir
131
150
132
151
133
- subroutine list_files (dir , files )
134
- character (len=* ), intent (in ) :: dir
152
+ recursive subroutine list_files (dir , files , recurse )
153
+ character (len=* ), intent (in ) :: dir
135
154
type (string_t), allocatable , intent (out ) :: files(:)
136
- character (len= :), allocatable :: temp_file
137
- integer :: stat, fh
155
+ logical , intent (in ), optional :: recurse
156
+
157
+ integer :: stat, fh, i
158
+ character (:), allocatable :: temp_file
159
+ type (string_t), allocatable :: dir_files(:)
160
+ type (string_t), allocatable :: sub_dir_files(:)
138
161
139
162
! Using `inquire` / exists on directories works with gfortran, but not ifort
140
163
if (.not. exists(dir)) then
@@ -160,7 +183,31 @@ subroutine list_files(dir, files)
160
183
161
184
open (newunit= fh, file= temp_file, status= ' old' )
162
185
files = read_lines(fh)
163
- close (fh, status= ' delete' )
186
+ close (fh,status= " delete" )
187
+
188
+ do i= 1 ,size (files)
189
+ files(i)% s = join_path(dir,files(i)% s)
190
+ end do
191
+
192
+ if (present (recurse)) then
193
+ if (recurse) then
194
+
195
+ allocate (sub_dir_files(0 ))
196
+
197
+ do i= 1 ,size (files)
198
+ if (is_dir(files(i)% s)) then
199
+
200
+ call list_files(files(i)% s, dir_files, recurse= .true. )
201
+ sub_dir_files = [sub_dir_files, dir_files]
202
+
203
+ end if
204
+ end do
205
+
206
+ files = [files, sub_dir_files]
207
+
208
+ end if
209
+ end if
210
+
164
211
end subroutine list_files
165
212
166
213
0 commit comments