|
This part of the guide contains detailed descriptions of the
BUSH built-in packages.
4.2 Console Input/Output (Text_IO)
The Text_IO package handles reading and writing to the console (or UNIX
tty device) and text files. (More on text files will be said in the next
section.) With the BUSH Text_IO package routines, "Text_IO" doesn't
have to be prefixed to the routine name.
put_line( [f,] e )
Write expression e with an appended line feed (new line) character
Example: put_line( 3*2 );
Ada Equivalent: Ada.Text_IO.Put_Line, Ada.Strings.Unbounded.Text_IO.Put_Line,
etc.
Parameters:
f |
in |
file_type |
standard_output |
file to direct output to |
e |
in |
not limited |
required |
the expressions to write |
put_line can take any type of parameter except limited types. This includes
enumerated types. Output can be redirected to standard error by using standard_error
as the file. The default file is standard_output.
=> put_line( standard_error, "an error occurred!" ); |
put( [f,] e [, p] )
Write expression e with optional numeric formatting
Example: put( 3*2 );
Ada Equivalent: Ada.Text_IO.Put, Ada.Strings.Unbounded.Text_IO.Put,
Ada.Text_IO.Editing
Parameters:
f |
in |
file_type |
standard_output |
the file to direct output to |
e |
in |
not limited |
required |
the expression to write |
p |
in |
string |
none |
format picture to write with |
put can take any type of parameter except limited types. This includes
enumerated types. Output can be redirected to standard error by using standard_error
as the file. The default file is standard_output. |
new_line [(f)]
write a line feed (new line)
Example: new_line;
Ada Equivalent: Ada.Text_IO.New_Line
Parameters:
f |
in |
file_type |
standard_output |
file to redirect output to |
Output can be redirected to standard error by using standard_error as
the file. The default file is standard_output. |
s := get_line [( f )]
read a string until the end of line is encountered. The default file
is standard_input.
Example: s := get_line;
Ada Equivalent: Ada.Text_IO.Get_Line, Ada.Strings.Unbounded.Text_IO
Parameters:
f |
in |
file_type |
standard_input |
the file to read input from |
s |
return value |
string |
required |
the string that was read |
|
get( [f,] c )
read the first character from a line on standard input and return it
as c
Example: get( ch );
Ada Equivalent: Ada.Text_IO.Get_Line, Ada.Strings.Unbounded.Text_IO
Parameters:
f |
in |
file_type |
standard_input |
the file to read input from |
c |
out |
character |
required |
the character read |
|
? e
put_line expression e to standard output
Restrictions: disabled with pragma ada_95
Example: ? 3*2;
Ada Equivalent: none (AdaScript extension taken from BASIC language)
Parameters:
e |
in |
not limited |
required |
the expression to write |
The question mark command "?" is a short-form for put_line to
standard output. It is intended as a command line convenience. |
c := inkey
Read a character from standard input. Don't write the character.
Restrictions: disabled with pragma ada_95
Example: ch := inkey;
Ada Equivalent: none (AdaScript extension)
Parameters:
c |
return value |
character |
required |
the key that was pressed |
get is provided for compatibility for Ada 95 but get is not very convenient.
inkey is a more useful function for reading a keypress. |
The file_type constants standard_input, standard_output and standard_error
can be used to redirect I/O to standard input, output or error respectively.
Ada's get_immediate is not implemented.
4.3 Text File I/O (Text_IO)
BASH opens files on the basis of file numbers or "file descriptors". In
AdaScript, files are declared using a file_type variable.
AdaScript's I/O system implements a subsection of the Ada Text_IO library.
The Text_IO package has a predefined enumerated type, file_mode, which
determines if a file should be read (in_file), written (out_file) or should
append to an existing file (append_file).
open( f, m, p )
Open existing file f as pathname p in file mode m.
Example: open( data, in_mode, "/home/ken/data.txt" );
Restrictions: out_mode not allowed in restricted shells
Ada Equivalent: Ada.Text_IO.Open
Parameters:
f |
out |
file_type |
required |
file variable representing the open file |
m |
in |
file_mode |
required |
open the file for reading, writing or appending |
p |
in |
string |
required |
the pathname of the file to open |
|
create( f [, m] [, n] )
Create a new file or open an existing file for writing. Also, create
temp files.
Example: create( temp_file );
Restrictions: out_mode not allowed in restricted shells
Ada Equivalent: Ada.Text_IO.Create
Parameters:
f |
out |
file_type |
required |
file variable representing the open file |
m |
in |
file_mode |
out_file |
open the file writing or appending |
n |
in |
string |
a temp file |
the pathname of the file to create. If none is given, a temp file will
be created. |
|
reset( f [, m] )
Reopen open file f in optional new file mode m
Example: reset( temp_file, in_mode );
Restrictions: out_mode not allowed in restricted shells
Ada Equivalent: Ada.Text_IO.Reset
Parameters:
f |
in out |
file_type |
required |
file variable representing the file to reopen |
m |
in |
file_mode |
current mode |
the new mode for the file. The default is to leave the mode unchanged. |
|
|
is_open( f )
Return true if file f is open
Example: if is_open( data ) then ...
Ada Equivalent: Ada.Text_IO.Is_Open
Parameters:
f |
in |
file_type |
required |
file variable representing the file to test |
|
end_of_file( f )
Return true if all data has been read from in_mode file f
Example: if end_of_file( data ) then ...
Ada Equivalent: Ada.Text_IO.End_Of_File
Parameters:
f |
in |
file_type |
required |
file variable representing the file to test |
|
end_of_line( f )
Return true if at the end of line for file f
Example: if end_of_line( data ) then ...
Ada Equivalent: Ada.Text_IO.End_Of_Line
Parameters:
f |
in |
file_type |
required |
file variable representing the file to test |
|
close( f )
Close and save the open file f
Example: close( data );
Ada Equivalent: Ada.Text_IO.Close
Parameters:
f |
in out |
file_type |
required |
file variable representing the file to close |
|
delete( f )
Close and delete the open file f
Example: delete( temp_file );
Restriction: not allowed in restricted shells
Ada Equivalent: Ada.Text_IO.Delete
Parameters:
f |
in out |
file_type |
required |
file variable representing the file to delete |
|
skip_line( [f] )
Read a line from standard input and discard it
Example: s := get_line;
Ada Equivalent: Ada.Text_IO.Skip_Line
Parameters:
f |
in |
file_type |
standard_input |
the file to read input from |
|
set_input( f )
Redirect standard_input to an open file
Example: set_input( data_file );
Ada Equivalent: Ada.Text_IO.Set_Input
Parameters:
f |
in |
file_type |
required |
the file to read input from |
Use set_input( standard_input ) to return to standard input. |
set_output( f )
Redirect standard_output to an open file
Example: set_output( data_file );
Ada Equivalent: Ada.Text_IO.Set_Output
Parameters:
f |
in |
file_type |
required |
the file to write output to |
Use set_output( standard_output ) to return to standard output. |
set_error( f )
Redirect standard_error to an open file
Example: set_error( data_file );
Ada Equivalent: Ada.Text_IO.Set_Error
Parameters:
f |
in |
file_type |
required |
the file to write error output to |
Use set_error( standard_error ) to return to standard error. |
i := line( f )
Return the number of lines read or written (with put_line, the number
of lines explicitly put to that file)
Example: if line( f ) > 33 then ...
Ada Equivalent: Ada.Text_IO.Line
Parameters:
i |
return value |
integer |
required |
the number of lines read or written |
f |
in |
file_type |
required |
file variable representing the file to test |
This function will work with standard_input, standard_output and standard_error.
However, it does not take into account lines read or written by operating
system commands. |
s := name( f )
Return the pathname of the open file.
Example: put_line( "error in file " & name( f ) );
Ada Equivalent: Ada.Text_IO.Name
Parameters:
s |
return value |
string |
required |
the pathname of the file |
f |
in |
file_type |
required |
file variable representing the file |
This function will work with standard_input, standard_output and standard_error.
However, it does not return a pathname since these files have no known
pathnames. |
m := mode( f )
Return the mode of the open file
Example: if mode( f ) = in_file then ...
Ada Equivalent: Ada.Text_IO.Mode
Parameters:
m |
return value |
file_mode |
required |
the current mode of the file |
f |
in |
file_type |
required |
file variable representing the file |
|
This function will work with standard_input, standard_output and standard_error.
The Text_IO package also defines three file aliases. These can be used
anywhere a file_type variable is expected.
-
current_input -- an alias for the current input file
-
current_output-- an alias for the current output file
-
current_error -- an alias for the current error file
The put, put_line, get, get_line and new_line commands are used to read
and write files.
=> f : file_type
=> create( f, out_file, "data.out" )
=> put_line( f, "Added to file" )
=> ? name( f )
data.out
=> ? mode( f )
out_file
=> ? line( f )
1
standard_input, standard_output and standard_error can be redirected
with the appropriate command to an open file.
=> set_output( f )
From now on, all output going to standard output is written to the file_type
variable f. The current_output function refers to the file being written
to (in this case, f). Output redirected to f is not counted by the line
function.
The file is closed with close.
=> close( f )
Back to Top
|