Subversion Repositories svn1

Rev

Rev 127 | Rev 131 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
91 - 1
/*************************************************************************
2
*           Copyright (C) 1995 Embedded Solutions
3
*                       All rights reserved
4
*
5
* file:     src\legtime.c
6
*
7
* purpose:  Leg time calculations
8
*
9
* functions
10
*   set_legs                    - Menu: Setup leg times
11
*   tm_lgs                      - Set up the leg start times for leg
12
*   leg_start                   - Set up the leg start times
13
*   tm_fixedstart               - Set the start time for a specific leg
14
*   tm_staggered                - Set a staggered start time for a specific leg
15
*   tm_clearleg                 - Clear start times on a specific leg
16
*   leg_ini                     - Initialize all stored times
17
*   tm_init                     - Initialize all team data
18
*   tm_gen                      - Generate dummy team names
19
*   legs_start_report           - Generate starters report
20
*   ls_timer                    - print the current leg entry
21
*   ls_team                     - print next ( numeric ) leg entry
22
*   sort_legs                   - Qsort callback function
23
*
24
* programmer: David Purdie
25
*
26
* revision  date        by      reason
27
*           11-Oct-89   DDP     leg-3 starttime. Now has an option to generate
28
*                               a printed report of teams and times
29
*
30
*           21-May-90   MV      now able to set start time and generate
31
*                               a printed report for any leg
32
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
33
*
34
**************************************************************************/
35
 
36
#include    "consts.h"
37
#include    "structs.h"
38
#include    "proto.h"
39
 
40
#if defined(HI_TECH_C) || defined(__TURBOC__)
41
#include    <alloc.h>
42
#endif
43
 
44
menu_table  leg_menu[] = {
45
    { '1', "Set start time from category", leg_start },
46
    { '2', "Clear all leg times", leg_ini },
47
    { '3', "Reset team information", tm_init },
48
    { '4', "Generate dummy team names", tm_gen },
130 - 49
    //{ '5', "Set calculated leg start", tm_lgs },
91 - 50
    { '6', "Set ordered incremental leg start", tm_lgs1 },
51
    { '7', "Set staggered start time", tm_staggered },
52
    { '8', "Set fixed start time", tm_fixedstart },
53
    { '9', "Clear single leg start times", tm_clearleg },
54
    { 'q', "Return to main menu", 0 },
55
    { '\0' }
56
};
57
 
58
/*========================================================================
59
 *
60
 *  Menu: Setup leg times
61
 *
62
 *  Purpose:
63
 *      This function is called to produce the menu that will
64
 *      setup leg times
65
 *
66
 *  Parameters:
67
 *      None
68
 *
69
 *  Returns:
70
 *      Nothing
71
 *
72
 *========================================================================*/
73
 
74
void set_legs(void)
75
{
76
    do_menu( "Leg time setup", "Select option", leg_menu );
77
}
78
 
79
/*========================================================================
80
 *
81
 *  Set up the leg start times for leg
82
 *
83
 *  Purpose:
84
 *      This function is called to Set up the leg start times for leg
85
 *      The start time is based on category information
86
 *
87
 *  Parameters:
88
 *      None
89
 *
90
 *  Returns:
91
 *      Nothing
92
 *
93
 *========================================================================*/
94
 
95
void tm_lgs1(void)
96
{
97
    time_t      delta;                           /* The time delta */
98
    time_t      starttime;
99
    int         report_it = FALSE;               /* Report the operation */
100
    int         clear_it = FALSE;
101
 
102
    t_legs     *data;                            /* Address of table */
103
    t_legs     *dptr;                            /* Moving pointer */
104
    int         num_records;                     /* Number of records in array */
105
    int         i;
106
 
107
    cur( 0, 5 );
108
    while( TRUE )
109
    {
110
        leg = 0;
111
        d_field( 0, 5, "Enter leg to set start time :", D_NUMBER, 1,
112
                 ( char * ) &leg, TRUE, M_UPDATE );
113
        if( leg == 0 )
114
            return;                              /* Null leg - just exit */
115
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
116
            break;
117
        beep();                                  /* Make a noise and wait for valid number */
118
    }
119
 
120
    cur( 0, 5 );
121
    printf ( "Setting start times for all valid teams from %-d to %-d for leg %d\n",
122
                config.min_team, config.max_team, leg );
123
 
124
    starttime = 0;
125
    do
126
    {
127
        d_time( 0, 6, "Enter the start time : ", &starttime, TRUE );
128
        if( abort_flag )
129
            return;
130
 
131
        if( starttime < 0 )
132
            beep();
133
 
134
    } while( starttime < 0 );
135
 
136
    delta = 0;
137
    do
138
    {
139
        d_time( 0, 7, "Enter the delta time : ", &delta, TRUE );
140
        if( abort_flag )
141
            return;
142
 
143
        if( delta < 0 )
144
            beep();
145
 
146
    } while( delta < 0 );
147
 
148
 
149
    report_it = getyes( "\nGenerate leg start report" );
150
    if( abort_flag )
151
        return;
152
 
153
    clear_it = getyes("Invalidate the leg start times" );
154
    if( abort_flag )
155
        return;
156
 
157
    /*
158
    **  Read existing data into memory
159
    */
160
    data = ( t_legs * ) calloc( ( unsigned ) ( config.max_team - config.min_team + 2 ), sizeof( t_legs ) ); /* Fetch memory */
161
    if( !data )
162
    {
163
        printf( "\nNo memory for report\n" );
164
        return;
165
    }
166
 
167
    /*
168
     * Extract the required data from the data base
169
     * Only save that information required for the operation
170
     *      - The end time of the previous leg
171
     */
172
    dptr = data;
173
    num_records = 0;
174
    for( i = config.min_team; i <= config.max_team; i++ )
175
    {
176
        if( valid_field( i ) && g_record( i, &team_buf ) )
177
        {
178
            dptr->numb = team_buf.numb;
179
            dptr->start = team_buf.leg[leg-1].end;
180
            dptr->flags = team_buf.flags;
181
            dptr++;
182
            num_records++;
183
        }
184
    }
185
 
186
    /*
187
     * Sort the data into some logical order 
188
     */
189
    qsort( ( char * ) data, num_records, sizeof( t_legs ), sort_legs );
190
 
191
    /*
192
    **  Update the team information based on the start order
193
    **  Ignore validity flags as the data will have been sorted with
194
    **  these in mind.
195
    */
196
    for( dptr = data, i = 0; i < num_records; i++, dptr++ )
197
    {
198
        if( valid_field( dptr->numb ) )
199
        {
200
            g_record( dptr->numb, &team_buf );
201
            team_buf.leg[leg].start = starttime;
202
            starttime += delta;
203
            team_buf.leg[leg].manual = TRUE;
204
 
205
            set_times( &team_buf );
206
            test_times( &team_buf, 0 );
207
            put_team_record( dptr->numb, &team_buf );
208
        }
209
 
210
        if( i % 10 == 0 )
211
        {
212
            cur( 0, 8 );
213
            printf( "Upto entry %d", i );
214
            flush_out();
215
        }
216
    }
217
 
218
    /*
219
    **  Release the data
220
    */
221
    free( data );
222
 
223
    if( report_it )
224
        legs_start_report(9);
225
 
226
    if ( clear_it )
227
        tm_clearleg_specified( leg, TRUE, 10 );
228
}
229
 
230
/*========================================================================
231
 *
232
 *  Set up the leg start times for leg
233
 *
234
 *  Purpose:
235
 *      This function is called to Set up the leg start times for leg
236
 *      The start time is based on current placing with:
237
 *          A constant offset added ( ie: lunch)
238
 *          A fixed increment
239
 *
240
 *  Parameters:
241
 *      None
242
 *
243
 *  Returns:
244
 *      Nothing
245
 *
246
 *========================================================================*/
247
 
130 - 248
void tm_lgs(int leg, time_t delta, bool report_it, bool clear_it)
91 - 249
{
250
 
251
    team = config.min_team;                      /* Team we are working with */
252
    while( team <= config.max_team )
253
    {
254
        if( valid_field( team ) )
255
        {
256
            g_record( team, &team_buf );
257
            if( team_buf.flags.valid && team_buf.leg[leg - 1].end > 0 )
258
            {
259
                team_buf.leg[leg].start = team_buf.leg[leg - 1].end + delta;
260
                team_buf.leg[leg].manual = TRUE;
261
            }
262
            else
263
                team_buf.leg[leg].start = ( time_t ) - 1;
264
 
265
            set_times( &team_buf );
266
            test_times( &team_buf, 0 );
267
            put_team_record( team, &team_buf );
268
        }
269
 
270
        if( team % 10 == 0 )
271
        {
130 - 272
 
91 - 273
            printf( "Upto team %d", team );
274
            flush_out();
275
        }
276
        team++;
277
    }
278
 
279
    if( report_it )
130 - 280
        legs_start_report(0);
91 - 281
 
282
    if ( clear_it )
130 - 283
        tm_clearleg_specified( leg, TRUE, 0 );
91 - 284
}
285
 
286
/*========================================================================
287
 *
288
 *  Set up the leg start times
289
 *
290
 *  Purpose:
291
 *      This function is called to Set up the leg start times
292
 *
293
 *  Parameters:
294
 *      None
295
 *
296
 *  Returns:
297
 *      Nothing
298
 *
299
 *========================================================================*/
300
 
301
void leg_start(void)
302
{
303
 
304
    team = config.min_team;                      /* Team we are working with */
126 david 305
//    cur( 0, 5 );
306
//    printf( "Setting start times for all valid teams from %-d to %-d \n",
307
//            config.min_team, config.max_team );
308
//    if( !getyes( "Continue operation" ) )
309
//        return;
91 - 310
 
311
    while( team <= config.max_team )
312
    {
313
        if( valid_field( team ) )
314
        {
315
            ( void ) g_record( team, &team_buf );
316
            if( team_buf.teamclass > 0 && team_buf.teamclass <= config.num_class )
317
                team_buf.leg[0].start =
318
                    config.team_class[team_buf.teamclass - 1].start;
319
            else
320
                team_buf.leg[0].start = ( time_t ) - 1;
321
            team_buf.leg[1].manual = 0;
322
            team_buf.leg[1].start = team_buf.leg[0].start;
323
            team_buf.leg[0].l_place = 0;
324
            team_buf.leg[0].le_place = 0;
325
            team_buf.leg[0].lec_place = 0;
326
            team_buf.leg[0].lc_place = 0;
327
            team_buf.leg[0].manual = FALSE;
328
            set_times( &team_buf );
329
            test_times( &team_buf, 0 );
330
            put_team_record( team, &team_buf );
331
        }
332
        if( team % 10 == 0 )
333
        {
334
            cur( 0, 8 );
335
            printf( "Upto team %d", team );
336
            flush_out();
337
        }
338
        team++;
339
    }
340
}
341
 
342
/*========================================================================
343
 *
344
 *  Set the start time for a specific leg
345
 *
346
 *  Purpose:
347
 *      This function is called to set the start time for a specific
348
 *      leg to a specified and fixed time
349
 *
350
 *  Parameters:
351
 *      None
352
 *
353
 *  Returns:
354
 *      Nothing
355
 *
356
 *========================================================================*/
357
 
358
void tm_fixedstart(void)
359
{
360
    time_t      starttime;                       /* The start time */
361
    int         report_it = FALSE;               /* Report the operation */
362
    int         clear_it = FALSE;
363
    int         line = 5;                        /* Current line */
364
 
365
    cur( 0, 5 );
366
    while( TRUE )
367
    {
368
        leg = 0;
369
        d_field( 0, line, "Enter leg to set start time :", D_NUMBER, 1,
370
                 ( char * ) &leg, TRUE, M_UPDATE );
371
        if( leg == 0 )
372
            return;                              /* Null leg - just exit */
373
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
374
            break;
375
        beep();                                  /* Make a noise and wait for valid number */
376
    }
377
    line++;
378
 
379
 
380
    team = config.min_team;                      /* Team we are working with */
381
    cur( 0, line++ );
382
    printf
383
        ( "Setting start times for all valid teams from %-d to %-d for leg %d\n",
384
          config.min_team, config.max_team, leg );
385
 
386
    starttime = 0;
387
    do
388
    {
389
        d_time( 0, line, "Enter the start time : ", &starttime, TRUE );
390
        if( abort_flag )
391
            return;
392
 
393
        if( starttime < 0 )
394
            beep();
395
 
396
    } while( starttime < 0 );
397
 
398
    line++;
399
    report_it = getyes( "\nGenerate leg start report" );
400
    if( abort_flag )
401
        return;
402
 
403
    line++;
404
    clear_it = getyes("Invalidate the leg start times" );
405
    if( abort_flag )
406
        return;
407
 
408
    while( team <= config.max_team )
409
    {
410
        if( valid_field( team ) )
411
        {
412
            g_record( team, &team_buf );
413
            if( team_buf.flags.valid )
414
            {
415
                team_buf.leg[leg].start = starttime;
416
                team_buf.leg[leg].manual = TRUE;
417
            }
418
            else
419
                team_buf.leg[leg].start = ( time_t ) - 1;
420
 
421
            set_times( &team_buf );
422
            test_times( &team_buf, 0 );
423
            put_team_record( team, &team_buf );
424
        }
425
 
426
        if( team % 10 == 0 )
427
        {
428
            cur( 0, 8 );
429
            printf( "Upto team %d", team );
430
            flush_out();
431
        }
432
        team++;
433
    }
434
 
435
    if( report_it )
436
        legs_start_report(++line);
437
 
438
    if ( clear_it )
439
        tm_clearleg_specified( leg, TRUE, ++line );
440
}
441
 
