summaryrefslogtreecommitdiff
path: root/src/benoit/arghandl.cc
blob: 5cae6ad8db0999e264bbb38bc038c7bd442fc936 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# include <benoit/archstr.hh>
# include <benoit/arghandl.hh>
# include <benoit/d/dobt.hh>
# include <benoit/d/imgfmt.hh>
# include <benoit/d/numthrds.hh>
# include <benoit/d/outimg.hh>
# include <benoit/d/resx.hh>
# include <benoit/d/resy.hh>
# include <benoit/exit.hh>
# include <benoit/helpscrn.hh>
# include <benoit/kernelstr.hh>
# include <benoit/log.hh>
# include <benoit/logfunc.hh>
# include <benoit/logfuncret.hh>
# include <benoit/print.hh>
# include <benoit/t/imgfmt.hh>
# include <fmt/core.h>
# include <string>
# include <unordered_map>
using namespace std::literals::string_literals;
void benoit::arghandl(int const & argc,char const * * & argv) {
	std::string const funcname = "benoit::arghandl(int const &,char const * * &)"s;
	benoit::logfunc(funcname);
	if(argc > 0x1) {
		benoit::log(fmt::format("{} argument(s) have been detected."s,(argc - 0x1)));
		for(int pos = 0x1;(pos < argc);++pos) {
			std::string arg = argv[pos];
			benoit::log(fmt::format("Found argument “{}”.",arg));
			std::string::size_type eqpos = arg.find("="s);
			if(eqpos != std::string::npos) {
				std::unordered_map<std::string,bool> strtobool = {
					{
						"false"s,
						false,
					},
					{
						"true"s,
						true,
					}
				};
				std::string invalvalforobj = "Unrecognised value “{}” for object “{}”."s;
				std::string obj = arg.substr(0x0,eqpos);
				benoit::log(funcname,fmt::format("Found object “{}”.",obj));
				std::string val = arg.substr(eqpos + 0x1);
				benoit::log(funcname,fmt::format("Found value “{}”.",val));
				if(obj == "force-backtrace"s) {
					if(!strtobool.contains(val)) {
						benoit::print(fmt::format(invalvalforobj,val,obj));
					}
					else {
						benoit::d::dobt = strtobool[val];
					}
				}
				else if(obj == "format"s) {
					if(val == "PNG"s) {
						benoit::d::imgfmt = benoit::t::imgfmt::png;
					}
					else if(val == "WebP"s) {
						benoit::d::imgfmt = benoit::t::imgfmt::webp;
					}
					else {
						benoit::print(fmt::format(invalvalforobj,val,obj),true);
					}
				}
				else if(obj == "height"s) {
					benoit::d::resy = std::stoi(val);
					if(benoit::d::resy > 0x10000) {
						benoit::print(fmt::format("Argument “{}” sets the height to {}, but the maximum width is 65536.",arg,benoit::d::resy),true);
						benoit::d::resy = 0x10000;
					}
				}
				else if(obj == "output"s) {
					benoit::d::outimg = val;
				}
				else if(obj == "threads"s) {
					benoit::d::numthrds = std::stoi(val);
					if(benoit::d::numthrds > 0x10000) {
						benoit::print(fmt::format("Argument “{}” sets the number of threads to {}, but the maximum number of threads is 65536."s,arg,benoit::d::numthrds),true);
						benoit::d::numthrds = 0x10000;
					}
				}
				else if(obj == "width"s) {
					benoit::d::resx = std::stoi(val);
					if(benoit::d::resx > 0x10000) {
						benoit::print(fmt::format("Argument “{}” sets the width to {}, but the maximum width is 65536."s,arg,benoit::d::resx),true);
						benoit::d::resx = 0x10000;
					}
				}
				else {
					benoit::print(fmt::format("Invalid object “{}”."s,obj),true);
				}
			}
			else {
				if((arg == "help"s) || (arg == "--help"s)) {
					benoit::helpscrn();
				}
				else {
					benoit::print(fmt::format("Invalid argument “{}”."s,arg),true);
				}
			}
		}
	}
	else {
		benoit::log("No arguments have been detected."s);
	}
	switch(benoit::d::imgfmt) {
	case benoit::t::imgfmt::png:
		benoit::d::outimg.append(".png"s);
		break;
	case benoit::t::imgfmt::webp:
		if((benoit::d::resx > 0x3FFF) || (benoit::d::resy > 0x3FFF)) {
			benoit::exit(EXIT_FAILURE,"WebP does not support a resolution of more than 16383."s);
		}
		benoit::d::outimg.append(".webp"s);
		break;
	}
	benoit::log(funcname,fmt::format("The output image will be \u201C{}\u201D."s,benoit::d::outimg));
	benoit::logfuncret(funcname);
}