@@ -7,7 +7,7 @@ module json_module
7
7
! json_module
8
8
!
9
9
! DESCRIPTION
10
- ! JSON-FORTRAN: A Fortran 2003/ 2008 JSON (JavaScript Object Notation) API.
10
+ ! JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.
11
11
!
12
12
! NOTES
13
13
! -Based on fson by Joseph A. Levin (see LICENSE below)
@@ -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,58 @@ 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
+ ! The file is assumed to be opened.
2921
+ !
2922
+ ! AUTHOR
2923
+ ! Jacob Williams
2924
+ !
2925
+ ! ********************************************************************************
2926
+
2927
+ implicit none
2928
+
2929
+ integer ,intent (in ) :: iunit
2930
+ character (len= :),allocatable ,intent (out ) :: line
2931
+
2932
+ integer ,parameter :: n_chunk = 256 ! chunk size [arbitrary]
2933
+ character (len=* ),parameter :: nfmt = ' (A256)' ! corresponding format statement
2934
+
2935
+ character (len= n_chunk) :: chunk
2936
+ integer :: istat,isize
2937
+
2938
+ ! initialize:
2939
+ line = ' '
2940
+
2941
+ ! rewind to beginning of the current record:
2942
+ backspace (iunit, iostat= istat)
2943
+
2944
+ ! loop to read in all the characters in the current record.
2945
+ ! [the line is read in chunks until the end of the line is reached]
2946
+ if (istat== 0 ) then
2947
+ do
2948
+ read (iunit,fmt= nfmt,advance= ' NO' ,size= isize,iostat= istat) chunk
2949
+ if (istat== 0 ) then
2950
+ line = line// chunk
2951
+ else
2952
+ if (isize> 0 ) line = line// chunk(1 :isize)
2953
+ exit
2954
+ end if
2955
+ end do
2956
+ end if
2957
+
2958
+ ! ********************************************************************************
2959
+ end subroutine get_current_line_from_file
2960
+ ! ********************************************************************************
2961
+
2912
2962
! ********************************************************************************
2913
2963
recursive subroutine parse_value (unit , value )
2914
2964
! ********************************************************************************
0 commit comments