aboutsummaryrefslogtreecommitdiff
path: root/libbutl/default-options.cxx
blob: 28f6fb7fb2d9d8b496b8a3da62ec0f195db691b1 (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
70
71
72
73
// file      : libbutl/default-options.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/default-options.mxx>
#endif

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <vector>
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.default_options;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
import butl.path;
import butl.optional;
import butl.small_vector;
#endif

#endif

using namespace std;

namespace butl
{
  optional<dir_path>
  default_options_start (const optional<dir_path>& home,
                         const vector<dir_path>& dirs)
  {
    if (home)
      assert (home->absolute () && home->normalized ());

    if (dirs.empty ())
      return nullopt;

    // Use the first directory as a start.
    //
    auto i (dirs.begin ());
    dir_path d (*i);

    // Try to find a common prefix for each subsequent directory.
    //
    for (++i; i != dirs.end (); ++i)
    {
      bool p (false);

      for (;
           !(d.root () || (home && d == *home));
           d = d.directory ())
      {
        if (i->sub (d))
        {
          p = true;
          break;
        }
      }

      if (!p)
        return nullopt;
    }

    return d;
  }
}