author | František Kučera <franta-hg@frantovo.cz> |
Wed, 27 Dec 2023 23:25:06 +0100 | |
branch | v_0 |
changeset 33 | 4620bba4fa40 |
parent 30 | 02972f051744 |
child 35 | dcb2ad86e23c |
permissions | -rw-r--r-- |
0 | 1 |
/** |
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
2 |
* OHP3D |
0 | 3 |
* Copyright © 2023 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, version 3 of the License. |
|
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 |
*/ |
|
17 |
||
18 |
#pragma once |
|
19 |
||
20 |
#include <vector> |
|
21 |
#include <string> |
|
22 |
||
23 |
#include "Configuration.h" |
|
24 |
||
25 |
class CLIParser { |
|
26 |
private: |
|
27 |
||
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
28 |
static const std::string OPTION_DOCUMENT; |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
static const std::string OPTION_SHADER; |
0 | 30 |
static const std::string OPTION_BACKGROUND_COLOR; |
31 |
static const std::string OPTION_ROOT_WINDOW; |
|
32 |
||
33 |
const std::string |
|
34 |
readNext(const std::vector<std::string>& arguments, int& i) { |
|
35 |
if (i < arguments.size()) return arguments[i++]; |
|
36 |
else throw std::logic_error("Missing CLI argument" |
|
37 |
+ (i > 0 ? (" after " + arguments[i - 1]) : "")); |
|
38 |
} |
|
39 |
||
40 |
bool parseBoolean(const std::string& value) { |
|
41 |
if (value == "true") return true; |
|
42 |
else if (value == "false") return false; |
|
43 |
else throw std::logic_error("Unable to parse boolean value: " |
|
44 |
+ value + " (expecting true or false)"); |
|
45 |
} |
|
46 |
||
47 |
unsigned long |
|
48 |
parseHexColor(const std::string& hex) { |
|
26
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
49 |
size_t count; |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
50 |
unsigned long rgb = std::stoul(hex, &count, 16); |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
51 |
if (count == 6 || count == 8 && hex.starts_with("0x")) return rgb; |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
52 |
else throw std::logic_error("Invalid hex color string"); |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
53 |
// the input should be (0x)?[0-9a-fA-F]{6}, however: |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
54 |
// values like 0x0123 are also accepted and interpreted as 000123 |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
55 |
// values like +0x012 are also accepted and interpreted as 000012 |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
56 |
} |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
57 |
|
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
58 |
unsigned long |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
59 |
parseUL(const std::string& str) { |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
60 |
int base = str.starts_with("0x") ? 16 : 10; |
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
61 |
return std::stoul(str, nullptr, base); |
0 | 62 |
} |
63 |
||
64 |
public: |
|
65 |
||
66 |
const Configuration parse(const std::vector<std::string>& arguments) { |
|
67 |
Configuration c; |
|
68 |
||
69 |
for (int i = 0; i < arguments.size();) { |
|
70 |
const std::string& option = readNext(arguments, i); |
|
71 |
||
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
72 |
if (option == OPTION_DOCUMENT) { |
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
73 |
Configuration::Document document; |
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
74 |
document.fileName = readNext(arguments, i); |
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
75 |
c.documents.push_back(document); |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
} else if (option == OPTION_SHADER) { |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
const auto type = readNext(arguments, i); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
const auto file = readNext(arguments, i); |
6
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
79 |
c.shaders.push_back({file, type}); |
0 | 80 |
} else if (option == OPTION_BACKGROUND_COLOR) { |
81 |
c.backgroundColor = parseHexColor(readNext(arguments, i)); |
|
82 |
} else if (option == OPTION_ROOT_WINDOW) { |
|
26
e7ceb915177e
support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
83 |
c.rootWindow = parseUL(readNext(arguments, i)); |
0 | 84 |
} else throw std::logic_error("Unsupported CLI option: " + option); |
85 |
} |
|
86 |
||
87 |
return c; |
|
88 |
} |
|
89 |
}; |
|
90 |
||
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
91 |
const std::string CLIParser::OPTION_DOCUMENT = "--document"; |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
const std::string CLIParser::OPTION_SHADER = "--shader"; |
0 | 93 |
const std::string CLIParser::OPTION_BACKGROUND_COLOR = "--background-color"; |
94 |
const std::string CLIParser::OPTION_ROOT_WINDOW = "--root-window"; |