@@ -2917,28 +2917,52 @@ end subroutine json_get_array
2917
2917
!
2918
2918
! SOURCE
2919
2919
2920
- subroutine json_parse (file , p )
2920
+ subroutine json_parse (file , p , unit )
2921
2921
2922
2922
implicit none
2923
2923
2924
2924
character (len=* ),intent (in ) :: file
2925
2925
type (json_value),pointer :: p
2926
+ integer ,intent (in ),optional :: unit
2926
2927
2927
2928
integer :: iunit, istat
2928
2929
character (len= :),allocatable :: line, arrow_str
2929
2930
character (len= 10 ) :: line_str, char_str
2931
+ logical :: is_open
2930
2932
2931
2933
! clean any exceptions and initialize:
2932
2934
call json_initialize()
2933
2935
2934
- ! open the file
2935
- open ( newunit = iunit, &
2936
- file = file, &
2937
- status = ' OLD' , &
2938
- action = ' READ' , &
2939
- form = ' FORMATTED' , &
2940
- position = ' REWIND' , &
2941
- iostat = istat)
2936
+ if (present (unit)) then
2937
+
2938
+ iunit = unit
2939
+
2940
+ ! check to see if the file is already open
2941
+ ! if it is, then use it, otherwise open the file.
2942
+ inquire (unit= iunit, opened= is_open, iostat= istat)
2943
+ if (istat== 0 .and. .not. is_open) then
2944
+ ! open the file
2945
+ open ( unit = iunit, &
2946
+ file = file, &
2947
+ status = ' OLD' , &
2948
+ action = ' READ' , &
2949
+ form = ' FORMATTED' , &
2950
+ position = ' REWIND' , &
2951
+ iostat = istat)
2952
+ end if
2953
+
2954
+ else
2955
+
2956
+ ! open the file with a new unit number:
2957
+ open ( newunit = iunit, &
2958
+ file = file, &
2959
+ status = ' OLD' , &
2960
+ action = ' READ' , &
2961
+ form = ' FORMATTED' , &
2962
+ position = ' REWIND' , &
2963
+ iostat = istat)
2964
+
2965
+ end if
2942
2966
2943
2967
if (istat== 0 ) then
2944
2968
@@ -3576,6 +3600,7 @@ subroutine parse_string(unit, string)
3576
3600
if (.not. exception_thrown) then
3577
3601
3578
3602
string = ' ' ! initialize string
3603
+ last = ' ' !
3579
3604
3580
3605
do
3581
3606
c = pop_char(unit, eof = eof, skip_ws = .false. )
@@ -3930,6 +3955,54 @@ subroutine real_to_string(rval,str)
3930
3955
end subroutine real_to_string
3931
3956
! ********************************************************************************
3932
3957
3958
+ ! ********************************************************************************
3959
+ ! ****f* json_module/valid_json_hex
3960
+ !
3961
+ ! NAME
3962
+ ! valid_json_hex
3963
+ !
3964
+ ! DESCRIPTION
3965
+ ! Returns true if the string is a valid 4-digit hex string.
3966
+ !
3967
+ ! EXAMPLE
3968
+ ! valid_json_hex('0000') !returns true
3969
+ ! valid_json_hex('ABC4') !returns true
3970
+ ! valid_json_hex('AB') !returns false (< 4 characters)
3971
+ ! valid_json_hex('WXYZ') !returns false (invalid characters)
3972
+ !
3973
+ ! AUTHOR
3974
+ ! Jacob Williams : 6/14/2014
3975
+ !
3976
+ ! SOURCE
3977
+
3978
+ function valid_json_hex (str ) result(valid)
3979
+
3980
+ implicit none
3981
+
3982
+ character (len=* ),intent (in ) :: str
3983
+ logical :: valid
3984
+
3985
+ integer :: n,i
3986
+
3987
+ ! an array of the valid hex characters:
3988
+ character (len= 1 ),dimension (16 ),parameter :: valid_chars = &
3989
+ [' 0' ,' 1' ,' 2' ,' 3' ,' 4' ,' 5' ,' 6' ,' 7' ,' 8' ,' 9' ,' A' ,' B' ,' C' ,' D' ,' E' ,' F' ]
3990
+
3991
+ ! initialize
3992
+ valid = .false.
3993
+
3994
+ ! check all the characters in the string:
3995
+ n = len (str)
3996
+ if (n== 4 ) then
3997
+ do i= 1 ,n
3998
+ if (.not. any (str(i:i)==valid_chars)) return
3999
+ end do
4000
+ valid = .true. ! all are in the set, so it is OK
4001
+ end if
4002
+
4003
+ end function valid_json_hex
4004
+ ! ********************************************************************************
4005
+
3933
4006
! ***********************************************************************************************************************************
3934
4007
end module json_module
3935
4008
! ***********************************************************************************************************************************
0 commit comments