simplified string conversion and related error handling

checking for valid characters is not sufficient because signs and
exponents are allowed to appear only at specific locations. Since
Fortrans internal file read will anyways complain about invalid
characters, the check doesn't even have a value.

reduced number of error codes by grouping all conversion related errors
This commit is contained in:
Martin Diehl 2023-12-20 06:30:58 +01:00
parent 4e3b9e6586
commit 8458ea5ecf
No known key found for this signature in database
GPG Key ID: 1FD50837275A0A9B
1 changed files with 11 additions and 26 deletions

View File

@ -380,17 +380,11 @@ integer function IO_strAsInt(str)
character(len=*), intent(in) :: str !< string for conversion to int value character(len=*), intent(in) :: str !< string for conversion to int value
integer :: readStatus integer :: readStatus
character(len=*), parameter :: VALIDCHARS = '0123456789+- '
valid: if (verify(str,VALIDCHARS) == 0) then read(str,*,iostat=readStatus) IO_strAsInt
read(str,*,iostat=readStatus) IO_strAsInt if (readStatus /= 0) call IO_error(111,'cannot represent "'//str//'" as integer')
if (readStatus /= 0) call IO_error(111,str)
else valid
IO_strAsInt = 0
call IO_error(111,str)
end if valid
end function IO_strAsInt end function IO_strAsInt
@ -402,27 +396,23 @@ real(pREAL) function IO_strAsReal(str)
character(len=*), intent(in) :: str !< string for conversion to real value character(len=*), intent(in) :: str !< string for conversion to real value
integer :: readStatus integer :: readStatus
character(len=*), parameter :: VALIDCHARS = '0123456789eE.+- '
valid: if (verify(str,VALIDCHARS) == 0) then read(str,*,iostat=readStatus) IO_strAsReal
read(str,*,iostat=readStatus) IO_strAsReal if (readStatus /= 0) call IO_error(111,'cannot represent "'//str//'" as real')
if (readStatus /= 0) call IO_error(112,str)
else valid
IO_strAsReal = 0.0_pREAL
call IO_error(112,str)
end if valid
end function IO_strAsReal end function IO_strAsReal
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief Return logical value from given string. !> @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) 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 if (trim(adjustl(str)) == 'True' .or. trim(adjustl(str)) == 'true') then
@ -430,8 +420,7 @@ logical function IO_strAsBool(str)
elseif (trim(adjustl(str)) == 'False' .or. trim(adjustl(str)) == 'false') then elseif (trim(adjustl(str)) == 'False' .or. trim(adjustl(str)) == 'false') then
IO_strAsBool = .false. IO_strAsBool = .false.
else else
IO_strAsBool = .false. call IO_error(111,'cannot represent "'//str//'" as boolean')
call IO_error(113,str)
end if end if
end function IO_strAsBool end function IO_strAsBool
@ -498,11 +487,7 @@ subroutine IO_error(error_ID,ext_msg,label1,ID1,label2,ID2)
case (110) case (110)
msg = 'invalid chunk selected' msg = 'invalid chunk selected'
case (111) case (111)
msg = 'invalid character for int:' msg = 'invalid string for conversion'
case (112)
msg = 'invalid character for real:'
case (113)
msg = 'invalid character for logical:'
case (114) case (114)
msg = 'cannot decode base64 string:' msg = 'cannot decode base64 string:'