author | František Kučera <franta-hg@frantovo.cz> |
Thu, 17 Jan 2019 13:13:06 +0100 | |
branch | v_0 |
changeset 13 | b1e861523740 |
parent 7 | 95e647bf1922 |
child 20 | 45d60cf711e1 |
permissions | -rw-r--r-- |
6 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
0
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#pragma once |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <locale.h> |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <string> |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <vector> |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <locale> |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <codecvt> |
7
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
26 |
#include <iostream> |
0
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
using namespace std; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
namespace relpipe { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
namespace cli { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
/** |
2 | 34 |
* a common header-only library for CLI programs |
0
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
*/ |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
class CLI { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
public: |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
CLI(int argc, char* argv[]) { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
setlocale(LC_ALL, ""); |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
this->argc = &argc; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
this->argv = &argv; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
program = convertor.from_bytes(argv[0]); |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
for (int i = 1; i < argc; i++) { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
args.insert(args.end(), convertor.from_bytes(argv[i])); |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
CLI(const CLI& orig) { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
virtual ~CLI() { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
const wstring programName() { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
return (const wstring) program; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
const vector<wstring> arguments() { |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
return (const vector<wstring>)args; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
static const int EXIT_CODE_SUCCESS = 0; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
static const int EXIT_CODE_UNEXPECTED_ERROR = 1; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
static const int EXIT_CODE_BAD_SYNTAX = 3; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
static const int EXIT_CODE_UNKNOWN_COMMAND = 4; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
static const int EXIT_CODE_DATA_ERROR = 5; |
13
b1e861523740
add BAD_CLI_ARGUMENTS exit code
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
72 |
static const int EXIT_CODE_BAD_CLI_ARGUMENTS = 6; |
0
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
7
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
74 |
/** |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
75 |
* std::cin and std::cout are "tied" by default i.e. reading from std::cin causes flush of the std::cout. |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
76 |
* Call this to "untie" them and have less write() calls (better buffering). |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
77 |
*/ |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
78 |
static void untieStdIO() { |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
79 |
std::cin.tie(nullptr); |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
80 |
} |
95e647bf1922
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
81 |
|
0
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
private: |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
int* argc; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
char*** argv; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
wstring program; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
vector<wstring> args; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
}; |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
|
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
} |
00d1dd4f816a
create netbeans project, add CLI.h from relpipe-in-cli / relpipe-in-fstab projects
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
} |