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