Debian Codegraph @

Descent3/args.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "args.h"
20 #include "mono.h"
21 #include <string.h>
22
23 #if defined(_WIN32)
24 #define strcasecmp stricmp
25 #endif
26
27 // rcg07062000 used to be static.
28 static int TotalArgs = 0;
29 char GameArgs[MAX_ARGS][MAX_CHARS_PER_ARG];
30
31 const char *GetArg(int index) {
32 if (index >= TotalArgs || index == 0)
33 return NULL;
34 return GameArgs[index];
35 }
36
37 // Gathers all arguments
38 void GatherArgs(const char *str) {
39 int i, t;
40 static int curarg = 1; // DAJ made static
41 int len = strlen(str);
42 bool gotquote = false;
43 for (t = 0, i = 0; i < len; i++) {
44 if (t == 0) {
45 if (str[i] == '\"') {
46 gotquote = true;
47 continue;
48 }
49 }
50 if (gotquote) {
51 if (str[i] != '\"' && str[i] != 0)
52 GameArgs[curarg][t++] = str[i];
53 else {
54 GameArgs[curarg][t] = 0;
55 t = 0;
56 gotquote = false;
57 curarg++;
58 }
59 } else {
60 if (str[i] != ' ' && str[i] != 0)
61 GameArgs[curarg][t++] = str[i];
62 else {
63 GameArgs[curarg][t] = 0;
64 t = 0;
65 curarg++;
66 }
67 }
68 }
69 GameArgs[curarg][t] = 0;
70 curarg++;
71 TotalArgs = curarg;
72
73 for (int q = 0; q < TotalArgs; q++)
74 mprintf(0, "GatherArgs: Arg (%d) is [%s].", q, GameArgs[q]);
75 }
76
77 void GatherArgs(char **argv) {
78 TotalArgs = 0;
79
80 for (int i = 0; ((i < MAX_ARGS) && (argv[i] != NULL)); i++) {
81 TotalArgs++;
82 strncpy(GameArgs[i], argv[i], MAX_CHARS_PER_ARG);
83 } // for
84
85 for (int q = 0; q < TotalArgs; q++)
86 mprintf(0, "GatherArgs: Arg (%d) is [%s].", q, GameArgs[q]);
87 } // GatherArgs
88
89 // Strip '-', '--', and '+' flag prefix, so --foo, -foo, +foo => foo, but pass through - -- + ++
90 const char *SkipArgPrefix(const char *arg) {
91 if (*arg != '\0' && *(arg + 1) != '\0') {
92 if (*arg == '-') {
93 if (*(arg + 1) == '-' && *(arg + 2) != '\0') {
94 return arg + 2;
95 }
96 if (*(arg + 1) != '-') {
97 return arg + 1;
98 }
99 } else if (*arg == '+' && *(arg + 1) != '+') {
100 return arg + 1;
101 }
102 }
103 return arg;
104 }
105
106 int FindArg(const char *which) {
107 if (which == nullptr)
108 return 0;
109
110 auto which_matches = [which = SkipArgPrefix(which)](char *arg) -> bool {
111 return strcasecmp(which, SkipArgPrefix(arg)) == 0;
112 };
113
114 for (int i = 1; i <= TotalArgs; i++) {
115 if (which_matches(GameArgs[i])) {
116 mprintf(0, "FindArg: Found [%s] at argument index (%d).", which, i);
117 return i;
118 }
119 }
120
121 mprintf(0, "FindArg: Did not find [%s] on command line.", which);
122 return 0;
123 }
124
125 // Returns index of argument sought, or 0 if not found
126 int FindArgChar(const char *which, char singleCharArg) {
127 for (int i = 1; i <= TotalArgs; i++) {
128 char *str = GameArgs[i];
129 if (str[0] == '-' && str[1] == singleCharArg && str[2] == '\0') {
130 mprintf(0, "FindArg: Found [-%c] at argument index (%d).", singleCharArg, i);
131 return i;
132 }
133 }
134
135 return FindArg(which);
136 }