Skip to content

Commit

Permalink
Merge branch '367-string-to-real-integer-conversion' into 'development'
Browse files Browse the repository at this point in the history
simplified string conversion and related error handling

Closes #367

See merge request damask/DAMASK!880
  • Loading branch information
eisenlohr committed Dec 20, 2023
2 parents 4e3b9e6 + 8458ea5 commit 5c71238
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions src/IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -380,17 +380,11 @@ integer function IO_strAsInt(str)

character(len=*), intent(in) :: str !< string for conversion to int value

integer :: readStatus
character(len=*), parameter :: VALIDCHARS = '0123456789+- '
integer :: readStatus


valid: if (verify(str,VALIDCHARS) == 0) then
read(str,*,iostat=readStatus) IO_strAsInt
if (readStatus /= 0) call IO_error(111,str)
else valid
IO_strAsInt = 0
call IO_error(111,str)
end if valid
read(str,*,iostat=readStatus) IO_strAsInt
if (readStatus /= 0) call IO_error(111,'cannot represent "'//str//'" as integer')

end function IO_strAsInt

Expand All @@ -402,36 +396,31 @@ real(pREAL) function IO_strAsReal(str)

character(len=*), intent(in) :: str !< string for conversion to real value

integer :: readStatus
character(len=*), parameter :: VALIDCHARS = '0123456789eE.+- '
integer :: readStatus


valid: if (verify(str,VALIDCHARS) == 0) then
read(str,*,iostat=readStatus) IO_strAsReal
if (readStatus /= 0) call IO_error(112,str)
else valid
IO_strAsReal = 0.0_pREAL
call IO_error(112,str)
end if valid
read(str,*,iostat=readStatus) IO_strAsReal
if (readStatus /= 0) call IO_error(111,'cannot represent "'//str//'" as real')

end function IO_strAsReal


!--------------------------------------------------------------------------------------------------
!> @brief Return logical value from given string.
!> @details: 'True' and 'true' are converted to .true.
!> @details: 'False' and 'false' are converted to .false.
!--------------------------------------------------------------------------------------------------
logical function IO_strAsBool(str)

character(len=*), intent(in) :: str !< string for conversion to int value
character(len=*), intent(in) :: str !< string for conversion to boolean


if (trim(adjustl(str)) == 'True' .or. trim(adjustl(str)) == 'true') then
IO_strAsBool = .true.
elseif (trim(adjustl(str)) == 'False' .or. trim(adjustl(str)) == 'false') then
IO_strAsBool = .false.
else
IO_strAsBool = .false.
call IO_error(113,str)
call IO_error(111,'cannot represent "'//str//'" as boolean')
end if

end function IO_strAsBool
Expand Down Expand Up @@ -498,11 +487,7 @@ subroutine IO_error(error_ID,ext_msg,label1,ID1,label2,ID2)
case (110)
msg = 'invalid chunk selected'
case (111)
msg = 'invalid character for int:'
case (112)
msg = 'invalid character for real:'
case (113)
msg = 'invalid character for logical:'
msg = 'invalid string for conversion'
case (114)
msg = 'cannot decode base64 string:'

Expand Down

0 comments on commit 5c71238

Please sign in to comment.