aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/utility.txx
blob: cdf510fb09c109cf14e755b85aa8400cb67f0ef9 (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
// file      : libbuild2/utility.txx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

namespace build2
{
  template <typename I, typename F>
  void
  append_option_values (cstrings& ss, const char* o, I b, I e, F&& get)
  {
    if (b != e)
    {
      ss.reserve (ss.size () + (e - b));

      for (; b != e; ++b)
      {
        ss.push_back (o);
        ss.push_back (get (*b));
      }
    }
  }

  template <typename I, typename F>
  void
  append_option_values (sha256& cs, const char* o, I b, I e, F&& get)
  {
    for (; b != e; ++b)
    {
      cs.append (o);
      cs.append (get (*b));
    }
  }

  template <typename I, typename F>
  void
  append_combined_option_values (strings& ss, const char* o, I b, I e, F&& get)
  {
    if (b != e)
    {
      ss.reserve (ss.size () + (e - b));

      for (; b != e; ++b)
        ss.push_back (string (o) += get (*b));
    }
  }

  template <typename K>
  basic_path<char, K>
  relative (const basic_path<char, K>& p)
  {
    using path = basic_path<char, K>;

    const dir_path& b (*relative_base);

    if (p.simple () || b.empty ())
      return p;

    if (p.sub (b))
      return p.leaf (b);

    if (p.root_directory () == b.root_directory ())
    {
      path r (p.relative (b));

      if (r.string ().size () < p.string ().size ())
        return r;
    }

    return p;
  }
}