442
/*========================================================================
443
 *
444
 *  Set a staggered start time for a specific leg
445
 *
446
 *  Purpose:
447
 *      This function is called to set the start time for a specific
448
 *      leg to a specified and a staggered time
449
 *
450
 *  Parameters:
451
 *      None
452
 *
453
 *  Returns:
454
 *      Nothing
455
 *
456
 *========================================================================*/
457
 
458
void tm_staggered(void)
459
{
460
    time_t      starttime;                       /* The start time */
461
    time_t      delta;                           /* The stagger */
462
    int         report_it = FALSE;               /* Report the operation */
463
    int         clear_it = FALSE;
464
    int         line = 5;                        /* Current line */
465
 
466
    cur( 0, line );
467
    while( TRUE )
468
    {
469
        leg = 0;
470
        d_field( 0, 5, "Enter leg to set start time :", D_NUMBER, 1,
471
                 ( char * ) &leg, TRUE, M_UPDATE );
472
        if( leg == 0 )
473
            return;                              /* Null leg - just exit */
474
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
475
            break;
476
        beep();                                  /* Make a noise and wait for valid number */
477
    }
478
    line++;
479
 
480
 
481
    team = config.min_team;                      /* Team we are working with */
482
    cur( 0, line++ );
483
    printf
484
        ( "Setting staggered start times for all valid teams from %-d to %-d for leg %d\n",
485
          config.min_team, config.max_team, leg );
486
 
487
    starttime = 0;
488
    do
489
    {
490
        d_time( 0, line, "Enter the start time : ", &starttime, TRUE );
491
        if( abort_flag )
492
            return;
493
 
494
        if( starttime < 0 )
495
            beep();
496
 
497
    } while( starttime < 0 );
498
    line++;
499
 
500
    delta = 0;
501
    do
502
    {
503
        d_time( 0, line, "Enter the stagger time : ", &delta, TRUE );
504
        if( abort_flag )
505
            return;
506
 
507
        if( delta < 0 )
508
            beep();
509
 
510
    } while( delta < 0 );
511
 
512
    line++;
513
    report_it = getyes( "\nGenerate leg start report" );
514
    if( abort_flag )
515
        return;
516
 
517
    line++;
518
    clear_it = getyes("Invalidate the leg start times" );
519
    if( abort_flag )
520
        return;
521
 
522
    while( team <= config.max_team )
523
    {
524
        if( valid_field( team ) )
525
        {
526
            g_record( team, &team_buf );
527
            if( team_buf.flags.valid )
528
            {
529
                team_buf.leg[leg].start = starttime;
530
                starttime += delta;
531
                team_buf.leg[leg].manual = TRUE;
532
            }
533
            else
534
                team_buf.leg[leg].start = ( time_t ) - 1;
535
 
536
            set_times( &team_buf );
537
            test_times( &team_buf, 0 );
538
            put_team_record( team, &team_buf );
539
        }
540
 
541
        if( team % 10 == 0 )
542
        {
543
            cur( 0, 9 );
544
            printf( "Upto team %d", team );
545
            flush_out();
546
        }
547
        team++;
548
    }
549
 
550
    if( report_it )
551
        legs_start_report(++line);
552
 
553
    if ( clear_it )
554
        tm_clearleg_specified( leg, TRUE,  ++line );
555
}
556
 
557
/*========================================================================
558
 *
559
 *  Clear start times on a specific leg
560
 *
561
 *  Purpose:
562
 *      This function is called to clear start times on a specifc leg
563
 *
564
 *  Parameters:
565
 *      None
566
 *
567
 *  Returns:
568
 *      Nothing
569
 *
570
 *========================================================================*/
