-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_argv.c
101 lines (86 loc) · 2.07 KB
/
core_argv.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
/**
* Parsing of command line arguments for core.c.
*
* Author: Tim Sjöstrand <tim.sjostrand@gmail.com>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "core_argv.h"
#include "str.h"
#include "graphics.h"
static int core_argv_is_arg(const char *arg, const char *name)
{
if(arg == NULL || name == NULL) {
return -1;
}
/* Allow both styles: -mount, --mount. */
if(arg[0] == '-') {
arg++;
} else {
return -1;
}
if(arg[0] == '-') {
arg++;
}
size_t arg_len = strnlen(arg, CORE_ARGV_NAME_MAX);
size_t name_len = strnlen(arg, CORE_ARGV_NAME_MAX);
if(arg_len != name_len) {
return -1;
}
return (strncmp(arg, name, arg_len) == 0) ? 0 : -1;
}
static int core_argv_get_value(int index, char *dst, int argc, char **argv)
{
/* End of argument vector? */
if(index+1 >= argc) {
return -1;
}
/* Is next on argv really a value? */
if(argv[index+1][0] == '-') {
return -1;
}
strncpy(dst, argv[index+1], CORE_ARGV_VALUE_MAX);
return 0;
}
int core_argv_parse(struct core_argv *dst, int argc, char **argv)
{
/* TODO: Nice API for defining possible options and what type of value they
* have.*/
for(int i=1; i < argc; i++) {
/* --mount <PATH> */
if(core_argv_is_arg(argv[i], "mount") == 0) {
if(core_argv_get_value(i, dst->mount, argc, argv) != 0) {
core_argv_error("Usage: --mount <PATH>\n");
return -1;
}
i++;
}
/* --game <PATH> */
if(core_argv_is_arg(argv[i], "game") == 0) {
if(core_argv_get_value(i, dst->game, argc, argv) != 0) {
core_argv_error("Usage: --game <PATH>\n");
return -1;
}
i++;
}
/* --windowed */
if(core_argv_is_arg(argv[i], "windowed") == 0) {
dst->window_mode = GRAPHICS_MODE_WINDOWED;
}
/* --borderless */
if (core_argv_is_arg(argv[i], "borderless") == 0) {
dst->window_mode = GRAPHICS_MODE_BORDERLESS;
}
}
/* Default values. */
if(str_empty(dst->mount, CORE_ARGV_VALUE_MAX)) {
str_set(dst->mount, CORE_ARGV_VALUE_MAX, "assets");
}
#if 0
if(str_empty(dst->game, CORE_ARGV_VALUE_MAX)) {
str_set(dst->game, CORE_ARGV_VALUE_MAX, "game.dll");
}
#endif
return 0;
}