@@ -2855,22 +2855,20 @@ subroutine json_parse(file, p)
2855
2855
character (len=* ),intent (in ) :: file
2856
2856
type (json_value),pointer :: p
2857
2857
2858
- integer :: iunit
2859
- integer :: istat
2860
-
2861
- character (len= 256 ) :: line
2858
+ integer :: iunit, istat
2859
+ character (len= :),allocatable :: line
2862
2860
2863
2861
! clean any exceptions and initialize:
2864
2862
call json_initialize()
2865
2863
2866
2864
! open the file
2867
- open ( newunit = iunit, &
2868
- file = file, &
2869
- status = ' OLD' , &
2870
- action = ' READ' , &
2871
- form = ' FORMATTED' , &
2872
- position = ' REWIND' , &
2873
- iostat = istat)
2865
+ open ( newunit = iunit, &
2866
+ file = file, &
2867
+ status = ' OLD' , &
2868
+ action = ' READ' , &
2869
+ form = ' FORMATTED' , &
2870
+ position = ' REWIND' , &
2871
+ iostat = istat)
2874
2872
2875
2873
if (istat== 0 ) then
2876
2874
@@ -2888,10 +2886,10 @@ subroutine json_parse(file, p)
2888
2886
! can print the line where the error occurred:
2889
2887
!
2890
2888
if (exception_thrown) then
2891
- backspace (iunit)
2892
- read (iunit, fmt= ' (A256)' ,iostat= istat) line
2889
+ call get_current_line_from_file(iunit,line)
2893
2890
if (istat== 0 ) err_message = err_message// new_line(' ' )// &
2894
- ' Error in line: ' // trim (line)
2891
+ ' Offending line: ' // trim (line)
2892
+ if (allocated (line)) deallocate (line)
2895
2893
end if
2896
2894
2897
2895
! close the file
@@ -2909,6 +2907,57 @@ subroutine json_parse(file, p)
2909
2907
end subroutine json_parse
2910
2908
! ********************************************************************************
2911
2909
2910
+ ! ********************************************************************************
2911
+ subroutine get_current_line_from_file (iunit ,line )
2912
+ ! ********************************************************************************
2913
+ ! ****f* json_module/get_current_line_from_file
2914
+ !
2915
+ ! NAME
2916
+ ! get_current_line_from_file
2917
+ !
2918
+ ! DESCRIPTION
2919
+ ! Rewind the file to the beginning of the current line, and return this line.
2920
+ !
2921
+ ! AUTHOR
2922
+ ! Jacob Williams
2923
+ !
2924
+ ! ********************************************************************************
2925
+
2926
+ implicit none
2927
+
2928
+ integer ,intent (in ) :: iunit
2929
+ character (len= :),allocatable ,intent (out ) :: line
2930
+
2931
+ integer ,parameter :: n_chunk = 256 ! chunk size [arbitrary]
2932
+ character (len=* ),parameter :: nfmt = ' (A256)' ! corresponding format statement
2933
+
2934
+ character (len= n_chunk) :: chunk
2935
+ integer :: istat,isize
2936
+
2937
+ ! initialize:
2938
+ line = ' '
2939
+
2940
+ ! rewind to beginning of the current record:
2941
+ backspace (iunit, iostat= istat)
2942
+
2943
+ ! loop to read in all the characters in the current record.
2944
+ ! [the line is read in chunks until the end of the line is reached]
2945
+ if (istat== 0 ) then
2946
+ do
2947
+ read (iunit,fmt= nfmt,advance= ' NO' ,size= isize,iostat= istat) chunk
2948
+ if (istat== 0 ) then
2949
+ line = line// chunk
2950
+ else
2951
+ if (isize> 0 ) line = line// chunk(1 :isize)
2952
+ exit
2953
+ end if
2954
+ end do
2955
+ end if
2956
+
2957
+ ! ********************************************************************************
2958
+ end subroutine get_current_line_from_file
2959
+ ! ********************************************************************************
2960
+
2912
2961
! ********************************************************************************
2913
2962
recursive subroutine parse_value (unit , value )
2914
2963
! ********************************************************************************
0 commit comments