home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / Test Code Folder / Problem 09 Test Code / Main.cp
Encoding:
Text File  |  1998-06-18  |  2.6 KB  |  104 lines  |  [TEXT/CWIE]

  1. #include "Solution.h"
  2.  
  3. #include "ProblemUtils.h"
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <Files.h>
  8. #include <Errors.h>
  9.  
  10. Handle indata;
  11. Boolean realcorrect;
  12.  
  13. static pascal UInt32 GetNextChar( Handle state_machine, UInt32 curstate )
  14. {
  15.     char line[MAX_LINE_LEN];
  16.     char *linep;
  17.     UInt32 thechar;
  18.     
  19.     linep = line;
  20.     if ( !realcorrect || !ProblemReadLineFromHandle( indata, line, MAX_LINE_LEN )
  21.             || !ProblemGetUInt32( &linep, &thechar ) ) {
  22.         realcorrect = false;
  23.         return 0;
  24.     }
  25.     return thechar;
  26. }
  27.  
  28. pascal OSErr CheckStateMachine( const FSSpec* infile, const FSSpec* outfile, Boolean *correct )
  29. {
  30.     OSErr err;
  31.     char line[MAX_LINE_LEN];
  32.     char *linep;
  33.     char command[MAX_LINE_LEN];
  34.     UInt32 state_count;
  35.     UInt32 char_count;
  36.     UInt32 from,to,start,fin;
  37.     UInt32 stop_state;
  38.     Handle state_machine;
  39.     
  40.     realcorrect = false;
  41.     
  42.     err = ProblemFileRead( infile, &indata );
  43.     ProblemLogError( err, "CheckStateMachine: ProblemFileRead" );
  44.     if ( err == noErr ) {
  45.         linep = line;
  46.         if ( !ProblemReadLineFromHandle( indata, line, MAX_LINE_LEN )
  47.                 || !ProblemGetUInt32( &linep, &state_count )
  48.                 || !ProblemGetUInt32( &linep, &char_count ) ) {
  49.             err = -1;
  50.             ProblemLogError( err, "CheckStateMachine: state_count,char_count" );
  51.         }
  52.     }
  53.     if ( err == noErr ) {
  54.         StateMachineInit( &state_machine, state_count, char_count );
  55.         
  56.         realcorrect = true;
  57.         while ( (err == noErr) && realcorrect && ProblemReadLineFromHandle( indata, line, MAX_LINE_LEN ) ) {
  58.             linep = line;
  59.             ProblemGetCString( &linep, command, MAX_LINE_LEN );
  60.             if ( strcmp( command, "ADD" ) == 0 ) {
  61.                 if ( ProblemGetUInt32( &linep, &from )
  62.                         && ProblemGetUInt32( &linep, &to )
  63.                         && ProblemGetUInt32( &linep, &start )
  64.                         && ProblemGetUInt32( &linep, &fin ) ) {
  65.                     AddTransition( state_machine, from, to, start, fin );
  66.                 } else {
  67.                     err = -1;
  68.                     ProblemLogError( err, "CheckStateMachine: ADD?" );
  69.                 }
  70.             } else if ( strcmp( command, "RUN" ) == 0 ) {
  71.                 ProblemGetUInt32( &linep, &start );
  72.                 RunStateMachine( state_machine, start, GetNextChar, &stop_state );
  73.                 linep = line;
  74.                 realcorrect = false;
  75.                 if ( ProblemReadLineFromHandle( indata, line, MAX_LINE_LEN ) ) {
  76.                     ProblemGetCString( &linep, command, MAX_LINE_LEN );
  77.                     if ( strcmp( command, "STOP" ) == 0 ) {
  78.                         if ( ProblemGetUInt32( &linep, &fin ) && (fin == stop_state) ) {
  79.                             realcorrect = true;
  80.                         }
  81.                     }
  82.                 }
  83.             } else {
  84.                 err = -2;
  85.                 ProblemLogError( err, "CheckStateMachine: What?" );
  86.             }
  87.             if ( err != noErr ) break;
  88.         }
  89.         
  90.         DisposeHandle( state_machine );
  91.     }
  92.     *correct = realcorrect;
  93.     return err;
  94. }
  95.  
  96. int main()
  97. {
  98.     printf( "Starting\n" );
  99.     
  100.     ProblemRunSingleFileTests( CheckStateMachine );
  101.  
  102.     return 0;
  103. }
  104.