Subversion Repositories svn1-original

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 root 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 },
49
    { '5', "Set calculated leg start", tm_lgs },
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("Reset 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, 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
 
248
void tm_lgs(void)
249
{
250
    time_t      delta;                           /* The time delta */
251
    int         report_it = FALSE;               /* Report the operation */
252
    int         clear_it = FALSE;
253
    int         line = 5;                        /* Current line */
254
 
255
    cur( 0, line );
256
    while( TRUE )
257
    {
258
        leg = 0;
259
        d_field( 0, line, "Enter leg to set start time :", D_NUMBER, 1,
260
                 ( char * ) &leg, TRUE, M_UPDATE );
261
        if( leg == 0 )
262
            return;                              /* Null leg - just exit */
263
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
264
            break;
265
        beep();                                  /* Make a noise and wait for valid number */
266
    }
267
 
268
 
269
    cur( 0, ++line );
270
    printf ( "Setting start times for all valid teams from %-d to %-d for leg %d\n",
271
                config.min_team, config.max_team, leg );
272
    delta = 0;
273
    line++;
274
    do
275
    {
276
        d_time( 0, line, "Enter the time difference : ", &delta, TRUE );
277
 
278
        if( abort_flag )
279
            return;
280
 
281
        if( delta < 0 )
282
            beep();
283
 
284
    } while( delta < 0 );
285
 
286
    line++;
287
    report_it = getyes( "\nGenerate leg start report" );
288
    if( abort_flag )
289
        return;
290
 
291
    line++;
292
    clear_it = getyes("Reset the leg start times" );
293
    if( abort_flag )
294
        return;
295
 
296
    line++;
297
    team = config.min_team;                      /* Team we are working with */
298
    while( team <= config.max_team )
299
    {
300
        if( valid_field( team ) )
301
        {
302
            g_record( team, &team_buf );
303
            if( team_buf.flags.valid && team_buf.leg[leg - 1].end > 0 )
304
            {
305
                team_buf.leg[leg].start = team_buf.leg[leg - 1].end + delta;
306
                team_buf.leg[leg].manual = TRUE;
307
            }
308
            else
309
                team_buf.leg[leg].start = ( time_t ) - 1;
310
 
311
            set_times( &team_buf );
312
            test_times( &team_buf, 0 );
313
            put_team_record( team, &team_buf );
314
        }
315
 
316
        if( team % 10 == 0 )
317
        {
318
            cur( 0, line );
319
            printf( "Upto team %d", team );
320
            flush_out();
321
        }
322
        team++;
323
    }
324
 
325
    if( report_it )
326
        legs_start_report(++line);
327
 
328
    if ( clear_it )
329
        tm_clearleg_specified( leg, ++line );
330
}
331
 
332
/*========================================================================
333
 *
334
 *  Set up the leg start times
335
 *
336
 *  Purpose:
337
 *      This function is called to Set up the leg start times
338
 *
339
 *  Parameters:
340
 *      None
341
 *
342
 *  Returns:
343
 *      Nothing
344
 *
345
 *========================================================================*/
346
 
347
void leg_start(void)
348
{
349
 
350
    team = config.min_team;                      /* Team we are working with */
351
    cur( 0, 5 );
352
    printf( "Setting start times for all valid teams from %-d to %-d \n",
353
            config.min_team, config.max_team );
354
    if( !getyes( "Continue operation" ) )
355
        return;
356
 
357
    while( team <= config.max_team )
358
    {
359
        if( valid_field( team ) )
360
        {
361
            ( void ) g_record( team, &team_buf );
362
            if( team_buf.class > 0 && team_buf.class <= config.num_class )
363
                team_buf.leg[0].start =
364
                    config.team_class[team_buf.class - 1].start;
365
            else
366
                team_buf.leg[0].start = ( time_t ) - 1;
367
            team_buf.leg[1].manual = 0;
368
            team_buf.leg[1].start = team_buf.leg[0].start;
369
            team_buf.leg[0].l_place = 0;
370
            team_buf.leg[0].le_place = 0;
371
            team_buf.leg[0].lec_place = 0;
372
            team_buf.leg[0].lc_place = 0;
373
            team_buf.leg[0].manual = FALSE;
374
            set_times( &team_buf );
375
            test_times( &team_buf, 0 );
376
            put_team_record( team, &team_buf );
377
        }
378
        if( team % 10 == 0 )
379
        {
380
            cur( 0, 8 );
381
            printf( "Upto team %d", team );
382
            flush_out();
383
        }
384
        team++;
385
    }
386
}
387
 
388
/*========================================================================
389
 *
390
 *  Set the start time for a specific leg
391
 *
392
 *  Purpose:
393
 *      This function is called to set the start time for a specific
394
 *      leg to a specified and fixed time
395
 *
396
 *  Parameters:
397
 *      None
398
 *
399
 *  Returns:
400
 *      Nothing
401
 *
402
 *========================================================================*/
403
 
404
void tm_fixedstart(void)
405
{
406
    time_t      starttime;                       /* The start time */
407
 
408
    cur( 0, 5 );
409
    while( TRUE )
410
    {
411
        leg = 0;
412
        d_field( 0, 5, "Enter leg to set start time :", D_NUMBER, 1,
413
                 ( char * ) &leg, TRUE, M_UPDATE );
414
        if( leg == 0 )
415
            return;                              /* Null leg - just exit */
416
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
417
            break;
418
        beep();                                  /* Make a noise and wait for valid number */
419
    }
420
 
421
 
422
    team = config.min_team;                      /* Team we are working with */
423
    cur( 0, 5 );
424
    printf
425
        ( "Setting start times for all valid teams from %-d to %-d for leg %d\n",
426
          config.min_team, config.max_team, leg );
427
 
428
    starttime = 0;
429
    do
430
    {
431
        d_time( 0, 6, "Enter the start time : ", &starttime, TRUE );
432
        if( abort_flag )
433
            return;
434
 
435
        if( starttime < 0 )
436
            beep();
437
 
438
    } while( starttime < 0 );
439
 
440
    while( team <= config.max_team )
441
    {
442
        if( valid_field( team ) )
443
        {
444
            g_record( team, &team_buf );
445
            if( team_buf.flags.valid )
446
            {
447
                team_buf.leg[leg].start = starttime;
448
                team_buf.leg[leg].manual = TRUE;
449
            }
450
            else
451
                team_buf.leg[leg].start = ( time_t ) - 1;
452
 
453
            set_times( &team_buf );
454
            test_times( &team_buf, 0 );
455
            put_team_record( team, &team_buf );
456
        }
457
 
458
        if( team % 10 == 0 )
459
        {
460
            cur( 0, 8 );
461
            printf( "Upto team %d", team );
462
            flush_out();
463
        }
464
        team++;
465
    }
466
}
467
 
468
/*========================================================================
469
 *
470
 *  Set a staggered start time for a specific leg
471
 *
472
 *  Purpose:
473
 *      This function is called to set the start time for a specific
474
 *      leg to a specified and a staggered time
475
 *
476
 *  Parameters:
477
 *      None
478
 *
479
 *  Returns:
480
 *      Nothing
481
 *
482
 *========================================================================*/
483
 
484
void tm_staggered(void)
485
{
486
    time_t      starttime;                       /* The start time */
487
    time_t      delta;                           /* The stagger */
488
 
489
    cur( 0, 5 );
490
    while( TRUE )
491
    {
492
        leg = 0;
493
        d_field( 0, 5, "Enter leg to set start time :", D_NUMBER, 1,
494
                 ( char * ) &leg, TRUE, M_UPDATE );
495
        if( leg == 0 )
496
            return;                              /* Null leg - just exit */
497
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
498
            break;
499
        beep();                                  /* Make a noise and wait for valid number */
500
    }
501
 
502
 
503
    team = config.min_team;                      /* Team we are working with */
504
    cur( 0, 5 );
505
    printf
506
        ( "Setting staggered start times for all valid teams from %-d to %-d for leg %d\n",
507
          config.min_team, config.max_team, leg );
508
 
509
    starttime = 0;
510
    do
511
    {
512
        d_time( 0, 6, "Enter the start time : ", &starttime, TRUE );
513
        if( abort_flag )
514
            return;
515
 
516
        if( starttime < 0 )
517
            beep();
518
 
519
    } while( starttime < 0 );
520
 
521
    delta = 0;
522
    do
523
    {
524
        d_time( 0, 7, "Enter the stagger time : ", &delta, TRUE );
525
        if( abort_flag )
526
            return;
527
 
528
        if( delta < 0 )
529
            beep();
530
 
531
    } while( delta < 0 );
532
 
533
 
534
    while( team <= config.max_team )
535
    {
536
        if( valid_field( team ) )
537
        {
538
            g_record( team, &team_buf );
539
            if( team_buf.flags.valid )
540
            {
541
                team_buf.leg[leg].start = starttime;
542
                starttime += delta;
543
                team_buf.leg[leg].manual = TRUE;
544
            }
545
            else
546
                team_buf.leg[leg].start = ( time_t ) - 1;
547
 
548
            set_times( &team_buf );
549
            test_times( &team_buf, 0 );
550
            put_team_record( team, &team_buf );
551
        }
552
 
553
        if( team % 10 == 0 )
554
        {
555
            cur( 0, 9 );
556
            printf( "Upto team %d", team );
557
            flush_out();
558
        }
559
        team++;
560
    }
561
}
562
 
563
/*========================================================================
564
 *
565
 *  Clear start times on a specific leg
566
 *
567
 *  Purpose:
568
 *      This function is called to clear start times on a specifc leg
569
 *
570
 *  Parameters:
571
 *      None
572
 *
573
 *  Returns:
574
 *      Nothing
575
 *
576
 *========================================================================*/
577
 
578
void tm_clearleg(void)
579
{
580
 
581
    cur( 0, 5 );
582
    while( TRUE )
583
    {
584
        leg = 0;
585
        d_field( 0, 5, "Enter leg to clear start time :", D_NUMBER, 1,
586
                 ( char * ) &leg, TRUE, M_UPDATE );
587
        if( leg == 0 )
588
            return;                              /* Null leg - just exit */
589
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
590
            break;
591
        beep();                                /* Make a noise and wait for valid number */
592
    }
593
 
594
 
595
    cur( 0, 5 );
596
    printf
597
        ( "Setting start times for all valid teams from %-d to %-d for leg %d\n",
598
          config.min_team, config.max_team, leg );
599
    if( !getyes( "Continue operation" ) )
600
        return;
601
 
602
    tm_clearleg_specified( leg, 6 );
603
}
604
 
605
/*========================================================================
606
 *
607
 *  Clear start times on a specific leg
608
 *
609
 *  Purpose:
610
 *      This function is called to clear start times on a specifc leg
611
 *
612
 *  Parameters:
613
 *      None
614
 *
615
 *  Returns:
616
 *      Nothing
617
 *
618
 *========================================================================*/
619
 
620
void tm_clearleg_specified(int cleg, int line)
621
{
622
 
623
    /*
624
    **  Validate the users argument
625
    */
626
    if( cleg == 0 )
627
        return;                              /* Null leg - just exit */
628
    if( cleg > config.num_legs )             /* Valid leg number - Exit loop */
629
        return;
630
    leg = cleg;
631
 
632
    team = config.min_team;                 /* Team we are working with */
633
    while( team <= config.max_team )
634
    {
635
        if( valid_field( team ) )
636
        {
637
            g_record( team, &team_buf );
638
            team_buf.leg[leg].manual = FALSE;
639
            team_buf.leg[leg].start = ( time_t ) - 1;
640
 
641
            set_times( &team_buf );
642
            test_times( &team_buf, 0 );
643
            put_team_record( team, &team_buf );
644
        }
645
        if( team % 10 == 0 )
646
        {
647
            cur( 0, line );
648
            printf( "Upto team %d", team );
649
            flush_out();
650
        }
651
        team++;
652
    }
653
}
654
 
655
 
656
/*========================================================================
657
 *
658
 *  Initialize all stored times
659
 *
660
 *  Purpose:
661
 *      This function is called to Initialize all stored times
662
 *
663
 *  Parameters:
664
 *      None
665
 *
666
 *  Returns:
667
 *      Nothing
668
 *
669
 *========================================================================*/
670
 
671
void leg_ini(void)
672
{
673
    int         i;
674
 
675
 
676
    team = config.min_team;                      /* Team we are working with */
677
    cur( 0, 5 );
678
    printf( "\nInitializing ALL times for all valid teams from %-d to %-d\n",
679
            config.min_team, config.max_team );
680
    if( !getyes( "Continue operation" ) )
681
        return;
682
 
683
    while( team <= config.max_team )
684
    {
685
        if( valid_field( team ) )
686
        {
687
            ( void ) g_record( team, &team_buf );
688
            for( i = 0; i <= MAX_LEGS; i++ )
689
            {
690
                team_buf.leg[i].start = team_buf.leg[i].end =
691
                    team_buf.leg[i].elapsed = ( time_t ) - 1;
692
                team_buf.leg[i].l_place = 0;
693
                team_buf.leg[i].le_place = 0;
694
                team_buf.leg[i].lec_place = 0;
695
                team_buf.leg[i].lc_place = 0;
696
                team_buf.leg[i].manual = FALSE;
697
            }
698
            if( team_buf.class > 0 && team_buf.class <= config.num_class )
699
                team_buf.leg[0].start =
700
                    config.team_class[team_buf.class - 1].start;
701
            else
702
                team_buf.leg[0].start = ( time_t ) - 1;
703
 
704
            team_buf.leg[1].start = team_buf.leg[0].start;
705
            team_buf.flags.disqualified = FALSE;
706
            team_buf.flags.non_equestrian = FALSE;
707
            put_team_record( team, &team_buf );
708
        }
709
        if( team % 10 == 0 )
710
        {
711
            cur( 0, 8 );
712
            printf( "Upto team %d", team );
713
            flush_out();
714
        }
715
        team++;
716
    }
717
}
718
 
719
/*========================================================================
720
 *
721
 *  Initialize all team data
722
 *
723
 *  Purpose:
724
 *      This function is called to Initialize all team data
725
 *
726
 *  Parameters:
727
 *      None
728
 *
729
 *  Returns:
730
 *      Nothing
731
 *
732
 *========================================================================*/
733
void tm_init(void)
734
{
735
 
736
    team = config.min_team;                      /* Team we are working with */
737
    cur( 0, 5 );
738
    printf
739
        ( "\nDelete ALL team information for all valid teams from %-d to %-d\n",
740
          config.min_team, config.max_team );
741
    if( !getyes( "Continue operation" ) )
742
        return;
743
 
744
    while( team <= config.max_team )
745
    {
746
        if( valid_field( team ) )
747
        {
748
            clr_team( team, &team_buf );
749
            put_team_record( team, &team_buf );
750
        }
751
        if( team % 10 == 0 )
752
        {
753
            cur( 0, 8 );
754
            printf( "Upto team %d", team );
755
            flush_out();
756
        }
757
        team++;
758
    }
759
}
760
 
761
/*========================================================================
762
 *
763
 *  Generate dummy team names
764
 *
765
 *  Purpose:
766
 *      This function is called to Generate dummy team names
767
 *
768
 *  Parameters:
769
 *      None
770
 *
771
 *  Returns:
772
 *      Nothing
773
 *
774
 *========================================================================*/
775
 
776
void tm_gen(void)
777
{
778
 
779
    team = config.min_team;                      /* Team we are working with */
780
    cur( 0, 5 );
781
    printf( "Generate DUMMY team names for all valid teams from %-d to %-d\n",
782
            config.min_team, config.max_team );
783
    if( !getyes( "Continue operation" ) )
784
        return;
785
 
786
    while( team <= config.max_team )
787
    {
788
        if( valid_field( team ) )
789
        {
790
            ( void ) g_record( team, &team_buf );
791
            team_buf.flags.valid = TRUE;
792
            sprintf( team_buf.name, "Team - %4.4d", team );
793
            team_buf.class = 1;                  /* Set default class */
794
            put_team_record( team, &team_buf );
795
        }
796
        if( team % 10 == 0 )
797
        {
798
            cur( 0, 8 );
799
            printf( "Upto team %d", team );
800
            flush_out();
801
        }
802
        team++;
803
    }
804
}
805
 
806
 
807
/*========================================================================
808
 *
809
 *  Generate starters report
810
 *
811
 *  Purpose:
812
 *      This routine is used to generate a list of leg start times
813
 *      This system can cope with a break at the start of leg
814
 *
815
 *  Parameters:
816
 *      None
817
 *
818
 *  Returns:
819
 *      Nothing
820
 *
821
 *========================================================================*/
822
 
823
void legs_start_report(int line)
824
{
825
    int         i;
826
    t_legs     *data;                            /* Address of table */
827
    t_legs     *dptr;                            /* Moving pointer */
828
    int         last_team;
829
    int         num_records = 0;                 /* Number of records in array */
830
    char        l_s[40];                         /* Name of start time file */
831
 
832
    cur( 0, line );
833
    printf( "Generating Starters report for Leg-%d", leg );
834
 
835
    data = ( t_legs * ) calloc( ( unsigned ) ( config.max_team - config.min_team + 2 ), sizeof( t_legs ) ); /* Fetch memory */
836
 
837
    if( !data )
838
    {
839
        printf( "\nNo memory for report\n" );
840
        return;
841
    }
842
 
843
    /*
844
     * Extract the required data from the data base
845
     * Only save that information required for the operation
846
     */
847
 
848
    dptr = data;
849
    for( i = config.min_team; i <= config.max_team; i++ )   /* Print team order data */
850
    {
851
        if( valid_field( i ) && g_record( i, &team_buf ) )
852
        {
853
            dptr->numb = team_buf.numb;
854
            dptr->start = team_buf.leg[leg].start;
855
            dptr->flags = team_buf.flags;
856
            dptr++;
857
            num_records++;
858
        }
859
    }
860
 
861
 
862
    /*
863
     * Sort the data into some logical order 
864
     */
865
    qsort( ( char * ) data, num_records, sizeof( t_legs ), sort_legs );
866
 
867
    /*
868
    **  Now print the data on the printer
869
    **      - Generte the name of the printer file
870
    **      - Open the printer
871
    */
872
 
873
    sprintf( l_s, "l_%d", leg );
874
    if( !open_printer( "", l_s, 80, FALSE, "Starters Report" ) )
875
        return;
876
 
877
    /*
878
     * Print out the header for the file 
879
     */
880
    print( "\nStarters report for leg:%d %-*s\n", leg, 18, config.leg_name[leg - 1] );
881
    print( "Team order listing\n\n" );
882
 
883
    print_underline (TRUE);
884
    print( "%-8s   %-5s   %-8s   |   %-5s   %-8s", "Time", "Team", "DeltaT", "Team", "Time" );
885
    print_underline (FALSE);
886
    print( "\n" );
887
 
888
    last_team = 0;
889
    for( dptr = data, i = 0; i < num_records; i++ )
890
    {
891
        ls_timer( dptr++, i );
892
        print( "   |   " );
893
        ls_team( &last_team, data );
894
        print( "\n" );
895
    }
896
 
897
    print( "\nTeams without marked times will be started when ALL marked teams\n" );
898
    print( "have been started.\n" );
899
 
900
    /*
901
    **  Release the resources
902
    **      - Printer
903
    **      - Memory
904
    */
905
    close_printer();
906
    free( data );
907
}
908
 
909
/*========================================================================
910
 *
911
 *  print the current leg entry
912
 *
913
 *  Purpose:
914
 *      This function is a helper routine to print the current leg entry
915
 *      in time : Team number order
916
 *
917
 *  Parameters:
918
 *      ptr     Pointer to entry
919
 *      num     Entry index
920
 *
921
 *  Returns:
922
 *      Nothing
923
 *
924
 *========================================================================*/
925
 
926
void ls_timer( t_legs * ptr, int num )
927
{
928
    time_t      time;
929
    time_t      delta;
930
 
931
    if( num == 0 )
932
        delta = ( time_t ) - 1;
933
    else
934
        delta = ptr[0].start - ptr[-1].start;
935
 
936
    time = ptr->start;
937
    if( ptr->flags.disqualified )
938
        time = ( time_t ) - 1;
939
 
940
    print( "%-8s   %-5d", time_fa( time, ptr->flags.disqualified ),
941
           ptr->numb );
942
    print( "   %-8s", time_fa( delta, TRUE ) );
943
}
944
 
945
 
946
/*========================================================================
947
 *
948
 *  print next ( numeric ) leg entry
949
 *
950
 *  Purpose:
951
 *      This helper function is called to print next ( numeric ) leg entry
952
 *      in Team # time order
953
 *
954
 *  Parameters:
955
 *      last        Last one found
956
 *                  Used to relocate my self
957
 *      data        Start of data
958
 *
959
 *  Returns:
960
 *      Nothing
961
 *
962
 *========================================================================*/
963
 
964
void ls_team( int *last, t_legs * data )
965
{
966
    t_legs     *min;
967
    time_t      time;
968
 
969
    for( min = 0; data->numb; data++ )
970
    {
971
        if( data->numb > *last && ( min == 0 || data->numb < min->numb ) )
972
            min = data;
973
    }
974
    *last = min->numb;                           /* Save current team as done */
975
 
976
    time = min->start;
977
    if( min->flags.disqualified )
978
        time = ( time_t ) - 1;
979
    print( "%-5d   %-8s", min->numb,
980
           time_fa( time, min->flags.disqualified ) );
981
}
982
 
983
/*========================================================================
984
 *
985
 *  Qsort callback function
986
 *
987
 *  Purpose:
988
 *      This function is provided to Qsort() to sort the teams
989
 *
990
 *  Parameters:
991
 *      a           - Leg entry to compare
992
 *      b           - Leg entry to compare
993
 *
994
 *
995
 *  Returns:
996
 *      -1      a < b
997
 *       0      a = b
998
 *       1      a > b
999
 *
1000
 *========================================================================*/
1001
 
1002
int sort_legs( const void * aa, const void * bb )
1003
{
1004
 
1005
    const t_legs * a = aa;
1006
    const t_legs * b = bb;
1007
 
1008
    int         a_bad;
1009
    int         b_bad;
1010
 
1011
    a_bad = a->flags.disqualified || a->start <= 0;
1012
    b_bad = b->flags.disqualified || b->start <= 0;
1013
 
1014
    if( a_bad || b_bad )                         /* Valid data has precedence */
1015
    {
1016
        if( a_bad && b_bad )
1017
            return ( a->numb - b->numb );
1018
        else
1019
            return ( a_bad ? 1 : -1 );
1020
    }
1021
 
1022
    if( a->start == b->start )
1023
        return ( a->numb - b->numb );
1024
    else
1025
    {
1026
        if( ( a->start > 0 ) && ( b->start > 0 ) )
1027
            return ( ( int ) ( a->start - b->start ) );
1028
        else
1029
            return ( ( a->start > 0 ) ? -1 : 1 );
1030
    }
1031
}
1032
 
1033
/********************************* EOF ***********************************/