Skip to content

Instantly share code, notes, and snippets.

@kjseefried
Created July 15, 2014 00:35
Show Gist options
  • Save kjseefried/1f09d70e015af3c20a03 to your computer and use it in GitHub Desktop.
Save kjseefried/1f09d70e015af3c20a03 to your computer and use it in GitHub Desktop.
Example file utility in Ada from PB
with Ada.Text_IO; with ADA.IO_EXCEPTIONS;
with Ada.Command_Line;
with Ada.Directories;
with Ada.Strings.Bounded;
procedure FileInfo is
-- rename used packages
package IO renames Ada.Text_IO;
package FS renames Ada.Directories;
package Cmd renames Ada.Command_Line;
-- alias the File_Size type
subtype Size_Type is FS.File_Size;
-- create bounded string
Max_Length : constant Positive := 512;
package FileName_String is new Ada.Strings.Bounded.Generic_Bounded_Length(Max_Length);
use FileName_String;
-- name of the info file
FileName : Bounded_String;
-- size of the file
FileSize : Size_Type;
begin
-- abort program if less than 1 argument
if 1 > Cmd.Argument_Count then
IO.Put_Line(Cmd.Command_Name & " requires at least 1 argument.");
Cmd.Set_Exit_Status(Cmd.Failure);
return;
end if;
-- get the first argument as input file
FileName := To_Bounded_String(Cmd.Argument(1));
-- check file existance
if FS.Exists(To_String(FileName)) then
IO.Put_Line("File '" & To_String(FileName) & "' exists.");
-- check file type
case FS.Kind(To_String(FileName)) is
when FS.Directory =>
IO.Put_Line(To_String(FileName) & " is a directory.");
when FS.Ordinary_File =>
IO.Put_Line(To_String(FileName) & " is an ordinary file.");
when FS.Special_File =>
IO.Put_Line(To_String(FileName) & " is a special file.");
end case;
-- get file size
FileSize := FS.Size(To_String(FileName));
IO.Put_Line("File size is " & Size_Type'Image(FileSize) & " bytes.");
else
IO.Put_Line("File '" & To_String(FileName) & "' does not exist.");
end if;
exception
when Constraint_Error =>
IO.Put_Line(Cmd.Command_Name & " requires at least 1 argument.");
when ADA.IO_EXCEPTIONS.NAME_ERROR =>
null;
end FileInfo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment