-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.cpp
96 lines (81 loc) · 1.77 KB
/
options.cpp
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
#include "options.hpp"
#include "version.h"
#include <thread>
#include <string>
#include <iostream>
#define REQUIRE(x) if (!(x)) return false;
template <typename T>
static inline bool parsePositiveNum(const char*& str, T& x, char endchar = '\0')
{
x = 0;
while (*str >= '0' && *str <= '9') {
x = 10 * x + (*str - '0');
str++;
}
return x && (*str == endchar);
}
static inline bool parseResolution(const char*& str, Options::Resolution& r)
{
REQUIRE(parsePositiveNum(str, r.x, 'x'));
str++;
return parsePositiveNum(str, r.y);
}
void Options::toDefault()
{
buffer = { .x = -1, .y = -1 };
threadCount = std::thread::hardware_concurrency();
port = 1234;
quiet = false;
fullscreen = false;
}
bool Options::parse(const char* const* argv)
{
toDefault();
if (!argv) {
return true;
}
argv++;
while (*argv) {
const char* arg = *argv;
REQUIRE(*(arg++) == '-');
REQUIRE(*arg);
while (*arg) {
switch (*(arg++)) {
case 'b': {
if (!*arg)
REQUIRE(arg = *++argv);
REQUIRE(parseResolution(arg, buffer));
} break;
case 't': {
if (!*arg)
REQUIRE(arg = *++argv);
REQUIRE(parsePositiveNum(arg, threadCount));
} break;
case 'p': {
if (!*arg)
REQUIRE(arg = *++argv);
REQUIRE(parsePositiveNum(arg, port));
} break;
case 'q': {
quiet = true;
} break;
case 'f': {
fullscreen = true;
} break;
default:
return false;
}
}
argv++;
}
return true;
}
void Options::printUsage(const char* const* argv)
{
std::cerr << RELEASE_NAME << std::endl;
std::cerr << "Usage: "
<< (argv[0] ? argv[0] : "pixelflood")
<< " [ -b BUFFER_RESOLUTION ] [ -t THREAD_COUNT ] [ -p PORT ] [ -q ] [ -f ]"
<< std::endl;
std::cerr << "Resolutions are formatted like 1280x720" << std::endl;
}