--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation; either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library; if not, write to the Free Software
--	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
--

--** @module string

#OPTION "E V" -- no compiler libraries, no vector space
#COMMENT "String library V1.1 04/06/90"
#COMMENT "(c) Copyright INMOS Limited 1988"
#USE "convert.lib"  -- for STRINGTOINT
--{{{  history
-- V1.0 22-Feb-89
-- V1.1  4-Jun-90 SRH
--}}}
--{{{  next.word.from.line
--* Determine the next word in a given line, skipping leading spaces
-- and tabs.
--
-- Leading spaces and ( horizontal ) tabs ( from line[ ptr ], ptr on
-- entry ) are skipped. A word continues until a space or tab or the
-- end of the string is encountered.
--
-- @param line a string containing the line from which a word is
--   desired to be noted.  The string is considered to be of length
--   (SIZE line).
-- @param ptr on entry: the index of line from which to start the
--   search for a word, i.e. the search begins at line[ ptr ] with ptr
--   increasing.  On exit: if ok is FALSE on entry, then unchanged; if
--   ok is TRUE on entry, then is either the index of the space or tab
--   immediately after the found word or is >= (SIZE line) ( where it
--   is only ever greater than if it was passed in as such ), whether
--   ok is subsequently set to FALSE or not.
-- @param len if ok is FALSE on entry, then 0; if ok is TRUE on entry
--   then gives the length of the first word found after the given
--   starting position ( whether ok is subsequently set to FALSE or not
--   ), which in the case of no word found is 0
-- @param word if ok is FALSE on entry, then undefined; if ok is TRUE
--   on entry, then this contains the found word from line in the first
--   len bytes, the remaining bytes being undefined, or if not large
--   enough to contain the word or no word found it is undefined and ok
--   is set to FALSE, though len gives the correct length of the word
--   found
-- @param ok on entry: if FALSE, then len is set to 0, ptr and ok
--   remain unchanged, and word is undefined; otherwise a search for a
--   word is carried out.  On exit: if FALSE on entry then FALSE; if
--   TRUE on entry: FALSE if no word found; FALSE if a word found that
--   was too large to fit into word; otherwise TRUE
PROC next.word.from.line (VAL []BYTE line, INT ptr, len, []BYTE word, BOOL ok)
  VAL tab IS '*T' :
  SEQ
    len := 0
    IF
      ok
        --{{{  skip leading spaces and tabs
        WHILE (ptr < (SIZE line)) AND ((line [ptr] = ' ') OR (line [ptr] = tab))
          ptr := ptr + 1
        --}}}
      TRUE
        SKIP
    IF
      ok AND (ptr < (SIZE line))
        --{{{  get the word
        INT end :
        SEQ
          IF -- look for end of word
            IF i = ptr FOR ((SIZE line) - ptr)
              (line [i] = ' ') OR (line [i] = tab)
                end := i
            TRUE
              end := (SIZE line)
        
          len := end - ptr
          IF
            len > (SIZE word)
              ok := FALSE    -- but leaves len saying the correct length
            TRUE
              [word FROM 0 FOR len] := [line FROM ptr FOR len]
          ptr := end -- this will point at the following space or tab, or
                     -- just off the end of line: ( SIZE line )
        --}}}
      TRUE
        ok := FALSE
:
--}}}
--{{{  next.int.from.line
--* Determine the next integer in a given line, skipping leading spaces
-- and tabs.
--
-- Leading spaces and ( horizontal ) tabs ( from line[ ptr ], ptr on
-- entry ) are skipped. The first sequence of characters found after
-- skipping spaces and tabs is taken to be the integer desired; the
-- integer continues until a space or tab or the end of the string is
-- encountered. A + or - are permissible as the first character of the
-- integer.
--
-- @param line a string containing the line from which an integer is
--   desired to be noted.  The string is considered to be of length
--   (SIZE line).
-- @param ptr on entry: the index of line from which to start the
--   search for an integer, i.e. the search begins at line[ ptr ] with
--   ptr increasing.  On exit: if ok is FALSE on entry, then unchanged;
--   if ok is TRUE on entry, then is either the index of the space or
--   tab immediately after the found integer or is >= (SIZE line) (
--   where it is only ever greater than if it was passed in as such ),
--   whether ok is subsequently set to FALSE or not.
-- @param number if ok is FALSE on entry, then undefined; if ok is TRUE
--   on entry: if ok TRUE on exit, the integer read, otherwise,
--   undefined
-- @param ok on entry: if FALSE, ptr and ok remain unchanged, and
--   number is undefined; otherwise a search for an integer is carried
--   out.  On exit: if FALSE on entry then FALSE; if TRUE on entry:
--   FALSE if there were no non-space or non-tab characters before the
--   end of the string; FALSE if the first sequence of non-space,
--   non-tab characters do not form an integer; FALSE if an integer
--   found that overflowed the range of INT; otherwise TRUE
PROC next.int.from.line (VAL []BYTE line, INT ptr, number, BOOL ok)
  -- reduced the size of int.string from 16 to 12; 4-Jun-90 SRH

  [12]BYTE int.string : -- there are 11 digits in MOSTNEG INT32 = -2147483648
  INT len :
  SEQ
    next.word.from.line (line, ptr, len, int.string, ok)
    #PRAGMA DEFINED int.string
    IF
      ok
        BOOL error :
        SEQ
          STRINGTOINT (error, number, [int.string FROM 0 FOR len])
          ok := NOT error
      TRUE
        SKIP
:
--}}}
