-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotal_keycode_name_size.cpp
More file actions
50 lines (45 loc) · 1.47 KB
/
total_keycode_name_size.cpp
File metadata and controls
50 lines (45 loc) · 1.47 KB
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
#include <cstring>
#include <cstdlib>
#include <cstdio>
const int VK_MAX = 0xFF;
const int VK_ASCII_LETTER_MIN = 0x41;
const int VK_ASCII_LETTER_MAX = 0x5A;
const int VK_ASCII_NUMBER_MIN = 0x30;
const int VK_ASCII_NUMBER_MAX = 0x39;
extern const char *keycode_names[];
int main(int argc, char **argv) {
FILE *out = stdout;
if(argc == 2 || argc > 3) {
fprintf(stderr, "Usage: total_keycode_name_size [<output_file> <define_name>]\n");
return EXIT_FAILURE;
} else if(argc == 3) {
out = fopen(argv[1], "w");
if(out == nullptr) {
perror("fopen");
return EXIT_FAILURE;
}
fprintf(out, "// Auto-generated by total_keycode_name_size.cpp\n// Words: ");
}
size_t total = 0;
for(int x = 0;x < VK_MAX;x++) {
if((x >= VK_ASCII_LETTER_MIN && x <= VK_ASCII_LETTER_MAX) || (x >= VK_ASCII_NUMBER_MIN && x <= VK_ASCII_NUMBER_MAX)) {
fprintf(out, "%c ", (char)x);
// 1 char + 1 space
total += 2;
} else if (keycode_names[x] != nullptr) {
// strlen + 1 space
fprintf(out, "%s ", keycode_names[x]);
total += strlen(keycode_names[x]) + 1;
} else {
fprintf(out, "0x%02X ", x);
// 0x + 2 hex digits + 1 space
total += 5;
}
}
if(argc == 3) {
fprintf(out, "\n#define %s %zu\n", argv[2], total);
fclose(out);
} else {
fprintf(out, "\n%zu\n", total);
}
}