--
--	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 compliler libs
#COMMENT "Stream i/o library, V1.1, 14/06/90"
#COMMENT "(c) Copyright INMOS Limited, 1988"
#INCLUDE "hostio.inc"
#INCLUDE "streamio.inc"
#USE "hostio.lib"
--* Read lines from a text file and output them on the given key stream
-- channel.
--
-- The value ft.terminated is sent on keys.out on termination.
-- Termination of this procedure is either when an error occurs or when
-- all characters in the file have been read. A '*c' is output to
-- terminate a text line.
--
-- @param fs from server
-- @param ts to server
-- @param keys.out for output
-- @param filename the name of the file to be opened.  The name of the
--   file must fit exactly into filename, i.e. there are (SIZE
--   filename) characters in the name of the file.  A directory
--   specification may form part of filename.
-- @param result spr.ok if the process was successful; otherwise it
--   takes on a value indicating what went wrong: spr.bad.packet.size
--   filename too large: (SIZE filename) > sp.max.openname.size
--   spr.bad.name          null file name >=spr.operation.failed  the
--   open failed or reading the file failed - see hostio.inc or iserver
--   documentation for further details
PROC so.keystream.from.file (CHAN OF SP fs, ts, CHAN OF KS keys.out,
                             VAL []BYTE filename, BYTE result)
  --{{{  history
  -- Introduced the end of file test to determine whether so.gets
  -- validly reached the end of the file or failed.  Without this
  -- test a valid functioning of this routine would have left
  -- left spr.operation.failed in result!  New version is 1.1
  -- ( old version was 0.01, 23-Dec-88 ); 14-June-90 SRH
  --}}}

  INT32 streamid :
  SEQ
    sp.open (fs, ts, filename, spt.text, spm.input, streamid, result)
    IF
      result = spr.ok
        SEQ
          [sp.max.readbuffer.size]BYTE buffer :
          INT len :
          WHILE result = spr.ok
            SEQ
              sp.gets (fs, ts, streamid, len, buffer, result)
              SEQ i = 0 FOR len
                keys.out ! INT buffer [i]
              keys.out ! INT ('*c')
          BYTE old.result : -- used to save any error value
          SEQ
            old.result := result
            so.eof( fs, ts, streamid, result )
            IF
              result = spr.ok -- then end of file validly reached
                old.result := spr.ok
              TRUE -- there must have been a read error
                SKIP
            sp.close (fs, ts, streamid, result)
            result     := old.result
      TRUE
        SKIP
    keys.out ! ft.terminated
:
