1 /* -----------------------------------------------------------------------------
5 Copyright (c) 2002, Randy Reddig & seaw0lf
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
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.
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.
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.
33 ----------------------------------------------------------------------------- */
36 * - fix p->curLine for parser routines. increased twice
41 #include "picointernal.h"
42 #include "globaldefs.h"
46 /* function pointers */
47 void *( *_pico_ptr_malloc )( size_t ) = malloc;
48 void ( *_pico_ptr_free )( void* ) = free;
49 void ( *_pico_ptr_load_file )( const char*, unsigned char**, int* ) = NULL;
50 void ( *_pico_ptr_free_file )( void* ) = NULL;
51 void ( *_pico_ptr_print )( int, const char* ) = NULL;
61 * kludged memory allocation wrapper
63 void *_pico_alloc( size_t size ){
66 /* some sanity checks */
70 if ( _pico_ptr_malloc == NULL ) {
75 ptr = _pico_ptr_malloc( size );
80 /* zero out allocated memory */
83 /* return pointer to allocated memory */
88 * _pico_calloc wrapper
90 void *_pico_calloc( size_t num, size_t size ){
93 /* some sanity checks */
94 if ( num == 0 || size == 0 ) {
97 if ( _pico_ptr_malloc == NULL ) {
101 /* allocate memory */
102 ptr = _pico_ptr_malloc( num * size );
107 /* zero out allocated memory */
108 memset( ptr,0,num * size );
110 /* return pointer to allocated memory */
115 * memory reallocation wrapper (note: only grows,
116 * but never shrinks or frees)
118 void *_pico_realloc( void **ptr, size_t oldSize, size_t newSize ){
125 if ( newSize < oldSize ) {
128 if ( _pico_ptr_malloc == NULL ) {
132 /* allocate new pointer */
133 ptr2 = _pico_alloc( newSize );
134 if ( ptr2 == NULL ) {
139 if ( *ptr != NULL ) {
140 memcpy( ptr2, *ptr, oldSize );
144 /* fix up and return */
149 /* _pico_clone_alloc:
150 * handy function for quick string allocation/copy. it clones
151 * the given string and returns a pointer to the new allocated
152 * clone (which must be freed by caller of course) or returns
153 * NULL on memory alloc or param errors. if 'size' is -1 the
154 * length of the input string is used, otherwise 'size' is used
155 * as custom clone size (the string is cropped to fit into mem
158 char *_pico_clone_alloc( const char *str ){
166 /* allocate memory */
167 cloned = _pico_alloc( strlen( str ) + 1 );
168 if ( cloned == NULL ) {
172 /* copy input string to cloned string */
173 strcpy( cloned, str );
175 /* return ptr to cloned string */
180 * wrapper around the free function pointer
182 void _pico_free( void *ptr ){
187 if ( _pico_ptr_free == NULL ) {
191 /* free the allocated memory */
192 _pico_ptr_free( ptr );
196 * wrapper around the loadfile function pointer
198 void _pico_load_file( const char *name, unsigned char **buffer, int *bufSize ){
200 if ( name == NULL ) {
204 if ( _pico_ptr_load_file == NULL ) {
208 /* do the actual call to read in the file; */
209 /* BUFFER IS ALLOCATED BY THE EXTERNAL LOADFILE FUNC */
210 _pico_ptr_load_file( name,buffer,bufSize );
214 * wrapper around the file free function pointer
216 void _pico_free_file( void *buffer ){
218 if ( buffer == NULL ) {
222 /* use default free */
223 if ( _pico_ptr_free_file == NULL ) {
227 /* free the allocated file */
228 _pico_ptr_free_file( buffer );
232 * wrapper around the print function pointer -sea
234 void _pico_printf( int level, const char *format, ... ){
239 if ( format == NULL ) {
242 if ( _pico_ptr_print == NULL ) {
247 va_start( argptr,format );
248 vsprintf( str,format,argptr );
251 /* remove linefeeds */
252 if ( str[ strlen( str ) - 1 ] == '\n' ) {
253 str[ strlen( str ) - 1 ] = '\0';
256 /* do the actual call */
257 _pico_ptr_print( level,str );
260 /* _pico_first_token:
261 * trims everything after the first whitespace-delimited token
264 void _pico_first_token( char *str ){
265 if ( !str || !*str ) {
268 while ( *str && !isspace( *str ) )
274 * left trims the given string -sea
276 char *_pico_strltrim( char *str ){
277 char *str1 = str, *str2 = str;
279 while ( isspace( *str2 ) ) str2++;
281 while ( *str2 != '\0' ) /* fix: ydnar */
288 * right trims the given string -sea
290 char *_pico_strrtrim( char *str ){
297 if ( allspace && !isspace( *str1 ) ) {
307 while ( ( isspace( *str1 ) ) && ( str1 >= str ) )
315 * pico internal string-to-lower routine.
317 char *_pico_strlwr( char *str ){
319 for ( cp = str; *cp; ++cp )
321 if ( 'A' <= *cp && *cp <= 'Z' ) {
322 *cp += ( 'a' - 'A' );
329 * counts how often the given char appears in str. -sea
331 int _pico_strchcount( char *str, int ch ){
333 while ( *str++ ) if ( *str == ch ) {
339 void _pico_zero_bounds( picoVec3_t mins, picoVec3_t maxs ){
341 for ( i = 0; i < 3; i++ )
348 void _pico_expand_bounds( picoVec3_t p, picoVec3_t mins, picoVec3_t maxs ){
350 for ( i = 0; i < 3; i++ )
353 if ( value < mins[i] ) {
356 if ( value > maxs[i] ) {
362 void _pico_zero_vec( picoVec3_t vec ){
363 vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = 0;
366 void _pico_zero_vec2( picoVec2_t vec ){
367 vec[ 0 ] = vec[ 1 ] = 0;
370 void _pico_zero_vec4( picoVec4_t vec ){
371 vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = vec[ 3 ] = 0;
374 void _pico_set_vec( picoVec3_t v, float a, float b, float c ){
380 void _pico_set_vec4( picoVec4_t v, float a, float b, float c, float d ){
387 void _pico_copy_vec( picoVec3_t src, picoVec3_t dest ){
388 dest[ 0 ] = src[ 0 ];
389 dest[ 1 ] = src[ 1 ];
390 dest[ 2 ] = src[ 2 ];
393 void _pico_copy_vec2( picoVec2_t src, picoVec2_t dest ){
394 dest[ 0 ] = src[ 0 ];
395 dest[ 1 ] = src[ 1 ];
398 void _pico_copy_vec4( picoVec4_t src, picoVec4_t dest ){
399 dest[ 0 ] = src[ 0 ];
400 dest[ 1 ] = src[ 1 ];
401 dest[ 2 ] = src[ 2 ];
402 dest[ 3 ] = src[ 3 ];
406 picoVec_t _pico_normalize_vec( picoVec3_t vec ){
409 len = sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] + vec[ 2 ] * vec[ 2 ] );
414 vec[ 0 ] *= (picoVec_t) ilen;
415 vec[ 1 ] *= (picoVec_t) ilen;
416 vec[ 2 ] *= (picoVec_t) ilen;
417 return (picoVec_t) len;
420 void _pico_add_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ){
421 dest[ 0 ] = a[ 0 ] + b[ 0 ];
422 dest[ 1 ] = a[ 1 ] + b[ 1 ];
423 dest[ 2 ] = a[ 2 ] + b[ 2 ];
426 void _pico_subtract_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ){
427 dest[ 0 ] = a[ 0 ] - b[ 0 ];
428 dest[ 1 ] = a[ 1 ] - b[ 1 ];
429 dest[ 2 ] = a[ 2 ] - b[ 2 ];
432 void _pico_scale_vec( picoVec3_t v, float scale, picoVec3_t dest ){
433 dest[ 0 ] = v[ 0 ] * scale;
434 dest[ 1 ] = v[ 1 ] * scale;
435 dest[ 2 ] = v[ 2 ] * scale;
438 void _pico_scale_vec4( picoVec4_t v, float scale, picoVec4_t dest ){
439 dest[ 0 ] = v[ 0 ] * scale;
440 dest[ 1 ] = v[ 1 ] * scale;
441 dest[ 2 ] = v[ 2 ] * scale;
442 dest[ 3 ] = v[ 3 ] * scale;
445 picoVec_t _pico_dot_vec( picoVec3_t a, picoVec3_t b ){
446 return a[ 0 ] * b[ 0 ] + a[ 1 ] * b[ 1 ] + a[ 2 ] * b[ 2 ];
449 void _pico_cross_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ){
450 dest[ 0 ] = a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ];
451 dest[ 1 ] = a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ];
452 dest[ 2 ] = a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ];
455 picoVec_t _pico_calc_plane( picoVec4_t plane, picoVec3_t a, picoVec3_t b, picoVec3_t c ){
458 _pico_subtract_vec( b, a, ba );
459 _pico_subtract_vec( c, a, ca );
460 _pico_cross_vec( ca, ba, plane );
461 plane[ 3 ] = _pico_dot_vec( a, plane );
462 return _pico_normalize_vec( plane );
465 const picoColor_t picoColor_white = { 255, 255, 255, 255 };
467 /* separate from _pico_set_vec4 */
468 void _pico_set_color( picoColor_t c, int r, int g, int b, int a ){
475 void _pico_copy_color( const picoColor_t src, picoColor_t dest ){
476 dest[ 0 ] = src[ 0 ];
477 dest[ 1 ] = src[ 1 ];
478 dest[ 2 ] = src[ 2 ];
479 dest[ 3 ] = src[ 3 ];
482 #if GDEF_ARCH_ENDIAN_BIG
484 int _pico_big_long( int src ) { return src; }
485 short _pico_big_short( short src ) { return src; }
486 float _pico_big_float( float src ) { return src; }
488 int _pico_little_long( int src ){
489 return ( ( src & 0xFF000000 ) >> 24 ) |
490 ( ( src & 0x00FF0000 ) >> 8 ) |
491 ( ( src & 0x0000FF00 ) << 8 ) |
492 ( ( src & 0x000000FF ) << 24 );
495 short _pico_little_short( short src ){
496 return ( ( src & 0xFF00 ) >> 8 ) |
497 ( ( src & 0x00FF ) << 8 );
500 float _pico_little_float( float src ){
501 floatSwapUnion in,out;
503 out.c[ 0 ] = in.c[ 3 ];
504 out.c[ 1 ] = in.c[ 2 ];
505 out.c[ 2 ] = in.c[ 1 ];
506 out.c[ 3 ] = in.c[ 0 ];
509 #else /*__BIG_ENDIAN__*/
511 int _pico_little_long( int src ) { return src; }
512 short _pico_little_short( short src ) { return src; }
513 float _pico_little_float( float src ) { return src; }
515 int _pico_big_long( int src ){
516 return ( ( src & 0xFF000000 ) >> 24 ) |
517 ( ( src & 0x00FF0000 ) >> 8 ) |
518 ( ( src & 0x0000FF00 ) << 8 ) |
519 ( ( src & 0x000000FF ) << 24 );
522 short _pico_big_short( short src ){
523 return ( ( src & 0xFF00 ) >> 8 ) |
524 ( ( src & 0x00FF ) << 8 );
527 float _pico_big_float( float src ){
528 floatSwapUnion in,out;
530 out.c[ 0 ] = in.c[ 3 ];
531 out.c[ 1 ] = in.c[ 2 ];
532 out.c[ 2 ] = in.c[ 1 ];
533 out.c[ 3 ] = in.c[ 0 ];
536 #endif /*__BIG_ENDIAN__*/
539 * case-insensitive strstr. -sea
541 const char *_pico_stristr( const char *str, const char *substr ){
542 const size_t sublen = strlen( substr );
545 if ( !_pico_strnicmp( str,substr,sublen ) ) {
558 changes dos \ style path separators to /
561 void _pico_unixify( char *path ){
562 if ( path == NULL ) {
567 if ( *path == '\\' ) {
575 * removes file name portion from given file path and converts
576 * the directory separators to un*x style. returns 1 on success
577 * or 0 when 'destSize' was exceeded. -sea
579 int _pico_nofname( const char *path, char *dest, int destSize ){
583 while ( ( *dest = *path ) != '\0' )
585 if ( *dest == '/' || *dest == '\\' ) {
601 * returns ptr to filename portion in given path or an empty
602 * string otherwise. given 'path' is not altered. -sea
604 const char *_pico_nopath( const char *path ){
606 src = path + ( strlen( path ) - 1 );
608 if ( path == NULL ) {
611 if ( !strchr( path,'/' ) && !strchr( path,'\\' ) ) {
615 while ( ( src-- ) != path )
617 if ( *src == '/' || *src == '\\' ) {
625 * sets/changes the file extension for the given filename
626 * or filepath's filename portion. the given 'path' *is*
627 * altered. leave 'ext' empty to remove extension. -sea
629 char *_pico_setfext( char *path, const char *ext ){
633 src = path + ( strlen( path ) - 1 );
638 if ( strlen( ext ) < 1 ) {
641 if ( strlen( path ) < 1 ) {
645 while ( ( src-- ) != path )
647 if ( *src == '/' || *src == '\\' ) {
665 * extracts one line from the given buffer and stores it in dest.
666 * returns -1 on error or the length of the line on success. i've
667 * removed string trimming here. this can be done manually by the
670 int _pico_getline( char *buf, int bufsize, char *dest, int destsize ){
674 if ( dest == NULL || destsize < 1 ) {
677 memset( dest,0,destsize );
680 if ( buf == NULL || bufsize < 1 ) {
685 for ( pos = 0; pos < bufsize && pos < destsize; pos++ )
687 if ( buf[pos] == '\n' ) {
690 dest[pos] = buf[pos];
692 /* terminate dest and return */
697 /* expecting fileName to be relative vfs model path */
698 void _pico_deduce_shadername( const char* fileName, const char* srcName, picoShader_t* shader ){
699 if( srcName == NULL || fileName == NULL )
701 char name[strlen( srcName ) + 1];
702 strcpy( name, srcName );
703 _pico_unixify( name );
704 _pico_setfext( name, NULL );
706 char path[strlen( fileName ) + strlen( name ) + 1];
707 _pico_nofname( fileName, path, strlen( fileName ) + strlen( name ) + 1 );
708 _pico_unixify( path );
710 if( !strchr( name , '/' ) ){ /* texture is likely in the folder, where model is */
711 strcat( path, name );
713 else if( name[0] == '/' || ( name[0] != '\0' && name[1] == ':' ) || strstr( name, ".." ) ){ /* absolute path or with .. */
714 const char* p = name;
715 for (; *p != '\0'; ++p )
716 if ( _pico_strnicmp( p, "/models/", 8 ) == 0 || _pico_strnicmp( p, "/textures/", 10 ) == 0 )
719 strcpy( path, p + 1 );
722 p = _pico_nopath( name );
727 PicoSetShaderName( shader, name );
731 _pico_printf( PICO_NORMAL, "PICO: substituting shader name: %s -> %s", srcName, path );
732 PicoSetShaderName( shader, path );
735 /* deduce shadernames from bitmap or shadername paths */
736 void _pico_deduce_shadernames( picoModel_t *model ){
737 for ( int i = 0; i < model->numShaders; ++i ){
738 /* skip null shaders */
739 if ( model->shader[i] == NULL )
742 const char* mapname = model->shader[i]->mapName;
743 const char* shadername = model->shader[i]->name;
744 if( mapname && *mapname )
745 _pico_deduce_shadername( model->fileName, mapname, model->shader[i] );
746 else if( shadername && *shadername )
747 _pico_deduce_shadername( model->fileName, shadername, model->shader[i] );
751 /* _pico_parse_skip_white:
752 * skips white spaces in current pico parser, sets *hasLFs
753 * to 1 if linefeeds were skipped, and either returns the
754 * parser's cursor pointer or NULL on error. -sea
756 void _pico_parse_skip_white( picoParser_t *p, int *hasLFs ){
758 if ( p == NULL || p->cursor == NULL ) {
762 /* skin white spaces */
766 if ( p->cursor < p->buffer ||
767 p->cursor >= p->max ) {
770 /* break for chars other than white spaces */
771 if ( *p->cursor > 0x20 ) {
774 if ( *p->cursor == 0x00 ) {
778 /* a bit of linefeed handling */
779 if ( *p->cursor == '\n' ) {
783 /* go to next character */
789 * allocates a new ascii parser object.
791 picoParser_t *_pico_new_parser( const picoByte_t *buffer, int bufSize ){
795 if ( buffer == NULL || bufSize <= 0 ) {
799 /* allocate reader */
800 p = _pico_alloc( sizeof( picoParser_t ) );
804 memset( p,0,sizeof( picoParser_t ) );
806 /* allocate token space */
809 p->token = _pico_alloc( p->tokenMax );
810 if ( p->token == NULL ) {
815 p->buffer = (const char *) buffer;
816 p->cursor = p->buffer;
817 p->bufSize = bufSize;
818 p->max = p->buffer + bufSize;
819 p->curLine = 1; /* sea: new */
821 /* return ptr to parser */
825 /* _pico_free_parser:
826 * frees an existing pico parser object.
828 void _pico_free_parser( picoParser_t *p ){
834 /* free the parser */
835 if ( p->token != NULL ) {
836 _pico_free( p->token );
842 * reads the next token from given pico parser object. if param
843 * 'allowLFs' is 1 it will read beyond linefeeds and return 0 when
844 * the EOF is reached. if 'allowLFs' is 0 it will return 0 when
845 * the EOL is reached. if 'handleQuoted' is 1 the parser function
846 * will handle "quoted" strings and return the data between the
847 * quotes as token. returns 0 on end/error or 1 on success. -sea
849 int _pico_parse_ex( picoParser_t *p, int allowLFs, int handleQuoted ){
854 if ( p == NULL || p->buffer == NULL ||
855 p->cursor < p->buffer ||
856 p->cursor >= p->max ) {
859 /* clear parser token */
861 p->token[ 0 ] = '\0';
864 /* skip whitespaces */
865 while ( p->cursor < p->max && *p->cursor <= 32 )
867 if ( *p->cursor == '\n' ) {
873 /* return if we're not allowed to go beyond lfs */
874 if ( ( hasLFs > 0 ) && !allowLFs ) {
878 /* get next quoted string */
879 if ( *p->cursor == '\"' && handleQuoted ) {
881 while ( p->cursor < p->max && *p->cursor )
883 if ( *p->cursor == '\\' ) {
884 if ( *( p->cursor + 1 ) == '"' ) {
887 p->token[ p->tokenSize++ ] = *p->cursor++;
890 else if ( *p->cursor == '\"' ) {
894 else if ( *p->cursor == '\n' ) {
897 p->token[ p->tokenSize++ ] = *p->cursor++;
899 /* terminate token */
900 p->token[ p->tokenSize ] = '\0';
903 /* otherwise get next word */
904 while ( p->cursor < p->max && *p->cursor > 32 )
906 if ( *p->cursor == '\n' ) {
909 p->token[ p->tokenSize++ ] = *p->cursor++;
911 /* terminate token */
912 p->token[ p->tokenSize ] = '\0';
916 /* _pico_parse_first:
917 * reads the first token from the next line and returns
918 * a pointer to it. returns NULL on EOL or EOF. -sea
920 char *_pico_parse_first( picoParser_t *p ){
926 /* try to read next token (with lfs & quots) */
927 if ( !_pico_parse_ex( p,1,1 ) ) {
931 /* return ptr to the token string */
936 * reads the next token from the parser and returns a pointer
937 * to it. quoted strings are handled as usual. returns NULL
938 * on EOL or EOF. -sea
940 char *_pico_parse( picoParser_t *p, int allowLFs ){
946 /* try to read next token (with quots) */
947 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
951 /* return ptr to the token string */
955 /* _pico_parse_skip_rest:
956 * skips the rest of the current line in parser.
958 void _pico_parse_skip_rest( picoParser_t *p ){
959 while ( _pico_parse_ex( p,0,0 ) ) ;
962 /* _pico_parse_skip_braced:
963 * parses/skips over a braced section. returns 1 on success
964 * or 0 on error (when there was no closing bracket and the
965 * end of buffer was reached or when the opening bracket was
968 int _pico_parse_skip_braced( picoParser_t *p ){
977 /* set the initial level for parsing */
980 /* skip braced section */
983 /* read next token (lfs allowed) */
984 if ( !_pico_parse_ex( p,1,1 ) ) {
985 /* end of parser buffer reached */
988 /* first token must be an opening bracket */
989 if ( firstToken && p->token[0] != '{' ) {
990 /* opening bracket missing */
993 /* we only check this once */
997 if ( p->token[1] == '\0' ) {
998 if ( p->token[0] == '{' ) {
1001 if ( p->token[0] == '}' ) {
1005 /* break if we're back at our starting level */
1010 /* successfully skipped braced section */
1014 int _pico_parse_check( picoParser_t *p, int allowLFs, char *str ){
1015 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
1018 if ( !strcmp( p->token,str ) ) {
1024 int _pico_parse_checki( picoParser_t *p, int allowLFs, char *str ){
1025 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
1028 if ( !_pico_stricmp( p->token,str ) ) {
1034 int _pico_parse_int( picoParser_t *p, int *out ){
1038 if ( p == NULL || out == NULL ) {
1042 /* get token and turn it into an integer */
1044 token = _pico_parse( p,0 );
1045 if ( token == NULL ) {
1048 *out = atoi( token );
1054 int _pico_parse_int_def( picoParser_t *p, int *out, int def ){
1058 if ( p == NULL || out == NULL ) {
1062 /* get token and turn it into an integer */
1064 token = _pico_parse( p,0 );
1065 if ( token == NULL ) {
1068 *out = atoi( token );
1074 int _pico_parse_float( picoParser_t *p, float *out ){
1078 if ( p == NULL || out == NULL ) {
1082 /* get token and turn it into a float */
1084 token = _pico_parse( p,0 );
1085 if ( token == NULL ) {
1088 *out = (float) atof( token );
1094 int _pico_parse_float_def( picoParser_t *p, float *out, float def ){
1098 if ( p == NULL || out == NULL ) {
1102 /* get token and turn it into a float */
1104 token = _pico_parse( p,0 );
1105 if ( token == NULL ) {
1108 *out = (float) atof( token );
1114 int _pico_parse_vec( picoParser_t *p, picoVec3_t out ){
1119 if ( p == NULL || out == NULL ) {
1123 /* zero out outination vector */
1124 _pico_zero_vec( out );
1126 /* parse three vector components */
1127 for ( i = 0; i < 3; i++ )
1129 token = _pico_parse( p,0 );
1130 if ( token == NULL ) {
1131 _pico_zero_vec( out );
1134 out[ i ] = (float) atof( token );
1140 int _pico_parse_vec_def( picoParser_t *p, picoVec3_t out, picoVec3_t def ){
1145 if ( p == NULL || out == NULL ) {
1149 /* assign default vector value */
1150 _pico_copy_vec( def,out );
1152 /* parse three vector components */
1153 for ( i = 0; i < 3; i++ )
1155 token = _pico_parse( p,0 );
1156 if ( token == NULL ) {
1157 _pico_copy_vec( def,out );
1160 out[ i ] = (float) atof( token );
1166 int _pico_parse_vec2( picoParser_t *p, picoVec2_t out ){
1171 if ( p == NULL || out == NULL ) {
1175 /* zero out outination vector */
1176 _pico_zero_vec2( out );
1178 /* parse two vector components */
1179 for ( i = 0; i < 2; i++ )
1181 token = _pico_parse( p,0 );
1182 if ( token == NULL ) {
1183 _pico_zero_vec2( out );
1186 out[ i ] = (float) atof( token );
1192 int _pico_parse_vec2_def( picoParser_t *p, picoVec2_t out, picoVec2_t def ){
1197 if ( p == NULL || out == NULL ) {
1201 /* assign default vector value */
1202 _pico_copy_vec2( def,out );
1204 /* parse two vector components */
1205 for ( i = 0; i < 2; i++ )
1207 token = _pico_parse( p,0 );
1208 if ( token == NULL ) {
1209 _pico_copy_vec2( def,out );
1212 out[ i ] = (float) atof( token );
1218 int _pico_parse_vec4( picoParser_t *p, picoVec4_t out ){
1223 if ( p == NULL || out == NULL ) {
1227 /* zero out outination vector */
1228 _pico_zero_vec4( out );
1230 /* parse four vector components */
1231 for ( i = 0; i < 4; i++ )
1233 token = _pico_parse( p,0 );
1234 if ( token == NULL ) {
1235 _pico_zero_vec4( out );
1238 out[ i ] = (float) atof( token );
1244 int _pico_parse_vec4_def( picoParser_t *p, picoVec4_t out, picoVec4_t def ){
1249 if ( p == NULL || out == NULL ) {
1253 /* assign default vector value */
1254 _pico_copy_vec4( def,out );
1256 /* parse four vector components */
1257 for ( i = 0; i < 4; i++ )
1259 token = _pico_parse( p,0 );
1260 if ( token == NULL ) {
1261 _pico_copy_vec4( def,out );
1264 out[ i ] = (float) atof( token );
1270 /* _pico_new_memstream:
1271 * allocates a new memorystream object.
1273 picoMemStream_t *_pico_new_memstream( const picoByte_t *buffer, int bufSize ){
1277 if ( buffer == NULL || bufSize <= 0 ) {
1281 /* allocate stream */
1282 s = _pico_alloc( sizeof( picoMemStream_t ) );
1286 memset( s,0,sizeof( picoMemStream_t ) );
1291 s->bufSize = bufSize;
1294 /* return ptr to stream */
1298 /* _pico_free_memstream:
1299 * frees an existing pico memorystream object.
1301 void _pico_free_memstream( picoMemStream_t *s ){
1307 /* free the stream */
1311 /* _pico_memstream_read:
1312 * reads data from a pico memorystream into a buffer.
1314 int _pico_memstream_read( picoMemStream_t *s, void *buffer, int len ){
1318 if ( s == NULL || buffer == NULL ) {
1322 if ( s->curPos + len > s->buffer + s->bufSize ) {
1323 s->flag |= PICO_IOEOF;
1324 len = s->buffer + s->bufSize - s->curPos;
1329 memcpy( buffer, s->curPos, len );
1334 /* _pico_memstream_read:
1335 * reads a character from a pico memorystream
1337 int _pico_memstream_getc( picoMemStream_t *s ){
1345 /* read the character */
1346 if ( _pico_memstream_read( s, &c, 1 ) == 0 ) {
1353 /* _pico_memstream_seek:
1354 * sets the current read position to a different location
1356 int _pico_memstream_seek( picoMemStream_t *s, long offset, int origin ){
1364 if ( origin == PICO_SEEK_SET ) {
1365 s->curPos = s->buffer + offset;
1366 overflow = s->curPos - ( s->buffer + s->bufSize );
1367 if ( overflow > 0 ) {
1368 s->curPos = s->buffer + s->bufSize;
1369 return offset - overflow;
1373 else if ( origin == PICO_SEEK_CUR ) {
1374 s->curPos += offset;
1375 overflow = s->curPos - ( s->buffer + s->bufSize );
1376 if ( overflow > 0 ) {
1377 s->curPos = s->buffer + s->bufSize;
1378 return offset - overflow;
1382 else if ( origin == PICO_SEEK_END ) {
1383 s->curPos = ( s->buffer + s->bufSize ) - offset;
1384 overflow = s->buffer - s->curPos;
1385 if ( overflow > 0 ) {
1386 s->curPos = s->buffer;
1387 return offset - overflow;
1395 /* _pico_memstream_tell:
1396 * returns the current read position in the pico memorystream
1398 long _pico_memstream_tell( picoMemStream_t *s ){
1404 return s->curPos - s->buffer;