571
 
572
void tm_clearleg(void)
573
{
574
 
575
    cur( 0, 5 );
576
    while( TRUE )
577
    {
578
        leg = 0;
579
        d_field( 0, 5, "Enter leg to clear start time :", D_NUMBER, 1,
580
                 ( char * ) &leg, TRUE, M_UPDATE );
581
        if( leg == 0 )
582
            return;                              /* Null leg - just exit */
583
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
584
            break;
585
        beep();                                /* Make a noise and wait for valid number */
586
    }
587
 
588
 
589
    cur( 0, 5 );
590
    printf
591
        ( "Setting start times for all valid teams from %-d to %-d for leg %d\n",
592
          config.min_team, config.max_team, leg );
593
    if( !getyes( "Continue operation" ) )
594
        return;
595
 
596
    tm_clearleg_specified( leg, FALSE, 6 );
597
}
598
 
599
/*========================================================================
600
 *
601
 *  Clear start times on a specific leg
602
 *
603
 *  Purpose:
604
 *      This function is called to clear start times on a specifc leg
605
 *
606
 *  Parameters:
607
 *      cleg                - Leg to clear
608
 *      manual              - Force manual, else leave alone
609
 *      line                - Current display line number
610
 *
611
 *  Returns:
612
 *      Nothing
613
 *
614
 *========================================================================*/
615
 
616
void tm_clearleg_specified(int cleg, bool manual, int line)
617
{
618
 
619
    /*
620
    **  Validate the users argument
621
    */
622
    if( cleg == 0 )
623
        return;                              /* Null leg - just exit */
624
    if( cleg > config.num_legs )             /* Valid leg number - Exit loop */
625
        return;
626
    leg = cleg;
627
 
628
    team = config.min_team;                 /* Team we are working with */
629
    while( team <= config.max_team )
630
    {
631
        if( valid_field( team ) )
632
        {
633
            g_record( team, &team_buf );
634
            team_buf.leg[leg].manual = manual;
635
            team_buf.leg[leg].start = ( time_t ) -1;
636
 
637
            set_times( &team_buf );
638
            test_times( &team_buf, 0 );
639
            put_team_record( team, &team_buf );
640
        }
641
        if( team % 10 == 0 )
642
        {
643
            cur( 0, line );
644
            printf( "Upto team %d", team );
645
            flush_out();
646
        }
647
        team++;
648
    }
649
}
650
 
651
 
652
/*========================================================================
653
 *
654
 *  Initialize all stored times
655
 *
656
 *  Purpose:
657
 *      This function is called to Initialize all stored times
658
 *
659
 *  Parameters:
660
 *      None
661
 *
662
 *  Returns:
663
 *      Nothing
664
 *
665
 *========================================================================*/
666
 
667
void leg_ini(void)
668
{
669
    int         i;
670
 
671
 
672
    team = config.min_team;                      /* Team we are working with */
673
    while( team <= config.max_team )
674
    {
675
        if( valid_field( team ) )
676
        {
677
            ( void ) g_record( team, &team_buf );
678
            for( i = 0; i <= MAX_LEGS; i++ )
679
            {
680
                team_buf.leg[i].start = team_buf.leg[i].end =
681
                    team_buf.leg[i].elapsed = ( time_t ) - 1;
682
                team_buf.leg[i].l_place = 0;
683
                team_buf.leg[i].le_place = 0;
684
                team_buf.leg[i].lec_place = 0;
685
                team_buf.leg[i].lc_place = 0;
686
                team_buf.leg[i].manual = FALSE;
687
            }
688
            if( team_buf.teamclass > 0 && team_buf.teamclass <= config.num_class )
689
                team_buf.leg[0].start =
690
                    config.team_class[team_buf.teamclass - 1].start;
691
            else
692
                team_buf.leg[0].start = ( time_t ) - 1;
693
 
694
            team_buf.leg[1].start = team_buf.leg[0].start;
695
            team_buf.flags.disqualified = FALSE;
696
            team_buf.flags.non_equestrian = FALSE;
697
            put_team_record( team, &team_buf );
698
        }
699
        if( team % 10 == 0 )
700
        {
701
            cur( 0, 8 );
702
            printf( "Upto team %d", team );
703
            flush_out();
704
        }
705
        team++;
706
    }
707
}
708
 
