snum0(3f) - [M_calculator] resolve a calculator expression into a string(return blank on errors)
(LICENSE:PD)
function snum0(inline0,ierr)
character(len=:),allocatable :: snum0(inline0)
character(len=*),intent(in) :: inline0 ! input string
integer,optional,intent(out) :: ierr
SNUM0() is used to return a string value up to (iclen_calc=512) characters
long from a string expression.
SNUM0() uses the calculator routine CALCULATOR(3f)
inline0 INLINE0 is a CHARACTER variable up to (iclen_calc=512) characters long that
is similar to a FORTRAN 77 expression.
ierr error code. If zero, no error occurred
Sample program:
program demo_snum0
use m_calculator, only: rnum0, snum0
implicit none
real :: rdum
character(len=80) :: ic,jc,kc
rdum=rnum0('A=83/2') ! set a variable in the calculator
kc=snum0('$MYTITLE="This is my title variable"')
ic=snum0('$STR("VALUE IS [",A,"]")')
jc=snum0('$MYTITLE')
write(*,*)'IC=',trim(ic)
write(*,*)'JC=',trim(jc)
write(*,*)'KC=',trim(kc)
end program demo_snum0
The output should look like
IC=VALUE IS [41.5]
JC=This is my title variable
KC=This is my title variable
o User-supplied routines:
All programs that call the calculator routine can supply their
own substitute_subroutine(3f) and substitute_C(3f) procedures. See
the example program for samples.
o The syntax of an expression is described in the main document of the
Calculator Library.
o See CALCULATOR(), RNUM0(), SNUM0(), EXPRESSION().
John S. Urban
Public Domain
AUTHOR John S. Urban
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | inline0 | |||
integer, | intent(out), | optional | :: | ierr |
function snum0(inline0,ierr) ! ident_19="@(#) M_calculator snum0(3f) resolve a calculator expression into a string" ! a few odd things are done because some compilers did not work as expected character(len=:),allocatable :: snum0 character(len=*),intent(in) :: inline0 ! input string integer,optional,intent(out) :: ierr character(len=iclen_calc) :: lcopy ! working string character(len=iclen_calc) :: inline ! working copy of input string integer :: ierr_local integer :: iend ! size of input string integer :: ilen doubleprecision :: dnum1 inline=inline0 ! some compilers need a copy of known length to work as expected ierr_local=0 if(inline.eq.' ')then ! what to do for a blank string snum0=' ' ! return a blank string else ! non-blank input expression iend=len(inline) ! size of working string lcopy=' ' ! initialize trimmed string lcopy=adjustl(inline(:iend)) ! trim leading spaces if(lcopy(1:1).eq.'$'.or.lcopy(1:1).eq.'"')then ! this is a string that needs evaluated dnum1=0.0d0 call expression(inline(:iend),dnum1,lcopy,ierr_local,ilen) if(ierr_local.ne.2)then ! check if expression was evaluated to a string successfully snum0=' ' ! input string was not resolved to a string endif snum0=lcopy(:max(1,ilen)) ! return whatever expression() returned else ! input was just a string, not an expression so just copy it snum0=inline(:iend) ! copy input string to output endif endif if(present(ierr))then ierr=ierr_local endif end function snum0