1C*GRCTOI -- convert character string to integer
2C+
3      INTEGER FUNCTION GRCTOI (S, I)
4      CHARACTER*(*) S
5      INTEGER I
6C
7C GRCTOI: attempt to read an integer from a character string, and return
8C the result. No attempt is made to avoid integer overflow. A valid
9C integer is any sequence of decimal digits.
10C
11C Returns:
12C  GRCTOI           : the value of the integer; if the first character
13C                    read is not a decimal digit, the value returned
14C                    is zero.
15C Arguments:
16C  S      (input)  : character string to be parsed.
17C  I      (in/out) : on input, I is the index of the first character
18C                    in S to be examined; on output, either it points
19C                    to the next character after a valid integer, or
20C                    it is equal to LEN(S)+1.
21C
22C--
23C  1985 Oct  8 - New routine, based on CTOI (T. J. Pearson).
24C  1997 Jun  3 - allow leading + or - sign (TJP).
25C-----------------------------------------------------------------------
26      INTEGER K, SIGN, X
27      CHARACTER*1 DIGITS(0:9)
28      DATA  DIGITS/'0','1','2','3','4','5','6','7','8','9'/
29C
30      X = 0
31      SIGN = +1
32      IF (I.GT.LEN(S)) GOTO 30
33      IF (S(I:I).EQ.'+') THEN
34         I = I+1
35      ELSE IF (S(I:I).EQ.'-') THEN
36         I = I+1
37         SIGN = -1
38      END IF
39 10   IF (I.GT.LEN(S)) GOTO 30
40      DO 20 K=0,9
41         IF (S(I:I).EQ.DIGITS(K)) THEN
42            X = X*10 + K
43            I = I+1
44            GOTO 10
45         END IF
46 20   CONTINUE
47 30   GRCTOI = X*SIGN
48      RETURN
49      END
50