aboutsummaryrefslogtreecommitdiff
path: root/tests/dir-iterator/driver.cxx
blob: cc1e7d6af6fff50e5c3962b570eb61c8bea64832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// file      : tests/dir-iterator/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cstddef> // size_t
#include <cassert>
#include <iostream>

#include <butl/path>
#include <butl/path-io>
#include <butl/filesystem>

using namespace std;
using namespace butl;

static const char* entry_type_string[] =
{
  "unk", "reg", "dir", "sym", "oth"
};

inline ostream&
operator<< (ostream& os, entry_type e)
{
  return os << entry_type_string[static_cast<size_t> (e)];
}

int
main (int argc, const char* argv[])
{
  if (argc != 2)
  {
    cerr << "usage: " << argv[0] << " <dir>" << endl;
    return 1;
  }

  try
  {
    for (const dir_entry& de: dir_iterator (dir_path (argv[1])))
    {
      cerr << de.ltype () << " ";

      if (de.ltype () == entry_type::symlink)
        cerr << de.type ();
      else
        cerr << "   ";

      cerr << " " << de.path () << endl;
    }
  }
  catch (const exception& e)
  {
    cerr << argv[1] << ": " << e.what () << endl;
    return 1;
  }
}