Skip to content

Commit 5a67bbf

Browse files
committed
added support for any length line when printing the line if an error is encountered when parsing the file.
build.sh does a clean build.
1 parent 02d89fa commit 5a67bbf

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

build.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/bin/sh
22

33
#
4-
# Build the json library and example program on Linux using ifort
4+
# This is just a simple script to
5+
# build the json-fortran library and
6+
# example program on Linux.
57
#
68
# Jacob Williams : 2/8/2014
79
#
@@ -35,6 +37,8 @@ ARCHIVERFLAGS='-cq'
3537
FEXT='.f90'
3638
OBJEXT='.o'
3739
LIBEXT='.a'
40+
MODEXT='.mod'
41+
WC='*'
3842

3943
LIBOUT='libjson'
4044
EXEOUT='json'
@@ -46,6 +50,11 @@ EXAMPLECODE='json_example'
4650
mkdir -p $BUILDDIR
4751
mkdir -p $BINDIR
4852

53+
#clean build:
54+
rm -f $BUILDDIR$WC$OBJEXT
55+
rm -f $BUILDDIR$WC$MODEXT
56+
rm -f $BUILDDIR$WC$LIBEXT
57+
4958
#build library:
5059
$FCOMPILER $FCOMPILERFLAGS -c $SRCDIR$MODCODE$FEXT $FCMODULEPATHFLAG$BUILDDIR
5160
mv $MODCODE$OBJEXT $BUILDDIR

src/json_example.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ subroutine print_error_message()
605605

606606
!print it if there is one:
607607
if (.not. status_ok) then
608-
write(*,*) error_msg
608+
write(*,'(A)') error_msg
609609
deallocate(error_msg)
610610
call json_clear_exceptions()
611611
end if

src/json_module.f90

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,22 +2855,20 @@ subroutine json_parse(file, p)
28552855
character(len=*),intent(in) :: file
28562856
type(json_value),pointer :: p
28572857

2858-
integer :: iunit
2859-
integer :: istat
2860-
2861-
character(len=256) :: line
2858+
integer :: iunit, istat
2859+
character(len=:),allocatable :: line
28622860

28632861
!clean any exceptions and initialize:
28642862
call json_initialize()
28652863

28662864
! 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)
28742872

28752873
if (istat==0) then
28762874

@@ -2888,10 +2886,10 @@ subroutine json_parse(file, p)
28882886
! can print the line where the error occurred:
28892887
!
28902888
if (exception_thrown) then
2891-
backspace(iunit)
2892-
read(iunit, fmt='(A256)',iostat=istat) line
2889+
call get_current_line_from_file(iunit,line)
28932890
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)
28952893
end if
28962894

28972895
! close the file
@@ -2909,6 +2907,57 @@ subroutine json_parse(file, p)
29092907
end subroutine json_parse
29102908
!********************************************************************************
29112909

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+
29122961
!********************************************************************************
29132962
recursive subroutine parse_value(unit, value)
29142963
!********************************************************************************

0 commit comments

Comments
 (0)