home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-05-25 | 3.4 KB | 131 lines | [TEXT/CWIE] |
- #include "Solution.h"
-
- #include "ProblemUtils.h"
-
- #include <stdio.h>
- #include <string.h>
- #include <Files.h>
- #include <Errors.h>
-
- static OSErr WriteEntryToHandle( Handle output, UInt32 field_count, StringsHandle entry )
- {
- OSErr err;
- Str255 field;
- unsigned char *p;
- long i;
-
- if ( entry ) {
- HLock( entry );
- p = (unsigned char *) *entry;
- for ( i = 1; i <= field_count; i++ ) {
- BlockMoveData( p, field, *p + 1 );
- p = p + *p + 1;
- ProblemP2CString( &field );
- err = ProblemWriteStringToHandle( output, (char *)field );
- if ( err != noErr ) break;
- if ( i < field_count ) {
- err = ProblemWriteStringToHandle( output, "," );
- if ( err != noErr ) break;
- }
- }
- HUnlock( entry );
- if ( err != noErr ) return err;
- err = ProblemWriteLineToHandle( output, "" );
- } else {
- err = ProblemWriteLineToHandle( output, "Null Entry" );
- }
- return err;
- }
-
- pascal OSErr CheckDatabase( const FSSpec* infile, const FSSpec* outfile )
- {
- OSErr err;
- Handle data;
- Handle result;
- char line[MAX_LINE_LEN];
- char *linep;
- char command[MAX_LINE_LEN];
- char numbers[MAX_LINE_LEN];
- Str255 field;
- UInt32 field_count,i;
- DatabaseHandle database;
- StringsHandle entry;
-
- result = NewHandle( 0 );
- err = MemError();
- ProblemLogError( err, "CheckDatabase: NewHandle: result" );
-
- err = ProblemFileRead( infile, &data );
- ProblemLogError( err, "CheckDatabase: ProblemFileRead" );
- if ( err == noErr ) {
- linep = line;
- if ( !ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) || !ProblemGetUInt32( &linep, &field_count ) ) {
- err = -1;
- ProblemLogError( err, "CheckDatabase: field_count" );
- }
- }
- if ( err == noErr ) {
- DatabaseInit( &database, field_count );
-
- while ( (err == noErr) && ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) ) {
- linep = line;
- ProblemGetCString( &linep, command, MAX_LINE_LEN );
- if ( strcmp( command, "ADD" ) == 0 ) {
- entry = NewHandle( 0 );
- err = MemError();
- ProblemLogError( err, "CheckDatabase: NewHandle: entry" );
- for ( i = 1; i <= field_count; i++ ) {
- ProblemGetString( &linep, field );
- err = PtrAndHand( field, entry, field[0] + 1 );
- if ( err != noErr ) break;
- ProblemLogError( err, "CheckDatabase: PtrAndHand" );
- }
- if ( err == noErr ) {
- DatabaseAddEntry( database, entry );
- }
- DisposeHandle( entry );
- } else if ( strcmp( command, "FIND" ) == 0 ) {
- ProblemGetUInt32( &linep, &i );
- ProblemGetString( &linep, field );
- DatabaseFindEntry( database, i, field, &entry );
- err = WriteEntryToHandle( result, field_count, entry );
- } else if ( strcmp( command, "DELETE" ) == 0 ) {
- ProblemGetUInt32( &linep, &i );
- ProblemGetString( &linep, field );
- DatabaseDeleteEntry( database, i, field );
- } else if ( strcmp( command, "COUNT" ) == 0 ) {
- i = DatabaseCount( database );
- sprintf( numbers, "%ld", i );
- err = ProblemWriteLineToHandle( result, numbers );
- }
- if ( err != noErr ) break;
- }
-
- if ( err == noErr ) {
- err = ProblemWriteLineToHandle( result, "Dumping" );
- for ( i = 1; i <= DatabaseCount( database ); i++ ) {
- DatabaseGetIndEntry( database, i, &entry );
- err = WriteEntryToHandle( result, field_count, entry );
- }
- }
-
- DisposeHandle( database );
- }
-
- err = ProblemFileWrite( outfile, result );
-
- DisposeHandle( result );
- DisposeHandle( data );
- return err;
- }
-
- int main()
- {
- printf( "Starting\n" );
-
- ProblemRunFileTests( CheckDatabase );
-
- return 0;
- }
-
-