aboutsummaryrefslogtreecommitdiff
path: root/tests/variable/driver.cxx
blob: 88167ad8bcfe42b3bf9f62bb238545df39c455a0 (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
// file      : tests/variable/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <ios>      // ios_base::failbit, ios_base::badbit
#include <string>
#include <cassert>
#include <cstddef>  // size_t
#include <iostream>

#include <butl/utility> // operator<<(ostream,exception)

#include <bbot/variable>

using namespace std;
using namespace butl;
using namespace bbot;

// Usage: argv[0] [-u]
//
// Read variables from STDIN (one per line) and serialize them to STDOUT (also
// one per line).
//
// -u  output variables being unquoted beforehand
//
int
main (int argc, char* argv[])
{
  assert (argc <= 2);
  bool unquote (false);

  if (argc == 2)
  {
    assert (argv[1] == string ("-u"));
    unquote = true;
  }

  cin.exceptions  (ios_base::badbit);
  cout.exceptions (ios_base::failbit | ios_base::badbit);

  string s;
  for (size_t l (1); getline (cin, s); ++l)
  {
    try
    {
      variable v (move (s));

      cout << (unquote
               ? v.unquoted ()
               : static_cast<const string&> (v))
           << '\n';
    }
    catch (const invalid_variable& e)
    {
      cerr << l << ':' << 1 + e.pos << ": error: " << e << endl;
      return 1;
    }
  }

  return 0;
}