Dir_iterators

Website:

https://github.com/pyjarrett/dir_iterators

Author:
  • Paul Jarrett
Maintainer:
  • Paul Jarrett <jarrett.paul.young@gmail.com>
License:

Apache-2.0

Version:

0.0.5

Alire CI:

Dependencies:

No dependency.

Dependents: Badge:

Ways of moving around directory trees

#dir #files #walk

Build Status Alire

Iterator-based directory walks

Provides convenient ways to walk directories based on Ada 2012 user-defined iterators.

Inspired by walkdir for Rust.

Walking a directory tree recursively

with Ada.Directories;
with Ada.Text_IO;
with Dir_Iterators.Recursive;

-- ...

Dir_Walk : constant Dir_Iterators.Recursive.Recursive_Dir_Walk
    := Dir_Iterators.Recursive.Walk (Dir);

for Dir_Entry of Dir_Walk loop
    Ada.Text_IO.Put_Line(Ada.Directories.Full_Name(Dir_Entry));
end loop;

Walking a directory tree recursively with a filter

Use a filter to prune directories and files from the walk.

with Ada.Directories;
with Ada.Text_IO;
with Dir_Iterators.Recursive;

package AD renames Ada.Directories;

-- ...

    procedure Foo (Include_Dot_Files : Boolean; Dir_Root : String) is
        function Filter (E : Ada.Directories.Directory_Entry_Type) return Boolean is
            Name : constant String := Ada.Directories.Simple_Name(E);
        begin
            return Include_Dot_Files
                or else (not (Name'Length > 1 and then Name(1) = '.'));
        end Filter;

        Walk : constant Dir_Iterators.Recursive.Recursive_Dir_Walk :=
            Dir_Iterators.Recursive.Walk (Dir_Root, Filter'Access);
    begin
        for Dir_Entry of Walk loop
            Ada.Text_IO.Put_Line(Ada.Directories.Full_Name(Dir_Entry));
        end loop;
    end Foo;