NCEPLIBS-bufr  11.6.0
 All Data Structures Files Functions Variables Pages
strnum.f
Go to the documentation of this file.
1 C> @file
2 C> @brief Decode an integer from a character string
3 
4 C> This subroutine decodes an integer from a character string. The
5 C> string should contain only digits and (optional) trailing blanks.
6 C> It should not contain any sign ('+' or '-') character nor any
7 C> leading blanks nor embedded blanks.
8 C>
9 C> @author J. Woollen
10 C> @date 1994-01-06
11 C>
12 C> @param[in] STR -- character*(*): String
13 C> @param[out] NUM -- integer: Value decoded from STR
14 C> - -1 = decode was unsuccessful
15 C>
16 C> <b>Program History Log:</b>
17 C> | Date | Programmer | Comments |
18 C> | -----|------------|----------|
19 C> | 1994-01-06 | J. Woollen | Original author |
20 C> | 2003-11-04 | J. Ator | Added documentation |
21 C> | 2009-04-21 | J. Ator | Use errwrt() |
22 C>
23  SUBROUTINE strnum(STR,NUM)
24 
25  CHARACTER*(*) str
26  CHARACTER*20 str2
27 
28  COMMON /quiet / iprt
29 
30 C-----------------------------------------------------------------------
31 C-----------------------------------------------------------------------
32 
33  num = 0
34  k = 0
35 
36 C Note that, in the following call to subroutine STRSUC, the output
37 C string STR2 is not used anywhere else in this routine. In fact,
38 C the only reason that subroutine STRSUC is being called here is to
39 C determine NUM, which, owing to the fact that the input string STR
40 C cannot contain any leading blanks, is equal to the number of
41 C 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 
55 C Note that NUM = -1 unambiguously indicates a bad decode since
56 C the input string cannot contain sign characters; thus, NUM is
57 C always positive if the decode is successful.
58 
59 99 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 
67 C EXIT
68 C ----
69 
70 100 RETURN
71  END
subroutine strsuc(STR1, STR2, LENS)
This subroutine removes leading and trailing blanks from a character string.
Definition: strsuc.f:23
subroutine strnum(STR, NUM)
This subroutine decodes an integer from a character string.
Definition: strnum.f:23
subroutine errwrt(STR)
This subroutine allows the user to specify a custom location for the logging of error and diagnostic ...
Definition: errwrt.f:41