forked from Y-Less/sscanf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkustom.cpp
238 lines (232 loc) · 4.96 KB
/
kustom.cpp
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
// Kustom
#include "SDK/amx/amx.h"
#include "sscanf.h"
#include "utils.h"
#include "data.h"
#include "specifiers.h"
#include <stdio.h>
extern logprintf_t
logprintf;
extern int
gOptions;
bool
DoK(AMX * amx, char ** defaults, char ** input, cell * cptr, bool optional)
{
// First, get the type of the array.
char *
type = GetMultiType(defaults);
if (!type)
{
return false;
}
if (optional)
{
// Optional parameters - just collect the data for
// now and use it later.
char *
opts = 0;
if (**defaults == '(')
{
++(*defaults);
SkipWhitespace(defaults);
// Got the start of the values.
opts = *defaults;
// Skip the defaults for now, we don't know the length yet.
bool
escape = false;
while (**defaults && (escape || **defaults != ')'))
{
if (**defaults == '\\')
{
escape = !escape;
}
else
{
escape = false;
}
++(*defaults);
}
if (**defaults)
{
if (opts == *defaults)
{
// No defaults found.
logprintf("sscanf warning: Empty default values.");
opts = 0;
}
// Found a valid end. Make it null for
// later array getting (easy to detect a
// null end and we'll never need to
// backtrack to here in the specifiers).
**defaults = '\0';
++(*defaults);
}
else
{
logprintf("sscanf warning: Unclosed default value.");
}
/*if (optional)
{
// Optional parameters are always separated by commans, not
// whatever the coder may choose.
TempDelimiter(",)");
// We need to copy the old save value for optional parts. If
// we don't and save gets set to false in the middle of the
// enum then when the code is called for a second time for the
// real values then save will already be false and they won't
// get saved.
switch (DoEnumValues(type, &opts, cptr, true))
{
case SSCANF_TRUE_RETURN:
break;
case SSCANF_CONT_RETURN:
logprintf("sscanf error: Insufficient default values.");
// FALLTHROUGH
default:
RestoreDelimiter();
return false;
}
RestoreDelimiter();
}*/
}
else
{
logprintf("sscanf warning: No default value found.");
}
if (!input || IsStringEnd(**input))
{
if (cptr)
{
if (opts)
{
if (gOptions & 16)
{
char
func[32];
int
idx;
sprintf(func, "sscanf_%s", type);
if (amx_FindPublic(amx, func, &idx))
{
logprintf("sscanf warning: Could not find function SSCANF:%s.", type);
}
else
{
cell
ret,
addr;
amx_PushString(amx, &addr, 0, opts, 0, 0);
amx_Exec(amx, &ret, idx);
amx_Release(amx, addr);
*cptr = ret;
return true;
}
}
else
{
*cptr = (cell)GetNumber(&opts);
return true;
}
}
*cptr = 0;
}
return true;
}
}
else if (!input || IsStringEnd(**input))
{
return false;
}
char
func[32];
int
idx;
sprintf(func, "sscanf_%s", type);
if (amx_FindPublic(amx, func, &idx))
{
logprintf("sscanf warning: Could not find function SSCANF:%s.", type);
}
else
{
char
* string = *input,
* outp = string,
* start = string;
if (IsDefaultDelimiter())
{
while (!IsWhitespace(*string))
{
if (*string == '\\')
{
if (IsEnd(*(string + 1)))
{
++string;
break;
}
if (*(string + 1) == '\\' || IsWhitespace(*(string + 1)))
{
++string;
}
}
if (outp != string)
{
*outp = *string;
}
++outp;
++string;
}
}
else
{
// Just a single word. Note that if your delimiter is a backslash
// you can't escape it - this is not a bug, just don't try use it as
// a delimiter and still expect to be able to use it in a string. I
// suppose that technically you could see this as a bug, but I
// choose to call it an undesirable feature (no-one has complained).
while (!IsEnd(*string) && !IsDelimiter(*string))
{
if (*string == '\\')
{
if (IsEnd(*(string + 1)))
{
++string;
break;
}
// Escape spaces, backspace and delimiters - this code
// is context independent so you can use a string with
// or without a delimiter and can still escape spaces.
if (*(string + 1) == '\\' || IsSpacer(*(string + 1)))
{
++string;
}
}
if (outp != string)
{
*outp = *string;
}
++outp;
++string;
}
}
if (!IsEnd(*string))
{
// Skip the final character.
*input = string + 1;
}
else
{
// Save the return.
*input = string;
}
// Add a null terminator.
*outp = '\0';
cell
ret,
addr;
amx_PushString(amx, &addr, 0, start, 0, 0);
amx_Exec(amx, &ret, idx);
amx_Release(amx, addr);
if (cptr) *cptr = ret;
}
return true;
}