--
--	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 convert

#OPTION "E V" -- no compiler libraries, no vector space
#COMMENT "Type conversion library V1.3 3/02/93"
#COMMENT "(c) Copyright INMOS Limited 1988"
PROC STRINGTOINT64 (BOOL Error, INT64 n, VAL []BYTE string)

  --{{{  history
  -- No digits behaviour corrected 19.2.88
  -- V1.0, 1-Jul-88
  -- Added V to OPTIONs; V1.1, 22/03/90 SRH
  -- Put ValidDigit into an include file, and converted it into an
  -- inline function.  TS/2044, V1.2, 18/01/93 SRH
  -- Bug fix TS/2075 for when there are invalid characters in the string
  -- after a negative number - it used to return the positive of the
  -- number.  V1.3, 3/02/93 SRH
  --}}}

  [2]INT n RETYPES n:
  SEQ
    n[0] := 0
    n[1] := 0
    Error := (SIZE string) = 0
    IF
      Error
        SKIP
      TRUE
        --{{{  (SIZE string) > 0
        INT chars.scanned, carry, digit :
        BOOL Negative, More :
        #INCLUDE "validch.occ" -- for ValidDigit
        SEQ
          Negative := string[0] = '-'
          IF
            (string[0] = '+') OR (string[0] = '-')
              chars.scanned := 1
            TRUE
              chars.scanned := 0
          More := chars.scanned < (SIZE string)
        
          digit := 0
          WHILE More AND (chars.scanned < (SIZE string))
            SEQ
              carry, n[0] := LONGPROD (n[0], 10, digit)
              carry, n[1] := LONGPROD (n[1], 10, carry)
              Error := Error OR (carry <> 0)
              digit, More := ValidDigit (string[chars.scanned])
              chars.scanned := chars.scanned + 1
          IF
            More
              SEQ
                carry, n[0] := LONGPROD (n[0], 10, digit)
                carry, n[1] := LONGPROD (n[1], 10, carry)
                Error := Error OR (carry <> 0)
                IF
                  Negative
                    SEQ
                      carry, n[0] := LONGDIFF (0, n[0], 0)
                      carry, n[1] := LONGDIFF (0, n[1], carry)
                      Error := Error OR (n[1] > 0)
                  TRUE
                    Error := Error OR (n[1] < 0)
            TRUE
              SEQ
                Error := TRUE
                IF
                  Negative
                    SEQ
                      carry, n[0] := LONGDIFF (0, n[0], 0)
                      carry, n[1] := LONGDIFF (0, n[1], carry)
                  TRUE
                    SKIP
        --}}}
:
