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 /* _pico_parse_skip_white:
698 * skips white spaces in current pico parser, sets *hasLFs
699 * to 1 if linefeeds were skipped, and either returns the
700 * parser's cursor pointer or NULL on error. -sea
702 void _pico_parse_skip_white( picoParser_t *p, int *hasLFs ){
704 if ( p == NULL || p->cursor == NULL ) {
708 /* skin white spaces */
712 if ( p->cursor < p->buffer ||
713 p->cursor >= p->max ) {
716 /* break for chars other than white spaces */
717 if ( *p->cursor > 0x20 ) {
720 if ( *p->cursor == 0x00 ) {
724 /* a bit of linefeed handling */
725 if ( *p->cursor == '\n' ) {
729 /* go to next character */
735 * allocates a new ascii parser object.
737 picoParser_t *_pico_new_parser( const picoByte_t *buffer, int bufSize ){
741 if ( buffer == NULL || bufSize <= 0 ) {
745 /* allocate reader */
746 p = _pico_alloc( sizeof( picoParser_t ) );
750 memset( p,0,sizeof( picoParser_t ) );
752 /* allocate token space */
755 p->token = _pico_alloc( p->tokenMax );
756 if ( p->token == NULL ) {
761 p->buffer = (const char *) buffer;
762 p->cursor = p->buffer;
763 p->bufSize = bufSize;
764 p->max = p->buffer + bufSize;
765 p->curLine = 1; /* sea: new */
767 /* return ptr to parser */
771 /* _pico_free_parser:
772 * frees an existing pico parser object.
774 void _pico_free_parser( picoParser_t *p ){
780 /* free the parser */
781 if ( p->token != NULL ) {
782 _pico_free( p->token );
788 * reads the next token from given pico parser object. if param
789 * 'allowLFs' is 1 it will read beyond linefeeds and return 0 when
790 * the EOF is reached. if 'allowLFs' is 0 it will return 0 when
791 * the EOL is reached. if 'handleQuoted' is 1 the parser function
792 * will handle "quoted" strings and return the data between the
793 * quotes as token. returns 0 on end/error or 1 on success. -sea
795 int _pico_parse_ex( picoParser_t *p, int allowLFs, int handleQuoted ){
800 if ( p == NULL || p->buffer == NULL ||
801 p->cursor < p->buffer ||
802 p->cursor >= p->max ) {
805 /* clear parser token */
807 p->token[ 0 ] = '\0';
810 /* skip whitespaces */
811 while ( p->cursor < p->max && *p->cursor <= 32 )
813 if ( *p->cursor == '\n' ) {
819 /* return if we're not allowed to go beyond lfs */
820 if ( ( hasLFs > 0 ) && !allowLFs ) {
824 /* get next quoted string */
825 if ( *p->cursor == '\"' && handleQuoted ) {
827 while ( p->cursor < p->max && *p->cursor )
829 if ( *p->cursor == '\\' ) {
830 if ( *( p->cursor + 1 ) == '"' ) {
833 p->token[ p->tokenSize++ ] = *p->cursor++;
836 else if ( *p->cursor == '\"' ) {
840 else if ( *p->cursor == '\n' ) {
843 p->token[ p->tokenSize++ ] = *p->cursor++;
845 /* terminate token */
846 p->token[ p->tokenSize ] = '\0';
849 /* otherwise get next word */
850 while ( p->cursor < p->max && *p->cursor > 32 )
852 if ( *p->cursor == '\n' ) {
855 p->token[ p->tokenSize++ ] = *p->cursor++;
857 /* terminate token */
858 p->token[ p->tokenSize ] = '\0';
862 /* _pico_parse_first:
863 * reads the first token from the next line and returns
864 * a pointer to it. returns NULL on EOL or EOF. -sea
866 char *_pico_parse_first( picoParser_t *p ){
872 /* try to read next token (with lfs & quots) */
873 if ( !_pico_parse_ex( p,1,1 ) ) {
877 /* return ptr to the token string */
882 * reads the next token from the parser and returns a pointer
883 * to it. quoted strings are handled as usual. returns NULL
884 * on EOL or EOF. -sea
886 char *_pico_parse( picoParser_t *p, int allowLFs ){
892 /* try to read next token (with quots) */
893 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
897 /* return ptr to the token string */
901 /* _pico_parse_skip_rest:
902 * skips the rest of the current line in parser.
904 void _pico_parse_skip_rest( picoParser_t *p ){
905 while ( _pico_parse_ex( p,0,0 ) ) ;
908 /* _pico_parse_skip_braced:
909 * parses/skips over a braced section. returns 1 on success
910 * or 0 on error (when there was no closing bracket and the
911 * end of buffer was reached or when the opening bracket was
914 int _pico_parse_skip_braced( picoParser_t *p ){
923 /* set the initial level for parsing */
926 /* skip braced section */
929 /* read next token (lfs allowed) */
930 if ( !_pico_parse_ex( p,1,1 ) ) {
931 /* end of parser buffer reached */
934 /* first token must be an opening bracket */
935 if ( firstToken && p->token[0] != '{' ) {
936 /* opening bracket missing */
939 /* we only check this once */
943 if ( p->token[1] == '\0' ) {
944 if ( p->token[0] == '{' ) {
947 if ( p->token[0] == '}' ) {
951 /* break if we're back at our starting level */
956 /* successfully skipped braced section */
960 int _pico_parse_check( picoParser_t *p, int allowLFs, char *str ){
961 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
964 if ( !strcmp( p->token,str ) ) {
970 int _pico_parse_checki( picoParser_t *p, int allowLFs, char *str ){
971 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
974 if ( !_pico_stricmp( p->token,str ) ) {
980 int _pico_parse_int( picoParser_t *p, int *out ){
984 if ( p == NULL || out == NULL ) {
988 /* get token and turn it into an integer */
990 token = _pico_parse( p,0 );
991 if ( token == NULL ) {
994 *out = atoi( token );
1000 int _pico_parse_int_def( picoParser_t *p, int *out, int def ){
1004 if ( p == NULL || out == NULL ) {
1008 /* get token and turn it into an integer */
1010 token = _pico_parse( p,0 );
1011 if ( token == NULL ) {
1014 *out = atoi( token );
1020 int _pico_parse_float( picoParser_t *p, float *out ){
1024 if ( p == NULL || out == NULL ) {
1028 /* get token and turn it into a float */
1030 token = _pico_parse( p,0 );
1031 if ( token == NULL ) {
1034 *out = (float) atof( token );
1040 int _pico_parse_float_def( picoParser_t *p, float *out, float def ){
1044 if ( p == NULL || out == NULL ) {
1048 /* get token and turn it into a float */
1050 token = _pico_parse( p,0 );
1051 if ( token == NULL ) {
1054 *out = (float) atof( token );
1060 int _pico_parse_vec( picoParser_t *p, picoVec3_t out ){
1065 if ( p == NULL || out == NULL ) {
1069 /* zero out outination vector */
1070 _pico_zero_vec( out );
1072 /* parse three vector components */
1073 for ( i = 0; i < 3; i++ )
1075 token = _pico_parse( p,0 );
1076 if ( token == NULL ) {
1077 _pico_zero_vec( out );
1080 out[ i ] = (float) atof( token );
1086 int _pico_parse_vec_def( picoParser_t *p, picoVec3_t out, picoVec3_t def ){
1091 if ( p == NULL || out == NULL ) {
1095 /* assign default vector value */
1096 _pico_copy_vec( def,out );
1098 /* parse three vector components */
1099 for ( i = 0; i < 3; i++ )
1101 token = _pico_parse( p,0 );
1102 if ( token == NULL ) {
1103 _pico_copy_vec( def,out );
1106 out[ i ] = (float) atof( token );
1112 int _pico_parse_vec2( picoParser_t *p, picoVec2_t out ){
1117 if ( p == NULL || out == NULL ) {
1121 /* zero out outination vector */
1122 _pico_zero_vec2( out );
1124 /* parse two vector components */
1125 for ( i = 0; i < 2; i++ )
1127 token = _pico_parse( p,0 );
1128 if ( token == NULL ) {
1129 _pico_zero_vec2( out );
1132 out[ i ] = (float) atof( token );
1138 int _pico_parse_vec2_def( picoParser_t *p, picoVec2_t out, picoVec2_t def ){
1143 if ( p == NULL || out == NULL ) {
1147 /* assign default vector value */
1148 _pico_copy_vec2( def,out );
1150 /* parse two vector components */
1151 for ( i = 0; i < 2; i++ )
1153 token = _pico_parse( p,0 );
1154 if ( token == NULL ) {
1155 _pico_copy_vec2( def,out );
1158 out[ i ] = (float) atof( token );
1164 int _pico_parse_vec4( picoParser_t *p, picoVec4_t out ){
1169 if ( p == NULL || out == NULL ) {
1173 /* zero out outination vector */
1174 _pico_zero_vec4( out );
1176 /* parse four vector components */
1177 for ( i = 0; i < 4; i++ )
1179 token = _pico_parse( p,0 );
1180 if ( token == NULL ) {
1181 _pico_zero_vec4( out );
1184 out[ i ] = (float) atof( token );
1190 int _pico_parse_vec4_def( picoParser_t *p, picoVec4_t out, picoVec4_t def ){
1195 if ( p == NULL || out == NULL ) {
1199 /* assign default vector value */
1200 _pico_copy_vec4( def,out );
1202 /* parse four vector components */
1203 for ( i = 0; i < 4; i++ )
1205 token = _pico_parse( p,0 );
1206 if ( token == NULL ) {
1207 _pico_copy_vec4( def,out );
1210 out[ i ] = (float) atof( token );
1216 /* _pico_new_memstream:
1217 * allocates a new memorystream object.
1219 picoMemStream_t *_pico_new_memstream( const picoByte_t *buffer, int bufSize ){
1223 if ( buffer == NULL || bufSize <= 0 ) {
1227 /* allocate stream */
1228 s = _pico_alloc( sizeof( picoMemStream_t ) );
1232 memset( s,0,sizeof( picoMemStream_t ) );
1237 s->bufSize = bufSize;
1240 /* return ptr to stream */
1244 /* _pico_free_memstream:
1245 * frees an existing pico memorystream object.
1247 void _pico_free_memstream( picoMemStream_t *s ){
1253 /* free the stream */
1257 /* _pico_memstream_read:
1258 * reads data from a pico memorystream into a buffer.
1260 int _pico_memstream_read( picoMemStream_t *s, void *buffer, int len ){
1264 if ( s == NULL || buffer == NULL ) {
1268 if ( s->curPos + len > s->buffer + s->bufSize ) {
1269 s->flag |= PICO_IOEOF;
1270 len = s->buffer + s->bufSize - s->curPos;
1275 memcpy( buffer, s->curPos, len );
1280 /* _pico_memstream_read:
1281 * reads a character from a pico memorystream
1283 int _pico_memstream_getc( picoMemStream_t *s ){
1291 /* read the character */
1292 if ( _pico_memstream_read( s, &c, 1 ) == 0 ) {
1299 /* _pico_memstream_seek:
1300 * sets the current read position to a different location
1302 int _pico_memstream_seek( picoMemStream_t *s, long offset, int origin ){
1310 if ( origin == PICO_SEEK_SET ) {
1311 s->curPos = s->buffer + offset;
1312 overflow = s->curPos - ( s->buffer + s->bufSize );
1313 if ( overflow > 0 ) {
1314 s->curPos = s->buffer + s->bufSize;
1315 return offset - overflow;
1319 else if ( origin == PICO_SEEK_CUR ) {
1320 s->curPos += offset;
1321 overflow = s->curPos - ( s->buffer + s->bufSize );
1322 if ( overflow > 0 ) {
1323 s->curPos = s->buffer + s->bufSize;
1324 return offset - overflow;
1328 else if ( origin == PICO_SEEK_END ) {
1329 s->curPos = ( s->buffer + s->bufSize ) - offset;
1330 overflow = s->buffer - s->curPos;
1331 if ( overflow > 0 ) {
1332 s->curPos = s->buffer;
1333 return offset - overflow;
1341 /* _pico_memstream_tell:
1342 * returns the current read position in the pico memorystream
1344 long _pico_memstream_tell( picoMemStream_t *s ){
1350 return s->curPos - s->buffer;