author | František Kučera <franta-hg@frantovo.cz> |
Sat, 04 Dec 2021 21:14:51 +0100 | |
branch | v_0 |
changeset 27 | 5de9d5db27cc |
parent 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 |
|
20
45d60cf711e1
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
6 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
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
|
17 |
#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
|
18 |
|
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 |
#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
|
20 |
#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
|
21 |
#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
|
22 |
#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
|
23 |
#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
|
24 |
#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
|
25 |
#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
|
26 |
|
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 |
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
|
28 |
|
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 |
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
|
30 |
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
|
31 |
|
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 |
/** |
2 | 33 |
* 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
|
34 |
*/ |
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 |
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
|
36 |
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
|
37 |
|
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 |
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
|
39 |
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
|
40 |
|
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 |
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
|
42 |
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
|
43 |
|
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 |
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
|
45 |
|
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 |
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
|
47 |
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
|
48 |
} |
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 |
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
|
53 |
} |
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 |
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
|
56 |
} |
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 |
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
|
59 |
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
|
60 |
} |
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 |
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
|
63 |
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
|
64 |
} |
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 |
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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
|
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
|
73 |
/** |
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 |
* 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
|
75 |
* 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
|
76 |
*/ |
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 |
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
|
78 |
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
|
79 |
} |
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 |
|
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
}; |
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 |
} |