Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CK,len=*), | intent(in) | :: | str |
Convert a string into an integer.
function string_to_integer(str) result(ival)
implicit none
character(kind=CK,len=*),intent(in) :: str
integer(IK) :: ival
character(kind=CDK,len=:),allocatable :: digits
integer(IK) :: ndigits_digits,ndigits,ierr
if (.not. 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 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