forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.cpp
More file actions
270 lines (238 loc) · 5.14 KB
/
args.cpp
File metadata and controls
270 lines (238 loc) · 5.14 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @file args.cpp
* Parses command line arguments.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "args.h"
#include <cstring>
#include "unc_ctype.h"
/**
* Store the values and allocate enough memory for the 'used' flags.
*
* @param argc The argc that was passed to main()
* @param argv The argv that was passed to main()
*/
Args::Args(int argc, char **argv)
{
m_count = argc;
m_values = argv;
int len = (argc >> 3) + 1;
m_used = new UINT8[len];
if (m_used != NULL)
{
memset(m_used, 0, len);
}
}
Args::~Args()
{
if (m_used != NULL)
{
delete[] m_used;
m_used = NULL;
}
m_count = 0;
}
/**
* Check for an exact match
*
* @param token The token string to match
* @return true/false -- Whether the argument was present
*/
bool Args::Present(const char *token)
{
int idx;
if (token != NULL)
{
for (idx = 0; idx < m_count; idx++)
{
if (strcmp(token, m_values[idx]) == 0)
{
SetUsed(idx);
return(true);
}
}
}
return(false);
}
/**
* Just call arg_params() with an index of 0.
*
* @param token The token string to match
* @return NULL or the pointer to the string
*/
const char *Args::Param(const char *token)
{
int idx = 0;
return(Params(token, idx));
}
/**
* Scan for a match
*
* @param token The token string to match
* @return NULL or the pointer to the string
*/
const char *Args::Params(const char *token, int& index)
{
int idx;
int token_len;
int arg_len;
if (token == NULL)
{
return(NULL);
}
token_len = (int)strlen(token);
for (idx = index; idx < m_count; idx++)
{
arg_len = (int)strlen(m_values[idx]);
if ((arg_len >= token_len) &&
(memcmp(token, m_values[idx], token_len) == 0))
{
SetUsed(idx);
if (arg_len > token_len)
{
if (m_values[idx][token_len] == '=')
{
token_len++;
}
index = idx + 1;
return(&m_values[idx][token_len]);
}
idx++;
index = idx + 1;
if (idx < m_count)
{
SetUsed(idx);
return(m_values[idx]);
}
return("");
}
}
return(NULL);
}
/**
* Gets whether an argument has been used, by index.
*
* @param idx The index of the argument
*/
bool Args::GetUsed(int idx)
{
if ((m_used != NULL) && (idx >= 0) && (idx < m_count))
{
return((m_used[idx >> 3] & (1 << (idx & 0x07))) != 0);
}
return(false);
}
/**
* Marks an argument as being used.
*
* @param idx The index of the argument
*/
void Args::SetUsed(int idx)
{
if ((m_used != NULL) && (idx >= 0) && (idx < m_count))
{
m_used[idx >> 3] |= (1 << (idx & 0x07));
}
}
/**
* This function retrieves all unused parameters.
* You must set the index before the first call.
* Set the index to 1 to skip argv[0].
*
* @param idx Pointer to the index
* @return NULL (done) or the pointer to the string
*/
const char *Args::Unused(int& index)
{
int idx;
if (m_used == NULL)
{
return(NULL);
}
for (idx = index; idx < m_count; idx++)
{
if (!GetUsed(idx))
{
index = idx + 1;
return(m_values[idx]);
}
}
index = m_count;
return(NULL);
}
/**
* Takes text and splits it into arguments.
* args is an array of char * pointers that will get populated.
* num_args is the maximum number of args split off.
* If there are more than num_args, the remaining text is ignored.
* Note that text is modified (zeroes are inserted)
*
* @param text The text to split (modified)
* @param args The char * array to populate
* @param num_args The number of items in args
* @return The number of arguments parsed (always <= num_args)
*/
int Args::SplitLine(char *text, char *args[], int num_args)
{
char cur_quote = 0;
bool in_backslash = false;
bool in_arg = false;
int argc = 0;
char *dest = text;
while ((*text != 0) && (argc <= num_args))
{
/* Detect the start of an arg */
if (!in_arg && !unc_isspace(*text))
{
in_arg = true;
args[argc] = dest;
argc++;
}
if (in_arg)
{
if (in_backslash)
{
in_backslash = false;
*dest = *text;
dest++;
}
else if (*text == '\\')
{
in_backslash = true;
}
else if (*text == cur_quote)
{
cur_quote = 0;
}
else if ((*text == '\'') || (*text == '"') || (*text == '`'))
{
cur_quote = *text;
}
else if (cur_quote != 0)
{
*dest = *text;
dest++;
}
else if (unc_isspace(*text))
{
*dest = 0;
dest++;
in_arg = false;
if (argc == num_args)
{
break;
}
}
else
{
*dest = *text;
dest++;
}
}
text++;
}
*dest = 0;
return(argc);
}