| 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 |
#define PICOINTERNAL_C |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
#include <string.h> |
|---|
| 48 |
#include "picointernal.h" |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
void *(*_pico_ptr_malloc )( size_t ) = malloc; |
|---|
| 54 |
void (*_pico_ptr_free )( void* ) = free; |
|---|
| 55 |
void (*_pico_ptr_load_file )( char*, unsigned char**, int* ) = NULL; |
|---|
| 56 |
void (*_pico_ptr_free_file )( void* ) = NULL; |
|---|
| 57 |
void (*_pico_ptr_print )( int, const char* ) = NULL; |
|---|
| 58 |
|
|---|
| 59 |
typedef union |
|---|
| 60 |
{ |
|---|
| 61 |
float f; |
|---|
| 62 |
char c[4]; |
|---|
| 63 |
} |
|---|
| 64 |
floatSwapUnion; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
void *_pico_alloc( size_t size ) |
|---|
| 70 |
{ |
|---|
| 71 |
void *ptr; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
if( size == 0 ) |
|---|
| 75 |
return NULL; |
|---|
| 76 |
if (_pico_ptr_malloc == NULL) |
|---|
| 77 |
return NULL; |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
ptr = _pico_ptr_malloc(size); |
|---|
| 81 |
if (ptr == NULL) |
|---|
| 82 |
return NULL; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
memset(ptr,0,size); |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
return ptr; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
void *_pico_calloc( size_t num, size_t size ) |
|---|
| 95 |
{ |
|---|
| 96 |
void *ptr; |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
if( num == 0 || size == 0 ) |
|---|
| 100 |
return NULL; |
|---|
| 101 |
if (_pico_ptr_malloc == NULL) |
|---|
| 102 |
return NULL; |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
ptr = _pico_ptr_malloc(num*size); |
|---|
| 106 |
if (ptr == NULL) |
|---|
| 107 |
return NULL; |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
memset(ptr,0,num*size); |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
return ptr; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
void *_pico_realloc( void **ptr, size_t oldSize, size_t newSize ) |
|---|
| 121 |
{ |
|---|
| 122 |
void *ptr2; |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
if( ptr == NULL ) |
|---|
| 126 |
return NULL; |
|---|
| 127 |
if( newSize < oldSize ) |
|---|
| 128 |
return *ptr; |
|---|
| 129 |
if (_pico_ptr_malloc == NULL) |
|---|
| 130 |
return NULL; |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
ptr2 = _pico_alloc( newSize ); |
|---|
| 134 |
if( ptr2 == NULL ) |
|---|
| 135 |
return NULL; |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
if( *ptr != NULL ) |
|---|
| 139 |
{ |
|---|
| 140 |
memcpy( ptr2, *ptr, oldSize ); |
|---|
| 141 |
_pico_free( *ptr ); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
*ptr = ptr2; |
|---|
| 146 |
return *ptr; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
char *_pico_clone_alloc( char *str, int size ) |
|---|
| 159 |
{ |
|---|
| 160 |
char *cloned; |
|---|
| 161 |
size_t cloneSize; |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
if (str == NULL) return NULL; |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
cloneSize = (size < 0) ? strlen(str) : size; |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
cloned = _pico_alloc( cloneSize+1 ); |
|---|
| 171 |
if (cloned == NULL) |
|---|
| 172 |
return NULL; |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
memset( cloned,0,cloneSize ); |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
if (cloneSize < strlen( str )) { |
|---|
| 179 |
memcpy( cloned,str,cloneSize ); |
|---|
| 180 |
cloned[ cloneSize ] = '\0'; |
|---|
| 181 |
} else { |
|---|
| 182 |
strcpy( cloned,str ); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
return cloned; |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
void _pico_free( void *ptr ) |
|---|
| 192 |
{ |
|---|
| 193 |
|
|---|
| 194 |
if( ptr == NULL ) |
|---|
| 195 |
return; |
|---|
| 196 |
if (_pico_ptr_free == NULL) |
|---|
| 197 |
return; |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
_pico_ptr_free( ptr ); |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
void _pico_load_file( char *name, unsigned char **buffer, int *bufSize ) |
|---|
| 207 |
{ |
|---|
| 208 |
|
|---|
| 209 |
if( name == NULL ) |
|---|
| 210 |
{ |
|---|
| 211 |
*bufSize = -1; |
|---|
| 212 |
return; |
|---|
| 213 |
} |
|---|
| 214 |
if (_pico_ptr_load_file == NULL) |
|---|
| 215 |
{ |
|---|
| 216 |
*bufSize = -1; |
|---|
| 217 |
return; |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
_pico_ptr_load_file( name,buffer,bufSize ); |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
void _pico_free_file( void *buffer ) |
|---|
| 228 |
{ |
|---|
| 229 |
|
|---|
| 230 |
if( buffer == NULL ) |
|---|
| 231 |
return; |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
if( _pico_ptr_free_file == NULL ) |
|---|
| 235 |
{ |
|---|
| 236 |
free( buffer ); |
|---|
| 237 |
return; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
_pico_ptr_free_file( buffer ); |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
void _pico_printf( int level, const char *format, ...) |
|---|
| 247 |
{ |
|---|
| 248 |
char str[4096]; |
|---|
| 249 |
va_list argptr; |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
if( format == NULL ) |
|---|
| 253 |
return; |
|---|
| 254 |
if (_pico_ptr_print == NULL) |
|---|
| 255 |
return; |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
va_start( argptr,format ); |
|---|
| 259 |
vsprintf( str,format,argptr ); |
|---|
| 260 |
va_end( argptr ); |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
if (str[ strlen(str)-1 ] == '\n') |
|---|
| 264 |
str[ strlen(str)-1 ] = '\0'; |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
_pico_ptr_print( level,str ); |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
char *_pico_strltrim( char *str ) |
|---|
| 274 |
{ |
|---|
| 275 |
char *str1 = str, *str2 = str; |
|---|
| 276 |
|
|---|
| 277 |
while (isspace(*str2)) str2++; |
|---|
| 278 |
if( str2 != str ) |
|---|
| 279 |
while( *str2 != '\0' ) |
|---|
| 280 |
*str1++ = *str2++; |
|---|
| 281 |
return str; |
|---|
| 282 |
} |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
char *_pico_strrtrim( char *str ) |
|---|
| 288 |
{ |
|---|
| 289 |
if (str && *str) |
|---|
| 290 |
{ |
|---|
| 291 |
char *str1 = str; |
|---|
| 292 |
int allspace = 1; |
|---|
| 293 |
|
|---|
| 294 |
while (*str1) |
|---|
| 295 |
{ |
|---|
| 296 |
if (allspace && !isspace(*str1)) allspace = 0; |
|---|
| 297 |
str1++; |
|---|
| 298 |
} |
|---|
| 299 |
if (allspace) *str = '\0'; |
|---|
| 300 |
else { |
|---|
| 301 |
str1--; |
|---|
| 302 |
while ((isspace(*str1)) && (str1 >= str)) |
|---|
| 303 |
*str1-- = '\0'; |
|---|
| 304 |
} |
|---|
| 305 |
} |
|---|
| 306 |
return str; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
char *_pico_strlwr( char *str ) |
|---|
| 313 |
{ |
|---|
| 314 |
char *cp; |
|---|
| 315 |
for (cp=str; *cp; ++cp) |
|---|
| 316 |
{ |
|---|
| 317 |
if ('A' <= *cp && *cp <= 'Z') |
|---|
| 318 |
{ |
|---|
| 319 |
*cp += ('a' - 'A'); |
|---|
| 320 |
} |
|---|
| 321 |
} |
|---|
| 322 |
return str; |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
int _pico_strchcount( char *str, int ch ) |
|---|
| 329 |
{ |
|---|
| 330 |
int count = 0; |
|---|
| 331 |
while (*str++) if (*str == ch) count++; |
|---|
| 332 |
return count; |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
void _pico_zero_bounds( picoVec3_t mins, picoVec3_t maxs ) |
|---|
| 336 |
{ |
|---|
| 337 |
int i; |
|---|
| 338 |
for (i=0; i<3; i++) |
|---|
| 339 |
{ |
|---|
| 340 |
mins[i] = +999999; |
|---|
| 341 |
maxs[i] = -999999; |
|---|
| 342 |
} |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
void _pico_expand_bounds( picoVec3_t p, picoVec3_t mins, picoVec3_t maxs ) |
|---|
| 346 |
{ |
|---|
| 347 |
int i; |
|---|
| 348 |
for (i=0; i<3; i++) |
|---|
| 349 |
{ |
|---|
| 350 |
float value = p[i]; |
|---|
| 351 |
if (value < mins[i]) mins[i] = value; |
|---|
| 352 |
if (value > maxs[i]) maxs[i] = value; |
|---|
| 353 |
} |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
void _pico_zero_vec( picoVec3_t vec ) |
|---|
| 357 |
{ |
|---|
| 358 |
vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = 0; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
void _pico_zero_vec2( picoVec2_t vec ) |
|---|
| 362 |
{ |
|---|
| 363 |
vec[ 0 ] = vec[ 1 ] = 0; |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
void _pico_zero_vec4( picoVec4_t vec ) |
|---|
| 367 |
{ |
|---|
| 368 |
vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = vec[ 3 ] = 0; |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
void _pico_set_vec( picoVec3_t v, float a, float b, float c ) |
|---|
| 372 |
{ |
|---|
| 373 |
v[ 0 ] = a; |
|---|
| 374 |
v[ 1 ] = b; |
|---|
| 375 |
v[ 2 ] = c; |
|---|
| 376 |
} |
|---|
| 377 |
|
|---|
| 378 |
void _pico_set_vec4( picoVec4_t v, float a, float b, float c, float d ) |
|---|
| 379 |
{ |
|---|
| 380 |
v[ 0 ] = a; |
|---|
| 381 |
v[ 1 ] = b; |
|---|
| 382 |
v[ 2 ] = c; |
|---|
| 383 |
v[ 3 ] = d; |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
void _pico_copy_vec( picoVec3_t src, picoVec3_t dest ) |
|---|
| 387 |
{ |
|---|
| 388 |
dest[ 0 ] = src[ 0 ]; |
|---|
| 389 |
dest[ 1 ] = src[ 1 ]; |
|---|
| 390 |
dest[ 2 ] = src[ 2 ]; |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
void _pico_copy_vec2( picoVec2_t src, picoVec2_t dest ) |
|---|
| 394 |
{ |
|---|
| 395 |
dest[ 0 ] = src[ 0 ]; |
|---|
| 396 |
dest[ 1 ] = src[ 1 ]; |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
void _pico_copy_vec4( picoVec4_t src, picoVec4_t dest ) |
|---|
| 400 |
{ |
|---|
| 401 |
dest[ 0 ] = src[ 0 ]; |
|---|
| 402 |
dest[ 1 ] = src[ 1 ]; |
|---|
| 403 |
dest[ 2 ] = src[ 2 ]; |
|---|
| 404 |
dest[ 3 ] = src[ 3 ]; |
|---|
| 405 |
} |
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
picoVec_t _pico_normalize_vec( picoVec3_t vec ) |
|---|
| 409 |
{ |
|---|
| 410 |
double len, ilen; |
|---|
| 411 |
|
|---|
| 412 |
len = sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] + vec[ 2 ] * vec[ 2 ] ); |
|---|
| 413 |
if( len == 0.0 ) return 0.0; |
|---|
| 414 |
ilen = 1.0 / len; |
|---|
| 415 |
vec[ 0 ] *= (picoVec_t) ilen; |
|---|
| 416 |
vec[ 1 ] *= (picoVec_t) ilen; |
|---|
| 417 |
vec[ 2 ] *= (picoVec_t) ilen; |
|---|
| 418 |
return (picoVec_t) len; |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
void _pico_add_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ) |
|---|
| 422 |
{ |
|---|
| 423 |
dest[ 0 ] = a[ 0 ] + b[ 0 ]; |
|---|
| 424 |
dest[ 1 ] = a[ 1 ] + b[ 1 ]; |
|---|
| 425 |
dest[ 2 ] = a[ 2 ] + b[ 2 ]; |
|---|
| 426 |
} |
|---|
| 427 |
|
|---|
| 428 |
void _pico_subtract_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ) |
|---|
| 429 |
{ |
|---|
| 430 |
dest[ 0 ] = a[ 0 ] - b[ 0 ]; |
|---|
| 431 |
dest[ 1 ] = a[ 1 ] - b[ 1 ]; |
|---|
| 432 |
dest[ 2 ] = a[ 2 ] - b[ 2 ]; |
|---|
| 433 |
} |
|---|
| 434 |
|
|---|
| 435 |
void _pico_scale_vec( picoVec3_t v, float scale, picoVec3_t dest ) |
|---|
| 436 |
{ |
|---|
| 437 |
dest[ 0 ] = v[ 0 ] * scale; |
|---|
| 438 |
dest[ 1 ] = v[ 1 ] * scale; |
|---|
| 439 |
dest[ 2 ] = v[ 2 ] * scale; |
|---|
| 440 |
} |
|---|
| 441 |
|
|---|
| 442 |
void _pico_scale_vec4( picoVec4_t v, float scale, picoVec4_t dest ) |
|---|
| 443 |
{ |
|---|
| 444 |
dest[ 0 ] = v[ 0 ] * scale; |
|---|
| 445 |
dest[ 1 ] = v[ 1 ] * scale; |
|---|
| 446 |
dest[ 2 ] = v[ 2 ] * scale; |
|---|
| 447 |
dest[ 3 ] = v[ 3 ] * scale; |
|---|
| 448 |
} |
|---|
| 449 |
|
|---|
| 450 |
picoVec_t _pico_dot_vec( picoVec3_t a, picoVec3_t b ) |
|---|
| 451 |
{ |
|---|
| 452 |
return a[ 0 ] * b[ 0 ] + a[ 1 ] * b[ 1 ] + a[ 2 ] * b[ 2 ]; |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
void _pico_cross_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ) |
|---|
| 456 |
{ |
|---|
| 457 |
dest[ 0 ] = a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ]; |
|---|
| 458 |
dest[ 1 ] = a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ]; |
|---|
| 459 |
dest[ 2 ] = a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ]; |
|---|
| 460 |
} |
|---|
| 461 |
|
|---|
| 462 |
picoVec_t _pico_calc_plane( picoVec4_t plane, picoVec3_t a, picoVec3_t b, picoVec3_t c ) |
|---|
| 463 |
{ |
|---|
| 464 |
picoVec3_t ba, ca; |
|---|
| 465 |
|
|---|
| 466 |
_pico_subtract_vec( b, a, ba ); |
|---|
| 467 |
_pico_subtract_vec( c, a, ca ); |
|---|
| 468 |
_pico_cross_vec( ca, ba, plane ); |
|---|
| 469 |
plane[ 3 ] = _pico_dot_vec( a, plane ); |
|---|
| 470 |
return _pico_normalize_vec( plane ); |
|---|
| 471 |
} |
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
void _pico_set_color( picoColor_t c, int r, int g, int b, int a ) |
|---|
| 475 |
{ |
|---|
| 476 |
c[ 0 ] = r; |
|---|
| 477 |
c[ 1 ] = g; |
|---|
| 478 |
c[ 2 ] = b; |
|---|
| 479 |
c[ 3 ] = a; |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
void _pico_copy_color( picoColor_t src, picoColor_t dest ) |
|---|
| 483 |
{ |
|---|
| 484 |
dest[ 0 ] = src[ 0 ]; |
|---|
| 485 |
dest[ 1 ] = src[ 1 ]; |
|---|
| 486 |
dest[ 2 ] = src[ 2 ]; |
|---|
| 487 |
dest[ 3 ] = src[ 3 ]; |
|---|
| 488 |
} |
|---|
| 489 |
|
|---|
| 490 |
#ifdef __BIG_ENDIAN__ |
|---|
| 491 |
|
|---|
| 492 |
int _pico_big_long ( int src ) { return src; } |
|---|
| 493 |
short _pico_big_short( short src ) { return src; } |
|---|
| 494 |
float _pico_big_float( float src ) { return src; } |
|---|
| 495 |
|
|---|
| 496 |
int _pico_little_long( int src ) |
|---|
| 497 |
{ |
|---|
| 498 |
return ((src & 0xFF000000) >> 24) | |
|---|
| 499 |
((src & 0x00FF0000) >> 8) | |
|---|
| 500 |
((src & 0x0000FF00) << 8) | |
|---|
| 501 |
((src & 0x000000FF) << 24); |
|---|
| 502 |
} |
|---|
| 503 |
|
|---|
| 504 |
short _pico_little_short( short src ) |
|---|
| 505 |
{ |
|---|
| 506 |
return ((src & 0xFF00) >> 8) | |
|---|
| 507 |
((src & 0x00FF) << 8); |
|---|
| 508 |
} |
|---|
| 509 |
|
|---|
| 510 |
float _pico_little_float( float src ) |
|---|
| 511 |
{ |
|---|
| 512 |
floatSwapUnion in,out; |
|---|
| 513 |
in.f = src; |
|---|
| 514 |
out.c[ 0 ] = in.c[ 3 ]; |
|---|
| 515 |
out.c[ 1 ] = in.c[ 2 ]; |
|---|
| 516 |
out.c[ 2 ] = in.c[ 1 ]; |
|---|
| 517 |
out.c[ 3 ] = in.c[ 0 ]; |
|---|
| 518 |
return out.f; |
|---|
| 519 |
} |
|---|
| 520 |
#else |
|---|
| 521 |
|
|---|
| 522 |
int _pico_little_long ( int src ) { return src; } |
|---|
| 523 |
short _pico_little_short( short src ) { return src; } |
|---|
| 524 |
float _pico_little_float( float src ) { return src; } |
|---|
| 525 |
|
|---|
| 526 |
int _pico_big_long( int src ) |
|---|
| 527 |
{ |
|---|
| 528 |
return ((src & 0xFF000000) >> 24) | |
|---|
| 529 |
((src & 0x00FF0000) >> 8) | |
|---|
| 530 |
((src & 0x0000FF00) << 8) | |
|---|
| 531 |
((src & 0x000000FF) << 24); |
|---|
| 532 |
} |
|---|
| 533 |
|
|---|
| 534 |
short _pico_big_short( short src ) |
|---|
| 535 |
{ |
|---|
| 536 |
return ((src & 0xFF00) >> 8) | |
|---|
| 537 |
((src & 0x00FF) << 8); |
|---|
| 538 |
} |
|---|
| 539 |
|
|---|
| 540 |
float _pico_big_float( float src ) |
|---|
| 541 |
{ |
|---|
| 542 |
floatSwapUnion in,out; |
|---|
| 543 |
in.f = src; |
|---|
| 544 |
out.c[ 0 ] = in.c[ 3 ]; |
|---|
| 545 |
out.c[ 1 ] = in.c[ 2 ]; |
|---|
| 546 |
out.c[ 2 ] = in.c[ 1 ]; |
|---|
| 547 |
out.c[ 3 ] = in.c[ 0 ]; |
|---|
| 548 |
return out.f; |
|---|
| 549 |
} |
|---|
| 550 |
#endif |
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
char *_pico_stristr( char *str, const char *substr ) |
|---|
| 556 |
{ |
|---|
| 557 |
const int sublen = strlen(substr); |
|---|
| 558 |
while (*str) |
|---|
| 559 |
{ |
|---|
| 560 |
if (!_pico_strnicmp(str,substr,sublen)) break; |
|---|
| 561 |
str++; |
|---|
| 562 |
} |
|---|
| 563 |
if (!(*str)) str = NULL; |
|---|
| 564 |
return str; |
|---|
| 565 |
} |
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
void _pico_unixify( char *path ) |
|---|
| 573 |
{ |
|---|
| 574 |
if( path == NULL ) |
|---|
| 575 |
return; |
|---|
| 576 |
while( *path ) |
|---|
| 577 |
{ |
|---|
| 578 |
if( *path == '\\' ) |
|---|
| 579 |
*path = '/'; |
|---|
| 580 |
path++; |
|---|
| 581 |
} |
|---|
| 582 |
} |
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
int _pico_nofname( const char *path, char *dest, int destSize ) |
|---|
| 590 |
{ |
|---|
| 591 |
int left = destSize; |
|---|
| 592 |
char *temp = dest; |
|---|
| 593 |
|
|---|
| 594 |
while ((*dest = *path) != '\0') |
|---|
| 595 |
{ |
|---|
| 596 |
if (*dest == '/' || *dest == '\\') |
|---|
| 597 |
{ |
|---|
| 598 |
temp = (dest + 1); |
|---|
| 599 |
*dest = '/'; |
|---|
| 600 |
} |
|---|
| 601 |
dest++; path++; |
|---|
| 602 |
|
|---|
| 603 |
if (--left < 1) |
|---|
| 604 |
{ |
|---|
| 605 |
*temp = '\0'; |
|---|
| 606 |
return 0; |
|---|
| 607 |
} |
|---|
| 608 |
} |
|---|
| 609 |
*temp = '\0'; |
|---|
| 610 |
return 1; |
|---|
| 611 |
} |
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
char *_pico_nopath( const char *path ) |
|---|
| 618 |
{ |
|---|
| 619 |
char *src; |
|---|
| 620 |
src = (char *)path + (strlen(path) - 1); |
|---|
| 621 |
|
|---|
| 622 |
if (path == NULL) return (char *)""; |
|---|
| 623 |
if (!strchr((char *)path,'/') && !strchr((char *)path,'\\')) |
|---|
| 624 |
return ((char *)path); |
|---|
| 625 |
|
|---|
| 626 |
while ((src--) != path) |
|---|
| 627 |
{ |
|---|
| 628 |
if (*src == '/' || *src == '\\') |
|---|
| 629 |
return (++src); |
|---|
| 630 |
} |
|---|
| 631 |
return (char *)""; |
|---|
| 632 |
} |
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
char *_pico_setfext( char *path, const char *ext ) |
|---|
| 640 |
{ |
|---|
| 641 |
char *src; |
|---|
| 642 |
int remfext = 0; |
|---|
| 643 |
|
|---|
| 644 |
src = path + (strlen(path) - 1); |
|---|
| 645 |
|
|---|
| 646 |
if (ext == NULL) ext = ""; |
|---|
| 647 |
if (strlen(ext ) < 1) remfext = 1; |
|---|
| 648 |
if (strlen(path) < 1) |
|---|
| 649 |
return path; |
|---|
| 650 |
|
|---|
| 651 |
while ((src--) != path) |
|---|
| 652 |
{ |
|---|
| 653 |
if (*src == '/' || *src == '\\') |
|---|
| 654 |
return path; |
|---|
| 655 |
|
|---|
| 656 |
if (*src == '.') |
|---|
| 657 |
{ |
|---|
| 658 |
if (remfext) |
|---|
| 659 |
{ |
|---|
| 660 |
*src = '\0'; |
|---|
| 661 |
return path; |
|---|
| 662 |
} |
|---|
| 663 |
*(++src) = '\0'; |
|---|
| 664 |
break; |
|---|
| 665 |
} |
|---|
| 666 |
} |
|---|
| 667 |
strcat(path,ext); |
|---|
| 668 |
return path; |
|---|
| 669 |
} |
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
int _pico_getline( char *buf, int bufsize, char *dest, int destsize ) |
|---|
| 678 |
{ |
|---|
| 679 |
int pos; |
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
if (dest == NULL || destsize < 1) return -1; |
|---|
| 683 |
memset( dest,0,destsize ); |
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
if (buf == NULL || bufsize < 1) |
|---|
| 687 |
return -1; |
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 |
for (pos=0; pos<bufsize && pos<destsize; pos++) |
|---|
| 691 |
{ |
|---|
| 692 |
if (buf[pos] == '\n') { pos++; break; } |
|---|
| 693 |
dest[pos] = buf[pos]; |
|---|
| 694 |
} |
|---|
| 695 |
|
|---|
| 696 |
dest[pos] = '\0'; |
|---|
| 697 |
return pos; |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
void _pico_parse_skip_white( picoParser_t *p, int *hasLFs ) |
|---|
| 706 |
{ |
|---|
| 707 |
|
|---|
| 708 |
if (p == NULL || p->cursor == NULL) |
|---|
| 709 |
return; |
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
while( 1 ) |
|---|
| 713 |
{ |
|---|
| 714 |
|
|---|
| 715 |
if (p->cursor < p->buffer || |
|---|
| 716 |
p->cursor >= p->max) |
|---|
| 717 |
{ |
|---|
| 718 |
return; |
|---|
| 719 |
} |
|---|
| 720 |
|
|---|
| 721 |
if (*p->cursor > 0x20) break; |
|---|
| 722 |
if (*p->cursor == 0x00) return; |
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
if (*p->cursor == '\n') |
|---|
| 726 |
{ |
|---|
| 727 |
*hasLFs = 1; |
|---|
| 728 |
p->curLine++; |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
p->cursor++; |
|---|
| 732 |
} |
|---|
| 733 |
} |
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
picoParser_t *_pico_new_parser( picoByte_t *buffer, int bufSize ) |
|---|
| 739 |
{ |
|---|
| 740 |
picoParser_t *p; |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
if( buffer == NULL || bufSize <= 0 ) |
|---|
| 744 |
return NULL; |
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
p = _pico_alloc( sizeof(picoParser_t) ); |
|---|
| 748 |
if (p == NULL) return NULL; |
|---|
| 749 |
memset( p,0,sizeof(picoParser_t) ); |
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
p->tokenSize = 0; |
|---|
| 753 |
p->tokenMax = 1024; |
|---|
| 754 |
p->token = _pico_alloc( p->tokenMax ); |
|---|
| 755 |
if( p->token == NULL ) |
|---|
| 756 |
{ |
|---|
| 757 |
_pico_free( p ); |
|---|
| 758 |
return NULL; |
|---|
| 759 |
} |
|---|
| 760 |
|
|---|
| 761 |
p->buffer = buffer; |
|---|
| 762 |
p->cursor = buffer; |
|---|
| 763 |
p->bufSize = bufSize; |
|---|
| 764 |
p->max = p->buffer + bufSize; |
|---|
| 765 |
p->curLine = 1; |
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
return p; |
|---|
| 769 |
} |
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
void _pico_free_parser( picoParser_t *p ) |
|---|
| 775 |
{ |
|---|
| 776 |
|
|---|
| 777 |
if (p == NULL) return; |
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
if (p->token != NULL) |
|---|
| 781 |
{ |
|---|
| 782 |
_pico_free( p->token ); |
|---|
| 783 |
} |
|---|
| 784 |
_pico_free( p ); |
|---|
| 785 |
} |
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
int _pico_parse_ex( picoParser_t *p, int allowLFs, int handleQuoted ) |
|---|
| 796 |
{ |
|---|
| 797 |
int hasLFs = 0; |
|---|
| 798 |
char *old; |
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
if( p == NULL || p->buffer == NULL || |
|---|
| 802 |
p->cursor < p->buffer || |
|---|
| 803 |
p->cursor >= p->max ) |
|---|
| 804 |
{ |
|---|
| 805 |
return 0; |
|---|
| 806 |
} |
|---|
| 807 |
|
|---|
| 808 |
p->tokenSize = 0; |
|---|
| 809 |
p->token[ 0 ] = '\0'; |
|---|
| 810 |
old = p->cursor; |
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
while( p->cursor < p->max && *p->cursor <= 32 ) |
|---|
| 814 |
{ |
|---|
| 815 |
if (*p->cursor == '\n') |
|---|
| 816 |
{ |
|---|
| 817 |
p->curLine++; |
|---|
| 818 |
hasLFs++; |
|---|
| 819 |
} |
|---|
| 820 |
p->cursor++; |
|---|
| 821 |
} |
|---|
| 822 |
|
|---|
| 823 |
if ((hasLFs > 0) && !allowLFs) |
|---|
| 824 |
{ |
|---|
| 825 |
p->cursor = old; |
|---|
| 826 |
return 0; |
|---|
| 827 |
} |
|---|
| 828 |
|
|---|
| 829 |
if (*p->cursor == '\"' && handleQuoted) |
|---|
| 830 |
{ |
|---|
| 831 |
p->cursor++; |
|---|
| 832 |
while (p->cursor < p->max && *p->cursor) |
|---|
| 833 |
{ |
|---|
| 834 |
if (*p->cursor == '\\') |
|---|
| 835 |
{ |
|---|
| 836 |
if (*(p->cursor+1) == '"') |
|---|
| 837 |
{ |
|---|
| 838 |
p->cursor++; |
|---|
| 839 |
} |
|---|
| 840 |
p->token[ p->tokenSize++ ] = *p->cursor++; |
|---|
| 841 |
continue; |
|---|
| 842 |
} |
|---|
| 843 |
else if (*p->cursor == '\"') |
|---|
| 844 |
{ |
|---|
| 845 |
p->cursor++; |
|---|
| 846 |
break; |
|---|
| 847 |
} |
|---|
| 848 |
else if (*p->cursor == '\n') |
|---|
| 849 |
{ |
|---|
| 850 |
p->curLine++; |
|---|
| 851 |
} |
|---|
| 852 |
p->token[ p->tokenSize++ ] = *p->cursor++; |
|---|
| 853 |
} |
|---|
| 854 |
|
|---|
| 855 |
p->token[ p->tokenSize ] = '\0'; |
|---|
| 856 |
return 1; |
|---|
| 857 |
} |
|---|
| 858 |
|
|---|
| 859 |
while( p->cursor < p->max && *p->cursor > 32 ) |
|---|
| 860 |
{ |
|---|
| 861 |
if (*p->cursor == '\n') |
|---|
| 862 |
{ |
|---|
| 863 |
p->curLine++; |
|---|
| 864 |
} |
|---|
| 865 |
p->token[ p->tokenSize++ ] = *p->cursor++; |
|---|
| 866 |
} |
|---|
| 867 |
|
|---|
| 868 |
p->token[ p->tokenSize ] = '\0'; |
|---|
| 869 |
return 1; |
|---|
| 870 |
} |
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
char *_pico_parse_first( picoParser_t *p ) |
|---|
| 877 |
{ |
|---|
| 878 |
|
|---|
| 879 |
if (p == NULL) return NULL; |
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
if (!_pico_parse_ex( p,1,1 )) |
|---|
| 883 |
return NULL; |
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
return p->token; |
|---|
| 887 |
} |
|---|
| 888 |
|
|---|
| 889 |
|
|---|
| 890 |
|
|---|
| 891 |
|
|---|
| 892 |
|
|---|
| 893 |
|
|---|
| 894 |
char *_pico_parse( picoParser_t *p, int allowLFs ) |
|---|
| 895 |
{ |
|---|
| 896 |
|
|---|
| 897 |
if (p == NULL) return NULL; |
|---|
| 898 |
|
|---|
| 899 |
|
|---|
| 900 |
if (!_pico_parse_ex( p,allowLFs,1 )) |
|---|
| 901 |
return NULL; |
|---|
| 902 |
|
|---|
| 903 |
|
|---|
| 904 |
return p->token; |
|---|
| 905 |
} |
|---|
| 906 |
|
|---|
| 907 |
|
|---|
| 908 |
|
|---|
| 909 |
|
|---|
| 910 |
void _pico_parse_skip_rest( picoParser_t *p ) |
|---|
| 911 |
{ |
|---|
| 912 |
while( _pico_parse_ex( p,0,0 ) ) ; |
|---|
| 913 |
} |
|---|
| 914 |
|
|---|
| 915 |
|
|---|
| 916 |
|
|---|
| 917 |
|
|---|
| 918 |
|
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 |
int _pico_parse_skip_braced( picoParser_t *p ) |
|---|
| 922 |
{ |
|---|
| 923 |
int firstToken = 1; |
|---|
| 924 |
int level; |
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
if (p == NULL) return 0; |
|---|
| 928 |
|
|---|
| 929 |
|
|---|
| 930 |
level = 0; |
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 |
while( 1 ) |
|---|
| 934 |
{ |
|---|
| 935 |
|
|---|
| 936 |
if (!_pico_parse_ex( p,1,1 )) |
|---|
| 937 |
{ |
|---|
| 938 |
|
|---|
| 939 |
return 0; |
|---|
| 940 |
} |
|---|
| 941 |
|
|---|
| 942 |
if (firstToken && p->token[0] != '{') |
|---|
| 943 |
{ |
|---|
| 944 |
|
|---|
| 945 |
return 0; |
|---|
| 946 |
} |
|---|
| 947 |
|
|---|
| 948 |
firstToken = 0; |
|---|
| 949 |
|
|---|
| 950 |
|
|---|
| 951 |
if (p->token[1] == '\0') |
|---|
| 952 |
{ |
|---|
| 953 |
if (p->token[0] == '{') level++; |
|---|
| 954 |
if (p->token[0] == '}') level--; |
|---|
| 955 |
} |
|---|
| 956 |
|
|---|
| 957 |
if (level == 0) break; |
|---|
| 958 |
} |
|---|
| 959 |
|
|---|
| 960 |
return 1; |
|---|
| 961 |
} |
|---|
| 962 |
|
|---|
| 963 |
int _pico_parse_check( picoParser_t *p, int allowLFs, char *str ) |
|---|
| 964 |
{ |
|---|
| 965 |
if (!_pico_parse_ex( p,allowLFs,1 )) |
|---|
| 966 |
return 0; |
|---|
| 967 |
if (!strcmp(p->token,str)) |
|---|
| 968 |
return 1; |
|---|
| 969 |
return 0; |
|---|
| 970 |
} |
|---|
| 971 |
|
|---|
| 972 |
int _pico_parse_checki( picoParser_t *p, int allowLFs, char *str ) |
|---|
| 973 |
{ |
|---|
| 974 |
if (!_pico_parse_ex( p,allowLFs,1 )) |
|---|
| 975 |
return 0; |
|---|
| 976 |
if (!_pico_stricmp(p->token,str)) |
|---|
| 977 |
return 1; |
|---|
| 978 |
return 0; |
|---|
| 979 |
} |
|---|
| 980 |
|
|---|
| 981 |
int _pico_parse_int( picoParser_t *p, int *out ) |
|---|
| 982 |
{ |
|---|
| 983 |
char *token; |
|---|
| 984 |
|
|---|
| 985 |
|
|---|
| 986 |
if (p == NULL || out == NULL) |
|---|
| 987 |
return 0; |
|---|
| 988 |
|
|---|
| 989 |
|
|---|
| 990 |
*out = 0; |
|---|
| 991 |
token = _pico_parse( p,0 ); |
|---|
| 992 |
if (token == NULL) return 0; |
|---|
| 993 |
*out = atoi( token ); |
|---|
| 994 |
|
|---|
| 995 |
|
|---|
| 996 |
return 1; |
|---|
| 997 |
} |
|---|
| 998 |
|
|---|
| 999 |
int _pico_parse_int_def( picoParser_t *p, int *out, int def ) |
|---|
| 1000 |
{ |
|---|
| 1001 |
char *token; |
|---|
| 1002 |
|
|---|
| 1003 |
|
|---|
| 1004 |
if (p == NULL || out == NULL) |
|---|
| 1005 |
return 0; |
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 |
*out = def; |
|---|
| 1009 |
token = _pico_parse( p,0 ); |
|---|
| 1010 |
if (token == NULL) return 0; |
|---|
| 1011 |
*out = atoi( token ); |
|---|
| 1012 |
|
|---|
| 1013 |
|
|---|
| 1014 |
return 1; |
|---|
| 1015 |
} |
|---|
| 1016 |
|
|---|
| 1017 |
int _pico_parse_float( picoParser_t *p, float *out ) |
|---|
| 1018 |
{ |
|---|
| 1019 |
char *token; |
|---|
| 1020 |
|
|---|
| 1021 |
|
|---|
| 1022 |
if (p == NULL || out == NULL) |
|---|
| 1023 |
return 0; |
|---|
| 1024 |
|
|---|
| 1025 |
|
|---|
| 1026 |
*out = 0.0f; |
|---|
| 1027 |
token = _pico_parse( p,0 ); |
|---|
| 1028 |
if (token == NULL) return 0; |
|---|
| 1029 |
*out = (float) atof( token ); |
|---|
| 1030 |
|
|---|
| 1031 |
|
|---|
| 1032 |
return 1; |
|---|
| 1033 |
} |
|---|
| 1034 |
|
|---|
| 1035 |
int _pico_parse_float_def( picoParser_t *p, float *out, float def ) |
|---|
| 1036 |
{ |
|---|
| 1037 |
char *token; |
|---|
| 1038 |
|
|---|
| 1039 |
|
|---|
| 1040 |
if (p == NULL || out == NULL) |
|---|
| 1041 |
return 0; |
|---|
| 1042 |
|
|---|
| 1043 |
|
|---|
| 1044 |
*out = def; |
|---|
| 1045 |
token = _pico_parse( p,0 ); |
|---|
| 1046 |
if (token == NULL) return 0; |
|---|
| 1047 |
*out = (float) atof( token ); |
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
return 1; |
|---|
| 1051 |
} |
|---|
| 1052 |
|
|---|
| 1053 |
int _pico_parse_vec( picoParser_t *p, picoVec3_t out ) |
|---|
| 1054 |
{ |
|---|
| 1055 |
char *token; |
|---|
| 1056 |
int i; |
|---|
| 1057 |
|
|---|
| 1058 |
|
|---|
| 1059 |
if (p == NULL || out == NULL) |
|---|
| 1060 |
return 0; |
|---|
| 1061 |
|
|---|
| 1062 |
|
|---|
| 1063 |
_pico_zero_vec( out ); |
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
for (i=0; i<3; i++) |
|---|
| 1067 |
{ |
|---|
| 1068 |
token = _pico_parse( p,0 ); |
|---|
| 1069 |
if (token == NULL) |
|---|
| 1070 |
{ |
|---|
| 1071 |
_pico_zero_vec( out ); |
|---|
| 1072 |
return 0; |
|---|
| 1073 |
} |
|---|
| 1074 |
out[ i ] = (float) atof( token ); |
|---|
| 1075 |
} |
|---|
| 1076 |
|
|---|
| 1077 |
return 1; |
|---|
| 1078 |
} |
|---|
| 1079 |
|
|---|
| 1080 |
int _pico_parse_vec_def( picoParser_t *p, picoVec3_t out, picoVec3_t def ) |
|---|
| 1081 |
{ |
|---|
| 1082 |
char *token; |
|---|
| 1083 |
int i; |
|---|
| 1084 |
|
|---|
| 1085 |
|
|---|
| 1086 |
if (p == NULL || out == NULL) |
|---|
| 1087 |
return 0; |
|---|
| 1088 |
|
|---|
| 1089 |
|
|---|
| 1090 |
_pico_copy_vec( def,out ); |
|---|
| 1091 |
|
|---|
| 1092 |
|
|---|
| 1093 |
for (i=0; i<3; i++) |
|---|
| 1094 |
{ |
|---|
| 1095 |
token = _pico_parse( p,0 ); |
|---|
| 1096 |
if (token == NULL) |
|---|
| 1097 |
{ |
|---|
| 1098 |
_pico_copy_vec( def,out ); |
|---|
| 1099 |
return 0; |
|---|
| 1100 |
} |
|---|
| 1101 |
out[ i ] = (float) atof( token ); |
|---|
| 1102 |
} |
|---|
| 1103 |
|
|---|
| 1104 |
return 1; |
|---|
| 1105 |
} |
|---|
| 1106 |
|
|---|
| 1107 |
int _pico_parse_vec2( picoParser_t *p, picoVec2_t out ) |
|---|
| 1108 |
{ |
|---|
| 1109 |
char *token; |
|---|
| 1110 |
int i; |
|---|
| 1111 |
|
|---|
| 1112 |
|
|---|
| 1113 |
if (p == NULL || out == NULL) |
|---|
| 1114 |
return 0; |
|---|
| 1115 |
|
|---|
| 1116 |
|
|---|
| 1117 |
_pico_zero_vec2( out ); |
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 |
for (i=0; i<2; i++) |
|---|
| 1121 |
{ |
|---|
| 1122 |
token = _pico_parse( p,0 ); |
|---|
| 1123 |
if (token == NULL) |
|---|
| 1124 |
{ |
|---|
| 1125 |
_pico_zero_vec2( out ); |
|---|
| 1126 |
return 0; |
|---|
| 1127 |
} |
|---|
| 1128 |
out[ i ] = (float) atof( token ); |
|---|
| 1129 |
} |
|---|
| 1130 |
|
|---|
| 1131 |
return 1; |
|---|
| 1132 |
} |
|---|
| 1133 |
|
|---|
| 1134 |
int _pico_parse_vec2_def( picoParser_t *p, picoVec2_t out, picoVec2_t def ) |
|---|
| 1135 |
{ |
|---|
| 1136 |
char *token; |
|---|
| 1137 |
int i; |
|---|
| 1138 |
|
|---|
| 1139 |
|
|---|
| 1140 |
if (p == NULL || out == NULL) |
|---|
| 1141 |
return 0; |
|---|
| 1142 |
|
|---|
| 1143 |
|
|---|
| 1144 |
_pico_copy_vec2( def,out ); |
|---|
| 1145 |
|
|---|
| 1146 |
|
|---|
| 1147 |
for (i=0; i<2; i++) |
|---|
| 1148 |
{ |
|---|
| 1149 |
token = _pico_parse( p,0 ); |
|---|
| 1150 |
if (token == NULL) |
|---|
| 1151 |
{ |
|---|
| 1152 |
_pico_copy_vec2( def,out ); |
|---|
| 1153 |
return 0; |
|---|
| 1154 |
} |
|---|
| 1155 |
out[ i ] = (float) atof( token ); |
|---|
| 1156 |
} |
|---|
| 1157 |
|
|---|
| 1158 |
return 1; |
|---|
| 1159 |
} |
|---|
| 1160 |
|
|---|
| 1161 |
int _pico_parse_vec4( picoParser_t *p, picoVec4_t out ) |
|---|
| 1162 |
{ |
|---|
| 1163 |
char *token; |
|---|
| 1164 |
int i; |
|---|
| 1165 |
|
|---|
| 1166 |
|
|---|
| 1167 |
if (p == NULL || out == NULL) |
|---|
| 1168 |
return 0; |
|---|
| 1169 |
|
|---|
| 1170 |
|
|---|
| 1171 |
_pico_zero_vec4( out ); |
|---|
| 1172 |
|
|---|
| 1173 |
|
|---|
| 1174 |
for (i=0; i<4; i++) |
|---|
| 1175 |
{ |
|---|
| 1176 |
token = _pico_parse( p,0 ); |
|---|
| 1177 |
if (token == NULL) |
|---|
| 1178 |
{ |
|---|
| 1179 |
_pico_zero_vec4( out ); |
|---|
| 1180 |
return 0; |
|---|
| 1181 |
} |
|---|
| 1182 |
out[ i ] = (float) atof( token ); |
|---|
| 1183 |
} |
|---|
| 1184 |
|
|---|
| 1185 |
return 1; |
|---|
| 1186 |
} |
|---|
| 1187 |
|
|---|
| 1188 |
int _pico_parse_vec4_def( picoParser_t *p, picoVec4_t out, picoVec4_t def ) |
|---|
| 1189 |
{ |
|---|
| 1190 |
char *token; |
|---|
| 1191 |
int i; |
|---|
| 1192 |
|
|---|
| 1193 |
|
|---|
| 1194 |
if (p == NULL || out == NULL) |
|---|
| 1195 |
return 0; |
|---|
| 1196 |
|
|---|
| 1197 |
|
|---|
| 1198 |
_pico_copy_vec4( def,out ); |
|---|
| 1199 |
|
|---|
| 1200 |
|
|---|
| 1201 |
for (i=0; i<4; i++) |
|---|
| 1202 |
{ |
|---|
| 1203 |
token = _pico_parse( p,0 ); |
|---|
| 1204 |
if (token == NULL) |
|---|
| 1205 |
{ |
|---|
| 1206 |
_pico_copy_vec4( def,out ); |
|---|
| 1207 |
return 0; |
|---|
| 1208 |
} |
|---|
| 1209 |
out[ i ] = (float) atof( token ); |
|---|
| 1210 |
} |
|---|
| 1211 |
|
|---|
| 1212 |
return 1; |
|---|
| 1213 |
} |
|---|
| 1214 |
|
|---|
| 1215 |
|
|---|
| 1216 |
|
|---|
| 1217 |
|
|---|
| 1218 |
picoMemStream_t *_pico_new_memstream( picoByte_t *buffer, int bufSize ) |
|---|
| 1219 |
{ |
|---|
| 1220 |
picoMemStream_t *s; |
|---|
| 1221 |
|
|---|
| 1222 |
|
|---|
| 1223 |
if( buffer == NULL || bufSize <= 0 ) |
|---|
| 1224 |
return NULL; |
|---|
| 1225 |
|
|---|
| 1226 |
|
|---|
| 1227 |
s = _pico_alloc( sizeof(picoMemStream_t) ); |
|---|
| 1228 |
if (s == NULL) return NULL; |
|---|
| 1229 |
memset( s,0,sizeof(picoMemStream_t) ); |
|---|
| 1230 |
|
|---|
| 1231 |
|
|---|
| 1232 |
s->buffer = buffer; |
|---|
| 1233 |
s->curPos = buffer; |
|---|
| 1234 |
s->bufSize = bufSize; |
|---|
| 1235 |
s->flag = 0; |
|---|
| 1236 |
|
|---|
| 1237 |
|
|---|
| 1238 |
return s; |
|---|
| 1239 |
} |
|---|
| 1240 |
|
|---|
| 1241 |
|
|---|
| 1242 |
|
|---|
| 1243 |
|
|---|
| 1244 |
void _pico_free_memstream( picoMemStream_t *s ) |
|---|
| 1245 |
{ |
|---|
| 1246 |
|
|---|
| 1247 |
if (s == NULL) return; |
|---|
| 1248 |
|
|---|
| 1249 |
|
|---|
| 1250 |
_pico_free( s ); |
|---|
| 1251 |
} |
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
|
|---|
| 1255 |
|
|---|
| 1256 |
int _pico_memstream_read( picoMemStream_t *s, void *buffer, int len ) |
|---|
| 1257 |
{ |
|---|
| 1258 |
int ret = 1; |
|---|
| 1259 |
|
|---|
| 1260 |
|
|---|
| 1261 |
if (s == NULL || buffer == NULL) |
|---|
| 1262 |
return 0; |
|---|
| 1263 |
|
|---|
| 1264 |
if (s->curPos + len > s->buffer + s->bufSize) |
|---|
| 1265 |
{ |
|---|
| 1266 |
s->flag |= PICO_IOEOF; |
|---|
| 1267 |
len = s->buffer + s->bufSize - s->curPos; |
|---|
| 1268 |
ret = 0; |
|---|
| 1269 |
} |
|---|
| 1270 |
|
|---|
| 1271 |
|
|---|
| 1272 |
memcpy( buffer, s->curPos, len ); |
|---|
| 1273 |
s->curPos += len; |
|---|
| 1274 |
return ret; |
|---|
| 1275 |
} |
|---|
| 1276 |
|
|---|
| 1277 |
|
|---|
| 1278 |
|
|---|
| 1279 |
|
|---|
| 1280 |
int _pico_memstream_getc( picoMemStream_t *s ) |
|---|
| 1281 |
{ |
|---|
| 1282 |
int c = 0; |
|---|
| 1283 |
|
|---|
| 1284 |
|
|---|
| 1285 |
if (s == NULL) |
|---|
| 1286 |
return -1; |
|---|
| 1287 |
|
|---|
| 1288 |
|
|---|
| 1289 |
if (_pico_memstream_read( s, &c, 1) == 0) |
|---|
| 1290 |
return -1; |
|---|
| 1291 |
|
|---|
| 1292 |
return c; |
|---|
| 1293 |
} |
|---|
| 1294 |
|
|---|
| 1295 |
|
|---|
| 1296 |
|
|---|
| 1297 |
|
|---|
| 1298 |
int _pico_memstream_seek( picoMemStream_t *s, long offset, int origin ) |
|---|
| 1299 |
{ |
|---|
| 1300 |
int overflow; |
|---|
| 1301 |
|
|---|
| 1302 |
|
|---|
| 1303 |
if (s == NULL) |
|---|
| 1304 |
return -1; |
|---|
| 1305 |
|
|---|
| 1306 |
if (origin == PICO_SEEK_SET) |
|---|
| 1307 |
{ |
|---|
| 1308 |
s->curPos = s->buffer + offset; |
|---|
| 1309 |
overflow = s->curPos - ( s->buffer + s->bufSize ); |
|---|
| 1310 |
if (overflow > 0) |
|---|
| 1311 |
{ |
|---|
| 1312 |
s->curPos = s->buffer + s->bufSize; |
|---|
| 1313 |
return offset - overflow; |
|---|
| 1314 |
} |
|---|
| 1315 |
return 0; |
|---|
| 1316 |
} |
|---|
| 1317 |
else if (origin == PICO_SEEK_CUR) |
|---|
| 1318 |
{ |
|---|
| 1319 |
s->curPos += offset; |
|---|
| 1320 |
overflow = s->curPos - ( s->buffer + s->bufSize ); |
|---|
| 1321 |
if (overflow > 0) |
|---|
| 1322 |
{ |
|---|
| 1323 |
s->curPos = s->buffer + s->bufSize; |
|---|
| 1324 |
return offset - overflow; |
|---|
| 1325 |
} |
|---|
| 1326 |
return 0; |
|---|
| 1327 |
} |
|---|
| 1328 |
else if (origin == PICO_SEEK_END) |
|---|
| 1329 |
{ |
|---|
| 1330 |
s->curPos = ( s->buffer + s->bufSize ) - offset; |
|---|
| 1331 |
overflow = s->buffer - s->curPos; |
|---|
| 1332 |
if (overflow > 0) |
|---|
| 1333 |
{ |
|---|
| 1334 |
s->curPos = s->buffer; |
|---|
| 1335 |
return offset - overflow; |
|---|
| 1336 |
} |
|---|
| 1337 |
return 0; |
|---|
| 1338 |
} |
|---|
| 1339 |
|
|---|
| 1340 |
return -1; |
|---|
| 1341 |
} |
|---|
| 1342 |
|
|---|
| 1343 |
|
|---|
| 1344 |
|
|---|
| 1345 |
|
|---|
| 1346 |
long _pico_memstream_tell( picoMemStream_t *s ) |
|---|
| 1347 |
{ |
|---|
| 1348 |
|
|---|
| 1349 |
if (s == NULL) |
|---|
| 1350 |
return -1; |
|---|
| 1351 |
|
|---|
| 1352 |
return s->curPos - s->buffer; |
|---|
| 1353 |
} |
|---|