CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 28 Dec 2023 00:37:59 +0100
branchv_0
changeset 35 dcb2ad86e23c
parent 30 02972f051744
permissions -rw-r--r--
add undocumented option: --dpi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
     2
 * OHP3D
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "Configuration.h"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
class CLIParser {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
private:
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	static const std::string OPTION_BACKGROUND_COLOR;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	static const std::string OPTION_ROOT_WINDOW;
35
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    32
	static const std::string OPTION_DPI;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	const std::string
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	readNext(const std::vector<std::string>& arguments, int& i) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		else throw std::logic_error("Missing CLI argument"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
				+ (i > 0 ? (" after " + arguments[i - 1]) : ""));
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	bool parseBoolean(const std::string& value) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		if (value == "true") return true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		else if (value == "false") return false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		else throw std::logic_error("Unable to parse boolean value: "
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
				+ value + " (expecting true or false)");
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	unsigned long
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	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
    50
		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
    51
		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
    52
		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
    53
		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
    54
		// 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
    55
		// 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
    56
		// 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
    57
	}
e7ceb915177e support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    58
e7ceb915177e support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    59
	unsigned long
e7ceb915177e support both decimal and hexadecimal notations for --root-window
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    60
	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
    61
		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
    62
		return std::stoul(str, nullptr, base);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
public:
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	const Configuration parse(const std::vector<std::string>& arguments) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		Configuration c;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		for (int i = 0; i < arguments.size();) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			const std::string& option = readNext(arguments, i);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    73
			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
    74
				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
    75
				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
    76
				c.documents.push_back(document);
35
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    77
			} else if (option == OPTION_DPI) {
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    78
				// intentionally not documented
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    79
				// will be probably replaced with absolute resolution
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    80
				// or removed (dynamic rendering accorging to the zoom level)
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    81
				c.dpi = std::stod(readNext(arguments, i));
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
			} else if (option == OPTION_SHADER) {
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
				const auto type = readNext(arguments, i);
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
				const auto file = readNext(arguments, i);
6
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    85
				c.shaders.push_back({file, type});
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			} else if (option == OPTION_BACKGROUND_COLOR) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
				c.backgroundColor = parseHexColor(readNext(arguments, i));
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
			} 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
    89
				c.rootWindow = parseUL(readNext(arguments, i));
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
			} else throw std::logic_error("Unsupported CLI option: " + option);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		return c;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    97
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
    98
const std::string CLIParser::OPTION_SHADER = "--shader";
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
const std::string CLIParser::OPTION_BACKGROUND_COLOR = "--background-color";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
const std::string CLIParser::OPTION_ROOT_WINDOW = "--root-window";
35
dcb2ad86e23c add undocumented option: --dpi
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   101
const std::string CLIParser::OPTION_DPI = "--dpi";