0% found this document useful (0 votes)
21 views

Variable-Length String Input in Ada

The document discusses using a recursive function called get_line to allow for variable-length string input in Ada. Get_line reads characters from a file until it reaches the end of the line, returning a string of the appropriate length without needing to declare a fixed size. This approach avoids needing excessive large string declarations or complex variable string types. The function is demonstrated being used in a simple command line interpreter to read commands without knowing the length in advance. Feedback from others helped improve earlier versions of describing this approach.

Uploaded by

9rl0im17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Variable-Length String Input in Ada

The document discusses using a recursive function called get_line to allow for variable-length string input in Ada. Get_line reads characters from a file until it reaches the end of the line, returning a string of the appropriate length without needing to declare a fixed size. This approach avoids needing excessive large string declarations or complex variable string types. The function is demonstrated being used in a simple command line interpreter to read commands without knowing the length in advance. Feedback from others helped improve earlier versions of describing this approach.

Uploaded by

9rl0im17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Variable-Length String Input in Ad a

Jeffrey R . Carter
Senior Engineer, Softwar e
Martin Marietta Astronautics Grou p
P. O . Box 17 9
Denver, CO 8020 1

On a number of occasions I have found myself discussing how long a string needs to be declare d
for input, usually user input . Is twenty characters enough? Is eighty too many? Doesn't 25 6
use too much memory? This question comes up even when dealing with some form of variabl e
string . The answer I always give is that the software should accept as many characters as th e
user enters, but have had to admit that declaring a string with POSITIVE'LAST characters is a
little excessive . I felt that there must be some way to use Ada to create a string of just the righ t
length, and have now figured out how to do that .

The basic concept is a recursive function which eventually returns a string of the lengt h
entered and skips the line terminator which marks the end of the string . This function resul t
can be used to initialize a constant string or be passed as the actual parameter to a subprogram
which has a string formal parameter of mode in . This function, which I call get_line, is :

with text_io ;
function get_line (file : text_io .file_type := text_io .current_input )
return string i s
char : character ;
begin °-• get_line
if text_io .end_of_line (file) the n
-° skip line terminator--ready to "get" next "line "
text_Lo .skip_line (file) ;
return "" ; — null string terminates recursio n
else
text Lo .get (file, char) ;
return char & get_line (file => file) ;
end if ;
end get_line ;

The default parameter value allows the function to be used without supplying an actua l
parameter . When this is done, the function reads from the default input file, which is the sam e
file read by the text_10 input subprograms which do not have file parameters .

The function can be used as described above ; for example, a simple command-line interprete r
might look like :

with text_io, get_line ;


procedure command_1ine_interpreter i s
prompt : constant string ">" ;

logout_command : constant string := "logout" ;


copy_command : constant string := "copy" ;
delete_command : constant string := "del" ;
catalog command : constant string "cat" ;

procedure copy_file (command_line : in string) is separate ;


procedure delete file (command_line : in string) is separate ;

Ada Letters, May/June 1989 Page 103 Volume 1X, Number 4


procedure catalog (command_line : in string) is separate ;
procedure execute_ program (command_line in string) is separate ;
begin -- command _ line_ interprete r
all commands : loo p
text_io .put (prompt) ;
get_command : declare
command : constant string := get_line ;
begin -- get_comman d
exit all_commands whe n
command'length >= logout_command'length and the n
command (command'firs
t command'first + logout_command' length - 1
_= logout comman d

if command'length >= copy command'length and the n


command (command'firs
t command'first + copy _ command' length - 1
= copy comman d
then
copy file (command_line => command) ;
elsif command'length >=delete__command'length and the n
command (command'firs
t command'first + delete_command'length - 1
= delete comman d
then
delete_file (command_line => command) ;
elsif command'length >= catalog_command'length and the n
command (command'firs t
command'first + catalog_command'length - 1
= catalog comman d
then
catalog (command_line => command) ;
else -- not recognized ; assume it's an executable progra m
execute program (command line => command) ;
end if ; — —
end get_command ;
end loop all__ commands ;
end command _ line_ interpreter ;

Apparently no one has described this approach to variable-length string input before . Muc h
work has been done on variable-string abstract data types in Ada . This simple function migh t
serve as an argument against the need for such a type, especially if it is being used only t o
facilitate input .

I would like to thank Geoff Mendal, technical editor of Ada Letters, for his valuable comments
on earlier versions of this article .

Ada Letters, May/June 1989 Page 104 Volume IX, Number 4

You might also like