| 1 |
/* ----------------------------------------------------------------------------- |
| 2 |
|
| 3 |
PicoModel Library |
| 4 |
|
| 5 |
Copyright (c) 2002, Randy Reddig & seaw0lf |
| 6 |
All rights reserved. |
| 7 |
|
| 8 |
Redistribution and use in source and binary forms, with or without modification, |
| 9 |
are permitted provided that the following conditions are met: |
| 10 |
|
| 11 |
Redistributions of source code must retain the above copyright notice, this list |
| 12 |
of conditions and the following disclaimer. |
| 13 |
|
| 14 |
Redistributions in binary form must reproduce the above copyright notice, this |
| 15 |
list of conditions and the following disclaimer in the documentation and/or |
| 16 |
other materials provided with the distribution. |
| 17 |
|
| 18 |
Neither the names of the copyright holders nor the names of its contributors may |
| 19 |
be used to endorse or promote products derived from this software without |
| 20 |
specific prior written permission. |
| 21 |
|
| 22 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 26 |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 29 |
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
----------------------------------------------------------------------------- */ |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/* marker */ |
| 38 |
#define PICOINTERNAL_C |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
/* todo: |
| 43 |
* - fix p->curLine for parser routines. increased twice |
| 44 |
*/ |
| 45 |
|
| 46 |
/* dependencies */ |
| 47 |
#include <string.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <stdint.h> |
| 50 |
#include "picointernal.h" |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
/* function pointers */ |
| 55 |
void *(*_pico_ptr_malloc )( size_t ) = NULL; // was pointing to malloc, but this might be a dll import func |
| 56 |
void (*_pico_ptr_free )( void* ) = NULL; // was pointing to free, but this might be a dll import func |
| 57 |
void (*_pico_ptr_load_file )( char*, unsigned char**, int* ) = NULL; |
| 58 |
void (*_pico_ptr_free_file )( void* ) = NULL; |
| 59 |
void (*_pico_ptr_print )( int, const char* ) = NULL; |
| 60 |
|
| 61 |
typedef union |
| 62 |
{ |
| 63 |
float f; |
| 64 |
char c[4]; |
| 65 |
} |
| 66 |
floatSwapUnion; |
| 67 |
|
| 68 |
/* _pico_alloc: |
| 69 |
* kludged memory allocation wrapper |
| 70 |
*/ |
| 71 |
void *_pico_alloc( size_t size ) |
| 72 |
{ |
| 73 |
void *ptr; |
| 74 |
|
| 75 |
/* some sanity checks */ |
| 76 |
if( size == 0 ) |
| 77 |
return NULL; |
| 78 |
if (_pico_ptr_malloc == NULL) |
| 79 |
return NULL; |
| 80 |
|
| 81 |
/* allocate memory */ |
| 82 |
ptr = _pico_ptr_malloc(size); |
| 83 |
if (ptr == NULL) |
| 84 |
return NULL; |
| 85 |
|
| 86 |
/* zero out allocated memory */ |
| 87 |
memset(ptr,0,size); |
| 88 |
|
| 89 |
/* return pointer to allocated memory */ |
| 90 |
return ptr; |
| 91 |
} |
| 92 |
|
| 93 |
/* _pico_calloc: |
| 94 |
* _pico_calloc wrapper |
| 95 |
*/ |
| 96 |
void *_pico_calloc( size_t num, size_t size ) |
| 97 |
{ |
| 98 |
void *ptr; |
| 99 |
|
| 100 |
/* some sanity checks */ |
| 101 |
if( num == 0 || size == 0 ) |
| 102 |
return NULL; |
| 103 |
if (_pico_ptr_malloc == NULL) |
| 104 |
return NULL; |
| 105 |
|
| 106 |
/* allocate memory */ |
| 107 |
ptr = _pico_ptr_malloc(num*size); |
| 108 |
if (ptr == NULL) |
| 109 |
return NULL; |
| 110 |
|
| 111 |
/* zero out allocated memory */ |
| 112 |
memset(ptr,0,num*size); |
| 113 |
|
| 114 |
/* return pointer to allocated memory */ |
| 115 |
return ptr; |
| 116 |
} |
| 117 |
|
| 118 |
/* _pico_realloc: |
| 119 |
* memory reallocation wrapper (note: only grows, |
| 120 |
* but never shrinks or frees) |
| 121 |
*/ |
| 122 |
void *_pico_realloc( void **ptr, size_t oldSize, size_t newSize ) |
| 123 |
{ |
| 124 |
void *ptr2; |
| 125 |
|
| 126 |
/* sanity checks */ |
| 127 |
if( ptr == NULL ) |
| 128 |
return NULL; |
| 129 |
if( newSize < oldSize ) |
| 130 |
return *ptr; |
| 131 |
if (_pico_ptr_malloc == NULL) |
| 132 |
return NULL; |
| 133 |
|
| 134 |
/* allocate new pointer */ |
| 135 |
ptr2 = _pico_alloc( newSize ); |
| 136 |
if( ptr2 == NULL ) |
| 137 |
return NULL; |
| 138 |
|
| 139 |
/* copy */ |
| 140 |
if( *ptr != NULL ) |
| 141 |
{ |
| 142 |
memcpy( ptr2, *ptr, oldSize ); |
| 143 |
_pico_free( *ptr ); |
| 144 |
} |
| 145 |
|
| 146 |
/* fix up and return */ |
| 147 |
*ptr = ptr2; |
| 148 |
return *ptr; |
| 149 |
} |
| 150 |
|
| 151 |
/* _pico_clone_alloc: |
| 152 |
* handy function for quick string allocation/copy. it clones |
| 153 |
* the given string and returns a pointer to the new allocated |
| 154 |
* clone (which must be freed by caller of course) or returns |
| 155 |
* NULL on memory alloc or param errors. if 'size' is -1 the |
| 156 |
* length of the input string is used, otherwise 'size' is used |
| 157 |
* as custom clone size (the string is cropped to fit into mem |
| 158 |
* if needed). -sea |
| 159 |
*/ |
| 160 |
char *_pico_clone_alloc( const char *str ) |
| 161 |
{ |
| 162 |
char* cloned; |
| 163 |
|
| 164 |
/* sanity check */ |
| 165 |
if (str == NULL) |
| 166 |
return NULL; |
| 167 |
|
| 168 |
/* allocate memory */ |
| 169 |
cloned = _pico_alloc( strlen(str) + 1 ); |
| 170 |
if (cloned == NULL) |
| 171 |
return NULL; |
| 172 |
|
| 173 |
/* copy input string to cloned string */ |
| 174 |
strcpy( cloned, str ); |
| 175 |
|
| 176 |
/* return ptr to cloned string */ |
| 177 |
return cloned; |
| 178 |
} |
| 179 |
|
| 180 |
/* _pico_free: |
| 181 |
* wrapper around the free function pointer |
| 182 |
*/ |
| 183 |
void _pico_free( void *ptr ) |
| 184 |
{ |
| 185 |
/* sanity checks */ |
| 186 |
if( ptr == NULL ) |
| 187 |
return; |
| 188 |
if (_pico_ptr_free == NULL) |
| 189 |
return; |
| 190 |
|
| 191 |
/* free the allocated memory */ |
| 192 |
_pico_ptr_free( ptr ); |
| 193 |
} |
| 194 |
|
| 195 |
/* _pico_load_file: |
| 196 |
* wrapper around the loadfile function pointer |
| 197 |
*/ |
| 198 |
void _pico_load_file( char *name, unsigned char **buffer, int *bufSize ) |
| 199 |
{ |
| 200 |
/* sanity checks */ |
| 201 |
if( name == NULL ) |
| 202 |
{ |
| 203 |
*bufSize = -1; |
| 204 |
return; |
| 205 |
} |
| 206 |
if (_pico_ptr_load_file == NULL) |
| 207 |
{ |
| 208 |
*bufSize = -1; |
| 209 |
return; |
| 210 |
} |
| 211 |
/* do the actual call to read in the file; */ |
| 212 |
/* BUFFER IS ALLOCATED BY THE EXTERNAL LOADFILE FUNC */ |
| 213 |
_pico_ptr_load_file( name,buffer,bufSize ); |
| 214 |
} |
| 215 |
|
| 216 |
/* _pico_free_file: |
| 217 |
* wrapper around the file free function pointer |
| 218 |
*/ |
| 219 |
void _pico_free_file( void *buffer ) |
| 220 |
{ |
| 221 |
/* sanity checks */ |
| 222 |
if( buffer == NULL ) |
| 223 |
return; |
| 224 |
|
| 225 |
/* use default free */ |
| 226 |
if( _pico_ptr_free_file == NULL ) |
| 227 |
{ |
| 228 |
free( buffer ); |
| 229 |
return; |
| 230 |
} |
| 231 |
/* free the allocated file */ |
| 232 |
_pico_ptr_free_file( buffer ); |
| 233 |
} |
| 234 |
|
| 235 |
/* _pico_printf: |
| 236 |
* wrapper around the print function pointer -sea |
| 237 |
*/ |
| 238 |
void _pico_printf( int level, const char *format, ...) |
| 239 |
{ |
| 240 |
char str[4096]; |
| 241 |
va_list argptr; |
| 242 |
|
| 243 |
/* sanity checks */ |
| 244 |
if( format == NULL ) |
| 245 |
return; |
| 246 |
if (_pico_ptr_print == NULL) |
| 247 |
return; |
| 248 |
|
| 249 |
/* format string */ |
| 250 |
va_start( argptr,format ); |
| 251 |
vsprintf( str,format,argptr ); |
| 252 |
va_end( argptr ); |
| 253 |
|
| 254 |
/* remove linefeeds */ |
| 255 |
if (str[ strlen(str)-1 ] == '\n') |
| 256 |
str[ strlen(str)-1 ] = '\0'; |
| 257 |
|
| 258 |
/* do the actual call */ |
| 259 |
_pico_ptr_print( level,str ); |
| 260 |
} |
| 261 |
|
| 262 |
/* _pico_first_token: |
| 263 |
* trims everything after the first whitespace-delimited token |
| 264 |
*/ |
| 265 |
|
| 266 |
void _pico_first_token( char *str ) |
| 267 |
{ |
| 268 |
if( !str || !*str ) |
| 269 |
return; |
| 270 |
while( *str && !isspace( *str ) ) |
| 271 |
str++; |
| 272 |
*str = '\0'; |
| 273 |
} |
| 274 |
|
| 275 |
/* _pico_strltrim: |
| 276 |
* left trims the given string -sea |
| 277 |
*/ |
| 278 |
char *_pico_strltrim( char *str ) |
| 279 |
{ |
| 280 |
char *str1 = str, *str2 = str; |
| 281 |
|
| 282 |
while (isspace(*str2)) str2++; |
| 283 |
if( str2 != str ) |
| 284 |
while( *str2 != '\0' ) /* fix: ydnar */ |
| 285 |
*str1++ = *str2++; |
| 286 |
return str; |
| 287 |
} |
| 288 |
|
| 289 |
/* _pico_strrtrim: |
| 290 |
* right trims the given string -sea |
| 291 |
*/ |
| 292 |
char *_pico_strrtrim( char *str ) |
| 293 |
{ |
| 294 |
if (str && *str) |
| 295 |
{ |
| 296 |
char *str1 = str; |
| 297 |
int allspace = 1; |
| 298 |
|
| 299 |
while (*str1) |
| 300 |
{ |
| 301 |
if (allspace && !isspace(*str1)) allspace = 0; |
| 302 |
str1++; |
| 303 |
} |
| 304 |
if (allspace) *str = '\0'; |
| 305 |
else { |
| 306 |
str1--; |
| 307 |
while ((isspace(*str1)) && (str1 >= str)) |
| 308 |
*str1-- = '\0'; |
| 309 |
} |
| 310 |
} |
| 311 |
return str; |
| 312 |
} |
| 313 |
|
| 314 |
/* _pico_strlwr: |
| 315 |
* pico internal string-to-lower routine. |
| 316 |
*/ |
| 317 |
char *_pico_strlwr( char *str ) |
| 318 |
{ |
| 319 |
char *cp; |
| 320 |
for (cp=str; *cp; ++cp) |
| 321 |
{ |
| 322 |
if ('A' <= *cp && *cp <= 'Z') |
| 323 |
{ |
| 324 |
*cp += ('a' - 'A'); |
| 325 |
} |
| 326 |
} |
| 327 |
return str; |
| 328 |
} |
| 329 |
|
| 330 |
/* _pico_strchcount: |
| 331 |
* counts how often the given char appears in str. -sea |
| 332 |
*/ |
| 333 |
int _pico_strchcount( char *str, int ch ) |
| 334 |
{ |
| 335 |
int count = 0; |
| 336 |
while (*str++) if (*str == ch) count++; |
| 337 |
return count; |
| 338 |
} |
| 339 |
|
| 340 |
void _pico_zero_bounds( picoVec3_t mins, picoVec3_t maxs ) |
| 341 |
{ |
| 342 |
int i; |
| 343 |
for (i=0; i<3; i++) |
| 344 |
{ |
| 345 |
mins[i] = +999999; |
| 346 |
maxs[i] = -999999; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
void _pico_expand_bounds( picoVec3_t p, picoVec3_t mins, picoVec3_t maxs ) |
| 351 |
{ |
| 352 |
int i; |
| 353 |
for (i=0; i<3; i++) |
| 354 |
{ |
| 355 |
double value = p[i]; |
| 356 |
if (value < mins[i]) mins[i] = value; |
| 357 |
if (value > maxs[i]) maxs[i] = value; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void _pico_zero_vec( picoVec3_t vec ) |
| 362 |
{ |
| 363 |
vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = 0; |
| 364 |
} |
| 365 |
|
| 366 |
void _pico_zero_vec2( picoVec2_t vec ) |
| 367 |
{ |
| 368 |
vec[ 0 ] = vec[ 1 ] = 0; |
| 369 |
} |
| 370 |
|
| 371 |
void _pico_zero_vec4( picoVec4_t vec ) |
| 372 |
{ |
| 373 |
vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = vec[ 3 ] = 0; |
| 374 |
} |
| 375 |
|
| 376 |
void _pico_set_vec( picoVec3_t v, double a, double b, double c ) |
| 377 |
{ |
| 378 |
v[ 0 ] = a; |
| 379 |
v[ 1 ] = b; |
| 380 |
v[ 2 ] = c; |
| 381 |
} |
| 382 |
|
| 383 |
void _pico_set_vec4( picoVec4_t v, double a, double b, double c, double d ) |
| 384 |
{ |
| 385 |
v[ 0 ] = a; |
| 386 |
v[ 1 ] = b; |
| 387 |
v[ 2 ] = c; |
| 388 |
v[ 3 ] = d; |
| 389 |
} |
| 390 |
|
| 391 |
void _pico_copy_vec( picoVec3_t src, picoVec3_t dest ) |
| 392 |
{ |
| 393 |
dest[ 0 ] = src[ 0 ]; |
| 394 |
dest[ 1 ] = src[ 1 ]; |
| 395 |
dest[ 2 ] = src[ 2 ]; |
| 396 |
} |
| 397 |
|
| 398 |
void _pico_copy_vec2( picoVec2_t src, picoVec2_t dest ) |
| 399 |
{ |
| 400 |
dest[ 0 ] = src[ 0 ]; |
| 401 |
dest[ 1 ] = src[ 1 ]; |
| 402 |
} |
| 403 |
|
| 404 |
void _pico_copy_vec4( picoVec4_t src, picoVec4_t dest ) |
| 405 |
{ |
| 406 |
dest[ 0 ] = src[ 0 ]; |
| 407 |
dest[ 1 ] = src[ 1 ]; |
| 408 |
dest[ 2 ] = src[ 2 ]; |
| 409 |
dest[ 3 ] = src[ 3 ]; |
| 410 |
} |
| 411 |
|
| 412 |
/* ydnar */ |
| 413 |
picoVec_t _pico_normalize_vec( picoVec3_t vec ) |
| 414 |
{ |
| 415 |
double len, ilen; |
| 416 |
|
| 417 |
len = sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] + vec[ 2 ] * vec[ 2 ] ); |
| 418 |
if( len == 0.0 ) return 0.0; |
| 419 |
ilen = 1.0 / len; |
| 420 |
vec[ 0 ] *= (picoVec_t) ilen; |
| 421 |
vec[ 1 ] *= (picoVec_t) ilen; |
| 422 |
vec[ 2 ] *= (picoVec_t) ilen; |
| 423 |
return (picoVec_t) len; |
| 424 |
} |
| 425 |
|
| 426 |
void _pico_add_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ) |
| 427 |
{ |
| 428 |
dest[ 0 ] = a[ 0 ] + b[ 0 ]; |
| 429 |
dest[ 1 ] = a[ 1 ] + b[ 1 ]; |
| 430 |
dest[ 2 ] = a[ 2 ] + b[ 2 ]; |
| 431 |
} |
| 432 |
|
| 433 |
void _pico_subtract_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ) |
| 434 |
{ |
| 435 |
dest[ 0 ] = a[ 0 ] - b[ 0 ]; |
| 436 |
dest[ 1 ] = a[ 1 ] - b[ 1 ]; |
| 437 |
dest[ 2 ] = a[ 2 ] - b[ 2 ]; |
| 438 |
} |
| 439 |
|
| 440 |
void _pico_scale_vec( picoVec3_t v, float scale, picoVec3_t dest ) |
| 441 |
{ |
| 442 |
dest[ 0 ] = v[ 0 ] * scale; |
| 443 |
dest[ 1 ] = v[ 1 ] * scale; |
| 444 |
dest[ 2 ] = v[ 2 ] * scale; |
| 445 |
} |
| 446 |
|
| 447 |
void _pico_scale_vec4( picoVec4_t v, float scale, picoVec4_t dest ) |
| 448 |
{ |
| 449 |
dest[ 0 ] = v[ 0 ] * scale; |
| 450 |
dest[ 1 ] = v[ 1 ] * scale; |
| 451 |
dest[ 2 ] = v[ 2 ] * scale; |
| 452 |
dest[ 3 ] = v[ 3 ] * scale; |
| 453 |
} |
| 454 |
|
| 455 |
picoVec_t _pico_dot_vec( picoVec3_t a, picoVec3_t b ) |
| 456 |
{ |
| 457 |
return a[ 0 ] * b[ 0 ] + a[ 1 ] * b[ 1 ] + a[ 2 ] * b[ 2 ]; |
| 458 |
} |
| 459 |
|
| 460 |
void _pico_cross_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ) |
| 461 |
{ |
| 462 |
dest[ 0 ] = a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ]; |
| 463 |
dest[ 1 ] = a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ]; |
| 464 |
dest[ 2 ] = a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ]; |
| 465 |
} |
| 466 |
|
| 467 |
picoVec_t _pico_calc_plane( picoVec4_t plane, picoVec3_t a, picoVec3_t b, picoVec3_t c ) |
| 468 |
{ |
| 469 |
picoVec3_t ba, ca; |
| 470 |
|
| 471 |
_pico_subtract_vec( b, a, ba ); |
| 472 |
_pico_subtract_vec( c, a, ca ); |
| 473 |
_pico_cross_vec( ca, ba, plane ); |
| 474 |
plane[ 3 ] = _pico_dot_vec( a, plane ); |
| 475 |
return _pico_normalize_vec( plane ); |
| 476 |
} |
| 477 |
|
| 478 |
/* separate from _pico_set_vec4 */ |
| 479 |
void _pico_set_color( picoColor_t c, int r, int g, int b, int a ) |
| 480 |
{ |
| 481 |
c[ 0 ] = (picoByte_t)r; |
| 482 |
c[ 1 ] = (picoByte_t)g; |
| 483 |
c[ 2 ] = (picoByte_t)b; |
| 484 |
c[ 3 ] = (picoByte_t)a; |
| 485 |
} |
| 486 |
|
| 487 |
void _pico_copy_color( picoColor_t src, picoColor_t dest ) |
| 488 |
{ |
| 489 |
dest[ 0 ] = src[ 0 ]; |
| 490 |
dest[ 1 ] = src[ 1 ]; |
| 491 |
dest[ 2 ] = src[ 2 ]; |
| 492 |
dest[ 3 ] = src[ 3 ]; |
| 493 |
} |
| 494 |
|
| 495 |
#ifdef __BIG_ENDIAN__ |
| 496 |
|
| 497 |
int _pico_big_long ( int src ) { return src; } |
| 498 |
short _pico_big_short( short src ) { return src; } |
| 499 |
float _pico_big_float( float src ) { return src; } |
| 500 |
|
| 501 |
int _pico_little_long( int src ) |
| 502 |
{ |
| 503 |
return ((src & 0xFF000000) >> 24) | |
| 504 |
((src & 0x00FF0000) >> 8) | |
| 505 |
((src & 0x0000FF00) << 8) | |
| 506 |
((src & 0x000000FF) << 24); |
| 507 |
} |
| 508 |
|
| 509 |
short _pico_little_short( short src ) |
| 510 |
{ |
| 511 |
return ((src & 0xFF00) >> 8) | |
| 512 |
((src & 0x00FF) << 8); |
| 513 |
} |
| 514 |
|
| 515 |
float _pico_little_float( float src ) |
| 516 |
{ |
| 517 |
floatSwapUnion in,out; |
| 518 |
in.f = src; |
| 519 |
out.c[ 0 ] = in.c[ 3 ]; |
| 520 |
out.c[ 1 ] = in.c[ 2 ]; |
| 521 |
out.c[ 2 ] = in.c[ 1 ]; |
| 522 |
out.c[ 3 ] = in.c[ 0 ]; |
| 523 |
return out.f; |
| 524 |
} |
| 525 |
#else /*__BIG_ENDIAN__*/ |
| 526 |
|
| 527 |
int _pico_little_long ( int src ) { return src; } |
| 528 |
short _pico_little_short( short src ) { return src; } |
| 529 |
float _pico_little_float( float src ) { return src; } |
| 530 |
|
| 531 |
int _pico_big_long( int src ) |
| 532 |
{ |
| 533 |
return ((src & 0xFF000000) >> 24) | |
| 534 |
((src & 0x00FF0000) >> 8) | |
| 535 |
((src & 0x0000FF00) << 8) | |
| 536 |
((src & 0x000000FF) << 24); |
| 537 |
} |
| 538 |
|
| 539 |
short _pico_big_short( short src ) |
| 540 |
{ |
| 541 |
return ((src & 0xFF00) >> 8) | |
| 542 |
((src & 0x00FF) << 8); |
| 543 |
} |
| 544 |
|
| 545 |
float _pico_big_float( float src ) |
| 546 |
{ |
| 547 |
floatSwapUnion in,out; |
| 548 |
in.f = src; |
| 549 |
out.c[ 0 ] = in.c[ 3 ]; |
| 550 |
out.c[ 1 ] = in.c[ 2 ]; |
| 551 |
out.c[ 2 ] = in.c[ 1 ]; |
| 552 |
out.c[ 3 ] = in.c[ 0 ]; |
| 553 |
return out.f; |
| 554 |
} |
| 555 |
#endif /*__BIG_ENDIAN__*/ |
| 556 |
|
| 557 |
/* _pico_stristr: |
| 558 |
* case-insensitive strstr. -sea |
| 559 |
*/ |
| 560 |
char *_pico_stristr( char *str, const char *substr ) |
| 561 |
{ |
| 562 |
const size_t sublen = strlen(substr); |
| 563 |
while (*str) |
| 564 |
{ |
| 565 |
if (!_pico_strnicmp(str,substr,sublen)) break; |
| 566 |
str++; |
| 567 |
} |
| 568 |
if (!(*str)) str = NULL; |
| 569 |
return str; |
| 570 |
} |
| 571 |
|
| 572 |
/* |
| 573 |
_pico_unixify() |
| 574 |
changes dos \ style path separators to / |
| 575 |
*/ |
| 576 |
|
| 577 |
void _pico_unixify( char *path ) |
| 578 |
{ |
| 579 |
if( path == NULL ) |
| 580 |
return; |
| 581 |
while( *path ) |
| 582 |
{ |
| 583 |
if( *path == '\\' ) |
| 584 |
*path = '/'; |
| 585 |
path++; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
/* _pico_nofname: |
| 590 |
* removes file name portion from given file path and converts |
| 591 |
* the directory separators to un*x style. returns 1 on success |
| 592 |
* or 0 when 'destSize' was exceeded. -sea |
| 593 |
*/ |
| 594 |
int _pico_nofname( const char *path, char *dest, int destSize ) |
| 595 |
{ |
| 596 |
int left = destSize; |
| 597 |
char *temp = dest; |
| 598 |
|
| 599 |
while ((*dest = *path) != '\0') |
| 600 |
{ |
| 601 |
if (*dest == '/' || *dest == '\\') |
| 602 |
{ |
| 603 |
temp = (dest + 1); |
| 604 |
*dest = '/'; |
| 605 |
} |
| 606 |
dest++; path++; |
| 607 |
|
| 608 |
if (--left < 1) |
| 609 |
{ |
| 610 |
*temp = '\0'; |
| 611 |
return 0; |
| 612 |
} |
| 613 |
} |
| 614 |
*temp = '\0'; |
| 615 |
return 1; |
| 616 |
} |
| 617 |
|
| 618 |
/* _pico_nopath: |
| 619 |
* returns ptr to filename portion in given path or an empty |
| 620 |
* string otherwise. given 'path' is not altered. -sea |
| 621 |
*/ |
| 622 |
const char *_pico_nopath( const char *path ) |
| 623 |
{ |
| 624 |
const char *src; |
| 625 |
src = path + (strlen(path) - 1); |
| 626 |
|
| 627 |
if (path == NULL) return ""; |
| 628 |
if (!strchr(path,'/') && !strchr(path,'\\')) |
| 629 |
return (path); |
| 630 |
|
| 631 |
while ((src--) != path) |
| 632 |
{ |
| 633 |
if (*src == '/' || *src == '\\') |
| 634 |
return (++src); |
| 635 |
} |
| 636 |
return ""; |
| 637 |
} |
| 638 |
|
| 639 |
/* _pico_setfext: |
| 640 |
* sets/changes the file extension for the given filename |
| 641 |
* or filepath's filename portion. the given 'path' *is* |
| 642 |
* altered. leave 'ext' empty to remove extension. -sea |
| 643 |
*/ |
| 644 |
char *_pico_setfext( char *path, const char *ext ) |
| 645 |
{ |
| 646 |
char *src; |
| 647 |
int remfext = 0; |
| 648 |
|
| 649 |
src = path + (strlen(path) - 1); |
| 650 |
|
| 651 |
if (ext == NULL) ext = ""; |
| 652 |
if (strlen(ext ) < 1) remfext = 1; |
| 653 |
if (strlen(path) < 1) |
| 654 |
return path; |
| 655 |
|
| 656 |
while ((src--) != path) |
| 657 |
{ |
| 658 |
if (*src == '/' || *src == '\\') |
| 659 |
return path; |
| 660 |
|
| 661 |
if (*src == '.') |
| 662 |
{ |
| 663 |
if (remfext) |
| 664 |
{ |
| 665 |
*src = '\0'; |
| 666 |
return path; |
| 667 |
} |
| 668 |
*(++src) = '\0'; |
| 669 |
break; |
| 670 |
} |
| 671 |
} |
| 672 |
strcat(path,ext); |
| 673 |
return path; |
| 674 |
} |
| 675 |
|
| 676 |
/* _pico_getline: |
| 677 |
* extracts one line from the given buffer and stores it in dest. |
| 678 |
* returns -1 on error or the length of the line on success. i've |
| 679 |
* removed string trimming here. this can be done manually by the |
| 680 |
* calling func. |
| 681 |
*/ |
| 682 |
int _pico_getline( char *buf, int bufsize, char *dest, int destsize ) |
| 683 |
{ |
| 684 |
int pos; |
| 685 |
|
| 686 |
/* check output */ |
| 687 |
if (dest == NULL || destsize < 1) return -1; |
| 688 |
memset( dest,0,destsize ); |
| 689 |
|
| 690 |
/* check input */ |
| 691 |
if (buf == NULL || bufsize < 1) |
| 692 |
return -1; |
| 693 |
|
| 694 |
/* get next line */ |
| 695 |
for (pos=0; pos<bufsize && pos<destsize; pos++) |
| 696 |
{ |
| 697 |
if (buf[pos] == '\n') { pos++; break; } |
| 698 |
dest[pos] = buf[pos]; |
| 699 |
} |
| 700 |
/* terminate dest and return */ |
| 701 |
dest[pos] = '\0'; |
| 702 |
return pos; |
| 703 |
} |
| 704 |
|
| 705 |
/* _pico_parse_skip_white: |
| 706 |
* skips white spaces in current pico parser, sets *hasLFs |
| 707 |
* to 1 if linefeeds were skipped, and either returns the |
| 708 |
* parser's cursor pointer or NULL on error. -sea |
| 709 |
*/ |
| 710 |
void _pico_parse_skip_white( picoParser_t *p, int *hasLFs ) |
| 711 |
{ |
| 712 |
/* sanity checks */ |
| 713 |
if (p == NULL || p->cursor == NULL) |
| 714 |
return; |
| 715 |
|
| 716 |
/* skin white spaces */ |
| 717 |
while( 1 ) |
| 718 |
{ |
| 719 |
/* sanity checks */ |
| 720 |
if (p->cursor < p->buffer || |
| 721 |
p->cursor >= p->max) |
| 722 |
{ |
| 723 |
return; |
| 724 |
} |
| 725 |
/* break for chars other than white spaces */ |
| 726 |
if (*p->cursor > 0x20) break; |
| 727 |
if (*p->cursor == 0x00) return; |
| 728 |
|
| 729 |
/* a bit of linefeed handling */ |
| 730 |
if (*p->cursor == '\n') |
| 731 |
{ |
| 732 |
*hasLFs = 1; |
| 733 |
p->curLine++; |
| 734 |
} |
| 735 |
/* go to next character */ |
| 736 |
p->cursor++; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
/* _pico_new_parser: |
| 741 |
* allocates a new ascii parser object. |
| 742 |
*/ |
| 743 |
picoParser_t *_pico_new_parser( picoByte_t *buffer, int bufSize ) |
| 744 |
{ |
| 745 |
picoParser_t *p; |
| 746 |
|
| 747 |
/* sanity check */ |
| 748 |
if( buffer == NULL || bufSize <= 0 ) |
| 749 |
return NULL; |
| 750 |
|
| 751 |
/* allocate reader */ |
| 752 |
p = _pico_alloc( sizeof(picoParser_t) ); |
| 753 |
if (p == NULL) return NULL; |
| 754 |
memset( p,0,sizeof(picoParser_t) ); |
| 755 |
|
| 756 |
/* allocate token space */ |
| 757 |
p->tokenSize = 0; |
| 758 |
p->tokenMax = 1024; |
| 759 |
p->token = _pico_alloc( p->tokenMax ); |
| 760 |
if( p->token == NULL ) |
| 761 |
{ |
| 762 |
_pico_free( p ); |
| 763 |
return NULL; |
| 764 |
} |
| 765 |
/* setup */ |
| 766 |
p->buffer = (char*)buffer; |
| 767 |
p->cursor = (char*)buffer; |
| 768 |
p->bufSize = bufSize; |
| 769 |
p->max = p->buffer + bufSize; |
| 770 |
p->curLine = 1; /* sea: new */ |
| 771 |
|
| 772 |
/* return ptr to parser */ |
| 773 |
return p; |
| 774 |
} |
| 775 |
|
| 776 |
/* _pico_free_parser: |
| 777 |
* frees an existing pico parser object. |
| 778 |
*/ |
| 779 |
void _pico_free_parser( picoParser_t *p ) |
| 780 |
{ |
| 781 |
/* sanity check */ |
| 782 |
if (p == NULL) return; |
| 783 |
|
| 784 |
/* free the parser */ |
| 785 |
if (p->token != NULL) |
| 786 |
{ |
| 787 |
_pico_free( p->token ); |
| 788 |
} |
| 789 |
_pico_free( p ); |
| 790 |
} |
| 791 |
|
| 792 |
/* _pico_parse_ex: |
| 793 |
* reads the next token from given pico parser object. if param |
| 794 |
* 'allowLFs' is 1 it will read beyond linefeeds and return 0 when |
| 795 |
* the EOF is reached. if 'allowLFs' is 0 it will return 0 when |
| 796 |
* the EOL is reached. if 'handleQuoted' is 1 the parser function |
| 797 |
* will handle "quoted" strings and return the data between the |
| 798 |
* quotes as token. returns 0 on end/error or 1 on success. -sea |
| 799 |
*/ |
| 800 |
int _pico_parse_ex( picoParser_t *p, int allowLFs, int handleQuoted ) |
| 801 |
{ |
| 802 |
int hasLFs = 0; |
| 803 |
char *old; |
| 804 |
|
| 805 |
/* sanity checks */ |
| 806 |
if( p == NULL || p->buffer == NULL || |
| 807 |
p->cursor < p->buffer || |
| 808 |
p->cursor >= p->max ) |
| 809 |
{ |
| 810 |
return 0; |
| 811 |
} |
| 812 |
/* clear parser token */ |
| 813 |
p->tokenSize = 0; |
| 814 |
p->token[ 0 ] = '\0'; |
| 815 |
old = p->cursor; |
| 816 |
|
| 817 |
/* skip whitespaces */ |
| 818 |
while( p->cursor < p->max && *p->cursor <= 32 ) |
| 819 |
{ |
| 820 |
if (*p->cursor == '\n') |
| 821 |
{ |
| 822 |
p->curLine++; |
| 823 |
hasLFs++; |
| 824 |
} |
| 825 |
p->cursor++; |
| 826 |
} |
| 827 |
/* return if we're not allowed to go beyond lfs */ |
| 828 |
if ((hasLFs > 0) && !allowLFs) |
| 829 |
{ |
| 830 |
p->cursor = old; |
| 831 |
return 0; |
| 832 |
} |
| 833 |
/* get next quoted string */ |
| 834 |
if (*p->cursor == '\"' && handleQuoted) |
| 835 |
{ |
| 836 |
p->cursor++; |
| 837 |
while (p->cursor < p->max && *p->cursor) |
| 838 |
{ |
| 839 |
if (*p->cursor == '\\') |
| 840 |
{ |
| 841 |
if (*(p->cursor+1) == '"') |
| 842 |
{ |
| 843 |
p->cursor++; |
| 844 |
} |
| 845 |
p->token[ p->tokenSize++ ] = *p->cursor++; |
| 846 |
continue; |
| 847 |
} |
| 848 |
else if (*p->cursor == '\"') |
| 849 |
{ |
| 850 |
p->cursor++; |
| 851 |
break; |
| 852 |
} |
| 853 |
else if (*p->cursor == '\n') |
| 854 |
{ |
| 855 |
p->curLine++; |
| 856 |
} |
| 857 |
p->token[ p->tokenSize++ ] = *p->cursor++; |
| 858 |
} |
| 859 |
/* terminate token */ |
| 860 |
p->token[ p->tokenSize ] = '\0'; |
| 861 |
return 1; |
| 862 |
} |
| 863 |
/* otherwise get next word */ |
| 864 |
while( p->cursor < p->max && *p->cursor > 32 ) |
| 865 |
{ |
| 866 |
if (*p->cursor == '\n') |
| 867 |
{ |
| 868 |
p->curLine++; |
| 869 |
} |
| 870 |
p->token[ p->tokenSize++ ] = *p->cursor++; |
| 871 |
} |
| 872 |
/* terminate token */ |
| 873 |
p->token[ p->tokenSize ] = '\0'; |
| 874 |
return 1; |
| 875 |
} |
| 876 |
|
| 877 |
/* _pico_parse_first: |
| 878 |
* reads the first token from the next line and returns |
| 879 |
* a pointer to it. returns NULL on EOL or EOF. -sea |
| 880 |
*/ |
| 881 |
char *_pico_parse_first( picoParser_t *p ) |
| 882 |
{ |
| 883 |
/* sanity check */ |
| 884 |
if (p == NULL) return NULL; |
| 885 |
|
| 886 |
/* try to read next token (with lfs & quots) */ |
| 887 |
if (!_pico_parse_ex( p,1,1 )) |
| 888 |
return NULL; |
| 889 |
|
| 890 |
/* return ptr to the token string */ |
| 891 |
return p->token; |
| 892 |
} |
| 893 |
|
| 894 |
/* _pico_parse: |
| 895 |
* reads the next token from the parser and returns a pointer |
| 896 |
* to it. quoted strings are handled as usual. returns NULL |
| 897 |
* on EOL or EOF. -sea |
| 898 |
*/ |
| 899 |
char *_pico_parse( picoParser_t *p, int allowLFs ) |
| 900 |
{ |
| 901 |
/* sanity check */ |
| 902 |
if (p == NULL) return NULL; |
| 903 |
|
| 904 |
/* try to read next token (with quots) */ |
| 905 |
if (!_pico_parse_ex( p,allowLFs,1 )) |
| 906 |
return NULL; |
| 907 |
|
| 908 |
/* return ptr to the token string */ |
| 909 |
return p->token; |
| 910 |
} |
| 911 |
|
| 912 |
/* _pico_parse_skip_rest: |
| 913 |
* skips the rest of the current line in parser. |
| 914 |
*/ |
| 915 |
void _pico_parse_skip_rest( picoParser_t *p ) |
| 916 |
{ |
| 917 |
while( _pico_parse_ex( p,0,0 ) ) ; |
| 918 |
} |
| 919 |
|
| 920 |
/* _pico_parse_skip_braced: |
| 921 |
* parses/skips over a braced section. returns 1 on success |
| 922 |
* or 0 on error (when there was no closing bracket and the |
| 923 |
* end of buffer was reached or when the opening bracket was |
| 924 |
* missing). |
| 925 |
*/ |
| 926 |
int _pico_parse_skip_braced( picoParser_t *p ) |
| 927 |
{ |
| 928 |
int firstToken = 1; |
| 929 |
int level; |
| 930 |
|
| 931 |
/* sanity check */ |
| 932 |
if (p == NULL) return 0; |
| 933 |
|
| 934 |
/* set the initial level for parsing */ |
| 935 |
level = 0; |
| 936 |
|
| 937 |
/* skip braced section */ |
| 938 |
while( 1 ) |
| 939 |
{ |
| 940 |
/* read next token (lfs allowed) */ |
| 941 |
if (!_pico_parse_ex( p,1,1 )) |
| 942 |
{ |
| 943 |
/* end of parser buffer reached */ |
| 944 |
return 0; |
| 945 |
} |
| 946 |
/* first token must be an opening bracket */ |
| 947 |
if (firstToken && p->token[0] != '{') |
| 948 |
{ |
| 949 |
/* opening bracket missing */ |
| 950 |
return 0; |
| 951 |
} |
| 952 |
/* we only check this once */ |
| 953 |
firstToken = 0; |
| 954 |
|
| 955 |
/* update level */ |
| 956 |
if (p->token[1] == '\0') |
| 957 |
{ |
| 958 |
if (p->token[0] == '{') level++; |
| 959 |
if (p->token[0] == '}') level--; |
| 960 |
} |
| 961 |
/* break if we're back at our starting level */ |
| 962 |
if (level == 0) break; |
| 963 |
} |
| 964 |
/* successfully skipped braced section */ |
| 965 |
return 1; |
| 966 |
} |
| 967 |
|
| 968 |
int _pico_parse_check( picoParser_t *p, int allowLFs, const char *str ) |
| 969 |
{ |
| 970 |
if (!_pico_parse_ex( p,allowLFs,1 )) |
| 971 |
return 0; |
| 972 |
if (!strcmp(p->token,str)) |
| 973 |
return 1; |
| 974 |
return 0; |
| 975 |
} |
| 976 |
|
| 977 |
int _pico_parse_checki( picoParser_t *p, int allowLFs, const char *str ) |
| 978 |
{ |
| 979 |
if (!_pico_parse_ex( p,allowLFs,1 )) |
| 980 |
return 0; |
| 981 |
if (!_pico_stricmp(p->token,str)) |
| 982 |
return 1; |
| 983 |
return 0; |
| 984 |
} |
| 985 |
|
| 986 |
int _pico_parse_int( picoParser_t *p, int *out ) |
| 987 |
{ |
| 988 |
char *token; |
| 989 |
|
| 990 |
/* sanity checks */ |
| 991 |
if (p == NULL || out == NULL) |
| 992 |
return 0; |
| 993 |
|
| 994 |
/* get token and turn it into an integer */ |
| 995 |
*out = 0; |
| 996 |
token = _pico_parse( p,0 ); |
| 997 |
if (token == NULL) return 0; |
| 998 |
*out = atoi( token ); |
| 999 |
|
| 1000 |
/* success */ |
| 1001 |
return 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
int _pico_parse_int_def( picoParser_t *p, int *out, int def ) |
| 1005 |
{ |
| 1006 |
char *token; |
| 1007 |
|
| 1008 |
/* sanity checks */ |
| 1009 |
if (p == NULL || out == NULL) |
| 1010 |
return 0; |
| 1011 |
|
| 1012 |
/* get token and turn it into an integer */ |
| 1013 |
*out = def; |
| 1014 |
token = _pico_parse( p,0 ); |
| 1015 |
if (token == NULL) return 0; |
| 1016 |
*out = atoi( token ); |
| 1017 |
|
| 1018 |
/* success */ |
| 1019 |
return 1; |
| 1020 |
} |
| 1021 |
|
| 1022 |
int _pico_parse_float( picoParser_t *p, float *out ) |
| 1023 |
{ |
| 1024 |
char *token; |
| 1025 |
|
| 1026 |
/* sanity checks */ |
| 1027 |
if (p == NULL || out == NULL) |
| 1028 |
return 0; |
| 1029 |
|
| 1030 |
/* get token and turn it into a float */ |
| 1031 |
*out = 0.0f; |
| 1032 |
token = _pico_parse( p,0 ); |
| 1033 |
if (token == NULL) return 0; |
| 1034 |
*out = (float) atof( token ); |
| 1035 |
|
| 1036 |
/* success */ |
| 1037 |
return 1; |
| 1038 |
} |
| 1039 |
|
| 1040 |
int _pico_parse_float_def( picoParser_t *p, float *out, float def ) |
| 1041 |
{ |
| 1042 |
char *token; |
| 1043 |
|
| 1044 |
/* sanity checks */ |
| 1045 |
if (p == NULL || out == NULL) |
| 1046 |
return 0; |
| 1047 |
|
| 1048 |
/* get token and turn it into a float */ |
| 1049 |
*out = def; |
| 1050 |
token = _pico_parse( p,0 ); |
| 1051 |
if (token == NULL) return 0; |
| 1052 |
*out = (float) atof( token ); |
| 1053 |
|
| 1054 |
/* success */ |
| 1055 |
return 1; |
| 1056 |
} |
| 1057 |
|
| 1058 |
int _pico_parse_double( picoParser_t *p, double *out ) |
| 1059 |
{ |
| 1060 |
char *token; |
| 1061 |
|
| 1062 |
/* sanity checks */ |
| 1063 |
if (p == NULL || out == NULL) |
| 1064 |
return 0; |
| 1065 |
|
| 1066 |
/* get token and turn it into a double */ |
| 1067 |
*out = 0; |
| 1068 |
token = _pico_parse( p,0 ); |
| 1069 |
if (token == NULL) return 0; |
| 1070 |
*out = (double) strtod( token, NULL ); |
| 1071 |
|
| 1072 |
/* success */ |
| 1073 |
return 1; |
| 1074 |
} |
| 1075 |
|
| 1076 |
int _pico_parse_double_def( picoParser_t *p, double *out, double def ) |
| 1077 |
{ |
| 1078 |
char *token; |
| 1079 |
|
| 1080 |
/* sanity checks */ |
| 1081 |
if (p == NULL || out == NULL) |
| 1082 |
return 0; |
| 1083 |
|
| 1084 |
/* get token and turn it into a double */ |
| 1085 |
*out = def; |
| 1086 |
token = _pico_parse( p,0 ); |
| 1087 |
if (token == NULL) return 0; |
| 1088 |
*out = (double) strtod( token, NULL ); |
| 1089 |
|
| 1090 |
/* success */ |
| 1091 |
return 1; |
| 1092 |
} |
| 1093 |
|
| 1094 |
int _pico_parse_vec( picoParser_t *p, picoVec3_t out ) |
| 1095 |
{ |
| 1096 |
char *token; |
| 1097 |
int i; |
| 1098 |
|
| 1099 |
/* sanity checks */ |
| 1100 |
if (p == NULL || out == NULL) |
| 1101 |
return 0; |
| 1102 |
|
| 1103 |
/* zero out outination vector */ |
| 1104 |
_pico_zero_vec( out ); |
| 1105 |
|
| 1106 |
/* parse three vector components */ |
| 1107 |
for (i=0; i<3; i++) |
| 1108 |
{ |
| 1109 |
token = _pico_parse( p,0 ); |
| 1110 |
if (token == NULL) |
| 1111 |
{ |
| 1112 |
_pico_zero_vec( out ); |
| 1113 |
return 0; |
| 1114 |
} |
| 1115 |
out[ i ] = (float) atof( token ); |
| 1116 |
} |
| 1117 |
/* success */ |
| 1118 |
return 1; |
| 1119 |
} |
| 1120 |
|
| 1121 |
int _pico_parse_vec_def( picoParser_t *p, picoVec3_t out, picoVec3_t def ) |
| 1122 |
{ |
| 1123 |
char *token; |
| 1124 |
int i; |
| 1125 |
|
| 1126 |
/* sanity checks */ |
| 1127 |
if (p == NULL || out == NULL) |
| 1128 |
return 0; |
| 1129 |
|
| 1130 |
/* assign default vector value */ |
| 1131 |
_pico_copy_vec( def,out ); |
| 1132 |
|
| 1133 |
/* parse three vector components */ |
| 1134 |
for (i=0; i<3; i++) |
| 1135 |
{ |
| 1136 |
token = _pico_parse( p,0 ); |
| 1137 |
if (token == NULL) |
| 1138 |
{ |
| 1139 |
_pico_copy_vec( def,out ); |
| 1140 |
return 0; |
| 1141 |
} |
| 1142 |
out[ i ] = (float) atof( token ); |
| 1143 |
} |
| 1144 |
/* success */ |
| 1145 |
return 1; |
| 1146 |
} |
| 1147 |
|
| 1148 |
int _pico_parse_vec2( picoParser_t *p, picoVec2_t out ) |
| 1149 |
{ |
| 1150 |
char *token; |
| 1151 |
int i; |
| 1152 |
|
| 1153 |
/* sanity checks */ |
| 1154 |
if (p == NULL || out == NULL) |
| 1155 |
return 0; |
| 1156 |
|
| 1157 |
/* zero out outination vector */ |
| 1158 |
_pico_zero_vec2( out ); |
| 1159 |
|
| 1160 |
/* parse two vector components */ |
| 1161 |
for (i=0; i<2; i++) |
| 1162 |
{ |
| 1163 |
token = _pico_parse( p,0 ); |
| 1164 |
if (token == NULL) |
| 1165 |
{ |
| 1166 |
_pico_zero_vec2( out ); |
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
out[ i ] = (float) atof( token ); |
| 1170 |
} |
| 1171 |
/* success */ |
| 1172 |
return 1; |
| 1173 |
} |
| 1174 |
|
| 1175 |
int _pico_parse_vec2_def( picoParser_t *p, picoVec2_t out, picoVec2_t def ) |
| 1176 |
{ |
| 1177 |
char *token; |
| 1178 |
int i; |
| 1179 |
|
| 1180 |
/* sanity checks */ |
| 1181 |
if (p == NULL || out == NULL) |
| 1182 |
return 0; |
| 1183 |
|
| 1184 |
/* assign default vector value */ |
| 1185 |
_pico_copy_vec2( def,out ); |
| 1186 |
|
| 1187 |
/* parse two vector components */ |
| 1188 |
for (i=0; i<2; i++) |
| 1189 |
{ |
| 1190 |
token = _pico_parse( p,0 ); |
| 1191 |
if (token == NULL) |
| 1192 |
{ |
| 1193 |
_pico_copy_vec2( def,out ); |
| 1194 |
return 0; |
| 1195 |
} |
| 1196 |
out[ i ] = (float) atof( token ); |
| 1197 |
} |
| 1198 |
/* success */ |
| 1199 |
return 1; |
| 1200 |
} |
| 1201 |
|
| 1202 |
int _pico_parse_vec4( picoParser_t *p, picoVec4_t out ) |
| 1203 |
{ |
| 1204 |
char *token; |
| 1205 |
int i; |
| 1206 |
|
| 1207 |
/* sanity checks */ |
| 1208 |
if (p == NULL || out == NULL) |
| 1209 |
return 0; |
| 1210 |
|
| 1211 |
/* zero out outination vector */ |
| 1212 |
_pico_zero_vec4( out ); |
| 1213 |
|
| 1214 |
/* parse four vector components */ |
| 1215 |
for (i=0; i<4; i++) |
| 1216 |
{ |
| 1217 |
token = _pico_parse( p,0 ); |
| 1218 |
if (token == NULL) |
| 1219 |
{ |
| 1220 |
_pico_zero_vec4( out ); |
| 1221 |
return 0; |
| 1222 |
} |
| 1223 |
out[ i ] = (float) atof( token ); |
| 1224 |
} |
| 1225 |
/* success */ |
| 1226 |
return 1; |
| 1227 |
} |
| 1228 |
|
| 1229 |
int _pico_parse_vec4_def( picoParser_t *p, picoVec4_t out, picoVec4_t def ) |
| 1230 |
{ |
| 1231 |
char *token; |
| 1232 |
int i; |
| 1233 |
|
| 1234 |
/* sanity checks */ |
| 1235 |
if (p == NULL || out == NULL) |
| 1236 |
return 0; |
| 1237 |
|
| 1238 |
/* assign default vector value */ |
| 1239 |
_pico_copy_vec4( def,out ); |
| 1240 |
|
| 1241 |
/* parse four vector components */ |
| 1242 |
for (i=0; i<4; i++) |
| 1243 |
{ |
| 1244 |
token = _pico_parse( p,0 ); |
| 1245 |
if (token == NULL) |
| 1246 |
{ |
| 1247 |
_pico_copy_vec4( def,out ); |
| 1248 |
return 0; |
| 1249 |
} |
| 1250 |
out[ i ] = (float) atof( token ); |
| 1251 |
} |
| 1252 |
/* success */ |
| 1253 |
return 1; |
| 1254 |
} |
| 1255 |
|
| 1256 |
/* _pico_new_memstream: |
| 1257 |
* allocates a new memorystream object. |
| 1258 |
*/ |
| 1259 |
picoMemStream_t *_pico_new_memstream( picoByte_t *buffer, int bufSize ) |
| 1260 |
{ |
| 1261 |
picoMemStream_t *s; |
| 1262 |
|
| 1263 |
/* sanity check */ |
| 1264 |
if( buffer == NULL || bufSize <= 0 ) |
| 1265 |
return NULL; |
| 1266 |
|
| 1267 |
/* allocate stream */ |
| 1268 |
s = _pico_alloc( sizeof(picoMemStream_t) ); |
| 1269 |
if (s == NULL) return NULL; |
| 1270 |
memset( s,0,sizeof(picoMemStream_t) ); |
| 1271 |
|
| 1272 |
/* setup */ |
| 1273 |
s->buffer = buffer; |
| 1274 |
s->curPos = buffer; |
| 1275 |
s->bufSize = bufSize; |
| 1276 |
s->flag = 0; |
| 1277 |
|
| 1278 |
/* return ptr to stream */ |
| 1279 |
return s; |
| 1280 |
} |
| 1281 |
|
| 1282 |
/* _pico_free_memstream: |
| 1283 |
* frees an existing pico memorystream object. |
| 1284 |
*/ |
| 1285 |
void _pico_free_memstream( picoMemStream_t *s ) |
| 1286 |
{ |
| 1287 |
/* sanity check */ |
| 1288 |
if (s == NULL) return; |
| 1289 |
|
| 1290 |
/* free the stream */ |
| 1291 |
_pico_free( s ); |
| 1292 |
} |
| 1293 |
|
| 1294 |
/* _pico_memstream_read: |
| 1295 |
* reads data from a pico memorystream into a buffer. |
| 1296 |
*/ |
| 1297 |
int _pico_memstream_read( picoMemStream_t *s, void *buffer, size_t len ) |
| 1298 |
{ |
| 1299 |
int ret = 1; |
| 1300 |
|
| 1301 |
/* sanity checks */ |
| 1302 |
if (s == NULL || buffer == NULL) |
| 1303 |
return 0; |
| 1304 |
|
| 1305 |
if (s->curPos + len > s->buffer + s->bufSize) |
| 1306 |
{ |
| 1307 |
s->flag |= PICO_IOEOF; |
| 1308 |
len = s->buffer + s->bufSize - s->curPos; |
| 1309 |
ret = 0; |
| 1310 |
} |
| 1311 |
|
| 1312 |
/* read the data */ |
| 1313 |
memcpy( buffer, s->curPos, len ); |
| 1314 |
s->curPos += len; |
| 1315 |
return ret; |
| 1316 |
} |
| 1317 |
|
| 1318 |
/* _pico_memstream_read: |
| 1319 |
* reads a character from a pico memorystream |
| 1320 |
*/ |
| 1321 |
int _pico_memstream_getc( picoMemStream_t *s ) |
| 1322 |
{ |
| 1323 |
int c = 0; |
| 1324 |
|
| 1325 |
/* sanity check */ |
| 1326 |
if (s == NULL) |
| 1327 |
return -1; |
| 1328 |
|
| 1329 |
/* read the character */ |
| 1330 |
if (_pico_memstream_read( s, &c, 1) == 0) |
| 1331 |
return -1; |
| 1332 |
|
| 1333 |
return c; |
| 1334 |
} |
| 1335 |
|
| 1336 |
/* _pico_memstream_seek: |
| 1337 |
* sets the current read position to a different location |
| 1338 |
*/ |
| 1339 |
int _pico_memstream_seek( picoMemStream_t *s, long offset, int origin ) |
| 1340 |
{ |
| 1341 |
intptr_t overflow; |
| 1342 |
|
| 1343 |
/* sanity check */ |
| 1344 |
if (s == NULL) |
| 1345 |
return -1; |
| 1346 |
|
| 1347 |
if (origin == PICO_SEEK_SET) |
| 1348 |
{ |
| 1349 |
s->curPos = s->buffer + offset; |
| 1350 |
overflow = s->curPos - ( s->buffer + s->bufSize ); |
| 1351 |
if (overflow > 0) |
| 1352 |
{ |
| 1353 |
s->curPos = s->buffer + s->bufSize; |
| 1354 |
return (int)(offset - overflow); |
| 1355 |
} |
| 1356 |
return 0; |
| 1357 |
} |
| 1358 |
else if (origin == PICO_SEEK_CUR) |
| 1359 |
{ |
| 1360 |
s->curPos += offset; |
| 1361 |
overflow = s->curPos - ( s->buffer + s->bufSize ); |
| 1362 |
if (overflow > 0) |
| 1363 |
{ |
| 1364 |
s->curPos = s->buffer + s->bufSize; |
| 1365 |
return (int)(offset - overflow); |
| 1366 |
} |
| 1367 |
return 0; |
| 1368 |
} |
| 1369 |
else if (origin == PICO_SEEK_END) |
| 1370 |
{ |
| 1371 |
s->curPos = ( s->buffer + s->bufSize ) - offset; |
| 1372 |
overflow = s->buffer - s->curPos; |
| 1373 |
if (overflow > 0) |
| 1374 |
{ |
| 1375 |
s->curPos = s->buffer; |
| 1376 |
return (int)(offset - overflow); |
| 1377 |
} |
| 1378 |
return 0; |
| 1379 |
} |
| 1380 |
|
| 1381 |
return -1; |
| 1382 |
} |
| 1383 |
|
| 1384 |
/* _pico_memstream_tell: |
| 1385 |
* returns the current read position in the pico memorystream |
| 1386 |
*/ |
| 1387 |
long _pico_memstream_tell( picoMemStream_t *s ) |
| 1388 |
{ |
| 1389 |
/* sanity check */ |
| 1390 |
if (s == NULL) |
| 1391 |
return -1; |
| 1392 |
|
| 1393 |
return (long)(s->curPos - s->buffer); |
| 1394 |
} |