forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(io): Add
exists
function for checking file or directory existence
- Loading branch information
Showing
8 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
ADD_EXAMPLE(exists) | ||
ADD_EXAMPLE(fmt_constants) | ||
#ADD_EXAMPLE(getline) | ||
ADD_EXAMPLE(loadnpy) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
program example_exists | ||
|
||
use stdlib_io_path, only: exists | ||
implicit none | ||
integer :: fh | ||
|
||
open (newunit=fh, file='example_exists.txt') | ||
print *, exists('example_exists.txt') ! .true. | ||
close (fh, status="delete") | ||
|
||
print *, exists('example_exists.txt') ! .false. | ||
print *, exists('./doc/specs/') ! Check if directory exists | ||
|
||
end program example_exists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
!> This module implements some useful functions on pathnames. | ||
!> To read or write files see [open()](../page/specs/stdlib_io.html#open), | ||
!> and for accessing the filesystem see the [os](../page/specs/stdlib_io.html) module. | ||
!> ([Specification](../page/specs/stdlib_io.html)) | ||
module stdlib_io_path | ||
|
||
implicit none | ||
private | ||
|
||
public :: exists | ||
|
||
contains | ||
|
||
!> Version: experimental | ||
!> | ||
!> Check if a file or directory exists | ||
!> ([Specification](../page/specs/stdlib_io.html#exists)) | ||
logical function exists(path) | ||
character(len=*), intent(in) :: path | ||
|
||
inquire (file=path, exist=exists) | ||
#ifdef __INTEL_COMPILER | ||
if (.not. exists) inquire (directory=file, exist=exists) | ||
#endif | ||
|
||
end function exists | ||
|
||
end module stdlib_io_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module test_path | ||
|
||
use stdlib_io_path, only: exists | ||
use testdrive, only: new_unittest, unittest_type, error_type, check | ||
implicit none | ||
private | ||
|
||
public :: collect_path | ||
|
||
contains | ||
|
||
!> Collect all exported unit tests | ||
subroutine collect_path(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
new_unittest("path_exists", test_path_exists) & | ||
] | ||
|
||
end subroutine collect_path | ||
|
||
subroutine test_path_exists(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
integer :: fh | ||
|
||
open (newunit=fh, file='test_path_exists.txt') | ||
call check(error, exists('test_path_exists.txt')) | ||
close (fh, status="delete") | ||
|
||
call check(error,.not. exists('test_path_exists.txt.not')) | ||
|
||
end subroutine test_path_exists | ||
|
||
end module test_path | ||
|
||
program tester | ||
|
||
use, intrinsic :: iso_fortran_env, only: error_unit | ||
use testdrive, only: run_testsuite, new_testsuite, testsuite_type | ||
use test_path, only: collect_path | ||
implicit none | ||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("path", collect_path) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write (error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write (error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
|
||
end program tester |