summaryrefslogtreecommitdiff
path: root/apps/plugins/sdl/wrappers.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/sdl/wrappers.c')
-rw-r--r--apps/plugins/sdl/wrappers.c122
1 files changed, 119 insertions, 3 deletions
diff --git a/apps/plugins/sdl/wrappers.c b/apps/plugins/sdl/wrappers.c
index ee512dd573..02e9db1992 100644
--- a/apps/plugins/sdl/wrappers.c
+++ b/apps/plugins/sdl/wrappers.c
@@ -165,9 +165,80 @@ static double rb_strtod(const char *str, char **endptr)
165 return number; 165 return number;
166} 166}
167 167
168double atof_wrapper(const char *str) 168// stolen from Quake
169float atof_wrapper (char *str)
169{ 170{
170 return rb_strtod(str, NULL); 171 double val;
172 int sign;
173 int c;
174 int decimal, total;
175
176 if (*str == '-')
177 {
178 sign = -1;
179 str++;
180 }
181 else
182 sign = 1;
183
184 val = 0;
185
186//
187// check for hex
188//
189 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
190 {
191 str += 2;
192 while (1)
193 {
194 c = *str++;
195 if (c >= '0' && c <= '9')
196 val = (val*16) + c - '0';
197 else if (c >= 'a' && c <= 'f')
198 val = (val*16) + c - 'a' + 10;
199 else if (c >= 'A' && c <= 'F')
200 val = (val*16) + c - 'A' + 10;
201 else
202 return val*sign;
203 }
204 }
205
206//
207// check for character
208//
209 if (str[0] == '\'')
210 {
211 return sign * str[1];
212 }
213
214//
215// assume decimal
216//
217 decimal = -1;
218 total = 0;
219 while (1)
220 {
221 c = *str++;
222 if (c == '.')
223 {
224 decimal = total;
225 continue;
226 }
227 if (c <'0' || c > '9')
228 break;
229 val = val*10 + c - '0';
230 total++;
231 }
232
233 if (decimal == -1)
234 return val*sign;
235 while (total > decimal)
236 {
237 val /= 10;
238 total--;
239 }
240
241 return val*sign;
171} 242}
172 243
173double sin_wrapper(double rads) 244double sin_wrapper(double rads)
@@ -217,6 +288,51 @@ float tan_wrapper(float f)
217 return sin_wrapper(f)/cos_wrapper(f); 288 return sin_wrapper(f)/cos_wrapper(f);
218} 289}
219 290
291// Total hack. Supports only format strings of the form %Cc, where C
292// is a format specifier and c is a delimiter. Surprisingly, most
293// format strings aren't that complicated to need a real fscanf. This
294// is just enough to make Quake run!
295int fscanf_wrapper(FILE *f, const char *fmt, ...)
296{
297 va_list ap;
298
299 va_start(ap, fmt);
300
301 if(strlen(fmt) != 3)
302 return 0; // not implemented
303
304 if(fmt[0] != '%')
305 return 0; // not implemented
306
307 char format = fmt[1];
308 char delim = fmt[2];
309
310 // extract argument
311 char buf[1024];
312 char *ptr = (format == 's' ? va_arg(ap, char*) : buf);
313 int c;
314 do {
315 c = fgetc(f);
316 *ptr++ = c;
317 } while(c != delim && c != EOF);
318
319 // overwrite delimiter
320 *(ptr-1) = 0;
321
322 //rb->splashf(HZ, "got argument %s, %s\n", fmt, buf);
323
324 switch(format)
325 {
326 case 'i':
327 *va_arg(ap, int*) = atoi(buf);
328 break;
329 case 'f':
330 *va_arg(ap, float*) = atof(buf);
331 break;
332 }
333 return 1;
334}
335
220/* stolen from doom */ 336/* stolen from doom */
221// Here is a hacked up printf command to get the output from the game. 337// Here is a hacked up printf command to get the output from the game.
222int printf_wrapper(const char *fmt, ...) 338int printf_wrapper(const char *fmt, ...)
@@ -272,7 +388,7 @@ int vsprintf_wrapper(char *str, const char *fmt, va_list ap)
272 388
273char *strcpy_wrapper(char *dest, const char *src) 389char *strcpy_wrapper(char *dest, const char *src)
274{ 390{
275 rb->strlcpy(dest, src, 999); 391 strlcpy(dest, src, 999);
276 return dest; 392 return dest;
277} 393}
278 394