--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--      Modifications Copyright (C) 2004  Fred Barnes
--
--	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.1, 14/05/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC simplified version 1995"

#INCLUDE "hostio.inc"
#INCLUDE "unixerrs.inc"
#USE "conv"

--{{{  OC.fwrite
VAL unix.max.writebuffer.size IS (sp.max.writebuffer.size/4) + 1:
#PRAGMA EXTERNAL "PROC C.fwrite (INT result, VAL INT32 handle, VAL [unix.max.writebuffer.size]INT buffer, VAL INT bytes.to.write, INT bytes.written)  = 0 "
--#PRAGMA EXTERNAL "PROC bytes.to.chars (VAL []BYTE from, []INT to) = 4"

PROC OC.fwrite (INT result, VAL INT32 handle, VAL []BYTE buffer, INT bytes.written)
  [unix.max.writebuffer.size]INT unixbuffer:
  SEQ
    bytes.to.chars (buffer, unixbuffer)
    C.fwrite (result, handle, unixbuffer, SIZE buffer, bytes.written)
:
--}}}
--{{{  sp.write
--* Write an unlimited number of bytes to a stream.
--
-- @param fs from server
-- @param ts to server
-- @param streamid the identification number of the open stream to
--   write to
-- @param data a string of bytes ALL of which are to be written to the
--   file
-- @param length this gives the number of bytes written; if length <>
--   (SIZE data) then an error occurred
-- @param result spr.ok if the request was successful; otherwise >=
--   spr.operation.failed - refer to hostio.inc or iserver
--   documentation for further details
PROC sp.write (CHAN OF SP fs, ts, VAL INT32 streamid, VAL []BYTE data,
               INT length, BYTE result)
  --{{{  more information
  -- Notes:    If more than a packet's worth of data is to be written
  --           to a file then one cannot guarantee that all of the data
  --           will be written to the file before any other process
  --           accesses the same file.
  --}}}
  --{{{  history
  -- Removed the unnecessary test ( result = spr.ok ) from the WHILE loop
  -- condition since the present implementation of sp.write always returns
  -- spr.ok; V1.1, SRH 14-May-90
  --}}}

  BOOL writing :
  SEQ
    writing := TRUE
    length  := 0
    WHILE writing AND (length < (SIZE data))
      INT length.to.write, length.written :
      SEQ
        IF
          ((SIZE data) - length) > sp.max.writebuffer.size
            length.to.write := sp.max.writebuffer.size
          TRUE
            length.to.write := ((SIZE data) - length)

        INT res:
        SEQ
          --{{{  Call Unix
          VAL buffer IS [data FROM length FOR (SIZE data) - length]:
          OC.fwrite (res, streamid, [buffer FOR length.to.write], length.written)
          IF
            res = 0
              result := spr.ok
            res = Unix.ACCESS.DENIED
              result := spr.bad.stream.use
            res = Unix.INVALID.HANDLE
              result := spr.invalid.streamid
            TRUE
              result := spr.operation.failed
          --}}}
          writing := (res = 0) AND (length.to.write = length.written)
          length  := length + length.written
:
--}}}  sp.write
