home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
sa104os2.zip
/
SATHR104.ZIP
/
SATHER
/
LIBRARY
/
IN.SA
< prev
next >
Wrap
Text File
|
1995-02-05
|
3KB
|
94 lines
-- Copyright (C) International Computer Science Institute, 1994. COPYRIGHT --
-- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
-- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in --
-- the file "Doc/License" of the Sather distribution. The license is also --
-- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA. --
--------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
-- in.sa: Input from stdin.
-------------------------------------------------------------------
class IN is
-- Direct access to stdin.
create:SAME is return self end;
get_char:INT is
-- Retrieve a single character as an INT from stdin, negative for
-- end of file.
return C_IN::c_in_getchar
end;
get_str:STR is
-- A string containing the characters on stdin up to the next
-- newline.
return get_line.str;
end;
get_line:FSTR is
-- A string buffer containing the characters on stdin up to the
-- next newline.
return append_line(void);
end;
private const size : INT := 256;
append_line(s:FSTR):FSTR is
-- Retrieve a string from stdin up to the next newline and append
-- it to `s'.
-- Implementation contributed by hikichi@srarc2.sra.co.jp
-- (Nobuyuki Hikichi).
bsize ::= size;
loop
buf ::= #FSTR(bsize); -- buffer to hold C string
C_IN::c_in_get_str(buf, 0, bsize);
buf.loc := bsize - 1;
m: INT := buf.index_of('\n');
if m >= 0 then buf.loc := m; return s + buf; end;
n: INT := buf.index_of('\0');
if n = -1 then s := s + buf; bsize := bsize * 2;
elsif n = 0 then return s;
elsif n <= (bsize - 1) then buf.loc := n; return s + buf;
else raise "IN::append_line, out of range [-1, bsize-1]";
end; -- if
end; -- loop
return s; -- NLP
end; -- fstr (s:FSTR)
error:BOOL is
-- true if an error has been encountered. Cleared by "clear".
return C_FILE::ferror(FILE::stdin_macro);
end;
eof:BOOL is
-- true if EOF has been encountered. Cleared by "clear".
return C_FILE::feof(FILE::stdin_macro);
end;
clear is
-- resets end_seen and error status
C_FILE::clearerr(FILE::stdin_macro);
end;
end; -- class IN
-------------------------------------------------------------------
external class C_IN is
-- Interface to C functions supporting IN.
c_in_getchar:INT; -- Retrieve a single character from
-- stdin as an INT, negative for end of file.
c_in_get_str(s:FSTR,st,sz:INT); -- Insert characters from
-- stdin into `s' starting at character `st', for at most `sz-1'
-- characters or until EOF or newline is read. Puts in
-- '\0' after these characters. Returns the index of the last
-- non-zero character. If EOF is read before any chars then returns
-- 0 and doesn't change anything.
end; -- external class C_IN
-------------------------------------------------------------------