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

#COMMENT "Host i/o library, V1.0, 25/05/90"
#COMMENT "(c) Copyright INMOS Limited 1990"
#INCLUDE "hostio.inc"
#USE "splib.lib"
#USE "convert.lib"
--{{{  so.fwrite.hex.int32
--* Write a 32-bit integer as hexadecimal ASCII characters preceded by
-- the '#' character to a file.
--
-- @param fs from server
-- @param ts to server
-- @param streamid the identification number of the stream to write to
-- @param n the 32-bit 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
-- @param result spr.ok if the integer was successfully written;
--   otherwise not all of the string could be written, in which case
--   result takes on a value of spr.notok
PROC so.fwrite.hex.int32 (CHAN OF SP fs, ts, VAL INT32 streamid, n,
                          VAL INT width, BYTE result)
  IF
    width < 0
      STOP
    TRUE
      [8]BYTE string :
      INT len, len.done :
      SEQ
        HEX32TOSTRING (len, string, n)
        sp.write (fs, ts, streamid, "#", len.done, result)
        --{{{  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" :
        --{{{  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
        
                sp.write (fs, ts, streamid, [fill.string FROM 0 FOR fill.this.time],
                          len.done, result)
                number.to.fill := number.to.fill - fill.this.time
        :
        --}}}sign.extend
        
        IF
          n < 0(INT32)
            sign.extend (width - len, effs)
          TRUE
            sign.extend (width - len, zeroes)
        --}}}sign extend if necessary
        --{{{  output the number, truncated if necessary
        INT start :
        SEQ
          IF
            len > width
              SEQ
                start := len - width
                len   := width
            TRUE
              start := 0
          sp.write (fs, ts, streamid, [string FROM start FOR len], len.done, result)
        --}}}output the number, truncated if necessary
        --{{{  check operation was ok
        IF
          (result = spr.ok) AND (len.done <> len)
            result := spr.notok
          TRUE
            SKIP
        --}}}check operation was ok
:
--}}}so.fwrite.hex.int32
--{{{  so.write.hex.int32
--* Write a 32-bit integer as hexadecimal ASCII characters preceded by
-- the '#' character to standard out.
--
-- @param fs from server
-- @param ts to server
-- @param n the 32-bit 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 so.write.hex.int32 (CHAN OF SP fs, ts, VAL INT32 n, VAL INT width)
  BYTE result :
  so.fwrite.hex.int32 (fs, ts, spid.stdout, n, width, result)
:
--}}}so.write.hex.int32
