1
+ ! > This module contains general routines for interacting with the file system
2
+ ! !
1
3
module fpm_filesystem
2
4
use ,intrinsic :: iso_fortran_env, only : stdin= >input_unit, stdout= >output_unit, stderr= >error_unit
3
5
use fpm_environment, only: get_os_type, &
@@ -15,6 +17,7 @@ module fpm_filesystem
15
17
contains
16
18
17
19
20
+ ! > return value of environment variable
18
21
subroutine env_variable (var , name )
19
22
character (len= :), allocatable , intent (out ) :: var
20
23
character (len=* ), intent (in ) :: name
@@ -36,9 +39,9 @@ subroutine env_variable(var, name)
36
39
end subroutine env_variable
37
40
38
41
42
+ ! > Extract filename from path with/without suffix
39
43
function basename (path ,suffix ) result (base)
40
- ! Extract filename from path with/without suffix
41
- !
44
+
42
45
character (* ), intent (In ) :: path
43
46
logical , intent (in ), optional :: suffix
44
47
character (:), allocatable :: base
@@ -71,13 +74,13 @@ function basename(path,suffix) result (base)
71
74
end function basename
72
75
73
76
77
+ ! > Canonicalize path for comparison
78
+ ! ! * Handles path string redundancies
79
+ ! ! * Does not test existence of path
80
+ ! !
81
+ ! ! To be replaced by realpath/_fullname in stdlib_os
82
+ ! !
74
83
function canon_path (path ) result(canon)
75
- ! Canonicalize path for comparison
76
- ! Handles path string redundancies
77
- ! Does not test existence of path
78
- !
79
- ! To be replaced by realpath/_fullname in stdlib_os
80
- !
81
84
character (* ), intent (in ) :: path
82
85
character (:), allocatable :: canon
83
86
@@ -141,9 +144,8 @@ function canon_path(path) result(canon)
141
144
end function canon_path
142
145
143
146
147
+ ! > Extract dirname from path
144
148
function dirname (path ) result (dir)
145
- ! Extract dirname from path
146
- !
147
149
character (* ), intent (in ) :: path
148
150
character (:), allocatable :: dir
149
151
@@ -152,6 +154,7 @@ function dirname(path) result (dir)
152
154
end function dirname
153
155
154
156
157
+ ! > test if a name matches an existing directory path
155
158
logical function is_dir (dir )
156
159
character (* ), intent (in ) :: dir
157
160
integer :: stat
@@ -171,9 +174,9 @@ logical function is_dir(dir)
171
174
end function is_dir
172
175
173
176
177
+ ! > Construct path by joining strings with os file separator
174
178
function join_path (a1 ,a2 ,a3 ,a4 ,a5 ) result(path)
175
- ! Construct path by joining strings with os file separator
176
- !
179
+
177
180
character (len=* ), intent (in ) :: a1, a2
178
181
character (len=* ), intent (in ), optional :: a3, a4, a5
179
182
character (len= :), allocatable :: path
@@ -209,8 +212,8 @@ function join_path(a1,a2,a3,a4,a5) result(path)
209
212
end function join_path
210
213
211
214
215
+ ! > Determine number or rows in a file given a LUN
212
216
integer function number_of_rows (s ) result(nrows)
213
- ! determine number or rows
214
217
integer ,intent (in ):: s
215
218
integer :: ios
216
219
character (len= 100 ) :: r
@@ -225,6 +228,7 @@ integer function number_of_rows(s) result(nrows)
225
228
end function number_of_rows
226
229
227
230
231
+ ! > read lines into an array of TYPE(STRING_T) variables
228
232
function read_lines (fh ) result(lines)
229
233
integer , intent (in ) :: fh
230
234
type (string_t), allocatable :: lines(:)
@@ -240,6 +244,7 @@ function read_lines(fh) result(lines)
240
244
241
245
end function read_lines
242
246
247
+ ! > Create a directory. Create subdirectories as needed
243
248
subroutine mkdir (dir )
244
249
character (len=* ), intent (in ) :: dir
245
250
integer :: stat
@@ -263,12 +268,12 @@ subroutine mkdir(dir)
263
268
end subroutine mkdir
264
269
265
270
271
+ ! > Get file & directory names in directory `dir`.
272
+ ! !
273
+ ! ! - File/directory names return are relative to cwd, ie. preprended with `dir`
274
+ ! ! - Includes files starting with `.` except current directory and parent directory
275
+ ! !
266
276
recursive subroutine list_files (dir , files , recurse )
267
- ! Get file & directory names in directory `dir`.
268
- !
269
- ! - File/directory names return are relative to cwd, ie. preprended with `dir`
270
- ! - Includes files starting with `.` except current directory and parent directory
271
- !
272
277
character (len=* ), intent (in ) :: dir
273
278
type (string_t), allocatable , intent (out ) :: files(:)
274
279
logical , intent (in ), optional :: recurse
@@ -329,18 +334,19 @@ recursive subroutine list_files(dir, files, recurse)
329
334
end subroutine list_files
330
335
331
336
337
+ ! > test if pathname already exists
332
338
logical function exists (filename ) result(r)
333
339
character (len=* ), intent (in ) :: filename
334
340
inquire (file= filename, exist= r)
335
341
end function
336
342
337
343
344
+ ! > Get a unused temporary filename
345
+ ! ! Calls posix 'tempnam' - not recommended, but
346
+ ! ! we have no security concerns for this application
347
+ ! ! and use here is temporary.
348
+ ! ! Works with MinGW
338
349
function get_temp_filename () result(tempfile)
339
- ! Get a unused temporary filename
340
- ! Calls posix 'tempnam' - not recommended, but
341
- ! we have no security concerns for this application
342
- ! and use here is temporary.
343
- ! Works with MinGW
344
350
!
345
351
use iso_c_binding, only: c_ptr, C_NULL_PTR, c_f_pointer
346
352
character (:), allocatable :: tempfile
@@ -374,9 +380,9 @@ end subroutine c_free
374
380
end function get_temp_filename
375
381
376
382
383
+ ! > Replace file system separators for windows
377
384
function windows_path (path ) result(winpath)
378
- ! Replace file system separators for windows
379
- !
385
+
380
386
character (* ), intent (in ) :: path
381
387
character (:), allocatable :: winpath
382
388
@@ -393,9 +399,9 @@ function windows_path(path) result(winpath)
393
399
end function windows_path
394
400
395
401
402
+ ! > Replace file system separators for unix
396
403
function unix_path (path ) result(nixpath)
397
- ! Replace file system separators for unix
398
- !
404
+
399
405
character (* ), intent (in ) :: path
400
406
character (:), allocatable :: nixpath
401
407
@@ -412,6 +418,7 @@ function unix_path(path) result(nixpath)
412
418
end function unix_path
413
419
414
420
421
+ ! > read a line of arbitrary length into a CHARACTER variable from the specified LUN
415
422
subroutine getline (unit , line , iostat , iomsg )
416
423
417
424
! > Formatted IO unit
@@ -453,6 +460,7 @@ subroutine getline(unit, line, iostat, iomsg)
453
460
end subroutine getline
454
461
455
462
463
+ ! > delete a file by filename
456
464
subroutine delete_file (file )
457
465
character (len=* ), intent (in ) :: file
458
466
logical :: exist
@@ -464,8 +472,8 @@ subroutine delete_file(file)
464
472
end if
465
473
end subroutine delete_file
466
474
467
- subroutine warnwrite (fname ,data )
468
475
! > write trimmed character data to a file if it does not exist
476
+ subroutine warnwrite (fname ,data )
469
477
character (len=* ),intent (in ) :: fname
470
478
character (len=* ),intent (in ) :: data (:)
471
479
@@ -478,8 +486,8 @@ subroutine warnwrite(fname,data)
478
486
479
487
end subroutine warnwrite
480
488
489
+ ! > procedure to open filename as a sequential "text" file
481
490
subroutine fileopen (filename ,lun ,ier )
482
- ! procedure to open filename as a sequential "text" file
483
491
484
492
character (len=* ),intent (in ) :: filename
485
493
integer ,intent (out ) :: lun
@@ -516,8 +524,8 @@ subroutine fileopen(filename,lun,ier)
516
524
517
525
end subroutine fileopen
518
526
527
+ ! > simple close of a LUN. On error show message and stop (by default)
519
528
subroutine fileclose (lun ,ier )
520
- ! simple close of a LUN. On error show message and stop (by default)
521
529
integer ,intent (in ) :: lun
522
530
integer ,intent (out ),optional :: ier
523
531
character (len= 256 ) :: message
@@ -535,8 +543,8 @@ subroutine fileclose(lun,ier)
535
543
endif
536
544
end subroutine fileclose
537
545
546
+ ! > procedure to write filedata to file filename
538
547
subroutine filewrite (filename ,filedata )
539
- ! procedure to write filedata to file filename
540
548
541
549
character (len=* ),intent (in ) :: filename
542
550
character (len=* ),intent (in ) :: filedata(:)
@@ -560,10 +568,10 @@ subroutine filewrite(filename,filedata)
560
568
561
569
end subroutine filewrite
562
570
571
+ ! > Returns string with special characters replaced with an underscore.
572
+ ! ! For now, only a hyphen is treated as a special character, but this can be
573
+ ! ! expanded to other characters if needed.
563
574
pure function to_fortran_name (string ) result(res)
564
- ! Returns string with special characters replaced with an underscore.
565
- ! For now, only a hyphen is treated as a special character, but this can be
566
- ! expanded to other characters if needed.
567
575
character (* ), intent (in ) :: string
568
576
character (len (string)) :: res
569
577
character , parameter :: SPECIAL_CHARACTERS(* ) = [' -' ]
0 commit comments