EiffelBase class
(HTML page generated by ISE Eiffel 4.2)
Eiffel Class
indexing
description: "Commonly used console input and output mechanisms. This class may be used as ancestor by classes needing its facilities.";
status: "See notice at end of class";
date: "$Date: 2007-03-30 19:10:11 +0000 (Fri, 30 Mar 2007) $";
revision: "$Revision: 95354 $"
class CONSOLE
inherit
PLAIN_TEXT_FILE
rename
make_open_read as make_open_stdin,
make_open_write as make_open_stdout
export
{NONE} all;
{ANY} separator, append, file_pointer, last_character, last_integer, last_real, last_string, last_double, file_readable, lastchar, lastint, lastreal, laststring, lastdouble
redefine
make_open_stdin, make_open_stdout, count, empty, exists, read_integer, read_real, read_double, read_character, read_line, read_stream, read_word, next_line, put_integer, put_boolean, put_real, put_double, put_string, put_character, new_line, end_of_file, file_close, readint, readreal, readdouble, readchar, readline, readstream, readword, putint, putbool, putreal, putdouble, putstring, putchar, dispose
end
creation {STD_FILES}
make_open_stdin,
make_open_stdout,
make_open_stderr
feature -- Initialization
make_open_stdin (fn: STRING) is
-- Create an unix standard input file.
do
make (fn);
file_pointer := console_def (0);
set_read_mode;
create last_string.make (256)
end;
make_open_stdout (fn: STRING) is
-- Create an unix standard output file.
do
make (fn);
file_pointer := console_def (1);
set_write_mode
end;
make_open_stderr (fn: STRING) is
-- Create an unix standard error file.
do
make (fn);
file_pointer := console_def (2);
set_write_mode
end;
feature -- Status report
end_of_file: BOOLEAN is
-- Have we reached the end of file?
do
Result := console_eof (file_pointer)
end;
exists: BOOLEAN is
-- Does file exist?
do
Result := true
end;
feature -- Removal
dispose is
-- This is closed by the operating system at completion.
do
end;
feature -- Input
read_integer is
-- Read a new integer from standard input.
-- Make result available in last_integer.
-- Was declared in CONSOLE as synonym of read_integer and readint.
do
last_integer := console_readint (file_pointer)
end;
readint is
-- Read a new integer from standard input.
-- Make result available in last_integer.
-- Was declared in CONSOLE as synonym of read_integer and readint.
do
last_integer := console_readint (file_pointer)
end;
read_real is
-- Read a new real from standard input.
-- Make result available in last_real.
-- Was declared in CONSOLE as synonym of read_real and readreal.
do
last_real := console_readreal (file_pointer)
end;
readreal is
-- Read a new real from standard input.
-- Make result available in last_real.
-- Was declared in CONSOLE as synonym of read_real and readreal.
do
last_real := console_readreal (file_pointer)
end;
read_double is
-- Read a new double from standard input.
-- Make result available in last_double.
-- Was declared in CONSOLE as synonym of read_double and readdouble.
do
last_double := console_readdouble (file_pointer)
end;
readdouble is
-- Read a new double from standard input.
-- Make result available in last_double.
-- Was declared in CONSOLE as synonym of read_double and readdouble.
do
last_double := console_readdouble (file_pointer)
end;
read_line is
-- Read a string until new line or end of file.
-- Make result available in last_string.
-- New line will be consumed but not part of last_string.
-- Was declared in CONSOLE as synonym of read_line and readline.
require
is_readable: file_readable
local
str_cap: INTEGER;
read: INTEGER;
str_area: ANY;
done: BOOLEAN
do
from
str_area := last_string.area;
str_cap := last_string.capacity
until
done
loop
read := read + console_readline (file_pointer, $str_area, str_cap, read);
if read > str_cap then
last_string.set_count (str_cap);
last_string.resize (str_cap + 1024);
str_area := last_string.area;
str_cap := last_string.capacity;
read := read - 1
else
last_string.set_count (read);
done := true
end
end;
if read < 1024 then
last_string.resize (read)
end
end;
readline is
-- Read a string until new line or end of file.
-- Make result available in last_string.
-- New line will be consumed but not part of last_string.
-- Was declared in CONSOLE as synonym of read_line and readline.
require
is_readable: file_readable
local
str_cap: INTEGER;
read: INTEGER;
str_area: ANY;
done: BOOLEAN
do
from
str_area := last_string.area;
str_cap := last_string.capacity
until
done
loop
read := read + console_readline (file_pointer, $str_area, str_cap, read);
if read > str_cap then
last_string.set_count (str_cap);
last_string.resize (str_cap + 1024);
str_area := last_string.area;
str_cap := last_string.capacity;
read := read - 1
else
last_string.set_count (read);
done := true
end
end;
if read < 1024 then
last_string.resize (read)
end
end;
read_stream (nb_char: INTEGER) is
-- Read a string of at most nb_char bound characters
-- from standard input.
-- Make result available in last_string.
-- Was declared in CONSOLE as synonym of read_stream and readstream.
local
new_count: INTEGER;
str_area: ANY
do
last_string.resize (nb_char);
str_area := last_string.area;
new_count := console_readstream (file_pointer, $str_area, nb_char);
last_string.set_count (new_count)
end;
readstream (nb_char: INTEGER) is
-- Read a string of at most nb_char bound characters
-- from standard input.
-- Make result available in last_string.
-- Was declared in CONSOLE as synonym of read_stream and readstream.
local
new_count: INTEGER;
str_area: ANY
do
last_string.resize (nb_char);
str_area := last_string.area;
new_count := console_readstream (file_pointer, $str_area, nb_char);
last_string.set_count (new_count)
end;
read_word is
-- Read a new word from standard input.
-- Make result available in last_string.
-- Was declared in CONSOLE as synonym of read_word and readword.
local
str_area: ANY;
str_cap: INTEGER;
done: BOOLEAN;
read: INTEGER
do
from
str_area := last_string.area;
str_cap := last_string.capacity
until
done
loop
read := read + console_readword (file_pointer, $str_area, str_cap, read);
if read > str_cap then
last_string.resize (str_cap + 1024);
str_area := last_string.area;
str_cap := last_string.capacity;
read := read - 1
else
last_string.set_count (read);
done := true
end
end;
if read < 1024 then
last_string.resize (read)
end;
separator := console_separator (file_pointer)
end;
readword is
-- Read a new word from standard input.
-- Make result available in last_string.
-- Was declared in CONSOLE as synonym of read_word and readword.
local
str_area: ANY;
str_cap: INTEGER;
done: BOOLEAN;
read: INTEGER
do
from
str_area := last_string.area;
str_cap := last_string.capacity
until
done
loop
read := read + console_readword (file_pointer, $str_area, str_cap, read);
if read > str_cap then
last_string.resize (str_cap + 1024);
str_area := last_string.area;
str_cap := last_string.capacity;
read := read - 1
else
last_string.set_count (read);
done := true
end
end;
if read < 1024 then
last_string.resize (read)
end;
separator := console_separator (file_pointer)
end;
read_character is
-- Read a new character from standard input.
-- Make result available in last_character.
-- Was declared in CONSOLE as synonym of read_character and readchar.
do
last_character := console_readchar (file_pointer)
end;
readchar is
-- Read a new character from standard input.
-- Make result available in last_character.
-- Was declared in CONSOLE as synonym of read_character and readchar.
do
last_character := console_readchar (file_pointer)
end;
next_line is
-- Move to next input line on standard input.
do
console_next_line (file_pointer)
end;
feature -- Output
put_character (c: CHARACTER) is
-- Write c at end of default output.
-- Was declared in CONSOLE as synonym of put_character and putchar.
do
console_pc (file_pointer, c)
end;
putchar (c: CHARACTER) is
-- Write c at end of default output.
-- Was declared in CONSOLE as synonym of put_character and putchar.
do
console_pc (file_pointer, c)
end;
put_string (s: STRING) is
-- Write s at end of default output.
-- Was declared in CONSOLE as synonym of put_string and putstring.
local
external_s: ANY
do
if s.count /= 0 then
external_s := s.to_c;
console_ps (file_pointer, $external_s, s.count)
end
end;
putstring (s: STRING) is
-- Write s at end of default output.
-- Was declared in CONSOLE as synonym of put_string and putstring.
local
external_s: ANY
do
if s.count /= 0 then
external_s := s.to_c;
console_ps (file_pointer, $external_s, s.count)
end
end;
put_real (r: REAL) is
-- Write r at end of default output.
-- Was declared in CONSOLE as synonym of put_real and putreal.
do
console_pr (file_pointer, r)
end;
putreal (r: REAL) is
-- Write r at end of default output.
-- Was declared in CONSOLE as synonym of put_real and putreal.
do
console_pr (file_pointer, r)
end;
put_double (d: DOUBLE) is
-- Write d at end of default output.
-- Was declared in CONSOLE as synonym of put_double and putdouble.
do
console_pd (file_pointer, d)
end;
putdouble (d: DOUBLE) is
-- Write d at end of default output.
-- Was declared in CONSOLE as synonym of put_double and putdouble.
do
console_pd (file_pointer, d)
end;
put_integer (i: INTEGER) is
-- Write i at end of default output.
-- Was declared in CONSOLE as synonym of put_integer and putint.
do
console_pi (file_pointer, i)
end;
putint (i: INTEGER) is
-- Write i at end of default output.
-- Was declared in CONSOLE as synonym of put_integer and putint.
do
console_pi (file_pointer, i)
end;
put_boolean (b: BOOLEAN) is
-- Write b at end of default output.
-- Was declared in CONSOLE as synonym of put_boolean and putbool.
do
if b then
put_string ("true")
else
put_string ("false")
end
end;
putbool (b: BOOLEAN) is
-- Write b at end of default output.
-- Was declared in CONSOLE as synonym of put_boolean and putbool.
do
if b then
put_string ("true")
else
put_string ("false")
end
end;
new_line is
-- Write line feed at end of default output.
do
console_tnwl (file_pointer)
end;
feature {NONE} -- Implementation
Count: INTEGER is 1;
-- Useless for CONSOLE class.
Empty: BOOLEAN is false;
-- Useless for CONSOLE class.
console_def (number: INTEGER): POINTER is
-- Convert number to the corresponding
-- file descriptor.
external
"C | %"eif_console.h%""
end;
console_eof (file: POINTER): BOOLEAN is
external
"C (FILE *): EIF_BOOLEAN | %"eif_console.h%""
end;
console_separator (file: POINTER): CHARACTER is
-- ASCII code of character following last word read
external
"C (FILE *): EIF_CHARACTER | %"eif_console.h%""
end;
console_ps (file: POINTER; s_name: POINTER; length: INTEGER) is
-- Write string s at end of file
external
"C (FILE *, char *, EIF_INTEGER) | %"eif_console.h%""
end;
console_pr (file: POINTER; r: REAL) is
-- Write real r at end of file
external
"C (FILE *, EIF_REAL) | %"eif_console.h%""
end;
console_pc (file: POINTER; c: CHARACTER) is
-- Write character c at end of file
external
"C (FILE *, EIF_CHARACTER) | %"eif_console.h%""
end;
console_pd (file: POINTER; d: DOUBLE) is
-- Write double d at end of file
external
"C (FILE *, EIF_DOUBLE) | %"eif_console.h%""
end;
console_pi (file: POINTER; i: INTEGER) is
-- Write integer i at end of file
external
"C (FILE *, EIF_INTEGER) | %"eif_console.h%""
end;
console_tnwl (file: POINTER) is
-- Write a new_line to file
external
"C (FILE *) | %"eif_console.h%""
end;
console_readreal (file: POINTER): REAL is
-- Read a real number from the console
external
"C (FILE *): EIF_REAL | %"eif_console.h%""
end;
console_readchar (file: POINTER): CHARACTER is
-- Read a character from the console
external
"C (FILE *): EIF_CHARACTER | %"eif_console.h%""
end;
console_readint (file: POINTER): INTEGER is
-- Read an integer from the console
external
"C (FILE *): EIF_INTEGER | %"eif_console.h%""
end;
console_readdouble (file: POINTER): DOUBLE is
-- Read a double from the console
external
"C (FILE *): EIF_DOUBLE | %"eif_console.h%""
end;
console_readword (file: POINTER; a_string: POINTER; length, begin: INTEGER): INTEGER is
-- Read a string excluding white space and stripping
-- leading white space from file into a_string.
-- White space characters are: blank, new_line,
-- tab, vertical tab, formfeed or end of file.
-- If it does not fit, result is length - begin + 1,
-- otherwise result is number of characters read.
external
"C (FILE *, char *, EIF_INTEGER, EIF_INTEGER): EIF_INTEGER | %"eif_console.h%""
end;
console_readline (file: POINTER; a_string: POINTER; length, begin: INTEGER): INTEGER is
-- Read a stream from the console
external
"C (FILE *, char *, EIF_INTEGER, EIF_INTEGER): EIF_INTEGER | %"eif_console.h%""
end;
console_next_line (file: POINTER) is
-- Move to next input line on standard input.
external
"C (FILE *) | %"eif_console.h%""
end;
console_readstream (file: POINTER; a_string: POINTER; length: INTEGER): INTEGER is
-- Read a stream from the console
external
"C (FILE *, char *, EIF_INTEGER): EIF_INTEGER | %"eif_console.h%""
end;
file_close (file: POINTER) is
-- Close file
external
"C (FILE *) | %"eif_console.h%""
alias
"console_file_close"
end;
end -- class CONSOLE
|