in.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand        --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  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 Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------


class IN < $IN_DEVICE

class IN < $IN_DEVICE is -- This class implements an object representing the standard OS-provided -- input channel. -- This is an adaptation of the original Sather library for the expanded -- file concept developed at the University of Waikato. Thanks are due to -- Noboyuki Hikichi (hikichi@srarc2.sra.co.jp) for the implementation of the -- append_line routine. -- Version 1.4 Nov 98. Copright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 5 Apr 96 kh Original -- 5 Apr 97 kh Modified for style -- 9 Jun 97 kh Modified for portability, etc -- 21 Oct 98 kh Improved portability and added pre/posts -- 19 Nov 98 kh Added input device abstraction const input_possible : BOOL := true ; private shared lib : LIBCHARS ; private shared my_size : CARD ; readonly shared my_chan : STD_CHANS ; create : SAME is -- This trivially provides for the object 'creation'. Note that this -- routine is provided solely for compatibility with the standard ouput -- and error channels. No object is actually created! if void(lib) then lib := LIBCHARS::default ; my_chan := STD_CHANS::stdin ; my_size := lib.my_size end ; return self end ; get_code : CHAR_CODE is -- Retrieve a character as its code representation from -- stdin. This permits encodings up to the size of UCS4 to be used. -- If the operation fails then the value CHAR_CODE::invalid, which never -- represents a character, is returned. res : CHAR_CODE := CHAR_CODE::null ; loop index : CARD := (my_size - 1).downto!(0) ; loc_octet : OCTET ; if ~FILE_SYS::getchar(out loc_octet,my_chan.chan) then return CHAR_CODE::invalid else res.aset(index,loc_octet) end end ; my_chan.read_line_mark := lib.Line_Mark.contains(res) ; return res end ; get_ch : CHAR is -- Retrieve a character from stdin. This permits encodings up to -- the size of UCS4 to be used in the default local environment. Note that -- this is always the environment in which stdin operates. return get_code.char end ; private const Def_Buff_Size : CARD := 256 ; -- A nominal initial size! append_line( str : FSTR ) : FSTR pre ~void(str) post (result = initial(str)) or (result.size > initial(str.size)) is -- This routine retrieves a string from the standard input channel up to, -- but not including, the next end of line indicator. The string is appended -- to str. buffer_size : CARD := Def_Buff_Size ; loop buffer : FBINSTR := FILE_SYS::channel_read(buffer_size, my_chan.chan) ; if void(buffer) then -- nothing - closed?? return str else loc_res : FSTR := FSTR::create(buffer.str) ; loc_res := loc_res.strip ; -- get rid of line mark if buffer_size > loc_res.loc then -- line end found my_chan.read_line_mark := true ; return str + loc_res else -- more to come! str := str + loc_res ; buffer_size := buffer_size * 2 end end end end ; get_line( lib : LIBCHARS ) : FSTR pre ~void(lib) post void(result) or (result.size > 0) is -- This routine returns a new FSTR containing the contents of the -- standard input channel up to but not including the next end of line -- indicator as determined in the given repertoire and encoding. loc_buff : FSTR := FSTR::create(lib) ; return append_line(loc_buff) end ; get_line : FSTR pre true post void(result) or (result.size > 0) is -- This routine returns a new FSTR containing the contents of the -- standard input channel up to but not including the next end of line -- indicator. return get_line(LIBCHARS::default) end ; get_str( lib : LIBCHARS ) : STR pre ~void(lib) post void(result) or (result.size > 0) is -- This routine returns a string containing the contents of the -- standard input channel up to but not including the next end of line -- indicator. loc_buff : FSTR := FSTR::create(lib) ; return append_line(loc_buff).str end ; get_str : STR pre true post void(result) or (result.size > 0) is -- This routine returns a string containing the contents of the -- standard input channel up to but not including the next end of line -- indicator. return get_str(LIBCHARS::default) end ; error : BOOL is -- This predicate returns true iff an error has been encountered since -- the most recent use of clear or object creation. If this occurs -- then it MUST be explicitly cleared! return FILE_SYS::error(STD_CHANS::stdin.chan) end ; clear pre true post ~error is -- This operation resets any error condition. FILE_SYS::clearerr(STD_CHANS::stdin.chan) end ; end ; -- IN