Convert a string into an integer.
Replacement for the parse_integer function in the original code.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(json_core), | intent(inout) | :: | json | |||
character(kind=CK,len=*), | intent(in) | :: | str |
function string_to_integer(json,str) result(ival)
implicit none
class(json_core),intent(inout) :: json
character(kind=CK,len=*),intent(in) :: str
integer(IK) :: ival
character(kind=CDK,len=:),allocatable :: digits
integer(IK) :: ndigits_digits,ndigits,ierr
if (.not. json%exception_thrown) then
! Compute how many digits we need to read
ndigits = 2*len_trim(str)
ndigits_digits = floor(log10(real(ndigits)))+1
allocate(character(kind=CDK,len=ndigits_digits) :: digits)
write(digits,'(I0)') ndigits !gfortran will have a runtime error with * edit descriptor here
! gfortran bug: '*' edit descriptor for ISO_10646 strings does bad stuff.
read(str,'(I'//trim(digits)//')',iostat=ierr) ival !string to integer
if (ierr/=0) then !if there was an error
ival = 0
call json%throw_exception('Error in string_to_integer: '//&
'string cannot be converted to an integer: '//&
trim(str))
end if
else
ival = 0
end if
end function string_to_integer