/************************************************************************* * Copyright (C) 1995 Embedded Solutions * All rights reserved * * file: src\legtime.c * * purpose: Leg time calculations * * functions * set_legs - Menu: Setup leg times * tm_lgs - Set up the leg start times for leg * leg_start - Set up the leg start times * tm_fixedstart - Set the start time for a specific leg * tm_staggered - Set a staggered start time for a specific leg * tm_clearleg - Clear start times on a specific leg * leg_ini - Initialize all stored times * tm_init - Initialize all team data * tm_gen - Generate dummy team names * legs_start_report - Generate starters report * ls_timer - print the current leg entry * ls_team - print next ( numeric ) leg entry * sort_legs - Qsort callback function * * programmer: David Purdie * * revision date by reason * 11-Oct-89 DDP leg-3 starttime. Now has an option to generate * a printed report of teams and times * * 21-May-90 MV now able to set start time and generate * a printed report for any leg * 00.0 27/01/95 DDP Tidies up the program and formatted the file * **************************************************************************/ #include "QDebug" #include "mainwindow.h" #include "consts.h" #include "structs.h" #include "proto.h" void ls_timer_short( t_legs * ptr, int num, bool suppress ); void ls_team_short( t_legs * ptr, int num, bool suppress ); int sort_team( const void * aa, const void * bb ); /*======================================================================== * * Set up the leg start times for leg * * Purpose: * This function is called to Set up the leg start times for leg * The start time is based on category information * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void tm_lgs1(int leg, time_t starttime, time_t delta, bool report_it, bool clear_it) { t_legs *data; /* Address of table */ t_legs *dptr; /* Moving pointer */ int num_records; /* Number of records in array */ int i; team_type team_buf; /* ** Read existing data into memory */ data = ( t_legs * ) calloc( ( unsigned ) ( config.max_team - config.min_team + 2 ), sizeof( t_legs ) ); /* Fetch memory */ if( !data ) { qDebug( "No memory for report" ); return; } /* * Extract the required data from the data base * Only save that information required for the operation * - The end time of the previous leg */ dptr = data; num_records = 0; for( i = config.min_team; i <= config.max_team; i++ ) { if( valid_field( i ) && g_record( i, &team_buf ) ) { dptr->numb = team_buf.numb; dptr->start = team_buf.leg[leg-1].end; dptr->flags = team_buf.flags; dptr++; num_records++; } } /* * Sort the data into some logical order */ qsort( ( char * ) data, num_records, sizeof( t_legs ), sort_legs ); /* ** Update the team information based on the start order ** Ignore validity flags as the data will have been sorted with ** these in mind. */ for( dptr = data, i = 0; i < num_records; i++, dptr++ ) { if( valid_field( dptr->numb ) ) { g_record( dptr->numb, &team_buf ); team_buf.leg[leg].start = starttime; starttime += delta; team_buf.leg[leg].manual = TRUE; set_times( &team_buf ); test_times( &team_buf, 0 ); put_team_record( dptr->numb, &team_buf ); } } /* ** Release the data */ free( data ); if( report_it ) { legs_start_report(leg); } if ( clear_it ) { tm_clearleg_specified( leg, TRUE); } } /*======================================================================== * * Set up the leg start times for leg * * Purpose: * This function is called to Set up the leg start times for leg * The start time is based on current placing with: * A constant offset added ( ie: lunch) * A fixed increment * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void tm_lgs(int leg, time_t delta, bool report_it, bool clear_it) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { g_record( team, &team_buf ); if( team_buf.flags.valid && team_buf.leg[leg - 1].end > 0 ) { team_buf.leg[leg].start = team_buf.leg[leg - 1].end + delta; team_buf.leg[leg].manual = TRUE; } else team_buf.leg[leg].start = ( time_t ) - 1; set_times( &team_buf ); test_times( &team_buf, 0 ); put_team_record( team, &team_buf ); } team++; } if( report_it ) legs_start_report(leg); if ( clear_it ) tm_clearleg_specified( leg, TRUE); } /*======================================================================== * * Set up the leg start times * * Purpose: * This function is called to Set up the leg start times * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void leg_start(void) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { ( void ) g_record( team, &team_buf ); if( team_buf.teamclass > 0 && team_buf.teamclass <= config.num_class ) team_buf.leg[0].start = config.team_class[team_buf.teamclass - 1].start; else team_buf.leg[0].start = ( time_t ) - 1; team_buf.leg[1].manual = 0; team_buf.leg[1].start = team_buf.leg[0].start; team_buf.leg[0].l_place = 0; team_buf.leg[0].le_place = 0; team_buf.leg[0].lec_place = 0; team_buf.leg[0].lc_place = 0; team_buf.leg[0].manual = FALSE; set_times( &team_buf ); test_times( &team_buf, 0 ); put_team_record( team, &team_buf ); } team++; } } /*======================================================================== * * Set the start time for a specific leg * * Purpose: * This function is called to set the start time for a specific * leg to a specified and fixed time * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void tm_fixedstart(int leg, time_t starttime, bool report_it, bool clear_it) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { g_record( team, &team_buf ); if( team_buf.flags.valid ) { team_buf.leg[leg].start = starttime; team_buf.leg[leg].manual = TRUE; } else team_buf.leg[leg].start = ( time_t ) - 1; set_times( &team_buf ); test_times( &team_buf, 0 ); put_team_record( team, &team_buf ); } team++; } if( report_it ) legs_start_report(leg); if ( clear_it ) tm_clearleg_specified( leg, TRUE ); } /*======================================================================== * * Set a staggered start time for a specific leg * * Purpose: * This function is called to set the start time for a specific * leg to a specified and a staggered time * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void tm_staggered(int leg, time_t starttime, time_t delta, bool report_it, bool clear_it ) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { g_record( team, &team_buf ); if( team_buf.flags.valid ) { team_buf.leg[leg].start = starttime; starttime += delta; team_buf.leg[leg].manual = TRUE; } else team_buf.leg[leg].start = ( time_t ) - 1; set_times( &team_buf ); test_times( &team_buf, 0 ); put_team_record( team, &team_buf ); } team++; } if( report_it ) legs_start_report(leg); if ( clear_it ) tm_clearleg_specified( leg, TRUE ); } /*======================================================================== * * Clear start times on a specific leg * * Purpose: * This function is called to clear start times on a specifc leg * * Parameters: * cleg - Leg to clear * manual - Force manual, else leave alone * * Returns: * Nothing * *========================================================================*/ void tm_clearleg_specified(int leg, bool manual) { team_type team_buf; /* ** Validate the users argument */ if( leg == 0 ) return; /* Null leg - just exit */ if( leg > config.num_legs ) /* Valid leg number - Exit loop */ return; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { g_record( team, &team_buf ); team_buf.leg[leg].manual = manual; team_buf.leg[leg].start = ( time_t ) -1; set_times( &team_buf ); test_times( &team_buf, 0 ); put_team_record( team, &team_buf ); } team++; } } /*======================================================================== * * Initialize all stored times * * Purpose: * This function is called to Initialize all stored times * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void leg_ini(void) { int i; team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { ( void ) g_record( team, &team_buf ); for( i = 0; i <= MAX_LEGS; i++ ) { team_buf.leg[i].start = team_buf.leg[i].end = team_buf.leg[i].elapsed = ( time_t ) - 1; team_buf.leg[i].l_place = 0; team_buf.leg[i].le_place = 0; team_buf.leg[i].lec_place = 0; team_buf.leg[i].lc_place = 0; team_buf.leg[i].manual = FALSE; } if( team_buf.teamclass > 0 && team_buf.teamclass <= config.num_class ) team_buf.leg[0].start = config.team_class[team_buf.teamclass - 1].start; else team_buf.leg[0].start = ( time_t ) - 1; team_buf.leg[1].start = team_buf.leg[0].start; team_buf.flags.disqualified = FALSE; team_buf.flags.non_equestrian = FALSE; put_team_record( team, &team_buf ); } team++; } } /*======================================================================== * * Initialize all team data * * Purpose: * This function is called to Initialize all team data * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void tm_init(void) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { clr_team( team, &team_buf ); put_team_record( team, &team_buf ); } team++; } } /*======================================================================== * * Generate dummy team names * * Purpose: * This function is called to Generate dummy team names * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void tm_gen(void) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { ( void ) g_record( team, &team_buf ); team_buf.flags.valid = TRUE; sprintf( team_buf.name, "Team - %4.4d", team ); team_buf.teamclass = 1; /* Set default class */ put_team_record( team, &team_buf ); } team++; } } void tm_recalcElapsed (int leg) { team_type team_buf; int team = config.min_team; /* Team we are working with */ while( team <= config.max_team ) { if( valid_field( team ) ) { g_record( team, &team_buf ); set_times( &team_buf ); test_times( &team_buf, leg ); put_team_record( team, &team_buf ); } team++; } } /*======================================================================== * * Generate starters report * * Purpose: * This routine is used to generate a list of leg start times * This system can cope with a break at the start of each leg * * Parameters: * None * * Returns: * Nothing * *========================================================================*/ void legs_start_report(int leg) { int i; t_legs *data; /* Address of table */ t_legs *dptr; /* Moving pointer */ int last_team; int num_records = 0; /* Number of records in array */ char l_s[40]; /* Name of start time file */ team_type team_buf; data = ( t_legs * ) calloc( ( unsigned ) ( config.max_team - config.min_team + 2 ), sizeof( t_legs ) ); /* Fetch memory */ if( !data ) { MainWindow::showMessage("No memory for report"); return; } /* * Extract the required data from the data base * Only save that information required for the operation */ dptr = data; for( i = config.min_team; i <= config.max_team; i++ ) /* Print team order data */ { if( valid_field( i ) && g_record( i, &team_buf ) ) { dptr->numb = team_buf.numb; dptr->start = team_buf.leg[leg].start; dptr->flags = team_buf.flags; dptr++; num_records++; } } /* * Sort the data into some logical order */ qsort( ( char * ) data, num_records, sizeof( t_legs ), sort_legs ); /* ** Now print the data on the printer ** - Generte the name of the printer file ** - Open the printer */ { sprintf( l_s, "l_%d", leg ); if( !open_printer( "", l_s, 80, text, "Starters Report" ) ) return; /* * Print out the header for the file */ print( "\nStarters report for leg:%d %-*s\n", leg, 18, config.leg_name[leg - 1] ); print( "Time order listing\n\n" ); print_underline (TRUE); print( "%-8s %-5s %-8s | %-5s %-8s", "Time", "Team", "DeltaT", "Team", "Time" ); print_underline (FALSE); print( "\n" ); last_team = 0; for( dptr = data, i = 0; i < num_records; i++ ) { ls_timer( dptr++, i, FALSE ); print( " | " ); ls_team( &last_team, data ); print( "\n" ); } print( "\nTeams without marked times will be started when ALL marked teams\n" ); print( "have been started.\n" ); close_printer(); /* ** Tell the main system about this new report */ MainWindow::registerReport(getPrinterFile(), "Leg Start"); } /* ** Alternate report format */ { int c1, c2, c3,c4, c1stop; sprintf( l_s, "l2_%d", leg ); if( !open_printer( "", l_s, 80, text, "Starters Report (2)" ) ) return; /* * Print out the header for the file */ print( "\nStarters report for leg:%d %-*s\n", leg, 18, config.leg_name[leg - 1] ); print( "Starting order listing\n\n" ); print_underline (TRUE); print( "%-8s %-5s | ", "Time", "Team"); print( "%-8s %-5s | ", "Time", "Team"); print( "%-8s %-5s | ", "Time", "Team"); print( "%-8s %-5s" , "Time", "Team"); print_underline (FALSE); print( "\n" ); /* ** Print in Two columns ** Need to figure out where the 2nd column starts */ c1 = 0; c2 = c1stop = (num_records + 3)/4; c3 = c2 * 2; c4 = c2 * 3; for( ; c1 < c1stop; c1++, c2++, c3++, c4++ ) { ls_timer_short( &data[c1], c1, c1 > c1stop ); print( " | " ); ls_timer_short( &data[c2], c2, c1 > c1stop); print( " | " ); ls_timer_short( &data[c3], c3, c1 > c1stop); print( " | " ); ls_timer_short( &data[c4], c4, c4 >= num_records); print( "\n" ); } print( "\nTeams without marked times will be started when ALL marked teams\n" ); print( "have been started.\n" ); close_printer(); /* ** Tell the main system about this new report */ MainWindow::registerReport(getPrinterFile(), "Leg Start(2)"); } /* ** Alternate report format - by Team Number */ #if 1 /* * Sort the data into some logical order */ qsort( ( char * ) data, num_records, sizeof( t_legs ), sort_team ); { int c1, c2, c3,c4, c1stop; sprintf( l_s, "l3_%d", leg ); if( !open_printer( "", l_s, 80, text, "Starters Report (3)" ) ) return; /* * Print out the header for the file */ print( "\nStarters report for leg:%d %-*s\n", leg, 18, config.leg_name[leg - 1] ); print( "Team order listing\n\n" ); print_underline (TRUE); print( "%-5s %-8s | ", "Team", "Time"); print( "%-5s %-8s | ", "Team", "Time"); print( "%-5s %-8s | ", "Team", "Time"); print( "%-5s %-8s" , "Team", "Time"); print_underline (FALSE); print( "\n" ); /* ** Print in Two columns ** Need to figure out where the 2nd column starts */ c1 = 0; c2 = c1stop = (num_records + 3)/4; c3 = c2 * 2; c4 = c2 * 3; for( ; c1 < c1stop; c1++, c2++, c3++, c4++ ) { ls_team_short( &data[c1], c1, c1 > c1stop ); print( " | " ); ls_team_short( &data[c2], c2, c1 > c1stop); print( " | " ); ls_team_short( &data[c3], c3, c1 > c1stop); print( " | " ); ls_team_short( &data[c4], c4, c4 >= num_records); print( "\n" ); } print( "\nTeams without marked times will be started when ALL marked teams\n" ); print( "have been started.\n" ); close_printer(); /* ** Tell the main system about this new report */ MainWindow::registerReport(getPrinterFile(), "Leg Start(3)"); } #endif /* ** Release the resources ** Release the data */ free( data ); } /*======================================================================== * * print the current leg entry * * Purpose: * This function is a helper routine to print the current leg entry * in time : Team number order * * Parameters: * ptr Pointer to entry * num Entry index * supress True: Entry is empty * * Returns: * Nothing * *========================================================================*/ void ls_timer( t_legs * ptr, int num, bool suppress ) { time_t time; time_t delta; bool flags; if ( suppress ) { print( "%-8s %-5s", "",""); print( " %-8s", ""); return; } if( num == 0 ) delta = ( time_t ) - 1; else delta = ptr[0].start - ptr[-1].start; time = ptr->start; flags = ptr->flags.disqualified; if(flags) time = ( time_t ) - 1; print( "%-8s %-5d", time_fa( time, flags ), ptr->numb ); print( " %-8s", time_fa( delta, TRUE ) ); } /*======================================================================== * * print the current leg entry - short form * * Purpose: * This function is a helper routine to print the current leg entry * in time : Team number order * * Parameters: * ptr Pointer to entry * num Entry index * supress True: Entry is empty * * Returns: * Nothing * *========================================================================*/ void ls_timer_short( t_legs * ptr, int num, bool suppress ) { time_t time; bool flags; if ( suppress ) { print( "%-8s %-5s", "",""); return; } time = ptr->start; flags = ptr->flags.disqualified; if(flags) time = ( time_t ) - 1; print( "%-8s %-5d", time_fa( time, flags ), ptr->numb ); } /*======================================================================== * * print next ( numeric ) leg entry * * Purpose: * This helper function is called to print next ( numeric ) leg entry * in Team # time order * * Parameters: * last Last one found * Used to relocate my self * data Start of data * * Returns: * Nothing * *========================================================================*/ void ls_team( int *last, t_legs * data ) { t_legs *min; time_t time; bool flags; for( min = 0; data->numb; data++ ) { if( data->numb > *last && ( min == 0 || data->numb < min->numb ) ) min = data; } *last = min->numb; /* Save current team as done */ time = min->start; flags = min->flags.disqualified; if( flags ) time = ( time_t ) - 1; print( "%-5d %-8s", min->numb, time_fa( time, flags ) ); } /*======================================================================== * * print next ( numeric ) leg entry - Short Form * * Purpose: * This helper function is called to print next ( numeric ) leg entry * in Team # time order * * Parameters: * last Last one found * Used to relocate my self * data Start of data * * Returns: * Nothing * *========================================================================*/ void ls_team_short( t_legs * ptr, int num, bool suppress ) { time_t time; bool flags; if ( suppress ) { print( "%-5s %-8s", "",""); return; } time = ptr->start; flags = ptr->flags.disqualified; if(flags) time = ( time_t ) - 1; print( "%-5d %-8s", ptr->numb, time_fa( time, flags ) ); } /*======================================================================== * * Qsort callback function * * Purpose: * This function is provided to Qsort() to sort the teams * * Parameters: * a - Leg entry to compare * b - Leg entry to compare * * * Returns: * -1 a < b * 0 a = b * 1 a > b * *========================================================================*/ int sort_legs( const void * aa, const void * bb ) { const t_legs * a = (const t_legs *)aa; const t_legs * b = (const t_legs *)bb; int a_bad; int b_bad; a_bad = a->flags.disqualified || a->start <= 0; b_bad = b->flags.disqualified || b->start <= 0; if( a_bad || b_bad ) /* Valid data has precedence */ { if( a_bad && b_bad ) return ( a->numb - b->numb ); else return ( a_bad ? 1 : -1 ); } if( a->start == b->start ) return ( a->numb - b->numb ); else { if( ( a->start > 0 ) && ( b->start > 0 ) ) return ( ( int ) ( a->start - b->start ) ); else return ( ( a->start > 0 ) ? -1 : 1 ); } } /*======================================================================== * * Qsort callback function * * Purpose: * This function is provided to Qsort() to sort the teams * * Parameters: * a - Leg entry to compare * b - Leg entry to compare * * * Returns: * -1 a < b * 0 a = b * 1 a > b * *========================================================================*/ int sort_team( const void * aa, const void * bb ) { const t_legs * a = (const t_legs *)aa; const t_legs * b = (const t_legs *)bb; return ( a->numb - b->numb ); } /********************************* EOF ***********************************/