-
Notifications
You must be signed in to change notification settings - Fork 49
/
kustom.cpp
257 lines (250 loc) · 5.48 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* sscanf 2.15.1
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the sscanf 2.0 SA:MP plugin.
*
* The Initial Developer of the Original Code is Alex "Y_Less" Cole.
* Portions created by the Initial Developer are Copyright (c) 2022
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Cheaterman
* DEntisT
* Emmet_
* karimcambridge
* kalacsparty
* Kirima
* leHeix
* maddinat0r
* Southclaws
* Y_Less
* ziggi
*
* Special Thanks to:
*
* SA:MP Team past, present, and future.
* maddinat0r, for hosting the repo for a very long time.
* Emmet_, for his efforts in maintaining it for almost a year.
*/
#include "amx/amx.h"
#include "sscanf.h"
#include "utils.h"
#include "data.h"
#include "specifiers.h"
#include <stdio.h>
extern logprintf_t
logprintf;
extern E_SSCANF_OPTIONS
gOptions;
bool
DoK(AMX * amx, char ** defaults, char ** input, cell * cptr, bool optional, bool all)
{
// 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.
SscanfWarning(10, "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
{
SscanfWarning(11, "Unclosed default value.");
}
}
else
{
SscanfWarning(12, "No default value found.");
}
if (!input || IsStringEnd(**input))
{
if (cptr)
{
if (opts)
{
if (gOptions & OLD_DEFAULT_KUSTOM)
{
char
func[32];
int
idx;
sprintf(func, "sscanf_%s", type);
if (amx_FindPublic(amx, func, &idx))
{
SscanfWarning(31, "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))
{
SetErrorCode(1004);
return false;
}
char
func[32];
int
idx;
sprintf(func, "sscanf_%s", type);
if (amx_FindPublic(amx, func, &idx))
{
SscanfWarning(31, "Could not find function SSCANF:%s.", type);
}
else
{
char
* string = *input,
* outp = string,
* start = string;
if (IsDefaultDelimiter())
{
while ((all && !IsEnd(*string)) || !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) && (all || !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;
}