NCEPLIBS-bufr 11.7.1
strnum.f
Go to the documentation of this file.
1C> @file
2C> @brief Decode an integer from a character string
3
4C> This subroutine decodes an integer from a character string. The
5C> string should contain only digits and (optional) trailing blanks.
6C> It should not contain any sign ('+' or '-') character nor any
7C> leading blanks nor embedded blanks.
8C>
9C> @author J. Woollen
10C> @date 1994-01-06
11C>
12C> @param[in] STR -- character*(*): String
13C> @param[out] NUM -- integer: Value decoded from STR
14C> - -1 = decode was unsuccessful
15C>
16C> <b>Program History Log:</b>
17C> | Date | Programmer | Comments |
18C> | -----|------------|----------|
19C> | 1994-01-06 | J. Woollen | Original author |
20C> | 2003-11-04 | J. Ator | Added documentation |
21C> | 2009-04-21 | J. Ator | Use errwrt() |
22C>
23 SUBROUTINE strnum(STR,NUM)
24
25 CHARACTER*(*) STR
26 CHARACTER*20 STR2
27
28 COMMON /quiet / iprt
29
30C-----------------------------------------------------------------------
31C-----------------------------------------------------------------------
32
33 num = 0
34 k = 0
35
36C Note that, in the following call to subroutine STRSUC, the output
37C string STR2 is not used anywhere else in this routine. In fact,
38C the only reason that subroutine STRSUC is being called here is to
39C determine NUM, which, owing to the fact that the input string STR
40C cannot contain any leading blanks, is equal to the number of
41C digits to be decoded from the beginning of STR.
42
43 CALL strsuc(str,str2,num)
44 IF(num.EQ.-1) GOTO 100
45
46 DO i=1,num
47 READ(str(i:i),'(I1)',err=99) j
48 IF(j.EQ.0 .AND. str(i:i).NE.'0') GOTO 99
49 k = k*10+j
50 ENDDO
51
52 num = k
53 GOTO 100
54
55C Note that NUM = -1 unambiguously indicates a bad decode since
56C the input string cannot contain sign characters; thus, NUM is
57C always positive if the decode is successful.
58
5999 num = -1
60 IF(iprt.GE.0) THEN
61 CALL errwrt('++++++++++++++BUFR ARCHIVE LIBRARY+++++++++++++++++')
62 CALL errwrt('BUFRLIB: STRNUM - BAD DECODE; RETURN WITH NUM = -1')
63 CALL errwrt('++++++++++++++BUFR ARCHIVE LIBRARY+++++++++++++++++')
64 CALL errwrt(' ')
65 ENDIF
66
67C EXIT
68C ----
69
70100 RETURN
71 END
subroutine errwrt(STR)
This subroutine allows the user to specify a custom location for the logging of error and diagnostic ...
Definition: errwrt.f:42
subroutine strnum(STR, NUM)
This subroutine decodes an integer from a character string.
Definition: strnum.f:24
subroutine strsuc(STR1, STR2, LENS)
This subroutine removes leading and trailing blanks from a character string.
Definition: strsuc.f:24