aboutsummaryrefslogtreecommitdiff
path: root/tests/dir-iterator/driver.cxx
blob: d3d25b82be29d602b8810b065240ada3ca88c053 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// file      : tests/dir-iterator/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 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/utility>    // operator<<(ostream, exception)
#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)];
}

// @@ Should we make the test silent unless -v arg passed. In silen mode could
// compare the output with a set of predefined dir entries.
//
int
main (int argc, const char* argv[])
{
  if (!(argc == 2 || (argc == 3 && argv[1] == string ("-v"))))
  {
    cerr << "usage: " << argv[0] << " [-v] <dir>" << endl;
    return 1;
  }

  bool v (argc == 3);
  const char* d (argv[argc - 1]);

  try
  {
    for (const dir_entry& de: dir_iterator (dir_path (d)))
    {
      entry_type lt (de.ltype ());
      entry_type t (lt == entry_type::symlink ? de.ltype () : lt);
      const path& p (de.path ());

      if (v)
      {
        cerr << lt << " ";

        if (lt == entry_type::symlink)
          cerr << t;
        else
          cerr << "   ";

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