18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

34 <strong>Ada</strong> <strong>in</strong>troduction: Part 1<br />

If the program echo is compiled with the package <strong>Ada</strong>.Command_L<strong>in</strong>e then the programmer has available the<br />

follow<strong>in</strong>g function calls:<br />

call of function<br />

Argument_Count<br />

Argument(1)<br />

Argument(2)<br />

Note:<br />

Returns<br />

The number of command l<strong>in</strong>e arguments. In this case,<br />

two.<br />

A str<strong>in</strong>g represent<strong>in</strong>g the first command l<strong>in</strong>e argument.<br />

In this case “Hello”.<br />

A str<strong>in</strong>g represent<strong>in</strong>g the second command l<strong>in</strong>e<br />

argument. In this case “there!”.<br />

It would be an error detected at run-time to access argument(3).<br />

The code for the program echo is as follows:<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Command_L<strong>in</strong>e;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Command_L<strong>in</strong>e;<br />

procedure Echo is<br />

beg<strong>in</strong><br />

for I <strong>in</strong> 1 .. Argument_Count loop --For each argument<br />

Put( Argument(I) ); -- Pr<strong>in</strong>t it<br />

if I /= Argument_Count then -- If not last<br />

Put(" "); -- Pr<strong>in</strong>t separator<br />

end if;<br />

end loop;<br />

New_L<strong>in</strong>e;<br />

end Echo;<br />

Note:<br />

See how the package <strong>Ada</strong>.Command_L<strong>in</strong>e has been used here.<br />

3.10.1 Putt<strong>in</strong>g it all together<br />

If compiled to the executable file echo, the program could be run on a Unix or MSDOS system to implement the<br />

command echo as follows:<br />

% echo Hello there!<br />

Which when run would write:<br />

Hello there!<br />

3.11 A better cat program<br />

By us<strong>in</strong>g, the package <strong>Ada</strong>.Command_l<strong>in</strong>e a better version of the cat program can be written. In this new<br />

version the files to be listed to the term<strong>in</strong>al are specified after the executable program name.<br />

The follow<strong>in</strong>g procedures and functions are used to control the read<strong>in</strong>g of data from a file.<br />

Function/Procedure<br />

Open<br />

Close<br />

Effect<br />

Opens an exist<strong>in</strong>g file. A file descriptor to this file is<br />

returned as the result. A file descriptor is of type:<br />

File_type <strong>in</strong> the package <strong>Ada</strong>.Text_Io.<br />

Close the open file.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!