@@ -867,6 +867,10 @@ end subroutine array_callback_func
867
867
! for indenting (Note: jsonlint.com uses 4 spaces)
868
868
integer (IK),parameter :: spaces_per_tab = 2
869
869
870
+ ! Variables for real string printing:
871
+
872
+ logical (LK) :: compact_real = .true. ! ! to use the "compact" form of real numbers for output
873
+
870
874
! find out the precision of the floating point number system
871
875
! and set safety factors
872
876
integer (IK),parameter :: rp_safety_factor = 1
@@ -881,25 +885,21 @@ end subroutine array_callback_func
881
885
real (max (maxexp,abs (maxexp)),&
882
886
kind= RK) ) )
883
887
884
- ! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
885
- integer (IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
886
- ! real format set by library initialization
887
- character (kind= CDK,len=* ),parameter :: int_fmt = ' (ss,I0)' ! minimum width format for integers
888
- character (kind= CK, len=* ),parameter :: star = ' *' ! for invalid numbers
889
-
890
- ! real string printing:
891
- character (kind= CDK,len= :),allocatable :: real_fmt ! the format string to use for real numbers
892
- ! [set in json_initialize]
893
- logical (LK) :: compact_real = .true. ! to use the "compact" form of real numbers for output
888
+ integer (IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
889
+ ! ! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
890
+ character (kind= CDK,len=* ),parameter :: int_fmt = ' (ss,I0)' ! ! minimum width format for integers
891
+ character (kind= CK, len=* ),parameter :: star = ' *' ! ! for invalid numbers
892
+ character (kind= CDK,len= :),allocatable :: real_fmt ! ! the format string to use for real numbers
893
+ ! ! it is set in [[json_initialize]]
894
894
895
895
!
896
896
! Note: the following global variables make this module non thread safe.
897
897
!
898
898
899
899
! exception handling [private variables]
900
- logical (LK) :: is_verbose = .false. ! if true, all exceptions are immediately printed to console
901
- logical (LK) :: exception_thrown = .false . ! the error flag
902
- character (kind= CK,len= :),allocatable :: err_message ! the error message
900
+ logical (LK) :: is_verbose = .false. ! ! if true, all exceptions are immediately printed to console
901
+ logical (LK) :: exception_thrown = .true . ! ! the error flag (by default, this is true to make sure that [[json_initialize]] is called.
902
+ character (kind= CK,len= :),allocatable :: err_message ! ! the error message
903
903
904
904
! temp vars used when parsing lines in file [private variables]
905
905
integer (IK) :: char_count = 0 ! character position in the current line
@@ -1642,59 +1642,71 @@ subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
1642
1642
1643
1643
implicit none
1644
1644
1645
- logical (LK),intent (in ),optional :: verbose ! ! mainly useful for debugging (default is false)
1646
- logical (LK),intent (in ),optional :: compact_reals ! ! to compact the real number strings for output
1647
- logical (LK),intent (in ),optional :: print_signs ! ! always print numeric sign (default is false)
1648
- character (len=* ,kind= CDK),intent (in ),optional :: real_format
1649
- ! ! exponential (default), scientific, engineering or general
1645
+ logical (LK),intent (in ),optional :: verbose ! ! mainly useful for debugging (default is false)
1646
+ logical (LK),intent (in ),optional :: compact_reals ! ! to compact the real number strings for output (default is true)
1647
+ logical (LK),intent (in ),optional :: print_signs ! ! always print numeric sign (default is false)
1648
+ character (len=* ,kind= CDK),intent (in ),optional :: real_format ! ! exponential (default), scientific, engineering or general
1650
1649
1651
1650
character (kind= CDK,len= 10 ) :: w,d,e
1652
1651
character (kind= CDK,len= 2 ) :: sgn, rl_edit_desc
1653
1652
integer (IK) :: istat
1654
1653
logical (LK) :: sgn_prnt
1655
1654
1656
-
1657
1655
! clear any errors from previous runs:
1658
1656
call json_clear_exceptions()
1659
1657
1660
- ! set defaults
1661
- sgn_prnt = .false.
1662
- if ( present ( print_signs) ) sgn_prnt = print_signs
1663
- if ( sgn_prnt ) then
1664
- sgn = ' sp'
1665
- else
1666
- sgn = ' ss'
1667
- end if
1658
+ ! Ensure gfortran bug work around "parameters" are set properly
1659
+ null_str = ' null'
1660
+ true_str = ' true'
1661
+ false_str = ' false'
1668
1662
1669
- rl_edit_desc = ' E'
1670
- if ( present ( real_format ) ) then
1671
- select case ( real_format )
1672
- case (' g' ,' G' ,' e' ,' E' ,' en' ,' EN' ,' es' ,' ES' )
1673
- rl_edit_desc = real_format
1674
- case default
1675
- call throw_exception(' Invalid real format, "' // trim (real_format) // ' ", passed to json_initialize.' // &
1676
- new_line(' a' ) // ' Acceptable formats are: "G", "E", "EN", and "ES".' )
1677
- end select
1678
- end if
1663
+ ! Just in case, clear these global variables also:
1664
+ pushed_index = 0
1665
+ pushed_char = ' '
1666
+ char_count = 0
1667
+ line_count = 1
1668
+ ipos = 1
1679
1669
1680
1670
# ifdef USE_UCS4
1681
1671
! reopen stdout and stderr with utf-8 encoding
1682
1672
open (output_unit,encoding= ' utf-8' )
1683
1673
open (error_unit, encoding= ' utf-8' )
1684
1674
# endif
1685
1675
1686
- ! Ensure gfortran bug work around "parameters" are set properly
1687
- null_str = ' null'
1688
- true_str = ' true'
1689
- false_str = ' false'
1676
+ ! verbose error printing:
1677
+ if (present (verbose)) is_verbose = verbose
1678
+
1679
+ ! Set the format for real numbers:
1680
+ ! [if not changing it, then it remains the same]
1681
+
1682
+ if ( (.not. allocated (real_fmt)) .or. & ! if this hasn't been done yet
1683
+ present (compact_reals) .or. &
1684
+ present (print_signs) .or. &
1685
+ present (real_format) ) then
1686
+
1687
+ if (present (compact_reals)) compact_real = compact_reals
1688
+
1689
+ ! set defaults
1690
+ sgn_prnt = .false.
1691
+ if ( present ( print_signs) ) sgn_prnt = print_signs
1692
+ if ( sgn_prnt ) then
1693
+ sgn = ' sp'
1694
+ else
1695
+ sgn = ' ss'
1696
+ end if
1690
1697
1691
- ! optional inputs (if not present, values remains unchanged):
1692
- if (present (verbose)) is_verbose = verbose
1693
- if (present (compact_reals)) compact_real = compact_reals
1698
+ rl_edit_desc = ' E'
1699
+ if ( present ( real_format ) ) then
1700
+ select case ( real_format )
1701
+ case (' g' ,' G' ,' e' ,' E' ,' en' ,' EN' ,' es' ,' ES' )
1702
+ rl_edit_desc = real_format
1703
+ case default
1704
+ call throw_exception(' Invalid real format, "' // trim (real_format) // ' ", passed to json_initialize.' // &
1705
+ new_line(' a' ) // ' Acceptable formats are: "G", "E", "EN", and "ES".' )
1706
+ end select
1707
+ end if
1694
1708
1695
- ! set the default output/input format for reals:
1696
- ! [this only needs to be done once, since it can't change]
1697
- if (.not. allocated (real_fmt)) then
1709
+ ! set the default output/input format for reals:
1698
1710
write (w,' (ss,I0)' ,iostat= istat) max_numeric_str_len
1699
1711
if (istat== 0 ) write (d,' (ss,I0)' ,iostat= istat) real_precision
1700
1712
if (istat== 0 ) write (e,' (ss,I0)' ,iostat= istat) real_exponent_digits
@@ -1703,14 +1715,8 @@ subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
1703
1715
else
1704
1716
real_fmt = ' (' // sgn // ' ,' // trim (rl_edit_desc) // ' 30.16E3)' ! just use this one (should never happen)
1705
1717
end if
1706
- end if
1707
1718
1708
- ! Just in case, clear these global variables also:
1709
- pushed_index = 0
1710
- pushed_char = ' '
1711
- char_count = 0
1712
- line_count = 1
1713
- ipos = 1
1719
+ end if
1714
1720
1715
1721
end subroutine json_initialize
1716
1722
! *****************************************************************************************
@@ -1815,7 +1821,7 @@ subroutine json_check_for_errors(status_ok, error_msg)
1815
1821
if (allocated (err_message)) then
1816
1822
error_msg = err_message
1817
1823
else
1818
- error_msg = ' Unknown Error'
1824
+ error_msg = ' Error: json_initialize() must be called first to initialize the module. '
1819
1825
end if
1820
1826
else
1821
1827
error_msg = ' '
0 commit comments