709
/*========================================================================
710
 *
711
 *  Initialize all team data
712
 *
713
 *  Purpose:
714
 *      This function is called to Initialize all team data
715
 *
716
 *  Parameters:
717
 *      None
718
 *
719
 *  Returns:
720
 *      Nothing
721
 *
722
 *========================================================================*/
723
void tm_init(void)
724
{
725
 
726
    team = config.min_team;                      /* Team we are working with */
727
    while( team <= config.max_team )
728
    {
729
        if( valid_field( team ) )
730
        {
731
            clr_team( team, &team_buf );
732
            put_team_record( team, &team_buf );
733
        }
734
        if( team % 10 == 0 )
735
        {
736
            cur( 0, 8 );
737
            printf( "Upto team %d", team );
738
            flush_out();
739
        }
740
        team++;
741
    }
742
}
743
 
744
/*========================================================================
745
 *
746
 *  Generate dummy team names
747
 *
748
 *  Purpose:
749
 *      This function is called to Generate dummy team names
750
 *
751
 *  Parameters:
752
 *      None
753
 *
754
 *  Returns:
755
 *      Nothing
756
 *
757
 *========================================================================*/
758
 
759
void tm_gen(void)
760
{
761
 
762
    team = config.min_team;                      /* Team we are working with */
763
    while( team <= config.max_team )
764
    {
765
        if( valid_field( team ) )
766
        {
767
            ( void ) g_record( team, &team_buf );
768
            team_buf.flags.valid = TRUE;
769
            sprintf( team_buf.name, "Team - %4.4d", team );
770
            team_buf.teamclass = 1;                  /* Set default class */
771
            put_team_record( team, &team_buf );
772
        }
773
        if( team % 10 == 0 )
774
        {
775
            cur( 0, 8 );
776
            printf( "Upto team %d", team );
777
            flush_out();
778
        }
779
        team++;
780
    }
781
}
782
 
783
 
784
/*========================================================================
785
 *
786
 *  Generate starters report
787
 *
788
 *  Purpose:
789
 *      This routine is used to generate a list of leg start times
790
 *      This system can cope with a break at the start of leg
791
 *
792
 *  Parameters:
793
 *      None
794
 *
795
 *  Returns:
796
 *      Nothing
797
 *
798
 *========================================================================*/
799
 
800
void legs_start_report(int line)
801
{
802
    int         i;
803
    t_legs     *data;                            /* Address of table */
804
    t_legs     *dptr;                            /* Moving pointer */
805
    int         last_team;
806
    int         num_records = 0;                 /* Number of records in array */
807
    char        l_s[40];                         /* Name of start time file */
808
 
809
    cur( 0, line );
810
    printf( "Generating Starters report for Leg-%d", leg );
811
 
812
    data = ( t_legs * ) calloc( ( unsigned ) ( config.max_team - config.min_team + 2 ), sizeof( t_legs ) ); /* Fetch memory */
813
 
814
    if( !data )
815
    {
816
        printf( "\nNo memory for report\n" );
817
        return;
818
    }
819
 
820
    /*
821
     * Extract the required data from the data base
822
     * Only save that information required for the operation
823
     */
824
 
825
    dptr = data;
826
    for( i = config.min_team; i <= config.max_team; i++ )   /* Print team order data */
827
    {
828
        if( valid_field( i ) && g_record( i, &team_buf ) )
829
        {
830
            dptr->numb = team_buf.numb;
831
            dptr->start = team_buf.leg[leg].start;
832
            dptr->flags = team_buf.flags;
833
            dptr++;
834
            num_records++;
835
        }
836
    }
837
 
838
 
839
    /*
840
     * Sort the data into some logical order 
841
     */
842
    qsort( ( char * ) data, num_records, sizeof( t_legs ), sort_legs );
843
 
844
    /*
845
    **  Now print the data on the printer
846
    **      - Generte the name of the printer file
847
    **      - Open the printer
848
    */
849
 
850
    sprintf( l_s, "l_%d", leg );
851
    if( !open_printer( "", l_s, 80, text, "Starters Report" ) )
852
        return;
853
 
854
    /*
855
     * Print out the header for the file 
856
     */
857
    print( "\nStarters report for leg:%d %-*s\n", leg, 18, config.leg_name[leg - 1] );
858
    print( "Team order listing\n\n" );
859
 
860
    print_underline (TRUE);
861
    print( "%-8s   %-5s   %-8s   |   %-5s   %-8s", "Time", "Team", "DeltaT", "Team", "Time" );
862
    print_underline (FALSE);
863
    print( "\n" );
864
 
865
    last_team = 0;
866
    for( dptr = data, i = 0; i < num_records; i++ )
867
    {
868
        ls_timer( dptr++, i );
869
        print( "   |   " );
870
        ls_team( &last_team, data );
871
        print( "\n" );
872
    }
873
 
874
    print( "\nTeams without marked times will be started when ALL marked teams\n" );
875
    print( "have been started.\n" );
876
 
877
    /*
878
    **  Release the resources
879
    **      - Printer
880
    **      - Memory
881
    */
882
    close_printer();
883
    free( data );
884
}
885
 
