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

namespace build2
{
  template <typename T>
  T
  run (const char* const* args,
       T (*f) (string&),
       bool err,
       bool ignore_exit,
       sha256* checksum)
  {
    process pr (start_run (args, err));
    ifdstream is (pr.in_ofd);

    T r;

    string l; // Last line of output.
    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);
    }

    is.close (); // Don't block.

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

    return r;
  }
}