aboutsummaryrefslogtreecommitdiff
path: root/build2/utility.txx
blob: 8b5bdd269ff1a1858dcb92186df2f80684df24f5 (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
// file      : build2/utility.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

namespace build2
{
  template <typename K>
  basic_path<char, K>
  relative (const basic_path<char, K>& p)
  {
    typedef basic_path<char, K> path;

    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;
  }

  template <typename T>
  T
  run (const process_path& pp,
       const char* args[],
       T (*f) (string&),
       bool err,
       bool ignore_exit,
       sha256* checksum)
  {
    process pr (run_start (pp, args, err));

    T r;
    string l; // Last line of output.

    try
    {
      ifdstream is (move (pr.in_ofd));

      while (is.peek () != ifdstream::traits_type::eof () && // Keep last line.
             getline (is, l))
      {
        trim (l);

        if (checksum != nullptr)
          checksum->append (l);

        if (r.empty ())
          r = f (l);
      }
    }
    catch (const io_error&)
    {
      // Presumably the child process failed. Let run_finish() deal with that.
    }

    if (!(run_finish (args, err, pr, l) || ignore_exit))
      r = T ();

    return r;
  }
}