      subroutine get_next_token(buf,bptr,limit,token,delimiters)
C     
C Description:
C
C This routine finds and returns the next token from a buffer 'buf'.
C
C A token is defined as the set of consecutive characters between
C two token delimiters. The 'next' token is the token found to the
C right of the buffer pointer 'bptr'. Token delimiters are passed 
C in as a string of characters. The beginning and end of the buffer
C act as token delimiters in all cases.
C
C Any character in the delimiter string may act as a delimiter.
C 'bptr' is set to the first character of buf that has not been examined
C (usually the first character of the next token).
C The routine advances 'bptr' after it examines characters so the
C routine can be used repeatedly to get all the tokens in a buffer.
C When the routine returns, 'bptr' points at the first non-delimiter
C after the found token or one past the limit.
C
C 'limit' is the last character to be examined in the buffer. If 'limit'
C is beyond the end of the buffer, the actual end of the buffer 
C is the effective limit.
C
C The routine can do no more effective work if 'bptr' is greater than 
C 'limit'. Use this fact as a test for ending a token search. Another
C criteria for ending the search is that 'token' will be returned
C as an empty string (unless you are looking for whitespace tokens).
C
C Warning! The effective limit is the minimum of the length of the
C buffer and 'limit'. If you test for 'bptr' > 'limit' as the ending
C criteria and 'limit' is greater than the length of the buffer, it
C won't be good.
C     
C
C Name         in/out type          Structure  Meaning
C buf          in     character(*)  scalar     The source string for the 
C                                              tokens.                 
C bptr         in/out integer       scalar     The pointer into the source
C                                              string 'buf'.
C limit        in     integer       scalar     The limiting value for 'ptr'
C token        out    character*(*) scalar     The token found in 'buf'
C delimiters   in     character*(*) scalar     The string containing all the
C                                              characters that can delimit
C                                              'token'
C
C Errors:
C
C There should be no errors returned from this routine. However, some
C unexpedted behavior can result if 'buf' is a string with no tokens.
C In this case, token will be returned as a string of blank characters
C and 'bptr' will be one larger than 'limit'. Also, if a valid token
C is a sequence of blank characters, then confusion reigns supreme.
C A blank character has unusually powerful connotations in FORTRAN and
C this fact causes some of the problem with this routine.
C
C This routine should be modified to include a flag that is set when
C a token is found and is reset when a token is not found. Future work.
C
      implicit none
      integer bptr,limit,ll,len,start,ptr
      character*(*) buf,token,delimiters
      logical done,finished
      token=' '
      ll=min(limit,len(buf))
      if (bptr .le. ll) then
         finished=.false.
      else
         finished=.true.
      end if
C
C Move past all delimiters
C
      if (.not. finished) then
         done=.false.
         do while (.not. done)
            ptr=index(delimiters,buf(bptr:bptr))
            if (ptr .ne. 0) then
               bptr=bptr+1
               if (bptr .gt. limit) then
                  done=.true.
                  finished=.true.
               end if
            else
               done=.true.
            end if
         end do
      end if
C
C Move past all characters in token
C
      if (.not. finished) then
         start=bptr
         done=.false.
         do while (.not. done)
            ptr=index(delimiters,buf(bptr:bptr))
            if (ptr .eq. 0) then
               bptr=bptr+1
               if (bptr .gt. limit) then
                  done=.true.
                  finished=.true.
                  token=buf(start:bptr-1)
               end if
            else
               token=buf(start:bptr-1)
               done=.true.
            end if
         end do
      end if
C     
C Move past all remaining delimiters so that 'bptr' points at the
C first character of the next token or one past the end of 'buf'.
C
      if (.not. finished) then
         done=.false.
         do while (.not. done)
            ptr=index(delimiters,buf(bptr:bptr))
            if (ptr .ne. 0) then
               bptr=bptr+1
               if (bptr .gt. limit) then
                  done=.true.
                  finished=.true.
               end if
            else
               done=.true.
            end if
         end do
      end if
      return
      end
