3rdParty-devel: Import plib files to patch

Future commits shall patch these files to fix some build-time issues in
Windows. Since, as of the time of this writing, CMake lacks a PATCH
command, files must be copied entirely.


git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@9578 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: ae23404d634e4146147f01f3d3d5faafccdea77e
Former-commit-id: bcde47f4b24ff356f32ebd222393bf731bed6ad8
This commit is contained in:
xavi92 2024-10-27 07:51:29 +00:00
parent a8d1009656
commit a99733cbed
3 changed files with 2347 additions and 0 deletions

View file

@ -0,0 +1,731 @@
/*
PLIB - A Suite of Portable Game Libraries
Copyright (C) 1998,2002 Steve Baker
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For further information visit http://plib.sourceforge.net
$Id: sl.h 1924 2004-04-06 13:32:26Z sjbaker $
*/
#ifndef __SL_H__
#define __SL_H__ 1
#include <stdio.h>
#include "slPortability.h"
#ifdef SL_USING_OSS_AUDIO
#define SLDSP_DEFAULT_DEVICE "/dev/dsp"
#elif defined(UL_WIN32)
#define SLDSP_DEFAULT_DEVICE "dsp"
#elif defined(UL_BSD)
#define SLDSP_DEFAULT_DEVICE "/dev/audio"
#elif defined(UL_IRIX)
#define SLDSP_DEFAULT_DEVICE "dsp" // dummy ...
#elif defined(UL_AIX)
#define SLDSP_DEFAULT_DEVICE "dsp" // dummy ...
#elif defined(UL_SOLARIS)
#define SLDSP_DEFAULT_DEVICE "/dev/audio"
#elif defined(UL_HPUX)
#define SLDSP_DEFAULT_DEVICE "/dev/audio"
#elif defined(UL_MACINTOSH) || defined(UL_MAC_OSX)
#define SLDSP_DEFAULT_DEVICE "dsp" // dummy
#else
#error "Port me !"
#endif
# define SL_TRUE 1
# define SL_FALSE 0
typedef unsigned char Uchar ;
typedef unsigned short Ushort ;
#define SL_DEFAULT_SAMPLING_RATE 11025
class slSample ;
class slPlayer ;
class slSamplePlayer ;
class slEnvelope ;
class slScheduler ;
class slDSP ;
extern const char *__slPendingError ;
class slDSP
{
private:
int stereo ;
int rate ;
int bps ;
int error ;
int fd ;
#if defined(SL_USING_OSS_AUDIO)
audio_buf_info buff_info ;
#elif defined(UL_BSD)
audio_info_t ainfo; // ioctl structure
audio_offset_t audio_offset; // offset in audiostream
long counter; // counter-written packets
#elif defined(UL_SOLARIS)
audio_info_t ainfo;
long counter;
#elif defined(UL_IRIX)
ALconfig config; // configuration stuff
ALport port; // .. we are here
#elif defined(UL_MACINTOSH) || defined(UL_MAC_OSX)
// Size of the data chunks written with write().
// This should be a multiple of 1024.
#define BUFFER_SIZE 8192
// Size of storage space for sound data.
// This should be evenly divisible by BUFFER_SIZE.
#define VIRTUAL_BUFFER_SIZE 131072
SndChannelPtr sndChannel ; // sound channel we are using
ExtSoundHeader extSndHeader ; // sound header for bufferCmd
SndCommand currentCmd ; // command we are sending to chnl
OSErr osErr ;
char *buf ; // buffer containing sound data
char *rpos ; // read position for buffer
char *wpos ; // write position for buffer
char *ptr ; // ptr to data from write()
float bytesPerSample ; // conversions
float bytesPerSecond ;
float secondsPerPacket ;
float secLeft ; // seconds left in buffer
float secUsed ; // seconds used in buffer
UnsignedWide lastTime ; // used for timing in secondsUsed()
UnsignedWide currTime ;
#endif
#if !defined(UL_WIN32) && !defined(UL_MACINTOSH) && !defined(UL_MAC_OSX)
int ioctl ( int cmd, int param = 0 )
{
if ( error ) return param ;
if ( ::ioctl ( fd, cmd, & param ) == -1 )
{
perror ( "slDSP: ioctl" ) ;
error = SL_TRUE ;
}
return param ;
}
#elif defined(UL_WIN32)
#define BUFFER_COUNT (3*8)
#define BUFFER_SIZE (1024*1)
friend void CALLBACK waveOutProc( HWAVEOUT hwo, UINT uMsg,
DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 );
HWAVEOUT hWaveOut; // device handle
WAVEFORMATEX Format; // open needs this
MMTIME mmt; // timing
WAVEHDR *wavehdr[ BUFFER_COUNT ]; // for round robin ..
int curr_header; // index of actual wavehdr
int counter; // counter-written packets
DWORD written; // counter-written samples
#endif
void open ( const char *device, int _rate, int _stereo, int _bps ) ;
void close () ;
void getBufferInfo () ;
void write ( void *buffer, size_t length ) ;
protected:
void setError () { error = SL_TRUE ; }
int getDriverBufferSize () ;
public:
slDSP ( int _rate = SL_DEFAULT_SAMPLING_RATE,
int _stereo = SL_FALSE, int _bps = 8 )
{
open ( SLDSP_DEFAULT_DEVICE, _rate, _stereo, _bps ) ;
}
slDSP ( const char *device, int _rate = SL_DEFAULT_SAMPLING_RATE,
int _stereo = SL_FALSE, int _bps = 8 )
{
open ( device, _rate, _stereo, _bps ) ;
}
~slDSP () { close () ; }
float secondsRemaining () ;
float secondsUsed () ;
void play ( void *buffer, size_t length ) { write ( buffer, length ) ; }
/* This was a typo - but people use it */
int not_working() const { return error ; }
int notWorking () const { return error ; }
int getBps () const { return bps ; }
int getRate () const { return rate ; }
int getStereo() const { return stereo ; }
void sync () ;
void stop () ;
} ;
class slSample
{
int ref_count ;
protected:
char *comment;
int rate ;
int bps ;
int stereo ;
Uchar *buffer ;
int length ;
void init ()
{
ref_count = 0 ;
comment = NULL ;
buffer = NULL ;
length = 0 ;
rate = SL_DEFAULT_SAMPLING_RATE ;
bps = 8 ;
stereo = SL_FALSE ;
}
public:
slSample () { init () ; }
slSample ( const Uchar *buff, int leng )
{
init () ;
setBuffer ( buff, leng ) ;
}
slSample ( const char *fname, const class slDSP *dsp = NULL )
{
if ( dsp != NULL && dsp->notWorking () )
dsp = NULL ;
init () ;
loadFile ( fname ) ;
autoMatch ( dsp ) ;
}
~slSample ()
{
if ( ref_count != 0 && __slPendingError == NULL )
__slPendingError =
"slSample: FATAL ERROR - Application deleted a sample while it was playing." ;
delete [] buffer ;
}
void ref () { ref_count++ ; }
void unRef () { ref_count-- ; }
int getPlayCount () const { return ref_count ; }
char *getComment () const { return comment ; }
void setComment ( const char *nc )
{
delete [] comment ;
comment = ulStrDup ( nc ) ;
}
Uchar *getBuffer () const { return buffer ; }
int getLength () const { return length ; }
void autoMatch ( const slDSP *dsp ) ;
void setBuffer ( const Uchar *buff, int leng )
{
delete [] buffer ;
buffer = new Uchar [ leng ] ;
if ( buff != NULL )
memcpy ( buffer, buff, leng ) ;
length = leng ;
}
/* These routines only set flags - use changeXXX () to convert a sound */
void setRate ( int r ) { rate = r ; }
void setBps ( int b ) { bps = b ; }
void setStereo ( int s ) { stereo = s ; }
int getRate () const { return rate ; }
int getBps () const { return bps ; }
int getStereo () const { return stereo ; }
float getDuration () const { return (float) getLength() /
(float) ( (getStereo()?2.0f:1.0f)*
(getBps()/8.0f)*getRate() ) ; }
int loadFile ( const char *fname ) ;
int loadRawFile ( const char *fname ) ;
int loadAUFile ( const char *fname ) ;
int loadWavFile ( const char *fname ) ;
void changeRate ( int r ) ;
void changeBps ( int b ) ;
void changeStereo ( int s ) ;
void changeToUnsigned () ;
void adjustVolume ( float vol ) ;
void print ( FILE *fd ) const
{
if ( buffer == NULL )
{
fprintf ( fd, "Empty sample buffer\n" ) ;
}
else
{
fprintf ( fd, "\"%s\"\n",(getComment() == NULL ||
getComment()[0]=='\0') ? "Sample" : comment ) ;
fprintf ( fd, "%s, %d bits per sample.\n",
getStereo() ? "Stereo" : "Mono", getBps() ) ;
fprintf ( fd, "%gKHz sample rate.\n", (float) getRate() / 1000.0f ) ;
fprintf ( fd, "%d bytes of samples == %g seconds duration.\n", getLength(), getDuration() ) ;
}
}
} ;
enum slSampleStatus
{
SL_SAMPLE_WAITING, /* Sound hasn't started playing yet */
SL_SAMPLE_RUNNING, /* Sound has started playing */
SL_SAMPLE_DONE , /* Sound is complete */
SL_SAMPLE_PAUSED /* Sound hasn't started playing yet */
} ;
enum slPreemptMode
{
SL_SAMPLE_CONTINUE, /* Don't allow yourself to be preempted */
SL_SAMPLE_ABORT , /* Abort playing the sound when preempted */
SL_SAMPLE_RESTART , /* Restart the sound when load permits */
SL_SAMPLE_MUTE , /* Continue silently until load permits */
SL_SAMPLE_DELAY /* Pause until load permits */
} ;
enum slReplayMode
{
SL_SAMPLE_LOOP, /* Loop sound so that it plays forever */
SL_SAMPLE_ONE_SHOT /* Play sound just once */
} ;
enum slEvent
{
SL_EVENT_COMPLETE, /* Sound finished playing */
SL_EVENT_LOOPED, /* Sound looped back to the start */
SL_EVENT_PREEMPTED /* Sound was preempted */
} ;
typedef void (*slCallBack) ( slSample *, slEvent, int ) ;
class slEnvelope
{
protected:
float *time ;
float *value ;
int nsteps ;
int ref_count ;
float previous_value ;
unsigned char prev_pitchenv ;
slReplayMode replay_mode ;
int getStepDelta ( float *_time, float *delta ) const ;
public:
slEnvelope ( int _nsteps, slReplayMode _rm, float *_times, float *_values )
{
ref_count = 0 ;
nsteps = _nsteps ;
time = new float [ nsteps ] ;
value = new float [ nsteps ] ;
memcpy ( time , _times , sizeof(float) * nsteps ) ;
memcpy ( value, _values, sizeof(float) * nsteps ) ;
prev_pitchenv = 0x80 ;
previous_value = 0.0f ;
replay_mode = _rm ;
}
slEnvelope ( int _nsteps = 1, slReplayMode _rm = SL_SAMPLE_ONE_SHOT )
{
ref_count = 0 ;
nsteps = _nsteps ;
time = new float [ nsteps ] ;
value = new float [ nsteps ] ;
prev_pitchenv = 0x80 ;
previous_value = 0.0f ;
for ( int i = 0 ; i < nsteps ; i++ )
time [ i ] = value [ i ] = 0.0 ;
replay_mode = _rm ;
}
~slEnvelope ()
{
if ( ref_count != 0 && __slPendingError == NULL )
__slPendingError =
"slEnvelope: FATAL ERROR - Application deleted an envelope while it was playing.\n" ;
delete [] time ;
delete [] value ;
}
void ref () { ref_count++ ; }
void unRef () { ref_count-- ; }
int getPlayCount () const { return ref_count ; }
void applyToLPFilter ( unsigned char *dst,
unsigned char *src,
int nframes, int start ) ;
void setStep ( int n, float _time, float _value )
{
if ( n >= 0 && n < nsteps )
{
time [ n ] = _time ;
value [ n ] = _value ;
}
}
float getStepValue ( int s ) const { return value [ s ] ; }
float getStepTime ( int s ) const { return time [ s ] ; }
int getNumSteps () const { return nsteps ; }
float getValue ( float _time ) const ;
void applyToPitch ( Uchar *dst, slPlayer *src, int nframes, int start, int next_env ) ;
void applyToInvPitch ( Uchar *dst, slPlayer *src, int nframes, int start, int next_env ) ;
void applyToVolume ( Uchar *dst, Uchar *src, int nframes, int start ) ;
void applyToInvVolume ( Uchar *dst, Uchar *src, int nframes, int start ) ;
} ;
#define SL_MAX_PRIORITY 16
#define SL_MAX_SAMPLES 32
#define SL_MAX_CALLBACKS (SL_MAX_SAMPLES * 2)
#define SL_MAX_ENVELOPES 32
#define SL_MAX_MIXERINPUTS 16
enum slEnvelopeType
{
SL_PITCH_ENVELOPE , SL_INVERSE_PITCH_ENVELOPE ,
SL_VOLUME_ENVELOPE, SL_INVERSE_VOLUME_ENVELOPE,
SL_FILTER_ENVELOPE, SL_INVERSE_FILTER_ENVELOPE,
SL_PAN_ENVELOPE , SL_INVERSE_PAN_ENVELOPE ,
SL_ECHO_ENVELOPE , SL_INVERSE_ECHO_ENVELOPE ,
SL_NULL_ENVELOPE
} ;
struct slPendingCallBack
{
slCallBack callback ;
slSample *sample ;
slEvent event ;
int magic ;
} ;
class slPlayer
{
protected:
slEnvelope *env [ SL_MAX_ENVELOPES ] ;
slEnvelopeType env_type [ SL_MAX_ENVELOPES ] ;
int env_start_time [ SL_MAX_ENVELOPES ] ;
slReplayMode replay_mode ;
slPreemptMode preempt_mode ;
slSampleStatus status ;
int priority ;
slCallBack callback ;
int magic ;
virtual void low_read ( int nframes, Uchar *dest ) = 0 ;
virtual void skip ( int nframes ) = 0 ;
public:
slPlayer ( slReplayMode rp_mode = SL_SAMPLE_ONE_SHOT,
int pri = 0, slPreemptMode pr_mode = SL_SAMPLE_DELAY,
int _magic = 0, slCallBack cb = NULL )
{
magic = _magic ;
callback = cb ;
for ( int i = 0 ; i < SL_MAX_ENVELOPES ; i++ )
{
env [ i ] = NULL ;
env_type [ i ] = SL_NULL_ENVELOPE ;
}
status = SL_SAMPLE_WAITING ;
replay_mode = rp_mode ;
preempt_mode = pr_mode ;
priority = pri ;
}
virtual ~slPlayer () ;
slPreemptMode getPreemptMode () const { return preempt_mode ; }
int getPriority () const
{
return ( isRunning() &&
preempt_mode == SL_SAMPLE_CONTINUE ) ? (SL_MAX_PRIORITY+1) :
priority ;
}
void addEnvelope ( int i, slEnvelope *_env, slEnvelopeType _type ) ;
virtual void pause ()
{
if ( status != SL_SAMPLE_DONE )
status = SL_SAMPLE_PAUSED ;
}
virtual void resume ()
{
if ( status == SL_SAMPLE_PAUSED )
status = SL_SAMPLE_RUNNING ;
}
virtual void reset ()
{
status = SL_SAMPLE_WAITING ;
}
virtual void start ()
{
status = SL_SAMPLE_RUNNING ;
}
virtual void stop ()
{
status = SL_SAMPLE_DONE ;
}
int getMagic () const { return magic ; }
int isWaiting () const { return status == SL_SAMPLE_WAITING ; }
int isPaused () const { return status == SL_SAMPLE_PAUSED ; }
int isRunning () const { return status == SL_SAMPLE_RUNNING ; }
int isDone () const { return status == SL_SAMPLE_DONE ; }
virtual int preempt ( int delay ) ;
virtual slSample *getSample () const = 0 ;
void read ( int nframes, Uchar *dest, int next_env = 0 ) ;
} ;
class MODfile ;
class slMODPlayer : public slPlayer
{
MODfile *mf ;
void low_read ( int nframes, Uchar *dest ) ;
void init ( const char *fname ) ;
public:
slMODPlayer ( const char *fname, slReplayMode rp_mode = SL_SAMPLE_ONE_SHOT,
int pri = 0, slPreemptMode pr_mode = SL_SAMPLE_DELAY,
int _magic = 0, slCallBack cb = NULL ) :
slPlayer ( rp_mode, pri, pr_mode, _magic, cb )
{
init ( fname ) ;
reset () ;
}
~slMODPlayer () ;
int preempt ( int delay ) ;
virtual slSample *getSample () const { return NULL ; }
void skip ( int nframes ) ;
} ;
class slSamplePlayer : public slPlayer
{
int lengthRemaining ; /* Sample frames remaining until repeat */
Uchar *bufferPos ; /* Sample frame to replay next */
slSample *sample ;
void low_read ( int nframes, Uchar *dest ) ;
public:
slSamplePlayer ( slSample *s, slReplayMode rp_mode = SL_SAMPLE_ONE_SHOT,
int pri = 0, slPreemptMode pr_mode = SL_SAMPLE_DELAY,
int _magic = 0, slCallBack cb = NULL ) :
slPlayer ( rp_mode, pri, pr_mode, _magic, cb )
{
sample = s ;
if ( sample ) sample -> ref () ;
reset () ;
}
~slSamplePlayer () ;
int preempt ( int delay ) ;
void reset ()
{
slPlayer::reset () ;
lengthRemaining = sample->getLength () ;
bufferPos = sample->getBuffer () ;
}
void start ()
{
slPlayer::start () ;
lengthRemaining = sample->getLength () ;
bufferPos = sample->getBuffer () ;
}
void stop ()
{
slPlayer::stop () ;
lengthRemaining = 0 ;
bufferPos = NULL ;
}
slSample *getSample () const { return sample ; }
void skip ( int nframes ) ;
} ;
class slScheduler : public slDSP
{
slPendingCallBack pending_callback [ SL_MAX_CALLBACKS ] ;
int num_pending_callbacks ;
float safety_margin ;
int mixer_buffer_size, mixer_gain ;
float seconds_per_buffer;
Uchar *mixer_buffer ;
Uchar *mixer_inputs [ SL_MAX_MIXERINPUTS + 1 ] ;
Uchar *mixer ;
int amount_left ;
slPlayer *player [ SL_MAX_SAMPLES ] ;
slPlayer *music ;
void init () ;
void realUpdate ( int dump_first = SL_FALSE ) ;
void initBuffers () ;
int now ;
static slScheduler *current ;
public:
slScheduler ( int _rate = SL_DEFAULT_SAMPLING_RATE ) : slDSP ( _rate, SL_FALSE, 8 ) { init () ; }
slScheduler ( const char *device,
int _rate = SL_DEFAULT_SAMPLING_RATE ) : slDSP ( device, _rate, SL_FALSE, 8 ) { init () ; }
~slScheduler () ;
static slScheduler *getCurrent () { return current ; }
int getTimeNow () const { return now ; }
float getElapsedTime ( int then ) const { return (float)(now-then)/(float)getRate() ; }
void setMaxConcurrent ( int mc );
int getMaxConcurrent () const ;
void flushCallBacks () ;
void addCallBack ( slCallBack c, slSample *s, slEvent e, int m ) ;
void update () { realUpdate ( SL_FALSE ) ; }
void dumpUpdate () { realUpdate ( SL_TRUE ) ; }
void resumeSample ( slSample *s = NULL, int magic = 0 ) ;
void resumeMusic ( int magic = 0 ) ;
void pauseSample ( slSample *s = NULL, int magic = 0 ) ;
void pauseMusic ( int magic = 0 ) ;
void stopSample ( slSample *s = NULL, int magic = 0 ) ;
void stopMusic ( int magic = 0 ) ;
void addSampleEnvelope ( slSample *s = NULL, int magic = 0,
int slot = 1, slEnvelope *e = NULL,
slEnvelopeType t = SL_VOLUME_ENVELOPE ) ;
void addMusicEnvelope ( int magic = 0,
int slot = 1, slEnvelope *e = NULL,
slEnvelopeType t = SL_VOLUME_ENVELOPE ) ;
int loopSample ( slSample *s, int pri = 0,
slPreemptMode mode = SL_SAMPLE_MUTE,
int magic = 0, slCallBack cb = NULL ) ;
int loopMusic ( const char *fname, int pri = 0,
slPreemptMode mode = SL_SAMPLE_MUTE,
int magic = 0, slCallBack cb = NULL ) ;
int playSample ( slSample *s, int pri = 1,
slPreemptMode mode = SL_SAMPLE_ABORT,
int magic = 0, slCallBack cb = NULL ) ;
int playMusic ( const char *fname, int pri = 1,
slPreemptMode mode = SL_SAMPLE_ABORT,
int magic = 0, slCallBack cb = NULL ) ;
void setSafetyMargin ( float seconds ) { safety_margin = seconds ; }
} ;
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,531 @@
/*
PLIB - A Suite of Portable Game Libraries
Copyright (C) 1998,2002 Steve Baker
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For further information visit http://plib.sourceforge.net
$Id: ssgParser.cxx 2117 2007-09-13 23:21:09Z fayjf $
*/
//
// File parser for SSG/PLIB
// Written by Dave McClurg (dpm@efn.org) in Feb-2000
// extended by Wolfram Kuss (w_kuss@rz-online.de) in Nov-2000
// This is mainly an lexical analyzer that extracts tokens from ascii-files
// Be sure to read the ssg-documentation, especially the chapter
// on loaders/writers
#define AM_IN_SSGPARSER_CXX 1
#include "ssgLocal.h"
#include "ssgParser.h"
static _ssgParserSpec default_spec =
{
"\r\n\t ", // delim_chars_skipable
0, // delim_chars_non_skipable
NULL, // pre_processor
0, // open_brace_chars
0, // close_brace_chars
'"', // quote_char
0, // comment_char
"//" // comment_string
} ;
// Output an error
void _ssgParser::error( const char *format, ... )
{
char msgbuff[ 255 ];
va_list argp;
char* msgptr = msgbuff;
if (linenum)
{
msgptr += sprintf ( msgptr,"%s, line %d: ",
path, linenum );
}
va_start( argp, format );
vsprintf( msgptr, format, argp );
va_end( argp );
ulSetError ( UL_WARNING, "%s", msgbuff ) ;
}
// Output a message
void _ssgParser::message( const char *format, ... )
{
char msgbuff[ 255 ];
va_list argp;
char* msgptr = msgbuff;
if (linenum)
{
msgptr += sprintf ( msgptr,"%s, line %d: ",
path, linenum );
}
va_start( argp, format );
vsprintf( msgptr, format, argp );
va_end( argp );
ulSetError ( UL_DEBUG, "%s", msgbuff ) ;
}
// Opens the file and does a few internal calculations based on the spec.
int _ssgParser::openFile( const char* fname, const _ssgParserSpec* _spec )
// returns TRUE on success
{
if ( !_spec ) _spec = &default_spec ;
if ( _spec->comment_string != NULL )
{ assert ( _spec->comment_string [0] != 0 );
}
memset(this,0,sizeof(_ssgParser));
memcpy( &spec, _spec, sizeof(spec) );
ssgGetCurrentOptions () -> makeModelPath ( path, fname ) ;
fileptr = fopen( path, "rb" );
if ( ! fileptr )
{
error("cannot open file: %s",path);
return FALSE;
}
eof = FALSE;
// Calculate anyDelimiter and return.
anyDelimiter[0] = 0;
int length = 0;
if ( spec.delim_chars_skipable != NULL )
{ length +=strlen ( spec.delim_chars_skipable);
strcat(anyDelimiter, spec.delim_chars_skipable);
}
if ( spec.delim_chars_non_skipable != NULL )
{ length += strlen ( spec.delim_chars_non_skipable ) ;
strcat ( anyDelimiter, spec.delim_chars_non_skipable ) ;
}
if ( spec.open_brace_chars != NULL )
{ length +=strlen ( spec.open_brace_chars );
strcat ( anyDelimiter, spec.open_brace_chars );
}
if ( spec.close_brace_chars != NULL )
{ length +=strlen ( spec.close_brace_chars ) ;
strcat ( anyDelimiter, spec.close_brace_chars ) ;
}
assert ( length < MAX_DELIMITER_CHARS );
return TRUE;
}
void _ssgParser::closeFile()
{
fclose( fileptr ) ;
fileptr = 0 ;
}
static char *EOF_string = "EOF reached";
static char *EOL_string = "EOL reached";
char* _ssgParser::getNextToken( const char* name )
// Fetches next token, even if it has to read over some empty or comment-only lines to get to it.
// Never returns NULL. Returns EOF_string on EOF.
{
while(!( curtok < numtok ))
{ //int startLevel = level;
//ulSetError(UL_DEBUG, "Forcing!");
if(getLine( -999 ) == NULL) // -999
{ if ( name )
error("missing %s",name) ;
return EOF_string;
}
assert(curtok==1);
curtok=0; // redo the get one token that getLine does
}
char* token = 0 ;
assert ( curtok < numtok );
token = tokptr [ curtok++ ] ;
return(token) ;
}
char *_ssgParser::peekAtNextToken( const char* name )
// Like getNextToken, but doesn't remove the token from the input stream
{
while(!( curtok < numtok ))
{ //int startLevel = level;
//ulSetError(UL_DEBUG, "Forcing!");
if(getLine( -999 ) == NULL) // -999
{ if ( name )
error("missing %s",name) ;
return EOF_string;
}
assert(curtok==1);
curtok=0; // redo the get one token that getLine does
}
char* token = 0 ;
assert ( curtok < numtok );
token = tokptr [ curtok ] ;
return(token) ;
}
int _ssgParser::getNextFloat( SGfloat &retVal, const char* name )
// returns TRUE on success
{
char *endptr, *token = getNextToken(name);
retVal = SGfloat(strtod( token, &endptr));
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain a floating point number but contains %s",name, token) ;
return FALSE;
}
}
int _ssgParser::getNextInt( int & retVal, const char* name )
// returns TRUE on success
{
char *endptr, *token = getNextToken(name);
retVal = int(strtol( token, &endptr, 10));
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain an integer number but contains %s",name, token) ;
return FALSE;
}
}
int _ssgParser::getNextString(char *&retVal, const char* name ) // returns TRUE on success
// wk: This is only for strings where we know they are inside spec.quote_chars, correct?
{
char *token = getNextToken( NULL );
if ( spec.quote_char && *token == spec.quote_char )
{
//knock off the quotes
token++ ;
int len = strlen( token ) ;
if (len > 0 && token[len-1] == spec.quote_char)
token[len-1] = 0;
}
if( name != NULL && strcmp( token, name ) )
{
error("Expected %s but got %s instead", name, token) ;
return FALSE;
}
retVal = token;
return TRUE;
}
int _ssgParser::getNextUInt( unsigned int & retVal, const char* name )
// returns TRUE on success
{ char *endptr, *token = getNextToken(name);
retVal = (unsigned int)(strtol( token, &endptr, 10));
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain an integer number but contains %s",name, token) ;
return FALSE;
}
}
void _ssgParser::expectNextToken( const char* name )
// Swallows the next token. If it is not name, then there is an error message
{
char* token = getNextToken(name);
if (strcmp(token,name))
error("missing %s",name) ;
}
// internal function. A token consisting of a single char has been found.
// This is copied to a new buffer, so that I have the space to add the 0.
void _ssgParser::addOneCharToken ( char *ptr )
{
assert( (long)onechartokenbuf_ptr- (long)onechartokenbuf < 4096 ) ; // Buffer overflow
onechartokenbuf_ptr [ 0 ] = *ptr;
onechartokenbuf_ptr [ 1 ] = 0;
tokptr [ numtok++ ] = onechartokenbuf_ptr;
onechartokenbuf_ptr += 2; // prepare for nect onechartoken
}
static const char *mystrchr( const char *string, int c )
// like strchr, but string may be NULL
{
if (string == NULL )
return NULL;
else
return strchr( string, c );
}
// gets the next line (no matter where it is), without tokenizing it
// useful for parsing text-formatted files which are identified by
// comments at the very beginning
char* _ssgParser::getRawLine()
// return NULL on eof
{
tokbuf[0]=0;
//get the next line with something on it
if ( fgets ( linebuf, sizeof(linebuf), fileptr ) == NULL )
{
eol = TRUE;
eof = TRUE;
return(0) ;
}
memcpy( tokbuf, linebuf, sizeof(linebuf) ) ;
return tokbuf;
}
// wk: This works and is IMHO robust.
// However, I feel it could be smaller, more elegant and readable.
char* _ssgParser::getLine( int startLevel )
// return NULL on eof or if (level < startLevel)
{
// throw away old tokens
tokbuf [ 0 ] = 0 ;
numtok = 0 ;
curtok = 0 ;
eol = FALSE;
onechartokenbuf_ptr = onechartokenbuf ;
//get the next line with something on it
char* ptr = tokbuf , *tptr;
while ( *ptr == 0 )
{
linenum++ ;
if ( fgets ( linebuf, sizeof(linebuf), fileptr ) == NULL )
{ eol = TRUE;
eof = TRUE;
return(0) ;
}
if(spec.pre_processor != NULL)
spec.pre_processor (linebuf);
memcpy( tokbuf, linebuf, sizeof(linebuf) ) ;
ptr = tokbuf ;
// check for comments
tptr=strchr(tokbuf, spec.comment_char);
if ( tptr != NULL )
*tptr = 0;
if ( spec.comment_string != NULL )
{
tptr=strstr(tokbuf, spec.comment_string);
if ( tptr != NULL )
*tptr = 0;
}
//skip delim_chars
if ( spec.delim_chars_skipable != NULL )
while ( *ptr && strchr(spec.delim_chars_skipable,*ptr) )
ptr++ ;
}
//tokenize the line
numtok = 0 ;
while ( *ptr )
{
//skip delim_chars
if ( spec.delim_chars_skipable != NULL )
while ( *ptr && strchr(spec.delim_chars_skipable,*ptr) )
ptr++ ;
if ( *ptr == 0 )
break; // only skipable stuff left, dont create another token.
// now unnessary?:
if ( *ptr == spec.comment_char )
{
*ptr = 0 ;
break;
}
//count the token
tokptr [ numtok++ ] = ptr ;
//handle quoted string
if ( spec.quote_char && *ptr == spec.quote_char )
{
ptr++ ;
while ( *ptr && *ptr != spec.quote_char )
ptr++ ;
}
//adjust level
if ( spec.open_brace_chars && *ptr && mystrchr(spec.open_brace_chars,*ptr) )
level++ ;
else if ( spec.close_brace_chars && *ptr && mystrchr(spec.close_brace_chars,*ptr) )
level-- ;
else
//find end of token
while ( *ptr && !strchr(anyDelimiter,*ptr) )
ptr++ ;
if ( *ptr != 0 )
if ( ptr == tokptr [ numtok-1 ] )
{ // we dont want tokens of length zero
assert(NULL==mystrchr(spec.delim_chars_skipable,*ptr));
// ptr is non-skipable, return it as token of length one
numtok--; // remove zero-length token
addOneCharToken ( ptr ) ; // and add new token instead
*ptr++ = 0;
continue;
}
//mark end of token
if( *ptr && ( mystrchr(spec.delim_chars_non_skipable,*ptr)
|| mystrchr(spec.open_brace_chars,*ptr)
|| mystrchr(spec.close_brace_chars,*ptr) ) )
{
// ptr is non-skipable, return it as token of length one
// additional to the one already in tokptr [ numtok-1 ].
addOneCharToken ( ptr ) ;
*ptr++ = 0;
}
if ( spec.delim_chars_skipable != NULL )
while ( *ptr && strchr(spec.delim_chars_skipable,*ptr) )
*ptr++ = 0 ;
}
if (level >= startLevel)
return parseToken (0) ;
return 0 ;
}
char* _ssgParser::parseToken( const char* name )
// returns the next token from the current line.
// Never returns NULL, but may return EOL_string
{
char* token = EOL_string ;
if ( curtok < numtok )
token = tokptr [ curtok++ ] ;
else
{ eol = TRUE;
if ( name )
error("missing %s",name) ;
}
return(token) ;
}
int _ssgParser::parseString(char *&retVal, const char* name ) // returns TRUE on success
// wk: This is only for strings where we know they are inside spec.quote_chars, correct?
{
char* token = EOL_string ;
retVal = EOL_string ;
if ( curtok >= numtok )
{ eol = TRUE;
if ( name )
error("missing %s",name) ;
return FALSE;
}
if ( numtok > 0 && spec.quote_char && *tokptr [ curtok ] == spec.quote_char )
{
token = tokptr [ curtok++ ] ;
//knock off the quotes
token++ ;
int len = strlen (token) ;
if (len > 0 && token[len-1] == spec.quote_char)
token[len-1] = 0 ;
}
else
{ if ( name )
error("missing %s",name) ;
return FALSE;
}
retVal = token;
return TRUE;
}
int _ssgParser::parseDouble( double &retVal, const char* name )
// returns TRUE on success
{
char *endptr, *token = parseToken(name);
retVal = strtod( token, &endptr);
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain a floating point number but contains %s",name, token) ;
return FALSE;
}
}
int _ssgParser::parseFloat( SGfloat &retVal, const char* name )
// returns TRUE on success
{
char *endptr, *token = parseToken(name);
retVal = SGfloat(strtod( token, &endptr));
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain a floating point number but contains %s",name, token) ;
return FALSE;
}
}
int _ssgParser::parseInt(int &retVal, const char* name )
// returns TRUE on success
{
char *endptr, *token = parseToken(name);
retVal = int(strtol( token, &endptr, 10));
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain an integer number but contains %s",name, token) ;
return FALSE;
}
}
int _ssgParser::parseUInt(unsigned int &retVal, const char* name )
// returns TRUE on success
{
char *endptr, *token = parseToken(name);
long l = strtol( token, &endptr, 10);
if (l<0)
message("The field %s should contain an UNSIGNED integer number but contains %s",name, token) ;
retVal = (unsigned int)(l);
if ( (endptr == NULL) || (*endptr == 0))
return TRUE;
else
{ error("The field %s should contain an integer number but contains %s",name, token) ;
return FALSE;
}
}
void _ssgParser::expect( const char* name )
// Swallows the next token. If it is not name, then there is an error message
{
char* token = parseToken(name);
if (strcmp(token,name))
error("missing %s",name) ;
}