Skip to content

Commit d6ae60f

Browse files
Merge pull request #607 from jacobwilliams/606-close-if-open
Draft: added a close_unit_if_open to json_parse_file
2 parents 840eb7e + be91cdc commit d6ae60f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/json_value_module.F90

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9749,20 +9749,29 @@ end subroutine json_prepare_parser
97499749
!@note When calling this routine, any exceptions thrown from previous
97509750
! calls will automatically be cleared.
97519751

9752-
subroutine json_parse_file(json, file, p, unit)
9752+
subroutine json_parse_file(json, file, p, unit, close_unit_if_open)
97539753

97549754
implicit none
97559755

97569756
class(json_core),intent(inout) :: json
97579757
character(kind=CDK,len=*),intent(in) :: file !! JSON file name
97589758
type(json_value),pointer :: p !! output structure
97599759
integer(IK),intent(in),optional :: unit !! file unit number (/= 0)
9760+
logical(LK),intent(in),optional :: close_unit_if_open !! if true, close unit if it was already open [defaults to true]
97609761

97619762
integer(IK) :: iunit !! file unit actually used
97629763
integer(IK) :: istat !! iostat flag
97639764
logical(LK) :: is_open !! if the file is already open
97649765
logical(LK) :: has_duplicate !! if checking for duplicate keys
97659766
character(kind=CK,len=:),allocatable :: path !! path to any duplicate key
9767+
logical(LK) :: unit_was_open !! track if unit was already open
9768+
logical(LK) :: close_if_open !! local copy of `close_unit_if_open`
9769+
9770+
if (present(close_unit_if_open)) then
9771+
close_if_open = close_unit_if_open
9772+
else
9773+
close_if_open = .true. ! default (original) behavior
9774+
end if
97669775

97679776
! clear any exceptions and initialize:
97689777
call json%initialize()
@@ -9780,7 +9789,8 @@ subroutine json_parse_file(json, file, p, unit)
97809789
! check to see if the file is already open
97819790
! if it is, then use it, otherwise open the file with the name given.
97829791
inquire(unit=iunit, opened=is_open, iostat=istat)
9783-
if (istat==0 .and. .not. is_open) then
9792+
unit_was_open = (istat==0 .and. is_open)
9793+
if (.not. unit_was_open) then
97849794
! open the file
97859795
open ( unit = iunit, &
97869796
file = file, &
@@ -9790,6 +9800,7 @@ subroutine json_parse_file(json, file, p, unit)
97909800
access = access_spec, &
97919801
iostat = istat &
97929802
FILE_ENCODING )
9803+
close_if_open = .true. ! we opened it, so we should close it later
97939804
else
97949805
! if the file is already open, then we need to make sure
97959806
! that it is open with the correct form/access/etc...
@@ -9806,6 +9817,7 @@ subroutine json_parse_file(json, file, p, unit)
98069817
access = access_spec, &
98079818
iostat = istat &
98089819
FILE_ENCODING )
9820+
close_if_open = .true. ! we opened it, so we should close it later
98099821

98109822
end if
98119823

@@ -9842,8 +9854,13 @@ subroutine json_parse_file(json, file, p, unit)
98429854
end if
98439855
end if
98449856

9845-
! close the file:
9846-
close(unit=iunit, iostat=istat)
9857+
! close the file only if we opened it, or if user specified to close it
9858+
if (.not. unit_was_open .or. close_if_open) then
9859+
close(unit=iunit, iostat=istat)
9860+
if (istat /= 0 .and. .not. json%exception_thrown) then
9861+
call json%throw_exception('Error closing file')
9862+
end if
9863+
end if
98479864

98489865
else
98499866

0 commit comments

Comments
 (0)