-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.c
117 lines (94 loc) · 3.45 KB
/
options.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "ascii.h"
#include "options.h"
// Default weights; must add up to 1.0
static const float weight_red = 0.2989f;
static const float weight_green = 0.5866f;
static const float weight_blue = 0.1145f;
unsigned short int opt_width = 80,
opt_height = 0,
auto_width = 0,
auto_height = 1;
char opt_address[OPTIONS_BUFF_SIZE] = "0.0.0.0",
opt_port [OPTIONS_BUFF_SIZE] = "9001";
unsigned short int opt_webcam_index = 0;
char ascii_palette[ASCII_PALETTE_SIZE + 1] =
" ...',;:clodxkO0KXNWM";
unsigned short int RED [ASCII_PALETTE_SIZE],
GREEN[ASCII_PALETTE_SIZE],
BLUE [ASCII_PALETTE_SIZE],
GRAY [ASCII_PALETTE_SIZE];
static struct option long_options[] = {
{"address", required_argument, NULL, 'a'},
{"port", required_argument, NULL, 'p'},
{"width", optional_argument, NULL, 'x'},
{"height", optional_argument, NULL, 'y'},
{"webcam-index", required_argument, NULL, 'c'},
{"help", optional_argument, NULL, 'h'},
{0, 0, 0, 0}
};
void options_init(int argc, char** argv) {
precalc_rgb(weight_red, weight_green, weight_blue);
while (1) {
int index = 0,
c = getopt_long(argc, argv, "a:p:x:y:c:h", long_options, &index);
if (c == -1)
break;
char argbuf[1024];
switch (c) {
case 0:
break;
case 'a':
snprintf(opt_address, OPTIONS_BUFF_SIZE, "%s", optarg);
break;
case 'p':
snprintf(opt_port, OPTIONS_BUFF_SIZE, "%s", optarg);
break;
case 'x':
snprintf(argbuf, OPTIONS_BUFF_SIZE, "%s", optarg);
opt_width = strtoint(argbuf);
break;
case 'y':
snprintf(argbuf, OPTIONS_BUFF_SIZE, "%s", optarg);
opt_height = strtoint(argbuf);
break;
case 'c':
snprintf(argbuf, OPTIONS_BUFF_SIZE, "%s", optarg);
opt_webcam_index = strtoint(argbuf);
break;
case '?':
fprintf(stderr, "Unknown option %c\n", optopt);
usage(stderr);
exit(EXIT_FAILURE);
break;
case 'h':
usage(stdout);
exit(EXIT_SUCCESS);
break;
default:
abort();
}
}
}
void usage(FILE* desc /* stdout|stderr*/ ) {
fprintf(desc, "ascii-chat\n");
fprintf(desc, "\toptions:\n");
fprintf(desc, "\t\t -a --address (server|client) \t ip address\n");
fprintf(desc, "\t\t -p --port (server|client) \t tcp port\n");
fprintf(desc, "\t\t -x --width (client) \t render width\n");
fprintf(desc, "\t\t -y --height (client) \t render height\n");
fprintf(desc, "\t\t -c --webcam-index (server) \t webcam device index\n");
fprintf(desc, "\t\t -h --help (server|client) \t print this help\n");
}
void precalc_rgb(const float red, const float green, const float blue) {
for (int n = 0; n < ASCII_PALETTE_SIZE; ++n) {
RED[n] = ((float) n) * red;
GREEN[n] = ((float) n) * green;
BLUE[n] = ((float) n) * blue;
GRAY[n] = ((float) n);
}
}