diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2016-11-04 08:50:12 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2016-11-04 09:26:34 +0200 |
commit | 89424b41ef62430a49012c5c57b1979f29029505 (patch) | |
tree | 04b5b8e9ed16c4801b55d416e2b11bf7813a9853 /tests/test/script/driver.cxx | |
parent | 89a997464b9ffd02580c5316b471dbd0779d86b9 (diff) |
Implement concurrent_runner
Diffstat (limited to 'tests/test/script/driver.cxx')
-rw-r--r-- | tests/test/script/driver.cxx | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/test/script/driver.cxx b/tests/test/script/driver.cxx new file mode 100644 index 0000000..b81172e --- /dev/null +++ b/tests/test/script/driver.cxx @@ -0,0 +1,76 @@ +// file : tests/test/script/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include <limits> // numeric_limits +#include <string> +#include <cassert> +#include <ostream> // endl, *bit +#include <iostream> +#include <exception> + +using namespace std; + +int +main (int argc, char* argv[]) +{ + // Usage: driver [-i <int>] [-s <int>] (-o <string>)* (-e <string>)* + // + int status (256); + int ifd (3); + + cout.exceptions (ostream::failbit | ostream::badbit); + cerr.exceptions (ostream::failbit | ostream::badbit); + + for (int i (1); i < argc; ++i) + { + string o (argv[i++]); + assert (i < argc); + + string v (argv[i]); + + auto toi = [] (const string& s) -> int + { + try + { + return stoi (s); + } + catch (const exception&) + { + assert (false); + } + }; + + if (o == "-i") + { + assert (ifd == 3); // Make sure is not set yet. + + ifd = toi (v); + assert (ifd >= 0 && ifd < 3); + + if (ifd == 0) + cin.ignore (numeric_limits<streamsize>::max ()); + else + (ifd == 1 ? cout : cerr) << cin.rdbuf (); + } + else if (o == "-o") + { + cout << v << endl; + } + else if (o == "-e") + { + cerr << v << endl; + } + else if (o == "-s") + { + assert (status == 256); // Make sure is not set yet. + + status = toi (v); + assert (status >= 0 && status < 256); + } + else + assert (false); + } + + return status == 256 ? 0 : status; +} |