aboutsummaryrefslogtreecommitdiff
path: root/clean/types-parsers.cxx
blob: 31f3a8da6e8bc69fbce1b6a6bce039b4d6c97108 (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
// file      : clean/types-parsers.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <chrono>
#include <string> // strtoull()

#include <clean/types-parsers.hxx>

#include <clean/options-types.hxx>
#include <clean/clean-options.hxx> // cli namespace

using namespace std;
using namespace brep;

namespace cli
{
  void parser<toolchain_timeouts>::
  parse (toolchain_timeouts& x, bool& xs, scanner& s)
  {
    const char* o (s.next ());

    if (!s.more ())
      throw missing_value (o);

    string ov (s.next ());
    size_t p (ov.find ('='));

    timestamp now (system_clock::now ());

    // Convert timeout duration into the time point.
    //
    auto timeout = [o, &ov, &now] (const string& tm) -> timestamp
    {
      char* e (nullptr);
      uint64_t t (strtoull (tm.c_str (), &e, 10));

      if (*e != '\0' || tm.empty ())
        throw invalid_value (o, ov);

      if (t == 0)
        return timestamp_nonexistent;

      return now - chrono::duration<uint64_t, ratio<86400>> (t);
    };

    if (p == string::npos)
      x[string ()] = timeout (ov); // Default timeout.
    else
    {
      string k (ov, 0, p);
      if (k.empty ())
        throw invalid_value (o, ov);

      x[k] = timeout (string (ov, p + 1));
    }

    xs = true;
  }
}