C
C FUNCTION 'NXTTOK'
C =================
C
C Find the next token in a character string
C
      FUNCTION NXTTOK (STR, SEP, IL, M1, M2)
C
C PARAMETERS
C
C      STR (I)    Character string to be searched for tokens
C      SEP (I)    Character string containing characters to be used as 
C                 separators; if this starts with a space then white space or 
C                 white space + one other separator character will be taken as 
C                 a single separator.
C       IL (I/O)  Position of last character examined in STR (Start with 0
C                 on initial call then use returned value on subsequent calls)
C       M1 (O)    Start position in STR of the token (0 if none found)
C       M2 (O)    End position in STR of the token
C
C   RETURN = -1 End of string
C          =  0 Blank token (There must be non-blank separators used for this
C               to occur)
C          >  0 No. of characters in the token
C
C====== Specification statements
C
      CHARACTER*(*) STR, SEP
      LOGICAL WHSPAC, NONSP
C
C====== Get token
C
      WHSPAC = .FALSE.
      IF (SEP(1:1).EQ.' ') WHSPAC = .TRUE.
      NCH = LEN (STR)
      M1 = 0
      M2 = 0

100   IL = IL + 1
      IF (IL.GT.NCH) THEN
         NXTTOK = -1
         RETURN
      ENDIF
      IF (WHSPAC.AND.STR(IL:IL).EQ.' ') GO TO 100
      IF (INDEX(SEP,STR(IL:IL)).GT.0) THEN
         NXTTOK = 0
         RETURN
      ENDIF
      M1 = IL
      M2 = M1
      NC = 1
120   IL = IL + 1
      IF (IL.GT.NCH) THEN
         NXTTOK = NC
         RETURN
      ENDIF
      IF (INDEX(SEP,STR(IL:IL)).GT.0) THEN
         IF (WHSPAC) THEN
            NONSP = .FALSE.
            IF (STR(IL:IL).NE.' ') NONSP = .TRUE.
130         IF (IL.GE.NCH) THEN
               NXTTOK = NC
               RETURN
            ENDIF
            IF (NONSP) THEN
               IF (STR(IL+1:IL+1).EQ.' ') THEN
                  IL = IL + 1
                  GO TO 130
               ENDIF
            ELSE
               IF (INDEX(SEP,STR(IL+1:IL+1)).GT.0) THEN
                  IL = IL + 1
                  IF (STR(IL:IL).NE.' ') NONSP = .TRUE.
                  GO TO 130
               ENDIF
            ENDIF
         ENDIF
         NXTTOK = NC
         RETURN
      ENDIF
      M2 = IL
      NC = NC + 1
      GO TO 120
      END
