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 /* separate from _pico_set_vec4 */
466 void _pico_set_color( picoColor_t c, int r, int g, int b, int a ){
473 void _pico_copy_color( picoColor_t src, picoColor_t dest ){
474 dest[ 0 ] = src[ 0 ];
475 dest[ 1 ] = src[ 1 ];
476 dest[ 2 ] = src[ 2 ];
477 dest[ 3 ] = src[ 3 ];
480 #if GDEF_ARCH_ENDIAN_BIG
482 int _pico_big_long( int src ) { return src; }
483 short _pico_big_short( short src ) { return src; }
484 float _pico_big_float( float src ) { return src; }
486 int _pico_little_long( int src ){
487 return ( ( src & 0xFF000000 ) >> 24 ) |
488 ( ( src & 0x00FF0000 ) >> 8 ) |
489 ( ( src & 0x0000FF00 ) << 8 ) |
490 ( ( src & 0x000000FF ) << 24 );
493 short _pico_little_short( short src ){
494 return ( ( src & 0xFF00 ) >> 8 ) |
495 ( ( src & 0x00FF ) << 8 );
498 float _pico_little_float( float src ){
499 floatSwapUnion in,out;
501 out.c[ 0 ] = in.c[ 3 ];
502 out.c[ 1 ] = in.c[ 2 ];
503 out.c[ 2 ] = in.c[ 1 ];
504 out.c[ 3 ] = in.c[ 0 ];
507 #else /*__BIG_ENDIAN__*/
509 int _pico_little_long( int src ) { return src; }
510 short _pico_little_short( short src ) { return src; }
511 float _pico_little_float( float src ) { return src; }
513 int _pico_big_long( int src ){
514 return ( ( src & 0xFF000000 ) >> 24 ) |
515 ( ( src & 0x00FF0000 ) >> 8 ) |
516 ( ( src & 0x0000FF00 ) << 8 ) |
517 ( ( src & 0x000000FF ) << 24 );
520 short _pico_big_short( short src ){
521 return ( ( src & 0xFF00 ) >> 8 ) |
522 ( ( src & 0x00FF ) << 8 );
525 float _pico_big_float( float src ){
526 floatSwapUnion in,out;
528 out.c[ 0 ] = in.c[ 3 ];
529 out.c[ 1 ] = in.c[ 2 ];
530 out.c[ 2 ] = in.c[ 1 ];
531 out.c[ 3 ] = in.c[ 0 ];
534 #endif /*__BIG_ENDIAN__*/
537 * case-insensitive strstr. -sea
539 const char *_pico_stristr( const char *str, const char *substr ){
540 const size_t sublen = strlen( substr );
543 if ( !_pico_strnicmp( str,substr,sublen ) ) {
556 changes dos \ style path separators to /
559 void _pico_unixify( char *path ){
560 if ( path == NULL ) {
565 if ( *path == '\\' ) {
573 * removes file name portion from given file path and converts
574 * the directory separators to un*x style. returns 1 on success
575 * or 0 when 'destSize' was exceeded. -sea
577 int _pico_nofname( const char *path, char *dest, int destSize ){
581 while ( ( *dest = *path ) != '\0' )
583 if ( *dest == '/' || *dest == '\\' ) {
599 * returns ptr to filename portion in given path or an empty
600 * string otherwise. given 'path' is not altered. -sea
602 const char *_pico_nopath( const char *path ){
604 src = path + ( strlen( path ) - 1 );
606 if ( path == NULL ) {
609 if ( !strchr( path,'/' ) && !strchr( path,'\\' ) ) {
613 while ( ( src-- ) != path )
615 if ( *src == '/' || *src == '\\' ) {
623 * sets/changes the file extension for the given filename
624 * or filepath's filename portion. the given 'path' *is*
625 * altered. leave 'ext' empty to remove extension. -sea
627 char *_pico_setfext( char *path, const char *ext ){
631 src = path + ( strlen( path ) - 1 );
636 if ( strlen( ext ) < 1 ) {
639 if ( strlen( path ) < 1 ) {
643 while ( ( src-- ) != path )
645 if ( *src == '/' || *src == '\\' ) {
663 * extracts one line from the given buffer and stores it in dest.
664 * returns -1 on error or the length of the line on success. i've
665 * removed string trimming here. this can be done manually by the
668 int _pico_getline( char *buf, int bufsize, char *dest, int destsize ){
672 if ( dest == NULL || destsize < 1 ) {
675 memset( dest,0,destsize );
678 if ( buf == NULL || bufsize < 1 ) {
683 for ( pos = 0; pos < bufsize && pos < destsize; pos++ )
685 if ( buf[pos] == '\n' ) {
688 dest[pos] = buf[pos];
690 /* terminate dest and return */
695 /* _pico_parse_skip_white:
696 * skips white spaces in current pico parser, sets *hasLFs
697 * to 1 if linefeeds were skipped, and either returns the
698 * parser's cursor pointer or NULL on error. -sea
700 void _pico_parse_skip_white( picoParser_t *p, int *hasLFs ){
702 if ( p == NULL || p->cursor == NULL ) {
706 /* skin white spaces */
710 if ( p->cursor < p->buffer ||
711 p->cursor >= p->max ) {
714 /* break for chars other than white spaces */
715 if ( *p->cursor > 0x20 ) {
718 if ( *p->cursor == 0x00 ) {
722 /* a bit of linefeed handling */
723 if ( *p->cursor == '\n' ) {
727 /* go to next character */
733 * allocates a new ascii parser object.
735 picoParser_t *_pico_new_parser( const picoByte_t *buffer, int bufSize ){
739 if ( buffer == NULL || bufSize <= 0 ) {
743 /* allocate reader */
744 p = _pico_alloc( sizeof( picoParser_t ) );
748 memset( p,0,sizeof( picoParser_t ) );
750 /* allocate token space */
753 p->token = _pico_alloc( p->tokenMax );
754 if ( p->token == NULL ) {
759 p->buffer = (const char *) buffer;
760 p->cursor = p->buffer;
761 p->bufSize = bufSize;
762 p->max = p->buffer + bufSize;
763 p->curLine = 1; /* sea: new */
765 /* return ptr to parser */
769 /* _pico_free_parser:
770 * frees an existing pico parser object.
772 void _pico_free_parser( picoParser_t *p ){
778 /* free the parser */
779 if ( p->token != NULL ) {
780 _pico_free( p->token );
786 * reads the next token from given pico parser object. if param
787 * 'allowLFs' is 1 it will read beyond linefeeds and return 0 when
788 * the EOF is reached. if 'allowLFs' is 0 it will return 0 when
789 * the EOL is reached. if 'handleQuoted' is 1 the parser function
790 * will handle "quoted" strings and return the data between the
791 * quotes as token. returns 0 on end/error or 1 on success. -sea
793 int _pico_parse_ex( picoParser_t *p, int allowLFs, int handleQuoted ){
798 if ( p == NULL || p->buffer == NULL ||
799 p->cursor < p->buffer ||
800 p->cursor >= p->max ) {
803 /* clear parser token */
805 p->token[ 0 ] = '\0';
808 /* skip whitespaces */
809 while ( p->cursor < p->max && *p->cursor <= 32 )
811 if ( *p->cursor == '\n' ) {
817 /* return if we're not allowed to go beyond lfs */
818 if ( ( hasLFs > 0 ) && !allowLFs ) {
822 /* get next quoted string */
823 if ( *p->cursor == '\"' && handleQuoted ) {
825 while ( p->cursor < p->max && *p->cursor )
827 if ( *p->cursor == '\\' ) {
828 if ( *( p->cursor + 1 ) == '"' ) {
831 p->token[ p->tokenSize++ ] = *p->cursor++;
834 else if ( *p->cursor == '\"' ) {
838 else if ( *p->cursor == '\n' ) {
841 p->token[ p->tokenSize++ ] = *p->cursor++;
843 /* terminate token */
844 p->token[ p->tokenSize ] = '\0';
847 /* otherwise get next word */
848 while ( p->cursor < p->max && *p->cursor > 32 )
850 if ( *p->cursor == '\n' ) {
853 p->token[ p->tokenSize++ ] = *p->cursor++;
855 /* terminate token */
856 p->token[ p->tokenSize ] = '\0';
860 /* _pico_parse_first:
861 * reads the first token from the next line and returns
862 * a pointer to it. returns NULL on EOL or EOF. -sea
864 char *_pico_parse_first( picoParser_t *p ){
870 /* try to read next token (with lfs & quots) */
871 if ( !_pico_parse_ex( p,1,1 ) ) {
875 /* return ptr to the token string */
880 * reads the next token from the parser and returns a pointer
881 * to it. quoted strings are handled as usual. returns NULL
882 * on EOL or EOF. -sea
884 char *_pico_parse( picoParser_t *p, int allowLFs ){
890 /* try to read next token (with quots) */
891 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
895 /* return ptr to the token string */
899 /* _pico_parse_skip_rest:
900 * skips the rest of the current line in parser.
902 void _pico_parse_skip_rest( picoParser_t *p ){
903 while ( _pico_parse_ex( p,0,0 ) ) ;
906 /* _pico_parse_skip_braced:
907 * parses/skips over a braced section. returns 1 on success
908 * or 0 on error (when there was no closing bracket and the
909 * end of buffer was reached or when the opening bracket was
912 int _pico_parse_skip_braced( picoParser_t *p ){
921 /* set the initial level for parsing */
924 /* skip braced section */
927 /* read next token (lfs allowed) */
928 if ( !_pico_parse_ex( p,1,1 ) ) {
929 /* end of parser buffer reached */
932 /* first token must be an opening bracket */
933 if ( firstToken && p->token[0] != '{' ) {
934 /* opening bracket missing */
937 /* we only check this once */
941 if ( p->token[1] == '\0' ) {
942 if ( p->token[0] == '{' ) {
945 if ( p->token[0] == '}' ) {
949 /* break if we're back at our starting level */
954 /* successfully skipped braced section */
958 int _pico_parse_check( picoParser_t *p, int allowLFs, char *str ){
959 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
962 if ( !strcmp( p->token,str ) ) {
968 int _pico_parse_checki( picoParser_t *p, int allowLFs, char *str ){
969 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
972 if ( !_pico_stricmp( p->token,str ) ) {
978 int _pico_parse_int( picoParser_t *p, int *out ){
982 if ( p == NULL || out == NULL ) {
986 /* get token and turn it into an integer */
988 token = _pico_parse( p,0 );
989 if ( token == NULL ) {
992 *out = atoi( token );
998 int _pico_parse_int_def( picoParser_t *p, int *out, int def ){
1002 if ( p == NULL || out == NULL ) {
1006 /* get token and turn it into an integer */
1008 token = _pico_parse( p,0 );
1009 if ( token == NULL ) {
1012 *out = atoi( token );
1018 int _pico_parse_float( picoParser_t *p, float *out ){
1022 if ( p == NULL || out == NULL ) {
1026 /* get token and turn it into a float */
1028 token = _pico_parse( p,0 );
1029 if ( token == NULL ) {
1032 *out = (float) atof( token );
1038 int _pico_parse_float_def( picoParser_t *p, float *out, float def ){
1042 if ( p == NULL || out == NULL ) {
1046 /* get token and turn it into a float */
1048 token = _pico_parse( p,0 );
1049 if ( token == NULL ) {
1052 *out = (float) atof( token );
1058 int _pico_parse_vec( picoParser_t *p, picoVec3_t out ){
1063 if ( p == NULL || out == NULL ) {
1067 /* zero out outination vector */
1068 _pico_zero_vec( out );
1070 /* parse three vector components */
1071 for ( i = 0; i < 3; i++ )
1073 token = _pico_parse( p,0 );
1074 if ( token == NULL ) {
1075 _pico_zero_vec( out );
1078 out[ i ] = (float) atof( token );
1084 int _pico_parse_vec_def( picoParser_t *p, picoVec3_t out, picoVec3_t def ){
1089 if ( p == NULL || out == NULL ) {
1093 /* assign default vector value */
1094 _pico_copy_vec( def,out );
1096 /* parse three vector components */
1097 for ( i = 0; i < 3; i++ )
1099 token = _pico_parse( p,0 );
1100 if ( token == NULL ) {
1101 _pico_copy_vec( def,out );
1104 out[ i ] = (float) atof( token );
1110 int _pico_parse_vec2( picoParser_t *p, picoVec2_t out ){
1115 if ( p == NULL || out == NULL ) {
1119 /* zero out outination vector */
1120 _pico_zero_vec2( out );
1122 /* parse two vector components */
1123 for ( i = 0; i < 2; i++ )
1125 token = _pico_parse( p,0 );
1126 if ( token == NULL ) {
1127 _pico_zero_vec2( out );
1130 out[ i ] = (float) atof( token );
1136 int _pico_parse_vec2_def( picoParser_t *p, picoVec2_t out, picoVec2_t def ){
1141 if ( p == NULL || out == NULL ) {
1145 /* assign default vector value */
1146 _pico_copy_vec2( def,out );
1148 /* parse two vector components */
1149 for ( i = 0; i < 2; i++ )
1151 token = _pico_parse( p,0 );
1152 if ( token == NULL ) {
1153 _pico_copy_vec2( def,out );
1156 out[ i ] = (float) atof( token );
1162 int _pico_parse_vec4( picoParser_t *p, picoVec4_t out ){
1167 if ( p == NULL || out == NULL ) {
1171 /* zero out outination vector */
1172 _pico_zero_vec4( out );
1174 /* parse four vector components */
1175 for ( i = 0; i < 4; i++ )
1177 token = _pico_parse( p,0 );
1178 if ( token == NULL ) {
1179 _pico_zero_vec4( out );
1182 out[ i ] = (float) atof( token );
1188 int _pico_parse_vec4_def( picoParser_t *p, picoVec4_t out, picoVec4_t def ){
1193 if ( p == NULL || out == NULL ) {
1197 /* assign default vector value */
1198 _pico_copy_vec4( def,out );
1200 /* parse four vector components */
1201 for ( i = 0; i < 4; i++ )
1203 token = _pico_parse( p,0 );
1204 if ( token == NULL ) {
1205 _pico_copy_vec4( def,out );
1208 out[ i ] = (float) atof( token );
1214 /* _pico_new_memstream:
1215 * allocates a new memorystream object.
1217 picoMemStream_t *_pico_new_memstream( const picoByte_t *buffer, int bufSize ){
1221 if ( buffer == NULL || bufSize <= 0 ) {
1225 /* allocate stream */
1226 s = _pico_alloc( sizeof( picoMemStream_t ) );
1230 memset( s,0,sizeof( picoMemStream_t ) );
1235 s->bufSize = bufSize;
1238 /* return ptr to stream */
1242 /* _pico_free_memstream:
1243 * frees an existing pico memorystream object.
1245 void _pico_free_memstream( picoMemStream_t *s ){
1251 /* free the stream */
1255 /* _pico_memstream_read:
1256 * reads data from a pico memorystream into a buffer.
1258 int _pico_memstream_read( picoMemStream_t *s, void *buffer, int len ){
1262 if ( s == NULL || buffer == NULL ) {
1266 if ( s->curPos + len > s->buffer + s->bufSize ) {
1267 s->flag |= PICO_IOEOF;
1268 len = s->buffer + s->bufSize - s->curPos;
1273 memcpy( buffer, s->curPos, len );
1278 /* _pico_memstream_read:
1279 * reads a character from a pico memorystream
1281 int _pico_memstream_getc( picoMemStream_t *s ){
1289 /* read the character */
1290 if ( _pico_memstream_read( s, &c, 1 ) == 0 ) {
1297 /* _pico_memstream_seek:
1298 * sets the current read position to a different location
1300 int _pico_memstream_seek( picoMemStream_t *s, long offset, int origin ){
1308 if ( origin == PICO_SEEK_SET ) {
1309 s->curPos = s->buffer + offset;
1310 overflow = s->curPos - ( s->buffer + s->bufSize );
1311 if ( overflow > 0 ) {
1312 s->curPos = s->buffer + s->bufSize;
1313 return offset - overflow;
1317 else if ( origin == PICO_SEEK_CUR ) {
1318 s->curPos += offset;
1319 overflow = s->curPos - ( s->buffer + s->bufSize );
1320 if ( overflow > 0 ) {
1321 s->curPos = s->buffer + s->bufSize;
1322 return offset - overflow;
1326 else if ( origin == PICO_SEEK_END ) {
1327 s->curPos = ( s->buffer + s->bufSize ) - offset;
1328 overflow = s->buffer - s->curPos;
1329 if ( overflow > 0 ) {
1330 s->curPos = s->buffer;
1331 return offset - overflow;
1339 /* _pico_memstream_tell:
1340 * returns the current read position in the pico memorystream
1342 long _pico_memstream_tell( picoMemStream_t *s ){
1348 return s->curPos - s->buffer;