886
/*========================================================================
887
 *
888
 *  print the current leg entry
889
 *
890
 *  Purpose:
891
 *      This function is a helper routine to print the current leg entry
892
 *      in time : Team number order
893
 *
894
 *  Parameters:
895
 *      ptr     Pointer to entry
896
 *      num     Entry index
897
 *
898
 *  Returns:
899
 *      Nothing
900
 *
901
 *========================================================================*/
902
 
903
void ls_timer( t_legs * ptr, int num )
904
{
905
    time_t      time;
906
    time_t      delta;
907
 
908
    if( num == 0 )
909
        delta = ( time_t ) - 1;
910
    else
911
        delta = ptr[0].start - ptr[-1].start;
912
 
913
    time = ptr->start;
914
    if( ptr->flags.disqualified )
915
        time = ( time_t ) - 1;
916
 
917
    print( "%-8s   %-5d", time_fa( time, ptr->flags.disqualified ),
918
           ptr->numb );
919
    print( "   %-8s", time_fa( delta, TRUE ) );
920
}
921
 
922
 
923
/*========================================================================
924
 *
925
 *  print next ( numeric ) leg entry
926
 *
927
 *  Purpose:
928
 *      This helper function is called to print next ( numeric ) leg entry
929
 *      in Team # time order
930
 *
931
 *  Parameters:
932
 *      last        Last one found
933
 *                  Used to relocate my self
934
 *      data        Start of data
935
 *
936
 *  Returns:
937
 *      Nothing
938
 *
939
 *========================================================================*/
940
 
941
void ls_team( int *last, t_legs * data )
942
{
943
    t_legs     *min;
944
    time_t      time;
945
 
946
    for( min = 0; data->numb; data++ )
947
    {
948
        if( data->numb > *last && ( min == 0 || data->numb < min->numb ) )
949
            min = data;
950
    }
951
    *last = min->numb;                           /* Save current team as done */
952
 
953
    time = min->start;
954
    if( min->flags.disqualified )
955
        time = ( time_t ) - 1;
956
    print( "%-5d   %-8s", min->numb,
957
           time_fa( time, min->flags.disqualified ) );
958
}
959
 
960
/*========================================================================
961
 *
962
 *  Qsort callback function
963
 *
964
 *  Purpose:
965
 *      This function is provided to Qsort() to sort the teams
966
 *
967
 *  Parameters:
968
 *      a           - Leg entry to compare
969
 *      b           - Leg entry to compare
970
 *
971
 *
972
 *  Returns:
973
 *      -1      a < b
974
 *       0      a = b
975
 *       1      a > b
976
 *
977
 *========================================================================*/
978
 
979
int sort_legs( const void * aa, const void * bb )
980
{
981
 
982
    const t_legs * a = (const t_legs *)aa;
983
    const t_legs * b = (const t_legs *)bb;
984
 
985
    int         a_bad;
986
    int         b_bad;
987
 
988
    a_bad = a->flags.disqualified || a->start <= 0;
989
    b_bad = b->flags.disqualified || b->start <= 0;
990
 
991
    if( a_bad || b_bad )                         /* Valid data has precedence */
992
    {
993
        if( a_bad && b_bad )
994
            return ( a->numb - b->numb );
995
        else
996
            return ( a_bad ? 1 : -1 );
997
    }
998
 
999
    if( a->start == b->start )
1000
        return ( a->numb - b->numb );
1001
    else
1002
    {
1003
        if( ( a->start > 0 ) && ( b->start > 0 ) )
1004
            return ( ( int ) ( a->start - b->start ) );
1005
        else
1006
            return ( ( a->start > 0 ) ? -1 : 1 );
1007
    }
1008
}
1009
 
1010
/********************************* EOF ***********************************/