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

#OPTION "E"  -- no compiler libs
#COMMENT "Stream i/o library, V1.2, 16/10/92"
#COMMENT "(c) Copyright INMOS Limited 1989, 1990"
#INCLUDE "streamio.inc"
#USE "convert.lib"

--{{{  history
-- Altered completely so that there is no longer a maximum limit on the
-- total size of string that is output.
-- Also defined it to be an error if the requested field width is negative.
-- New version is 1.1 ( old version was 0.01, 23-Dec-88 )  13-June-90 SRH
-- V1.2, 16-Oct-92 SRH: parameter 'field' -> 'width', for consistency.
--}}}

--{{{  ss.write.int
--* Write to the given screen stream an integer as decimal ASCII
-- digits, padded out with leading spaces to the specified field width.
--
-- If the field width is too small for the number, then it is widened
-- as necessary; a zero value for the field width will give minimum
-- width; a negative field width is an error.
--
-- @param scrn for output
-- @param number the integer that is desired to be written
-- @param width the desired field width of the string
PROC ss.write.int (CHAN OF SS scrn, VAL INT number, width )
  IF
    width < 0
      STOP
    TRUE
      [11]BYTE string : -- 11 characters in -2147483648 = MOSTNEG INT32;
      INT len :
      SEQ
        INTTOSTRING (len, string, number)
        --{{{  output any necessary spaces
        VAL spaces IS "                    " :  -- 20 spaces
        INT number.of.spaces, fill.this.time :
        SEQ
          number.of.spaces := width - len
          WHILE number.of.spaces > 0
            SEQ
              IF
                number.of.spaces > (SIZE spaces)
                  fill.this.time := SIZE spaces
                TRUE
                  fill.this.time := number.of.spaces
              scrn ! st.out.string;
                INT32 fill.this.time :: [spaces FROM 0 FOR fill.this.time]
              number.of.spaces := number.of.spaces - fill.this.time
        --}}}output any necessary spaces
        -- now output the string representation of the integer
        scrn ! st.out.string; INT32 len :: [string FROM 0 FOR len]
:
--}}}ss.write.int
--{{{  ss.write.hex.int
--* Write an integer as hexadecimal ASCII characters preceded by the
-- '#' character to the given screen stream.
--
-- @param scrn for output
-- @param number the integer to write out
-- @param width the field width to use when writing the hex; if width
--   is larger than the size of the number then the number is padded
--   with leading 0's or F's as appropriate; if width is smaller than
--   the size of the number then the number is truncated at the left to
--   width characters; width does not take the '#' into account. A
--   negative field width is an error.
PROC ss.write.hex.int (CHAN OF SS scrn, VAL INT number, width )
  IF
    width < 0
      STOP
    TRUE
      [8]BYTE string :
      INT len :
      SEQ
        HEXTOSTRING (len, string, number)
        scrn ! st.out.byte; '#'
        --{{{  sign extend if necessary
        VAL size.fill.string IS 10 :
        VAL [size.fill.string]BYTE zeroes IS "0000000000" :
        VAL [size.fill.string]BYTE effs   IS "FFFFFFFFFF" :
        --{{{  PROC sign.extend
        PROC sign.extend (VAL INT how.many, VAL [size.fill.string]BYTE fill.string)
          INT number.to.fill, fill.this.time :
          SEQ
            number.to.fill := how.many
            WHILE number.to.fill > 0
              SEQ
                IF
                  number.to.fill > (SIZE fill.string)
                    fill.this.time := SIZE fill.string
                  TRUE
                    fill.this.time := number.to.fill
                scrn ! st.out.string;
                  INT32 fill.this.time :: [fill.string FROM 0 FOR fill.this.time]
                number.to.fill := number.to.fill - fill.this.time
        :
        --}}}
        IF
          number < 0
            sign.extend (width - len, effs)
          TRUE
            sign.extend (width - len, zeroes)
        --}}}
        --{{{  output the number, truncated if necessary
        INT start :
        SEQ
          IF
            len > width
              SEQ
                start := len - width
                len   := width
            TRUE
              start := 0
          scrn ! st.out.string; INT32 len :: [string FROM start FOR len]
        --}}}
:
--}}}ss.write.hex.int
