Line Filters in Ada
Here’s the translation of the Go line filter program to Ada, with explanations in Markdown format suitable for Hugo:
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
Here’s an example line filter in Ada that writes a capitalized version of all input text. You can use this pattern to write your own Ada line filters.
In this Ada version:
- We use the
Ada.Text_IO
package for input/output operations. Ada.Characters.Handling
provides theTo_Upper
function for converting strings to uppercase.Ada.Strings.Unbounded
is used for handling variable-length strings.
The main loop reads lines from standard input using Get_Line
until it reaches the end of file. Each line is converted to uppercase using To_Upper
and then printed to standard output with Put_Line
.
Error handling is done using an exception handler. If any error occurs during the execution, it will be caught, printed to standard error, and the program will exit with a failure status.
To try out our line filter, first make a file with a few lowercase lines.
Then compile and use the line filter to get uppercase lines.
This Ada implementation provides the same functionality as the original Go program, reading input line by line, converting each line to uppercase, and writing the result to standard output.