/* * Sequence.c * Author: Hannah C. Tang (hctang@cs) * * Implementation of public and private functions for the * Sequence struct * * $Id: Sequence.c,v 1.1 2001/12/18 00:24:16 cse466_t Exp $ * */ #include #include #include "Sequence.h" #include "common.h" /* * PRIVATE: * * Constants for internal use * * Note: these constants *must* be kept the same size as the * Sequence struct! */ static const unsigned char MAX_LENGTH = 0xFF; static const unsigned char MAX_PERIOD = 0x0F; static const unsigned char MAX_MODE = 0x0F; /* This is D190. 1D190 is Unicode for the "mezzo" charecter - 'm' */ const struct Sequence MODE_BEGIN = { 209, 9, 0 }; /* This is D18F. 1D18F is Unicode for the "piano" charecter - 'p' */ const struct Sequence PROGRAM_BEGIN= { 209, 8, 15 }; /* This is D10B. 1D10B is Unicode for the "repeat" sign */ const struct Sequence INF_LOOP_BEGIN = { 209, 0, 11 }; /* This is D109. 1D109 is Unicode for the "dal signa" charecter */ const struct Sequence GOTO_LOOP = { 209, 0, 9 }; /* Dynamically allocate Sequence struct(s) */ struct Sequence* Sequence_mallocOne( void ) { struct Sequence *pS = ( struct Sequence* ) malloc( sizeof( struct Sequence ) ); if( pS == NULL ) { fprintf( stderr, "Out of memory allocating a Sequence!\n" ); exit( 0 ); } return pS; } struct Sequence* Sequence_mallocMany( unsigned int numSequences ) { struct Sequence *pS; if( numSequences == 0 ) { return NULL; } pS = ( struct Sequence* ) malloc( sizeof( struct Sequence ) * numSequences ); if( pS == NULL ) { fprintf( stderr, "Out of memory allocating %d Sequences!\n", numSequences ); exit( 0 ); } return pS; } /* Free dynamically allocated structs */ void Sequence_free( struct Sequence *pS ) { if( pS != NULL ) { free( pS ); } } void Sequence_print( const struct Sequence *pS, FILE *stream ) { fprintf( stream, "(l: %d, p: %d, m: %d)", pS->length, pS->period, pS->mode ); } unsigned int Sequence_getLength( const struct Sequence *pS ) { return pS->length; } Bool Sequence_setLength( struct Sequence *pS, unsigned int length ) { /* This length will overflow our length field. Return a failure */ if( length > MAX_LENGTH ) { return Bool_false; } pS->length = length; return Bool_true; } unsigned int Sequence_getPeriod( const struct Sequence *pS ) { return pS->period; } Bool Sequence_setPeriod( struct Sequence *pS, unsigned int period ) { /* This period will overflow our period field. Return a failure */ if( period > MAX_PERIOD ) { return Bool_false; } pS->period = period; return Bool_true; } unsigned int Sequence_getMode( const struct Sequence *pS ) { return pS->mode; } Bool Sequence_setMode( struct Sequence *pS, unsigned int mode ) { /* This mode will overflow our mode field. Return a failure */ if( mode > MAX_MODE ) { return Bool_false; } pS->mode = mode; return Bool_true; } void Sequence_getAll( const struct Sequence *pS, unsigned int *pLength, unsigned int *pPeriod, unsigned int *pMode ) { if( pLength != NULL ) { *pLength = Sequence_getLength( pS ); } if( pPeriod != NULL ) { *pPeriod = Sequence_getPeriod( pS ); } if( pMode != NULL ) { *pMode = Sequence_getMode( pS ); } return; } Bool Sequence_setAll( struct Sequence *pS, unsigned int length, unsigned int period, unsigned int mode ) { Bool result; result = Sequence_setLength( pS, length ); if( result == Bool_false ) { return result; } result = Sequence_setPeriod( pS, period ); if( result == Bool_false ) { return result; } Sequence_setMode( pS, mode ); if( result == Bool_false ) { return result; } return result; }