/************************************************************************* * Copyright (C) 1992 Embedded Solutions. * All rights reserved * * file: noname.c * * purpose: Create marathon time record in a manner * compatible with the BRMR program, as per the GETX07 * program. * * functions: * main - Program entry point * get_team_number - Get a team number from the user * read_field_data - Read in existing team info * display_help - Display help to the user * * programmer: David Purdie (DDP) * * revision date by reason * 00.0 19-Mar-94 DDP created * **************************************************************************/ #include #include #include #include #define TRUE 1 #define FALSE 0 /* ** Global variables */ int leg = 0; char ans; FILE *file; char filename[12]; char *leg_table[] = { "Run", "Canoe", "Swim", "Horse", "Cycle", }; time_t teamtime[1000]; /* ** Forward prototypes */ void read_field_data( FILE * file ); void display_help( int ); /*======================================================================== * * Program entry point * * Purpose: * This function is called start the program * * Parameters: * argc - Number of arguments * argv - Address of argument pointers * * Returns: * Nothing * Exit with valid exit codes * *========================================================================*/ main( int argc, char *argv[] ) { char input[30]; int team; time_t *teamptr; time_t curtime; char data_entered = FALSE; /* ** Display HELP */ display_help( 0 ); /* ** Determine the leg to get data for */ if( argc >= 2 ) leg = atoi( argv[1] ); /* ** Initialise the team data base */ for( team = 0, teamptr = teamtime; team < 999; team++, teamptr++ ) { *teamptr = 0; } do { while( leg == 0 ) { printf( "Which leg to collect data for : " ); scanf( "%d", &leg ); fflush( stdin ); } if( ( leg == 0 ) || ( leg > sizeof( leg_table ) / sizeof( leg_table[0] ) ) ) { printf( "Invalid leg\n" ); leg = 0; continue; } sprintf( filename, "LEG%1.1d", leg ); file = fopen( filename, "at+" ); if( file == NULL ) { printf( "Error opening file\n" ); exit( 1 ); } fseek( file, 0, SEEK_END ); if( ftell( file ) != 0 ) { printf( "Leg data already exists. Append new data ? " ); scanf( "%c", &ans ); fflush( stdin ); if( toupper( ans ) != 'Y' ) { fclose( file ); leg = 0; continue; } read_field_data( file ); } } while( leg == 0 ); /* ** Main loop: Collect team numbers from the uer */ while( 1 ) { /* ** Get the team number */ printf( "Enter team > " ); team = get_team_number(); teamptr = &teamtime[team % 1000]; /* ** Team-999 Exit */ if( team == 999 ) break; /* ** Just display the time */ else if( team == 0 ) { time( &curtime ); printf( " %8.8s\n", ctime( &curtime ) + 11 ); continue; } /* ** Save data to disk */ else if( team == -1 ) { if( data_entered ) { fflush( file ); fclose( file ); file = fopen( filename, "at+" ); if( file == NULL ) { printf( " Panic Cannot re-open data file\n" ); exit( 1 ); } else printf( " Data saved to disk\n" ); data_entered = FALSE; } else printf( "\r" ); continue; } else if( team == -2 ) display_help( 1 ); /* ** Determine operation ** 1..998 Enter team data */ else if( team < 999 ) { if( *teamptr ) { printf( " Already entered at %8.8s\a\n", ctime( teamptr ) + 11 ); } else { time( teamptr ); printf( " %8.8s\n", ctime( teamptr ) + 11 ); fprintf( file, "%3.3d %8.8s\n", team, ctime( teamptr ) + 11 ); data_entered = TRUE; } } /* ** Overwrite team information ** 1001..1998 */ else if( team < 1999 ) { if( *teamptr ) { printf( " Was %8.8s. ", ctime( teamptr ) + 11 ); time( teamptr ); printf( " Now %8.8s\a\n", ctime( teamptr ) + 11 ); fprintf( file, "%3.3d %8.8s\n", team, ctime( teamptr ) + 11 ); data_entered = TRUE; } } /* ** Display team time ** 2001..2998 */ else if( team < 2999 ) { if( *teamptr ) { printf( " Entered as %8.8s\a\n", ctime( teamptr ) + 11 ); } else printf( " Has not been entered\n" ); } /* ** Invalid team number */ else { printf( " Invalid team number\a\n" ); } } /* ** Happy ending. Save the data and exit ** As some data may be duplicated on disk we should re-write the ** Entire file */ fclose( file ); printf( "\nCreating ordered data file\n" ); file = fopen( "LEG.WRK", "wtt" ); if( file == NULL ) { printf( "Error creating linear data record of times\n" ); exit( 1 ); } for( team = 1, teamptr = &teamtime[1]; team < 999; team++, teamptr++ ) { if( *teamptr ) fprintf( file, "%3.3d %8.8s\n", team, ctime( teamptr ) + 11 ); } fclose( file ); remove( filename ); if( rename( "LEG.WRK", filename ) != 0 ) { perror( "Error renaming work file" ); exit( 1 ); } exit( 0 ); } /*======================================================================== * * Get a team number from the user * * Purpose: * This function is called to get a team number from the user * * Parameters: * None * * Returns: * Team number * 0 - Just display time * -1 - Time to save data * -2 - Display help * *===========================================================================*/ int get_team_number(void) { int team = 0; int num_digits = 0; int ch; time_t dummy; time_t last_time = time( &dummy ); /* ** Collect a number */ for( ;; ) { if( kbhit() ) { ch = getch(); /* ** Process the character */ if( ch == '?' && num_digits == 0 ) return ( -2 ); if( ( ch == '\r' ) || ( ch == '\n' ) || ( ch == ' ' ) || ( ch == ',' ) || ( ch == 0x1b ) || ( ch == 0x03 ) ) return ( team ); else if( ( ch >= '0' && ch <= '9' ) && ( num_digits < 4 ) ) { num_digits += 1; team = team * 10 + ch - '0'; putch( ch ); } else if( ( ch == '\b' || ch == 0x177 ) && ( num_digits ) ) { num_digits -= 1; team = team / 10; cputs( "\b \b" ); } else putch( '\a' ); } /* ** No key available ** Check time and save data if required */ else if( num_digits == 0 ) { if( time( &dummy ) - last_time > 20 ) return ( -1 ); } } } /*======================================================================== * * Read in existing team info * * Purpose: * This function is called to Read in existing team info * * Parameters: * file - File pointer * * Returns: * Nothing * *========================================================================*/ void read_field_data( FILE * file ) { int cnt; int team, hh, mm, ss; /* ** Start from the begining */ fseek( file, 0, SEEK_SET ); while( feof( file ) == 0 ) { cnt = fscanf( file, "%d %d:%d:%d", &team, &hh, &mm, &ss ); if( cnt == 4 && team > 0 && team < 9999 ) { teamtime[team] = ( long ) ss + ( ( long ) mm * 60 ) + ( ( long ) hh * 60 * 60 ); } } /* ** Ensure file is at end */ fseek( file, 0, SEEK_END ); } /*======================================================================== * * Display help to the user * * Purpose: * This function is called to Display help to the user * * Parameters: * type 1 - Short help * - Long help * * Returns: * Nothing * *========================================================================*/ void display_help( int type ) { printf( "\n" ); if( type != 1 ) { printf ( "This program will simulate marathon data collection on a PC\n" ); printf( "This program will save data to disk periodically\n" ); printf( "Commands are:\n" ); } printf( " nnn - Register team time\n" ); printf( "1nnn - Force team time, override any existing time\n" ); printf( "2nnn - Display team time\n" ); printf( " 999 - Exit program (DO NOT USE CONTROL-C)\n" ); } /********************************* EOF ***********************************/