1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
c This function returns the first sensical integral value of the string, sz.
c It ignores whitespace before the first character and between the sign,
c if any, and the first digit. WARNING, it will return zero if the string
c is empty.
integer function atoi(sz)
implicit none
character*(*) sz
integer fnblnk, f_strpbrk, iachar
integer ndx, iLength
integer iInt, iTmp
logical bNeg, bTmp
c o quit if the string is empty
ndx = fnblnk(sz)
if (ndx.eq.0) then
atoi = 0
return
end if
iLength = len(sz)
c o iInt will be the local atoi value
iInt = 0
c o record a sign
bNeg = (sz(ndx:ndx).eq.'-')
iTmp = f_strpbrk('-+',sz(ndx:ndx))
if (iTmp.gt.0) then
if (ndx.eq.iLength) then
atoi = 0
return
end if
iTmp = fnblnk(sz(ndx+1:))
ndx = ndx + iTmp
end if
c o scan the digits
bTmp = .true.
do while (bTmp)
iTmp = iachar(sz(ndx:ndx)) - 48
if (iTmp.ge.0.and.iTmp.le.9) then
iInt = 10*iInt + iTmp
ndx = ndx + 1
bTmp = (ndx.le.iLength)
else
bTmp = .false.
end if
end do
C o negate the result if necessary and assign atoi
if (bNeg) iInt = -iInt
atoi = iInt
return
end
|