Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
logical(kind=LK), | intent(in), | optional | :: | verbose | mainly useful for debugging (default is false) |
|
logical(kind=LK), | intent(in), | optional | :: | compact_reals | to compact the real number strings for output (default is true) |
|
logical(kind=LK), | intent(in), | optional | :: | print_signs | always print numeric sign (default is false) |
|
character(kind=len=*,CDK), | intent(in), | optional | :: | real_format | exponential (default), scientific, engineering or general |
Initialize the JSON-Fortran module. The routine must be called before any of the routines are used. It can also be called after using the module and encountering exceptions.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(kind=CDK,len=10), | public | :: | w | ||||
character(kind=CDK,len=10), | public | :: | d | ||||
character(kind=CDK,len=10), | public | :: | e | ||||
character(kind=CDK,len=2), | public | :: | sgn | ||||
character(kind=CDK,len=2), | public | :: | rl_edit_desc | ||||
integer(kind=IK), | public | :: | istat | ||||
logical(kind=LK), | public | :: | sgn_prnt |
subroutine json_initialize(verbose,compact_reals,print_signs,real_format)
implicit none
logical(LK),intent(in),optional :: verbose !! mainly useful for debugging (default is false)
logical(LK),intent(in),optional :: compact_reals !! to compact the real number strings for output (default is true)
logical(LK),intent(in),optional :: print_signs !! always print numeric sign (default is false)
character(len=*,kind=CDK),intent(in),optional :: real_format !! exponential (default), scientific, engineering or general
character(kind=CDK,len=10) :: w,d,e
character(kind=CDK,len=2) :: sgn, rl_edit_desc
integer(IK) :: istat
logical(LK) :: sgn_prnt
!clear any errors from previous runs:
call json_clear_exceptions()
!Ensure gfortran bug work around "parameters" are set properly
null_str = 'null'
true_str = 'true'
false_str = 'false'
!Just in case, clear these global variables also:
pushed_index = 0
pushed_char = ''
char_count = 0
line_count = 1
ipos = 1
# ifdef USE_UCS4
! reopen stdout and stderr with utf-8 encoding
open(output_unit,encoding='utf-8')
open(error_unit, encoding='utf-8')
# endif
!verbose error printing:
if (present(verbose)) is_verbose = verbose
!Set the format for real numbers:
! [if not changing it, then it remains the same]
if ( (.not. allocated(real_fmt)) .or. & ! if this hasn't been done yet
present(compact_reals) .or. &
present(print_signs) .or. &
present(real_format) ) then
!allow the special case where real format is '*':
! [this overrides the other options]
if (present(real_format)) then
if (real_format==star) then
compact_real = .false.
real_fmt = star
return
end if
end if
if (present(compact_reals)) compact_real = compact_reals
!set defaults
sgn_prnt = .false.
if ( present( print_signs) ) sgn_prnt = print_signs
if ( sgn_prnt ) then
sgn = 'sp'
else
sgn = 'ss'
end if
rl_edit_desc = 'E'
if ( present( real_format ) ) then
select case ( real_format )
case ('g','G','e','E','en','EN','es','ES')
rl_edit_desc = real_format
case default
call throw_exception('Invalid real format, "' // trim(real_format) // '", passed to json_initialize.'// &
new_line('a') // 'Acceptable formats are: "G", "E", "EN", and "ES".' )
end select
end if
! set the default output/input format for reals:
write(w,'(ss,I0)',iostat=istat) max_numeric_str_len
if (istat==0) write(d,'(ss,I0)',iostat=istat) real_precision
if (istat==0) write(e,'(ss,I0)',iostat=istat) real_exponent_digits
if (istat==0) then
real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) // trim(w) // '.' // trim(d) // 'E' // trim(e) // ')'
else
real_fmt = '(' // sgn // ',' // trim(rl_edit_desc) // '30.16E3)' !just use this one (should never happen)
end if
end if
end subroutine json_initialize