@@ -2917,28 +2917,52 @@ end subroutine json_get_array
29172917!
29182918! SOURCE
29192919
2920- subroutine json_parse (file , p )
2920+ subroutine json_parse (file , p , unit )
29212921
29222922 implicit none
29232923
29242924 character (len=* ),intent (in ) :: file
29252925 type (json_value),pointer :: p
2926+ integer ,intent (in ),optional :: unit
29262927
29272928 integer :: iunit, istat
29282929 character (len= :),allocatable :: line, arrow_str
29292930 character (len= 10 ) :: line_str, char_str
2931+ logical :: is_open
29302932
29312933 ! clean any exceptions and initialize:
29322934 call json_initialize()
29332935
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
29422966
29432967 if (istat== 0 ) then
29442968
@@ -3576,6 +3600,7 @@ subroutine parse_string(unit, string)
35763600 if (.not. exception_thrown) then
35773601
35783602 string = ' ' ! initialize string
3603+ last = ' ' !
35793604
35803605 do
35813606 c = pop_char(unit, eof = eof, skip_ws = .false. )
@@ -3930,6 +3955,54 @@ subroutine real_to_string(rval,str)
39303955 end subroutine real_to_string
39313956! ********************************************************************************
39323957
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+
39334006! ***********************************************************************************************************************************
39344007 end module json_module
39354008! ***********************************************************************************************************************************
0 commit comments