home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / Test Code Folder / Problem 05 Test Code / Main.cp
Encoding:
Text File  |  1998-05-25  |  3.4 KB  |  131 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. static OSErr WriteEntryToHandle( Handle output, UInt32 field_count, StringsHandle entry )
  11. {
  12.     OSErr err;
  13.     Str255 field;
  14.     unsigned char *p;
  15.     long i;
  16.  
  17.     if ( entry ) {
  18.         HLock( entry );
  19.         p = (unsigned char *) *entry;
  20.         for ( i = 1; i <= field_count; i++ ) {
  21.             BlockMoveData( p, field, *p + 1 );
  22.             p = p + *p + 1;
  23.             ProblemP2CString( &field );
  24.             err = ProblemWriteStringToHandle( output, (char *)field );
  25.             if ( err != noErr ) break;
  26.             if ( i < field_count ) {
  27.                 err = ProblemWriteStringToHandle( output, "," );
  28.                 if ( err != noErr ) break;
  29.             }
  30.         }
  31.         HUnlock( entry );
  32.         if ( err != noErr ) return err;
  33.         err = ProblemWriteLineToHandle( output, "" );
  34.     } else {
  35.         err = ProblemWriteLineToHandle( output, "Null Entry" );
  36.     }
  37.     return err;
  38. }
  39.  
  40. pascal OSErr CheckDatabase( const FSSpec* infile, const FSSpec* outfile )
  41. {
  42.     OSErr err;
  43.     Handle data;
  44.     Handle result;
  45.     char line[MAX_LINE_LEN];
  46.     char *linep;
  47.     char command[MAX_LINE_LEN];
  48.     char numbers[MAX_LINE_LEN];
  49.     Str255 field;
  50.     UInt32 field_count,i;
  51.     DatabaseHandle database;
  52.     StringsHandle entry;
  53.     
  54.     result = NewHandle( 0 );
  55.     err = MemError();
  56.     ProblemLogError( err, "CheckDatabase: NewHandle: result" );
  57.  
  58.     err = ProblemFileRead( infile, &data );
  59.     ProblemLogError( err, "CheckDatabase: ProblemFileRead" );
  60.     if ( err == noErr ) {
  61.         linep = line;
  62.         if ( !ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) || !ProblemGetUInt32( &linep, &field_count ) ) {
  63.             err = -1;
  64.             ProblemLogError( err, "CheckDatabase: field_count" );
  65.         }
  66.     }
  67.     if ( err == noErr ) {
  68.         DatabaseInit( &database, field_count );
  69.         
  70.         while ( (err == noErr) && ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) ) {
  71.             linep = line;
  72.             ProblemGetCString( &linep, command, MAX_LINE_LEN );
  73.             if ( strcmp( command, "ADD" ) == 0 ) {
  74.                 entry = NewHandle( 0 );
  75.                 err = MemError();
  76.                 ProblemLogError( err, "CheckDatabase: NewHandle: entry" );
  77.                 for ( i = 1; i <= field_count; i++ ) {
  78.                     ProblemGetString( &linep, field );
  79.                     err = PtrAndHand( field, entry, field[0] + 1 );
  80.                     if ( err != noErr ) break;
  81.                     ProblemLogError( err, "CheckDatabase: PtrAndHand" );
  82.                 }
  83.                 if ( err == noErr ) {
  84.                     DatabaseAddEntry( database, entry );
  85.                 }
  86.                 DisposeHandle( entry );
  87.             } else if ( strcmp( command, "FIND" ) == 0 ) {
  88.                 ProblemGetUInt32( &linep, &i );
  89.                 ProblemGetString( &linep, field );
  90.                 DatabaseFindEntry( database, i, field, &entry );
  91.                 err = WriteEntryToHandle( result, field_count, entry );
  92.             } else if ( strcmp( command, "DELETE" ) == 0 ) {
  93.                 ProblemGetUInt32( &linep, &i );
  94.                 ProblemGetString( &linep, field );
  95.                 DatabaseDeleteEntry( database, i, field );
  96.             } else if ( strcmp( command, "COUNT" ) == 0 ) {
  97.                 i = DatabaseCount( database );
  98.                 sprintf( numbers, "%ld", i );
  99.                 err = ProblemWriteLineToHandle( result, numbers );
  100.             }
  101.             if ( err != noErr ) break;
  102.         }
  103.         
  104.         if ( err == noErr ) {
  105.             err = ProblemWriteLineToHandle( result, "Dumping" );
  106.             for ( i = 1; i <= DatabaseCount( database ); i++ ) {
  107.                 DatabaseGetIndEntry( database, i, &entry );
  108.                 err = WriteEntryToHandle( result, field_count, entry );
  109.             }
  110.         }
  111.         
  112.         DisposeHandle( database );
  113.     }
  114.     
  115.     err = ProblemFileWrite( outfile, result );
  116.     
  117.     DisposeHandle( result );
  118.     DisposeHandle( data );
  119.     return err;
  120. }
  121.  
  122. int main()
  123. {
  124.     printf( "Starting\n" );
  125.     
  126.     ProblemRunFileTests( CheckDatabase );
  127.  
  128.     return 0;
  129. }
  130.  
  131.