Subversion Repositories svn1

Rev

Rev 195 | Rev 203 | 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\report.c
6
*
7
* purpose:  PREFIX -
8
*
9
* functions
10
*       report                  - Report menu
11
*       pri_team                - Print in team order
12
*       pri_leg                 - Print in given leg finishing time
13
*       p_place                 - Print place information
14
*       p_team                  - Print team information
15
*       pri_eleg                - Print on elapsed times for given leg
16
*       pe_place                - Print place and elapsed time information
17
*       pe_team                 - Print team and elapsed time information
18
*       pri_final               - Print final results
19
*       pri_interim             - Print interim results
20
*       print_class_header      - Print a class header
21
*       print_class_stats       - Generate the class stats
22
*       print_legend            - Print the legend
23
*       pi_place                - Return place data
24
*       pr_place                - Return place data
25
*       ck_data                 - Check data for bad times
26
*       srt_place               - Update placing information
27
*       do_big_sort             - Main sort routine for final data
28
*       sort                    - Sort in memory buffer
29
*       sort_comp               - qsort comparison function
30
*       load                    - load report data into memory
31
*       gen_stats               - Generate all the stats
32
*
33
* programmer: David Purdie
34
*
35
* revision  date        by      reason
36
*    e388   11-Oct-88           Option in the final printout to only
37
*                               produce the main result sheet. The leg
38
*                               placing printout is not produced.
39
*
40
*                               Changes to the display format of unknown
41
*                               times of disqualified teams. Only affects
42
*                               the leg-printouts.
43
*                               Disqualified teams show as -- -- --
44
*                               Otherwise they show as     ** ** **
45
*                               or a valid time.
46
*
47
*   e339    31-Oct-88   DDP     Added the "Interim Report" facility
48
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
49
*   00.1    06-sep-02   DDP     Added support for HTML report generation
50
*
51
**************************************************************************/
52
 
53
#include    <stdio.h>
54
#include    "consts.h"
55
#include    "structs.h"
56
#include    "proto.h"
170 - 57
#include    "mainwindow.h"
91 - 58
 
59
void pri_awards_html(void);
60
void pri_awards(void);
61
void pri_master_index(void);
62
char *placing ( int place );
63
void pri_name_index(void);
64
void pri_name_index_body( void );
65
void pri_all_reports (void );
66
void pri_leg_body(int leg);
67
void pri_eleg_body(int leg);
68
void pri_csv_data ( void );
69
 
70
 
71
#define MAX_PLACE 11
72
const char * place_text[] =
73
{
74
    "Zero'th",
75
    "First",
76
    "Second",
77
    "Third",
78
    "Fourth",
79
    "Fifth",
80
    "Sixth",
81
    "Seventh",
82
    "Eighth",
83
    "Ninth",
84
    "Tenth",
85
    "Eleventh"
86
};
87
 
88
int         sort_leg;
89
int         sort_mode;
90
report_type report_html = text;
91
bool        report_all = FALSE;
92
 
93
/* Parameters used by the sort routine to govern its actions */
94
 
95
#define S_L     1                                /* Elasped times */
96
#define S_LE    2                                /* Leg end time */
97
#define S_LC    3                                /* Elapsed times per class */
98
#define S_LEC   4                                /* Leg end time per class */
99
#define S_FIN   5                                /* Sort on finish time at given leg */
100
#define S_TEAM  6                                /* Sort on team order */
101
#define S_CLASS 7                                /* Sort on class/team order */
102
 
103
/*
104
**  Various checking modes
105
*/
106
#define C_ELAPSED   1                            /* Check elapsed times */
107
#define C_END       2                            /* Check end times */
108
#define C_DISQUAL   3                            /* Check disqualified teams */
109
 
110
/*
111
**  Data
112
*/
113
ty_s_data  *sort_data = 0;                       /* pointer to memory */
114
ty_s_aux   *sort_aux = 0;                        /* pointer to aux sort info */
195 david 115
ty_s_namedata *sort_name_data = 0;               /* pointer to name info */
116
unsigned    int sort_num;                        /* Number in the array */
117
unsigned    int sort_num_data;                   /* Number in the array */
91 - 118
ty_stats        stats;                           /* Holds statistics */
119
 
120
/*========================================================================
121
 *
122
 *  Print in team order
123
 *
124
 *  Purpose:
125
 *      This function is called to Print in team order
126
 *      This function may also be used to create an HTML suite of files
127
 *      within the result set
128
 *
129
 *  Parameters:
130
 *      None
131
 *
132
 *  Returns:
133
 *      Nothing
134
 *
135
 *========================================================================*/
136
 
137
void pri_team(void)
138
{
139
    int         i, k;
170 - 140
    team_type   team_buf;
91 - 141
 
142
    if( !open_printer( "", "name", 132, report_html, "Team Names" ) )
143
        return;
144
 
145
    /*
146
     * Print out the data 
147
     * Print out the header
148
     */
149
 
150
    print( "\n" );
151
 
152
    print_underline( TRUE );
153
    print( "%-*s %-*s %-*s", MAX_TM_NAME + 5, "Entry number and name",
154
               LEN_CLASS_NAME, "Category",
155
               config.num_countries == 0 ? 1 : LEN_CNTRY_NAME,
156
               config.num_countries == 0 ? "" : "Country" );
157
    for( k = 0; k < MAX_MEMB; k++ )
158
    {
159
        print( " %-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
160
    }
161
    print_underline( FALSE ) ;
162
    print( "\n" );
163
 
164
    for( i = config.min_team; i <= config.max_team; i++ )
165
    {
166
        if( valid_field( i ) && g_record( i, &team_buf ) )
167
        {
168
            /*
169
            **  If printing an HTML report then we need to mark
170
            **  the entry with a reference so that we can link to it
171
            */
172
            if ( report_html == html )
173
            {
174
                print( "<A NAME=\"Team_%04d\">",team_buf.numb );
175
                print( "</A>" );
176
            }
177
 
178
            /*
179
            **  Basic information
180
            **      - Team number - with Xref back to full result
181
            **      - Full team name
182
            **      - Full categoray name - with Xref to category results
183
            **      - Country name
184
            */
185
            if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
186
            print( "%4d",       team_buf.numb );
187
            if ( report_html == html ) print( "</A>" );
188
 
189
            print( " %-*s ",     MAX_TM_NAME, team_buf.name );
190
 
191
            if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">",url_encode(p_filename(filebase, config.team_class[team_buf.teamclass - 1].abr ,"html")), team_buf.numb );
192
            print( "%-*s",     LEN_CLASS_NAME, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].full_name );
193
            if ( report_html == html ) print( "</A>" );
194
 
195
            print( " %-*s",     config.num_countries == 0 ? 1 : LEN_CNTRY_NAME,
196
                                config.num_countries == 0
197
                                || team_buf.country ==
198
 
199
 
200
            for( k = 0; k < MAX_MEMB; k++ )
201
                print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
202
 
203
            print( "\n" );
204
        }
205
    }
206
    close_printer();
207
}
208
 
209
/*========================================================================
210
 *
211
 *  Print in name order
212
 *
213
 *  Purpose:
214
 *      This function is called to print a list of all known competitors
215
 *      This function may also be used to create an HTML suite of files
216
 *      within the result set
217
 *
218
 *  Parameters:
219
 *      None
220
 *
221
 *  Returns:
222
 *      Nothing
223
 *
224
 *========================================================================*/
225
void pri_name_index_body( void )
226
{
227
    ty_s_namedata *ptr;
170 - 228
    team_type   team_buf;
91 - 229
    unsigned    num;
230
    int i;
231
    unsigned int k;
232
    int num_names;
233
 
234
    /*
235
    **  Determine the number of names to allow for
236
    *   Based on the total number of teams
237
    */
238
    num = config.max_team - config.min_team + 1 ;
239
    num *= MAX_MEMB;
240
 
241
    sort_name_data = ( ty_s_namedata * ) calloc ( num , sizeof( ty_s_namedata ) );
242
 
243
    if( sort_name_data == 0 )
244
    {
170 - 245
        MainWindow::showMessage("Error in allocating memory");
91 - 246
        return;
247
    }
248
 
249
    /*
250
    **  Read all teams an extract name information
251
    */
252
    ptr = sort_name_data;
253
    for( i = config.min_team; i <= config.max_team; i++ )
254
    {
255
        if( valid_field( i ) && g_record( i, &team_buf ) )
256
        {
257
            num_names = 0;
258
            for( k = 0; k < MAX_MEMB; k++ )
259
            {
260
                if ( team_buf.members[k].name[0] )
261
                {
262
                    ptr->team = i;
263
                    ptr->leg = k;
264
                    ptr->teamclass = team_buf.teamclass;
265
                    strncpy( ptr->name,team_buf.members[k].name, sizeof(team_buf.members[k].name));
266
                    ptr++;
267
                    num_names++;
268
                }
269
            }
270
 
271
            if ( num_names == 0 )
272
            {
273
                ptr->team = i;
274
                ptr->leg = 0;
275
                ptr->teamclass = team_buf.teamclass;
276
                strncpy( ptr->name,team_buf.name, sizeof(team_buf.members[k].name));
277
                ptr++;
278
            }
279
 
280
        }
281
    }
282
    sort_num_data = ptr - sort_name_data;
283
 
284
    /*
285
    **  Now stort the entries by name:team:leg
286
    */
287
    qsort( ( char * ) sort_name_data, sort_num_data, sizeof( ty_s_namedata ), sort_comp_cname );
288
 
289
    /*
290
    **  Now generate the report
291
    */
292
 
293
    if( !open_printer( "", "competitor", 80, report_html, "Competitor Names" ) )
294
        return;
295
 
296
    /*
297
     * Print out the data 
298
     * Print out the header
299
     */
300
    print( "\n" );
301
 
302
    print_underline( TRUE );
303
    print( "%-*s %-*s %-*s %-*s", MAX_TM_NAME + 5, "Competitor name",
304
               6, "Leg",
305
               5, "Team",
306
               LEN_CLASS_NAME, "Category"
307
               );
308
    print_underline( FALSE ) ;
309
    print( "\n" );
310
 
311
    ptr = sort_name_data;
312
    for( k = 1; k <= sort_num_data; k++, ptr++ )
313
    {
314
        print( "%-*s", MAX_TM_NAME + 5, ptr->name );
315
        print( " " );
316
 
317
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), ptr->team );
318
        print( "%-*d", 6, ptr->leg + 1 );
319
        if ( report_html == html ) print( "</A>" );
320
        print( " " );
321
 
322
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), ptr->team );
323
        print( "%-*d", 5, ptr->team );
324
        if ( report_html == html ) print( "</A>" );
325
        print( " " );
326
 
327
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">",(p_filename(filebase, config.team_class[ptr->teamclass - 1].abr ,"html")), ptr->team );
328
        print( "%-*s", LEN_CLASS_NAME, ptr->teamclass == 0 ? "" : config.team_class[ptr->teamclass - 1].abr );
329
        if ( report_html == html ) print( "</A>" );
330
        print( " " );
331
 
332
        if ( ptr->multi ) print( "* ");
333
        print( "\n" );
334
    }
335
 
336
    print_legend( -1, 0 );
337
    close_printer();
338
 
339
}
340
 
341
/*========================================================================
342
 *
343
 *  Print in given leg finishing time
344
 *
345
 *  Purpose:
346
 *      This function is called to Print in given leg finishing time
347
 *
348
 *  Parameters:
349
 *      None
350
 *
351
 *  Returns:
352
 *      Nothing
353
 *
354
 *========================================================================*/
355
 
356
void pri_leg_body(int leg)    
357
{
358
    ty_s_data  *ptr;
359
    int         i, k;
195 david 360
 
91 - 361
    /*
362
     * Sort the data in finishing order 
363
     */
364
 
365
    if( ck_data( leg, C_END ) )
366
        return;                                  /* Check data for this leg */
367
    sort_team_data( leg, S_FIN );                /* Sort the data */
368
 
369
 
370
    /*
371
     * Now print the data on the printer 
372
     */
373
    if( !open_printer( "",
374
                        tprintf("lg%1.1d", leg ),
375
                        80,
376
                        report_html,
377
                        leg ? tprintf ("Finish order for Leg %d.", leg)
378
                            : "Final team finish order"
379
                     ) )
380
    {
381
        return;
382
    }
383
 
384
    /*
385
     * Print out the data 
386
     */
387
    print( "PRELIMINARY RESULTS ONLY\n\n" );
388
 
389
    print_underline( TRUE );
390
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
391
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
392
    print( "|" );
393
    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
394
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
395
    print_underline( FALSE );
396
 
397
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
398
    {
399
        p_place( ptr++, leg, k++ );
400
        p_place( ptr++, leg, k++ );
401
        print( "|" );
402
        while( i <= config.max_team && !valid_field( i ) )
403
            i++;
404
        p_team( i++, leg );
405
        while( i <= config.max_team && !valid_field( i ) )
406
            i++;
407
        p_team( i++, leg );
408
        print( "\n" );
409
    }
410
 
411
    print_underline( TRUE );
412
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
413
//    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
414
    print( "|" );
415
//    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
416
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
417
    print_underline( FALSE );
418
 
419
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
420
    {
421
        p_place( ptr++, leg, k++ );
422
//        p_place( ptr++, leg, k++ );
423
        print( "|" );
424
        while( i <= config.max_team && !valid_field( i ) )
425
            i++;
426
        p_team( i++, leg );
427
//        while( i <= config.max_team && !valid_field( i ) )
428
//            i++;
429
//        p_team( i++, leg );
430
        print( "\n" );
431
    }
432
 
433
 
434
    /*
435
     * Insert the leg statistics 
436
     */
437
 
438
    gen_stats();                               /* Generate all stats */
439
    print( "\nLeg statistics\n" );
440
    print( "Fastest team: %4d time : %s.",
441
           stats.fast.team[leg][0], time_a( stats.fast.time[leg][0] ) );
442
    print( " Average time: %s\n", time_a( stats.average[leg][0] ) );
443
 
444
    close_printer();
445
}
446
 
447
/*========================================================================
448
 *
449
 *  Print place information
450
 *
451
 *  Purpose:
452
 *      This helper function is called to Print place information
453
 *      in a 20-character field
454
 *
455
 *  Parameters:
456
 *      ptr         Address of the place data
457
 *      leg         Leg to print
458
 *      k           Current index into sorted array. Simply
459
 *                  used to determine if the entry is valid
460
 *                  or if it should be space-filled
461
 *
462
 *  Returns:
463
 *      Nothing
464
 *
465
 *========================================================================*/
466
 
467
void p_place( ty_s_data * ptr, int leg, unsigned k )
468
{
469
    if( k < sort_num )
470
    {
471
        print( "%4.4s %4d %8s  ",
472
               pr_place( ptr->place, ptr->flags.bad_times ),
473
               ptr->team, time_fa( ptr->leg[leg], ptr->flags.disqualified ) );
474
    }
475
    else
476
    {
477
        print( "%20s", "" );
478
    }
479
}
480
 
481
/*========================================================================
482
 *
483
 *  Print team information
484
 *
485
 *  Purpose:
486
 *      This helper function is called to Print team information
487
 *      in a 20-character field
488
 *
489
 *  Parameters:
490
 *      i           team to print
491
 *      leg         Leg to print
492
 *
493
 *  Returns:
494
 *      Nothing
495
 *
496
 *========================================================================*/
497
 
498
void p_team( int i, int leg )
499
{
500
    ty_s_data  *ptra;                            /* Pointer to sort data */
501
    int         found = FALSE;
502
    unsigned    j;
503
 
504
    if( valid_field( i ) )
505
    {
506
        ptra = sort_data;
507
        for( j = 1; j <= sort_num; j++, ptra++ )
508
            if( i == ptra->team )
509
            {
510
                found = TRUE;
511
                break;
512
            }
513
    }
514
    if( found )
515
    {
516
        print( "%4d %4.4s %8s  ",
517
               ptra->team,
518
               pr_place( ptra->place, ptra->flags.bad_times ),
519
               time_fa( ptra->leg[leg], ptra->flags.disqualified ) );
520
    }
521
    else
522
    {
523
        print( "%20s", "" );
524
    }
525
}
526
 
527
/*========================================================================
528
 *
529
 *  Print on elapsed times for given leg
530
 *
531
 *  Purpose:
532
 *      This function is called to Print on elapsed times for given leg
533
 *
534
 *  Parameters:
195 david 535
 *      leg             - Leg number to print
91 - 536
 *
537
 *  Returns:
538
 *      Nothing
539
 *
540
 *========================================================================*/
541
 
542
void pri_eleg_body( int leg)
543
{
544
    ty_s_data  *ptr;
545
    int         i, k;
546
 
547
    /*
158 david 548
     * Sort the data in finishing order
91 - 549
     */
550
 
551
    if( ck_data( leg, C_ELAPSED ) )
552
        return;                                  /* Check data for this leg */
553
    sort_team_data( leg, S_L );                  /* Sort the data on elapsed time */
554
 
555
 
556
    /*
158 david 557
     * Now print the data on the printer
91 - 558
     */
559
    if( !open_printer( "", tprintf( "le%1.1d", leg ),
560
                       80,
561
                       report_html,
562
                       leg ? tprintf( "Elapsed time order for Leg %d.", leg )
563
                           : tprintf( "Final elapsed team finishing order." )
564
                     ) )
565
    {
566
        return;
567
    }
568
 
569
    /*
158 david 570
     * Print out the data
91 - 571
     */
572
    print( "PRELIMINARY RESULTS ONLY\n\n" );
573
 
574
    print_underline( TRUE );
575
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
576
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
577
    print( "|" );
578
    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
579
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
580
    print_underline( FALSE );
581
 
582
 
583
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
584
    {
585
        pe_place( ptr++, leg, k++ );
586
        pe_place( ptr++, leg, k++ );
587
        print( "|" );
588
        while( i <= config.max_team && !valid_field( i ) )
589
            i++;
590
        pe_team( i++, leg );
591
        while( i <= config.max_team && !valid_field( i ) )
592
            i++;
593
        pe_team( i++, leg );
594
        print( "\n" );
595
    }
596
 
597
    print( "\n\n" );
598
    print_underline( TRUE );
599
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
600
//    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
601
    print( "|" );
602
//    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
603
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
604
    print_underline( FALSE );
605
 
606
 
607
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
608
    {
609
        pe_place( ptr++, leg, k++ );
610
//        pe_place( ptr++, leg, k++ );
611
        print( "|" );
612
        while( i <= config.max_team && !valid_field( i ) )
613
            i++;
614
        pe_team( i++, leg );
615
//        while( i <= config.max_team && !valid_field( i ) )
616
//            i++;
617
//        pe_team( i++, leg );
618
        print( "\n" );
619
    }
620
 
621
 
622
    /*
158 david 623
     * Insert the leg statistics
91 - 624
     */
625
 
626
    gen_stats();                               /* Generate all stats */
627
    print( "\nLeg statistics\n" );
628
    print( "Fastest team: %4d time : %s.",
629
           stats.fast.team[leg][0], time_a( stats.fast.time[leg][0] ) );
630
    print( " Average time: %s\n", time_a( stats.average[leg][0] ) );
631
 
632
    close_printer();
633
}
634
 
158 david 635
///*========================================================================
636
// *
637
// *  Print place information
638
// *
639
// *  Purpose:
640
// *      This helper function is called to Print place and elapsed information
641
// *      in a 20-character field
642
// *
643
// *  Parameters:
644
// *      ptr         Address of the place data
645
// *      leg         Leg to print
646
// *      k           Current index into sorted array. Simply
647
// *                  used to determine if the entry is valid
648
// *                  or if it should be space-filled
649
// *
650
// *  Returns:
651
// *      Nothing
652
// *
653
// *========================================================================*/
91 - 654
 
655
void pe_place( ty_s_data * ptr, int leg, unsigned k )
656
{
657
    if( k < sort_num )
658
    {
659
        print( "%4.4s %4d %8s  ",
660
               pr_place( ptr->place, ptr->flags.bad_times ),
661
               ptr->team,
662
               time_fa( ptr->lege[leg], ptr->flags.disqualified ) );
663
    }
664
    else
665
    {
666
        print( "%20s", "" );
667
    }
668
}
669
 
670
/*========================================================================
671
 *
672
 *  Print team information
673
 *
674
 *  Purpose:
675
 *      This helper function is called to Print team and elapsed time
195 david 676
 *      information in a 20-character field
91 - 677
 *
678
 *  Parameters:
679
 *      i           Team to print
680
 *      leg         Leg to print
681
 *
682
 *  Returns:
683
 *      Nothing
684
 *
685
 *========================================================================*/
686
 
687
void pe_team( int i, int leg )
688
{
689
    ty_s_data  *ptra;                            /* Pointer to sort data */
690
    int         found = FALSE;
691
    unsigned    j;
692
 
693
    if( valid_field( i ) )
694
    {
695
        ptra = sort_data;
696
        for( j = 1; j <= sort_num; j++, ptra++ )
697
            if( i == ptra->team )
698
            {
699
                found = TRUE;
700
                break;
701
            }
702
    }
703
    if( found )
704
    {
705
        print( "%4d %4.4s %8s  ",
706
               ptra->team,
707
               pr_place( ptra->place, ptra->flags.bad_times ),
708
               time_fa( ptra->lege[leg], ptra->flags.disqualified ) );
709
    }
710
    else
711
    {
712
        print( "%20s", "" );
713
    }
714
}
715
 
716
/*========================================================================
717
 *
718
 *  Print final results in HTML
719
 *
720
 *  Purpose:
721
 *      This function is called to Print final results with HTML formatting
722
 *      All result files are created
723
 *
724
 *  Parameters:
725
 *      None
726
 *
727
 *  Returns:
728
 *      Nothing
729
 *
730
 *========================================================================*/
731
 
732
void pri_final_html(void)
733
{
734
    /*
735
    **  Generate ALL results with HTML tags
736
    */
737
    report_html = html;
738
    pri_final();
739
    report_html = printed;
740
    pri_final();
741
    report_html = text;
742
}
743
 
744
 
745
/*========================================================================
746
 *
747
 *  Print final results
748
 *
749
 *  Purpose:
750
 *      This function is called to Print final results
751
 *
752
 *  Parameters:
753
 *      None
754
 *
755
 *  Returns:
756
 *      Nothing
757
 *
758
 *========================================================================*/
759
 
760
void pri_final(void)
761
{
762
    ty_s_data  *ptr;
170 - 763
    team_type   team_buf;
91 - 764
    unsigned    i;
765
    int         j, last_class;
766
    char        *report_title;
767
    bool        class_done[MAX_CLASS+1];
768
    int         lcount;
769
 
770
    if( ck_data( -1, C_ELAPSED ) )
771
        return;
772
    /*
773
    **  Sort on every thing
774
    **  Then generate all the stats too
775
    */
776
    do_big_sort();
777
    gen_stats();
778
 
779
    /*
780
     * Now print the data on the printer 
781
     */
782
    if( !open_printer( "", "finish", 132, report_html, "Finishing Order" ) )
783
        return;
784
 
785
    /*
786
     * Print out the data 
787
     */
788
    print_class_header( -1, TRUE );                      /* Print the header */
789
 
790
    ptr = sort_data;
791
    sort_team_data( 0, S_L );                     /* Re-sort on elapsed time */
792
    lcount = 0;
793
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
794
    {
795
        if ( ptr->teamclass == config.nonequestrian_class )
796
            continue;
797
 
798
        g_record( ptr->team, &team_buf );
799
 
800
        /*
801
        **  If printing an HTML report then we need to mark
802
        **  the entry with a reference so that we can link to it
803
        */
804
        if ( report_html == html )
805
        {
806
            print( "<A NAME=\"Team_%04d\"></A>",team_buf.numb );
807
        }
808
 
809
        /*
810
        **  Print the basics (Finishing order)
811
        **      - Place within complete field
812
        **      - Team number - with HTML reference to team file
813
        **      - Class
814
        */
815
        if ( report_html == printed && lcount %5 == 4 ) print_underline( TRUE );
816
        print( "%4.4s ", pr_place( team_buf.leg[0].l_place, ptr->flags.bad_times ) );
817
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
818
        print( "%4d",  team_buf.numb );
819
        if ( report_html == html ) print( "</A>" );
820
 
821
        print( " %-*s", 3, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
822
 
823
        /*
824
        **  Print the per-leg data
825
        **      - Time
826
        **      - Leg place
827
        **      - End place
828
        */
829
        for( j = 1; j <= config.num_legs; j++ )
830
        {
831
            /*
832
            **  Ensure that non-equestrian leg data is not displayed
833
            */
834
            if ( j == config.equestrian_leg && team_buf.flags.non_equestrian )
835
            {
836
                print( "  %-8s %4.4s %4.4s", "-- NE --", "NE","NE");
837
            }
838
            else
839
            {
840
                print( "  %-8s %4.4s %4.4s",
841
                    time_a( team_buf.leg[j].elapsed ),
842
                    pr_place( team_buf.leg[j].l_place, ptr->flags.bad_times ),
843
                    pr_place( team_buf.leg[j].le_place,ptr->flags.bad_times )
844
                  );
845
            }
846
        }
847
 
848
       /*
849
        **  Print the trailer (Finishing order)
850
        **      - Total time
851
        **      - Category place - with reference to category file
852
        */
853
//        print( "  %-8s ",  time_a( team_buf.leg[0].elapsed ) );
854
        print( "  %-8s ", time_a( ptr->lege[0] ) );
855
 
856
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">",url_encode(p_filename(filebase, config.team_class[team_buf.teamclass - 1].abr ,"html")), team_buf.numb );
857
        print( "%-4.4s", pr_place_ne( team_buf.leg[0].lc_place, ptr->flags.bad_times, ptr->flags.non_equestrian ) );
858
        if ( report_html == html ) print( "</A>" );
859
        if ( report_html == printed && lcount %5 == 4 ) print_underline( FALSE );
860
        lcount++;
861
        print( "\n" );
862
    }
863
 
864
    print_class_stats( -1, TRUE );              /* Print statistics */
865
    print_legend(-1, 1 );                       /* Print the legend */
866
    close_printer();                            /* Close the printer */
867
 
868
    /*
869
     * Now produce a breakdown on a class by class basis 
870
     * Now print out the class placement information
871
     */
872
 
195 david 873
       sort_team_data( 0, S_LC );                 /* Generate class placement data */
91 - 874
    last_class = -1;                           /* Invalid class to start with */
875
    memset ( class_done, 0, sizeof(class_done));
876
 
877
 
878
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
879
    {
880
        /*
881
        **  Detect a change in the "class"
882
        **  All data is within the one array of data
883
        **  Use the in-memory class as this MAY differ from that stored
884
        **  The non-equestrian class does this.
885
        */
886
        if( last_class != ptr->teamclass )
887
        {
888
            if( last_class >= 0 )
889
            {
890
                print_class_stats( last_class, TRUE );
891
                print_legend( last_class, 1 );
892
                close_printer();
893
            }
894
 
895
            report_title = tprintf( "Category results for : %-*s", LEN_CLASS_NAME, ptr->teamclass == 0 ? "" : config.team_class[ptr->teamclass - 1].full_name );
896
 
897
            if( !open_printer( "", config.team_class[ptr->teamclass - 1].abr, 132, report_html, report_title ) )
898
                continue;
899
            print_class_header( last_class = ptr->teamclass, TRUE );
900
 
901
            /*
902
            **  Mark the class as done
903
            */
904
            class_done[ptr->teamclass] = TRUE;
905
            lcount = 0;
906
        }
907
 
908
        /*
909
        **  Now read in the team record
910
        */
911
        g_record( ptr->team, &team_buf );
912
 
913
        /*
914
        **  If printing an HTML report then we need to mark
915
        **  the entry with a reference so that we can link to it
916
        */
917
        if ( report_html == html )
918
            print( "<A NAME=\"Team_%04d\"></A>",team_buf.numb );
919
 
920
        /*
921
        **  Print the basics
922
        **      - Place within the class
923
        **      - Team number - with HTML reference to team file
924
        **      - Class
925
        */
926
 
927
        if ( report_html == printed && lcount %5 == 4 ) print_underline( TRUE );
928
        print( "%4.4s ", pr_place( team_buf.leg[0].lc_place, ptr->flags.bad_times ) );
929
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
930
        print( "%4d",  team_buf.numb );
931
        if ( report_html == html ) print( "</A>" );
932
        print( " %-*s", 3, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
933
 
934
        for( j = 1; j <= config.num_legs; j++ )
935
        {
936
            /*
937
            **  Ensure that non-equestrian leg data is not displayed
938
            */
939
            if ( j == config.equestrian_leg && (ptr->teamclass == config.nonequestrian_class || ptr->flags.non_equestrian) )
940
            {
941
                print( "  %-8s %4.4s %4.4s", "-- NE --", "NE","NE");
942
            }
943
            else
944
            {
945
                print( "  %-8s %4.4s %4.4s", time_a( team_buf.leg[j].elapsed ),
946
                       pr_place( team_buf.leg[j].lc_place,  ptr->flags.bad_times ),
947
                       pr_place( team_buf.leg[j].lec_place, ptr->flags.bad_times ) );
948
            }
949
        }
950
 
951
        /*
952
        **  Print the trailer
953
        **      - Total time
954
        **      - Overall place - with reference to overall place file
955
        */
956
        /* print( "  %-8s ", time_a( team_buf.leg[0].elapsed ) ); */
957
        print( "  %-8s ", time_a( ptr->lege[0] ) );
958
 
959
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
960
        print( "%4.4s", pr_place( team_buf.leg[0].l_place, ptr->flags.bad_times || (ptr->teamclass == config.nonequestrian_class)));
961
        if ( report_html == html ) print( "</A>" );
962
 
963
        if ( report_html == printed && lcount %5 == 4 ) print_underline( FALSE );
964
        lcount++;
965
        print( "\n" );
966
    }
967
 
968
    print_class_stats( last_class, TRUE );
969
    print_legend(last_class,1);
970
    close_printer();
971
 
972
    /*
973
    **  Pickup missed classes and create a report
974
    */
975
    for( j = 1; j <= config.num_class; j++ )
976
    {
977
        if ( class_done[j] )
978
        {
979
            continue;
980
        }
981
 
982
        report_title = tprintf( "Category results for : %-*s", LEN_CLASS_NAME, config.team_class[j - 1].full_name );
983
 
984
        if( !open_printer( "", config.team_class[j - 1].abr, 132, report_html, report_title ) )
985
            continue;
986
        print_class_header( j-1, TRUE );
987
        print( "\nThere were no competitors in this class\n" );
988
        print_legend(j,1);
989
        close_printer();
990
    }
991
 
992
 
993
    /*
994
    **  If we are generating an HTML report then we need to create the file
995
    **  that contains all the team names - the assumption is that this data
996
    **  is available
997
    */
998
    if ( report_html )
999
    {
1000
        pri_team();
1001
    }
1002
 
1003
    /*
1004
    **  Generate the awards report.
1005
    **  This is only available as an HTML report
1006
    */
1007
    if ( report_html )
1008
    {
1009
        pri_awards_html();
1010
    }
1011
 
1012
    /*
1013
    **  Generate the master index page
1014
    */
1015
    if ( report_html )
1016
    {
1017
        pri_master_index();
1018
    }
1019
 
1020
    pri_name_index_body();
1021
}
1022
 
1023
/*========================================================================
1024
 *
1025
 *  Place to text
1026
 *
1027
 *  Purpose:
1028
 *      This function is called to convert a place to text
1029
 *
1030
 *  Parameters:
1031
 *      place
1032
 *
1033
 *  Returns:
1034
 *      text
1035
 *
1036
 *========================================================================*/
1037
 
1038
char *placing ( int place )
1039
{
1040
    if ( place > MAX_PLACE )
1041
    {
1042
        return tprintf( "Place: %d", place);
1043
    }
1044
    return tprintf ("%s Place", place_text[place]);
1045
}
1046
 
1047
/*========================================================================
1048
 *
1049
 *  Print award results
1050
 *
1051
 *  Purpose:
1052
 *      This function is called to Print award results
1053
 *      Keep the page to 80 cols, so that it can be pronted on A4
1054
 *
1055
 *  Parameters:
1056
 *      None
1057
 *
1058
 *  Returns:
1059
 *      Nothing
1060
 *
1061
 *========================================================================*/
1062
void pri_awards_html(void)
1063
{
1064
    report_type saved = report_html;
1065
    /*
1066
    **  Generate ALL results with HTML tags
1067
    */
1068
    report_html = html;
1069
    pri_awards();
1070
    report_html = saved;
1071
}
1072
 
1073
/*========================================================================
1074
 *
1075
 *  Print award results
1076
 *
1077
 *  Purpose:
1078
 *      This function is called to Print award results
1079
 *      Keep the page to 80 cols, so that it can be pronted on A4
1080
 *
1081
 *  Parameters:
1082
 *      None
1083
 *
1084
 *  Returns:
1085
 *      Nothing
1086
 *
1087
 *========================================================================*/
1088
void pri_awards(void)
1089
{
1090
    int j;
1091
    int i;
1092
    int k;
1093
    int windex;
1094
    int winmax;
1095
    ty_s_data  *ptr;
170 - 1096
    team_type   team_buf;
91 - 1097
    int         last_class;
1098
    char    solid_line[100];
1099
 
1100
    /*
1101
    **  Calculate Summary information
1102
    **  Should cache the data
1103
    */
1104
    t_class_summary sdata;
1105
    calc_class_summary( & sdata );
1106
 
1107
 
1108
    if( !open_printer( "", "awards", 80, report_html, "Prizes and Awards" ) )
1109
        return;
1110
 
1111
    memset ( solid_line, 0, sizeof( solid_line ));
1112
    memset ( solid_line, '-', 80 );
1113
 
1114
    /*
1115
    **  Generate an index for this page
1116
    */
1117
    print( "\n");
1118
    if ( report_html == html )
1119
    {
1120
        print( "<hr>" );
1121
        print( "<A NAME=\"%s\"></A>",url_encode("INDEX"));
1122
    }
1123
    print( "Award Categories");
1124
 
1125
    for( j = 1; j <= config.num_class; j++ )
1126
    {
1127
        /*
1128
        **  Header for the class
1129
        */
1130
        if ( config.class_winners[j-1] <= 0 )
1131
            continue;
1132
 
1133
        winmax = config.class_winners[j-1];
1134
        {
1135
            int valid =   sdata.teamclass[j].total
1136
                        - sdata.teamclass[j].disqualified;
1137
//                        - sdata.teamclass[j].non_equestrian;
1138
            if ( valid < winmax )
1139
                winmax = valid;
1140
        }
1141
 
1142
        print( "\n");
1143
        print( "    ");
1144
        if ( report_html == html ) print( "<A HREF=\"#%s\">",url_encode(config.team_class[j-1].full_name));
1145
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[j-1].full_name ));
1146
        if ( report_html == html ) print( "</A>" );
1147
        print( "  %3d Awards", winmax );
1148
        if ( config.class_winners[j-1] != winmax )
1149
            print( " from a maximum of %3d", config.class_winners[j-1] );
1150
 
1151
    }
1152
 
1153
    /*
1154
    **  Manual entries
1155
    */
1156
    print( "\n");
1157
    print( "    ");
1158
    if ( report_html == html ) print( "<A HREF=\"#%s\">",url_encode("Hall Of Fame"));
1159
    print( "%s",  "Hall Of Fame" );
1160
    if ( report_html == html ) print( "</A>" );
1161
 
1162
    print( "\n");
1163
    print( "    ");
1164
    if ( report_html == html ) print( "<A HREF=\"#%s\">",url_encode("FASTEST"));
1165
    print( "%s",  "FASTEST" );
1166
    if ( report_html == html ) print( "</A>" );
1167
 
1168
    /*
1169
    **  Sort the data by class
1170
    */
1171
    sort_team_data( 0, S_LC );      /* Generate class placement data */
1172
    last_class = -1;                /* Invalid class to start with */
1173
 
1174
    /*
1175
    **  Process each category
1176
    */
1177
    print( "\n");
1178
    for( j = 1; ; j++ )
1179
    {
1180
        /*
1181
        **  Tail for previous entry
1182
        */
1183
        if ( j != 1 )
1184
            if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1185
 
1186
        if ( j > config.num_class  )
1187
            break;
1188
 
1189
        /*
1190
        **  Header for the class
1191
        */
1192
        print( "\n");
1193
        if ( report_html == html )
1194
        {
1195
            print( "<hr>" );
1196
            print( "<A name=\"%s\"></A>",url_encode(config.team_class[j-1].full_name));
1197
        }
1198
        else
1199
        {
1200
            print( "%s\n", solid_line);
1201
        }
1202
        print( "Category: ");
1203
        if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[j - 1].abr ,"html")));
1204
        print( "%s",  config.team_class[j-1].full_name );
1205
        if ( report_html == html ) print( "</A>" );
1206
 
1207
        if ( config.class_winners[j-1] <= 0 )
1208
        {
1209
            print( "\n");
1210
            print( "No winners awarded" );
1211
            continue;
1212
        }
1213
 
1214
        /*
1215
        **  Enties for 'n' the best teams as configured
1216
        */
1217
        windex = 0;                     /* Winners done */
1218
        unsigned int i;
1219
        for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1220
        {
1221
            if ( ptr->teamclass != j )
1222
            {
1223
                continue;
1224
            }
1225
 
1226
            /*
1227
            **  Now read in the team record
1228
            */
1229
            if( valid_field( ptr->team ) && g_record( ptr->team, &team_buf ) )
1230
            {
1231
                /*
1232
                **  Ensure we have a valid team
1233
                **  Can't award disqualified teams
1234
                **  Can't award NE teams unless its a NE award
1235
                */
1236
                if ( ptr->flags.bad_times )
1237
                    break;
1238
 
1239
                if ( ptr->teamclass != config.nonequestrian_class  && ptr->flags.non_equestrian )
1240
                    break;
1241
 
1242
                /*
1243
                **  Count the entry
1244
                */
1245
                windex++;
1246
 
1247
                /*
1248
                **  If printing an HTML report then we need to mark
1249
                **  the entry with a reference so that we can link to it
1250
                */
1251
                print( "\n");
1252
                if ( report_html == html )
1253
                {
1254
                    print( "<A NAME=\"Team_%04d\">",team_buf.numb );
1255
                    print( "</A>" );
1256
                }
1257
 
1258
                /*
1259
                **  Basic information
1260
                **      - Team number - with Xref back to full result
1261
                **      - Full team name
1262
                **      - Full categoray name
1263
                */
1264
                print( "%s", placing(windex) );
1265
 
1266
                print( "  Team Name: ");
1267
                if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1268
                print( "%-*s ",     MAX_TM_NAME, team_buf.name );
1269
                    if ( report_html == html ) print( "</A>" );
1270
 
1271
                print( "  Number: ");
1272
                if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
1273
                print( "%4d",       team_buf.numb );
1274
                if ( report_html == html ) print( "</A>" );
1275
 
1276
 
1277
                for( k = 0; k < MAX_MEMB; k++ )
1278
                {
1279
 
1280
                    /*
1281
                    **  Skip equestrian leg in the non-equestion display
1282
                    */
1283
                    if ( k + 1 == config.equestrian_leg && ptr->teamclass == config.nonequestrian_class)
1284
                        continue;
1285
 
1286
                    print( "\n");
1287
                    print( "    ");
1288
                    print( "%-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
1289
                    print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
1290
 
1291
                    print( "  %-8s", time_a( team_buf.leg[k+1].elapsed ) );
1292
                }
1293
 
1294
                print( "\n");
1295
                print( "    ");
1296
                print_bold( TRUE );
1297
                print( "%-*s %-*s  %-8s", MAX_PERSON_NAME , "Total" ,MAX_PERSON_NAME, "",time_a( team_buf.leg[0].elapsed ) );
1298
                print_bold( FALSE );
1299
                print( "\n" );
1300
            }
1301
 
1302
 
1303
            /*
1304
            **  More to do
1305
            */
1306
            if ( windex >= config.class_winners[j-1] )
1307
            {
1308
                break;
1309
            }
1310
        }
1311
    }
1312
 
1313
    /*
1314
    **  Generate the Hall of Fame information
1315
    */
1316
    print( "\n");
1317
    if ( report_html == html )
1318
    {
1319
        print( "<hr>" );
1320
        print( "<A name=\"%s\"></A>",url_encode("Hall Of Fame"));
1321
    }
1322
    else
1323
    {
1324
        print( "%s\n", solid_line);
1325
    }
1326
    print( "%s",  "Hall of Fame" );
1327
 
1328
    if ( config.num_fame  )
1329
    {
1330
        for( i = 1; i <= config.num_fame; i++ )
1331
        {
1332
            print( "\n");
1333
            print( "    %-*s", MAX_PERSON_NAME, config.hall_fame[i-1] );
1334
        }
1335
    }
1336
    else
1337
    {
195 david 1338
        qDebug( "There are no new stars for the Hall of Fame");
91 - 1339
    }
1340
    if ( report_html == html ) print( "\n");
1341
    if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1342
 
1343
    /*
1344
    **  Generate the FASTEST information
1345
    */
1346
    print( "\n" );
1347
    print( "\n");
1348
    if ( report_html == html )
1349
    {
1350
        print( "<hr>" );
1351
        print( "<A name=\"%s\"></A>",url_encode("FASTEST"));
1352
    }
1353
    else
1354
    {
1355
        print( "%s\n", solid_line);
1356
    }
1357
    print( "%s",  "FASTEST" );
1358
 
1359
    /*
1360
    **  Sort the data and then generate the stats - again
1361
    */
1362
    do_big_sort();
1363
    gen_stats();
1364
 
1365
    for( i = 1; i <= config.num_legs; i++ )
1366
    {
1367
        g_record( stats.fast.team[i][0], &team_buf );
1368
 
1369
        print( "\n");
1370
        print( "    %-13s ", config.leg_name[i - 1] );
1371
        print( "  Name: ");
1372
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1373
        print( "%-*s", MAX_PERSON_NAME, team_buf.members[i-1].name );
1374
        if ( report_html == html ) print( "</A>" );
1375
        print( "  Team :");
1376
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
1377
        print( "%4d" , stats.fast.team[i][0] );
1378
        if ( report_html == html ) print( "</A> " );
1379
        print( "Time:%s ", time_a( stats.fast.time[i][0] ) );
1380
 
1381
    }
1382
 
1383
    if ( report_html == html ) print( "\n");
1384
    if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1385
    print( "\n");
1386
    close_printer();
1387
}
1388
 
1389
/*========================================================================
1390
 *
1391
 *  pri_master_index
1392
 *
1393
 *  Purpose:
1394
 *      This function is called to create an HTML page that references all
1395
 *      the other pages that have been generated
1396
 *
1397
 *      Assume that they are in the same directory
1398
 *
1399
 *  Parameters:
1400
 *      None
1401
 *
1402
 *  Returns:
1403
 *      Nothing
1404
 *
1405
 *========================================================================*/
1406
void pri_master_index_entry(const char *name, const char *text)
1407
{
1408
    print( "<tr><td>");
1409
    print ("<A HREF=\"%s\">%s</A>\n", url_encode(p_filename(filebase, name ,"html")), text );
1410
}
1411
 
1412
 
1413
void pri_master_index(void)
1414
{
1415
    int j;
1416
 
1417
    report_html = html;
1418
    if( !open_printer( "", "index", 132, report_html, "Master Index" ) )
1419
        return;
1420
 
1421
    /*
1422
    **  Names
1423
    */
1424
    print( "<TABLE border=0 align=center>" );
1425
    pri_master_index_entry( "name", "Team list" );
1426
    pri_master_index_entry( "competitor", "Competitor list" );
1427
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
1428
    pri_master_index_entry( "awards", "Prizes and Awards" );
1429
    print( "<tr><td>\n" );
1430
 
1431
    print( "\n" );
1432
    for( j = 1; j <= config.num_class; j++ )
1433
    {
1434
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
1435
    }
1436
 
1437
    print( "</TABLE>" );
1438
 
1439
    close_printer();
1440
 
1441
    /*
1442
    **  A small page to hold the Leg End displays
1443
    */
1444
 
1445
    if( !open_printer( "", "legindex", 132, report_html, "Master Index with trace data" ) )
1446
        return;
1447
 
1448
    /*
1449
    **  Names
1450
    */
1451
    print( "<TABLE border=0 align=center>" );
1452
#if 1
1453
    pri_master_index_entry( "name", "Team list" );
1454
    pri_master_index_entry( "competitor", "Competitor list" );
1455
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
1456
    pri_master_index_entry( "awards", "Prizes and Awards" );
1457
    print( "<tr><td>\n" );
1458
 
1459
    print( "\n" );
1460
    for( j = 1; j <= config.num_class; j++ )
1461
    {
1462
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
1463
    }
1464
#endif
1465
    print( "<tr><td>\n" );
1466
 
1467
    print( "\n" );
170 - 1468
    for ( int leg = 1; leg <= config.num_legs; leg ++ )
91 - 1469
    {
1470
        pri_master_index_entry( tprintf("lg%1.1d", leg), tprintf("Leg End Results for: %d", leg) );    
1471
    }
1472
 
1473
    print( "<tr><td>\n" );
1474
    print( "\n" );
1475
 
170 - 1476
    for ( int leg = 1; leg <= config.num_legs; leg ++ )
91 - 1477
    {
1478
        pri_master_index_entry( tprintf("le%1.1d", leg), tprintf("Leg Elapsed Time Results for: %d", leg) );
1479
    }    
1480
 
1481
 
1482
    print( "</TABLE>" );
1483
 
1484
    close_printer();
1485
 
1486
}
1487
 
1488
 
1489
/*========================================================================
1490
 *
1491
 *  Print interim results
1492
 *
1493
 *  Purpose:
1494
 *      This function is called to Print interim results
1495
 *
1496
 *  Parameters:
1497
 *      None
1498
 *
1499
 *  Returns:
1500
 *      Nothing
1501
 *
1502
 *========================================================================*/
1503
 
1504
void pri_interim(void)
1505
{
1506
    ty_s_data  *ptr;
170 - 1507
    team_type   team_buf;
91 - 1508
    unsigned    i;
1509
    int         j, last_class;
1510
    char       *report_title;
1511
 
1512
    if ( ! report_all )
1513
    {
195 david 1514
        ck_data( -1, C_DISQUAL );              /* Check the data - dummy check */
91 - 1515
    }
1516
    do_big_sort();                             /* Sort on every thing */
1517
    gen_stats();                               /* Generate the stats too */
1518
 
1519
    /*
1520
     * Now print the data on the printer 
1521
     */
1522
 
1523
    if( !open_printer( "", "int", 132, report_html, "Interim Results" ) )
1524
        return;
1525
 
1526
    /*
1527
     * Print out the data 
1528
     */
1529
    print_class_header( -1, FALSE );                     /* Print the header */
1530
 
1531
    ptr = sort_data;
1532
    sort_team_data( 0, S_TEAM );                   /* Re-sort on team number */
1533
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1534
    {
1535
        if ( ptr->teamclass == config.nonequestrian_class )
1536
            continue;
1537
 
1538
        g_record( ptr->team, &team_buf );
1539
 
1540
        print( "%4d %4.4s %-*s",
1541
               team_buf.numb,
1542
               pi_place( team_buf.leg[config.num_legs].le_place,
1543
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ),
1544
               3,
1545
               team_buf.teamclass ==
1546
 
1547
        for( j = 1; j <= config.num_legs; j++ )
1548
        {
1549
            print( "  %-8s %4.4s %4.4s",
1550
                   time_fa( team_buf.leg[j].elapsed,
1551
                            ptr->flags.disqualified ),
1552
                   pi_place( team_buf.leg[j].l_place, ptr->flags.disqualified,
1553
                             team_buf.leg[j].elapsed ),
1554
                   pi_place( team_buf.leg[j].le_place,
1555
                             ptr->flags.disqualified,
1556
                             team_buf.leg[j].elapsed ) );
1557
        }
1558
        print( "  %-8s %4.4s\n",
1559
               time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ),
1560
               pi_place( team_buf.leg[config.num_legs].lec_place,
1561
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ) );
1562
    }
1563
 
1564
    print_class_stats( -1, FALSE );             /* Print statistics */
1565
    print_legend(-1, 1);                        /* Print the legend */
1566
    close_printer();                            /* Close the printer */
1567
 
1568
 
1569
    /*
1570
     * Now produce a breakdown on a class by class basis 
1571
     * Now print out the class placement information
1572
     */
1573
 
1574
    sort_team_data( 0, S_CLASS );              /* Generate class placement data */
1575
    last_class = -1;                             /* Invalid class to start with */
1576
 
1577
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1578
    {
1579
        /*
1580
        **  Detect a change in the "class"
1581
        **  All data is within the one array of data
1582
        **  Use the in-memory class as this MAY differ from that stored
1583
        **  The non-equestrian class does this.
1584
        */
1585
        if( last_class != ptr->teamclass )
1586
        {
1587
            if( last_class >= 0 )
1588
            {
1589
                print_class_stats( last_class, TRUE );
1590
                print_legend(last_class, 1);
1591
                close_printer();
1592
            }
1593
 
1594
            report_title = tprintf( "Interim Category results for : %-*s", LEN_CLASS_NAME, team_buf.teamclass == 0 ? "" : config.team_class[ptr->teamclass - 1].full_name );
1595
 
1596
            if( !open_printer( "", tprintf( "i%2s", config.team_class[ptr->teamclass - 1].abr ), 132, report_html, report_title ) )
1597
                continue;
1598
            print_class_header( last_class = ptr->teamclass, FALSE );
1599
        }
1600
 
1601
        /*
1602
        **  Now read in the team record
1603
        */
1604
        g_record( ptr->team, &team_buf );
1605
        print( "%4d %4.4s %-*s",
1606
               team_buf.numb,
1607
               pi_place( team_buf.leg[config.num_legs].lec_place,
1608
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ),
1609
               3,
1610
               team_buf.teamclass ==
1611
 
1612
        for( j = 1; j <= config.num_legs; j++ )
1613
        {
1614
            print( "  %-8s %4.4s %4.4s",
1615
                   time_fa( team_buf.leg[j].elapsed,
1616
                            ptr->flags.disqualified ),
1617
                   pi_place( team_buf.leg[j].lc_place,
1618
                             ptr->flags.disqualified,
1619
                             team_buf.leg[j].elapsed ),
1620
                   pi_place( team_buf.leg[j].lec_place,
1621
                             ptr->flags.disqualified,
1622
                             team_buf.leg[j].elapsed ) );
1623
        }
1624
        print( "  %-8s %4.4s\n",
1625
               time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ),
1626
               pi_place( team_buf.leg[config.num_legs].le_place,
1627
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ) );
1628
    }
1629
 
1630
    print_class_stats( last_class, FALSE );
1631
    print_legend(last_class, 1);
1632
    close_printer();
1633
 
1634
}
1635
 
1636
/*----------------------------------------------------------------------------
1637
** FUNCTION           : pri_csv_data
1638
**
1639
** DESCRIPTION        : Generate a CSV file of all the report data
1640
**                      It can then be used to play with the data externally
1641
**
1642
**
1643
** INPUTS             : None
1644
**
1645
** RETURNS            : Yes it does
1646
**
1647
----------------------------------------------------------------------------*/
1648
 
1649
 
1650
void pri_csv_data ( void )
1651
{
1652
    int i;
1653
    int j;
1654
    int age_sum;
170 - 1655
    team_type   team_buf;
91 - 1656
 
1657
    /*
1658
    **  Sort on every thing
1659
    **  Then generate all the stats too
1660
    */
1661
    do_big_sort();
1662
    gen_stats();
1663
 
1664
    /*
1665
     * Now print the data on the printer 
1666
     */
1667
 
1668
    if( !open_printer( "full_data", "csv", 2000, text, NULL ) )
1669
        return;
1670
 
1671
    /*
1672
    **  Print headings
1673
    */
1674
    csv_print( "%s",   "Team Number" );
1675
    csv_print( "%s",   "Team Name" );
1676
 
1677
    csv_print( "%s",    "Class Full");
1678
    csv_print( "%s",    "Class Abr");
1679
    csv_print( "%s",    "Class Start Time");
1680
    csv_print( "%s",    "Class Start Time Number");
1681
 
1682
    csv_print( "%s",    "Team Country");
1683
 
1684
    for( j = 1; j <= config.num_legs; j++ )
1685
    {
1686
        csv_print( "%s", "Leg Number" );
1687
        csv_print( "%s", "Leg Name");
1688
        csv_print( "%s", "Competitor Name");
1689
        csv_print( "%s", "Sex" );
1690
        csv_print( "%s", "Age");
1691
        csv_print( "%s", "Start Time");
1692
        csv_print( "%s", "Start Time Number");
1693
        csv_print( "%s", "End Time" );
1694
        csv_print( "%s", "End Time Number" );
1695
        csv_print( "%s", "Elapsed Time");
1696
        csv_print( "%s", "Elapsed Time Number");
1697
        csv_print( "%s", "Leg Place");
1698
        csv_print( "%s", "Leg End Place");
1699
        csv_print( "%s", "Leg Class Place");
1700
        csv_print( "%s", "Leg End Class Place");
1701
        csv_print( "%s", "Manual");
1702
    }
1703
 
1704
    j = 0;
1705
    csv_print( "%s", "Team Start Time");
1706
    csv_print( "%s", "Team Start Time Number");
1707
    csv_print( "%s", "Team End Time");
1708
    csv_print( "%s", "Team End Time Number");
1709
    csv_print( "%s", "Team Elapsed Time");
1710
    csv_print( "%s", "Team Elapsed Time Number");
1711
//            csv_print( "%s", team_buf.leg[j].l_place );
1712
    csv_print( "%s", "Team Leg End Place");
1713
//            csv_print( "%s", team_buf.leg[j].lc_place );
1714
    csv_print( "%s", "Team Leg Class Place");
1715
//            csv_print( "%s", team_buf.leg[j].manual );
1716
 
1717
    csv_print( "%s", "Total Team Age");
1718
    csv_print( "%s", "Flag:valid Team");
1719
    csv_print( "%s", "Flag:bad_times" );
1720
    csv_print( "%s", "Flag:disqualified" );
1721
    csv_print( "%s", "Flag:non_equestrian" );
1722
    csv_print("\n");
1723
 
1724
 
1725
    for( i = config.min_team; i <= config.max_team; i++ )
1726
    {
1727
        if( valid_field( i ) && g_record( i, &team_buf ) )
1728
        {
1729
            /*
1730
            **  Basic information
1731
            **      - Team number - with Xref back to full result
1732
            **      - Full team name
1733
            **      - Full categoray name - with Xref to category results
1734
            **      - Country name
1735
            */
1736
            csv_print( "%d",   team_buf.numb );
1737
            csv_print( "%s",   team_buf.name );
1738
 
1739
            csv_print( "%s",    team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].full_name );
1740
            csv_print( "%s",    team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
1741
            csv_print( "%s",    time_a (team_buf.teamclass == 0 ? 0 : config.team_class[team_buf.teamclass - 1].start ));
1742
            csv_print( "%d",    team_buf.teamclass == 0 ? 0 : config.team_class[team_buf.teamclass - 1].start );
1743
 
1744
            csv_print( "%s",    config.num_countries == 0
1745
                                || team_buf.country ==
1746
 
1747
 
1748
            age_sum = 0;
1749
            for( j = 1; j <= config.num_legs; j++ )
1750
            {
1751
                csv_print( "%d", j );
1752
                csv_print( "%s", config.leg_name[j - 1] );
1753
                csv_print( "%s", team_buf.members[j-1].name );
1754
                csv_print( "%s", ( team_buf.members[j-1].sex == male ) ? "Male" : "Female" );
1755
                csv_print( "%d", team_buf.members[j-1].age );
1756
                if ( age_sum >= 0 )
1757
                {
1758
                    ushort age = team_buf.members[j-1].age;
1759
                    if ( age > 0 && age < 255 )
1760
                    {
1761
                        age_sum += age;
1762
                    }
1763
                    else
1764
                    {
1765
                        age_sum = -1;
1766
                    }
1767
                }
1768
 
1769
 
1770
                csv_print( "%s", time_a(team_buf.leg[j].start ));
1771
                csv_print( "%d", team_buf.leg[j].start );
1772
                csv_print( "%s", time_a(team_buf.leg[j].end ));
1773
                csv_print( "%d", team_buf.leg[j].end );
1774
                csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
1775
                csv_print( "%d", team_buf.leg[j].elapsed );
1776
                csv_print( "%d", team_buf.leg[j].l_place );
1777
                csv_print( "%d", team_buf.leg[j].le_place );
1778
                csv_print( "%d", team_buf.leg[j].lc_place );
1779
                csv_print( "%d", team_buf.leg[j].lec_place );
1780
                csv_print( "%d", team_buf.leg[j].manual );
1781
            }
1782
 
1783
            j = 0;
1784
            csv_print( "%s", time_a(team_buf.leg[j].start ));
1785
            csv_print( "%d", team_buf.leg[j].start );
1786
            csv_print( "%s", time_a(team_buf.leg[j].end ));
1787
            csv_print( "%d", team_buf.leg[j].end );
1788
            csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
1789
            csv_print( "%d", team_buf.leg[j].elapsed );
1790
//            csv_print( "%d", team_buf.leg[j].l_place );
1791
            csv_print( "%d", team_buf.leg[j].le_place );
1792
//            csv_print( "%d", team_buf.leg[j].lc_place );
1793
            csv_print( "%d", team_buf.leg[j].lec_place );
1794
//            csv_print( "%d", team_buf.leg[j].manual );
1795
 
1796
            csv_print( "%d", age_sum );
1797
            csv_print( "%d", team_buf.flags.valid );
1798
            csv_print( "%d", team_buf.flags.bad_times );
1799
            csv_print( "%d", team_buf.flags.disqualified );
1800
            csv_print( "%d", team_buf.flags.non_equestrian );
1801
 
1802
//How about class placings
1803
 
1804
 
1805
            csv_print( "\n" );
1806
        }
1807
    }
1808
 
1809
    close_printer();
1810
}
1811
 
1812
 
1813
/*========================================================================
1814
 *
1815
 *  Print all reports at once
1816
 *  Its all so fast, these days ...
1817
 *
1818
 *  Purpose:
1819
 *      This function is called to print all reports at once
1820
 *
1821
 *  Parameters:
1822
 *      None
1823
 *
1824
 *  Returns:
1825
 *      Nothing
1826
 *
1827
 *========================================================================*/
1828
 
1829
void pri_all_reports ( void )
1830
{
1831
    int leg;
1832
    report_all = TRUE;
1833
 
1834
    pri_team();
1835
 
1836
    for ( leg = 1; leg <= config.num_legs; leg ++ )
1837
    {
1838
        pri_leg_body ( leg );
1839
        pri_eleg_body ( leg );
1840
 
1841
        report_html = html;
1842
 
1843
        pri_leg_body ( leg );
1844
        pri_eleg_body ( leg );        
1845
 
1846
        report_html = text;
1847
    }
1848
 
1849
    pri_final();
1850
    pri_final_html();
1851
    pri_csv_data();
1852
    pri_summary();
1853
    pri_awards_html();
1854
    pri_awards();
1855
    pri_master_index();
1856
 
168 david 1857
    pri_interim();
1858
 
91 - 1859
    report_all = FALSE;
1860
}
1861
 
1862
 
1863
/*========================================================================
1864
 *
1865
 *  Print a class header
1866
 *
1867
 *  Purpose:
1868
 *      This function is called to print a class header
1869
 *
1870
 *  Parameters:
1871
 *      class           Name of this class
1872
 *      final           False - prelim results
1873
 *
1874
 *  Returns:
1875
 *      Nothing
1876
 *
1877
 *========================================================================*/
1878
 
1879
void print_class_header( int teamclass, int final )
1880
{
1881
    int         j;
1882
 
1883
    /*
1884
    **  Give a clear indication that the report is preliminary
1885
    */
1886
 
1887
    if( !final )
1888
        print( "PRELIMINARY RESULTS ONLY\n\n" );
1889
 
1890
    /*
1891
    **  Now printout the column headings
1892
    **  This is a two line display
1893
    **
1894
    **  Line-1  Leg names
1895
    */
1896
    print( "%-*s %-*s %-*s", 4, "", 4, "", 3, "" );
1897
    for( j = 1; j <= config.num_legs; j++ )
1898
    {
1899
        print_bold( TRUE );
1900
        print( "  %-*s", 18, config.leg_name[j - 1] );
1901
        print_bold( FALSE );
1902
    }
1903
    print( "  %-8s %-4s\n", "Total", ( teamclass < 0 ) ? "Cat" : "Fin" );
1904
 
1905
 
1906
    /*
1907
    **  Line-2  Details
1908
    */
1909
    print_underline( TRUE );
1910
    print( "%-*s %*s %-*s", 4, final ? "Plce" : "Team",
1911
                            4, final ? "Team" : "Plce",
1912
                            3, "Cat" );
1913
 
1914
    for( j = 1; j <= config.num_legs; j++ )
1915
        print( "  %-8s %-4s %-4s", "Time", " LP", " EP" );
1916
 
1917
    print( "  %-8s %-4s\n", "Time", "Plce" );
1918
    print_underline( FALSE );
1919
}
1920
 
1921
/*========================================================================
1922
 *
1923
 *  Generate the class stats
1924
 *
1925
 *  Purpose:
1926
 *      This function is called to Generate the class stats
1927
 *
1928
 *  Parameters:
1929
 *      c           Class to print
1930
 *      final       TRUE: Final data
1931
 *                  FALSE: Interim data
1932
 *
1933
 *  Returns:
1934
 *      Nothing
1935
 *
1936
 *========================================================================*/
1937
 
1938
void print_class_stats( int c, int final )
1939
{
1940
    int         i, j;
1941
    const char        *title;
1942
 
1943
    if( c < 0 )
1944
    {
1945
        title = "Event";
1946
        c = 0;
1947
    }
1948
    else
1949
    {
1950
        title = "Category";
1951
    }
1952
 
1953
    print( "\n" );
1954
    if ( report_html ) print_underline(TRUE);
1955
    print( "%s statistics", title );
1956
    if ( report_html ) print_underline(FALSE);
1957
    print( "\n" );
1958
 
1959
 
1960
    /*
1961
    **  Print the names of the different legs
1962
    */
1963
    print( "%-*s       ", LEN_CLASS_NAME, "" );
1964
    for( i = 1; i <= config.num_legs; i++ )
1965
    {
1966
        print_bold( TRUE );
1967
        print( "%-13s  ", config.leg_name[i - 1] );
1968
        print_bold( FALSE );
1969
    }
1970
    print( "%-13s  \n", final ? "Total" : "" );
1971
 
1972
    /*
1973
    **  Print the fastest teams for each leg and overall
1974
    **  Add cross references to the team names for the fastest teams
1975
    */
1976
    print( "%*s : ", LEN_CLASS_NAME, "Fastest" );
1977
    for( i = 0; i <= config.num_legs; i++ )
1978
    {
1979
        j = i + 1;
1980
        if( i >= config.num_legs )
1981
        {
1982
            if( final )
1983
                j = 0;                           /* Leg-0 last */
1984
            else
1985
                break;
1986
        }
1987
 
1988
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), stats.fast.team[j][c] );
1989
        print( "%4d",  stats.fast.team[j][c] );
1990
        if ( report_html == html ) print( "</A>" );
1991
        print( " %s  ", time_a( stats.fast.time[j][c] ) );
1992
    }
1993
    print( "\n" );
1994
 
1995
    /*
1996
    **  Print the average time for each leg
1997
    */
1998
    print( "%*s : ", LEN_CLASS_NAME, "Average" );
1999
    for( i = 0; i <= config.num_legs; i++ )
2000
    {
2001
        j = i + 1;
2002
        if( i >= config.num_legs )
2003
        {
2004
            if( final )
2005
                j = 0;                           /* Leg-0 last */
2006
            else
2007
                break;
2008
        }
2009
        print( "     %s  ", time_a( stats.average[j][c] ) );
2010
    }
2011
}
2012
 
2013
/*========================================================================
2014
 *
2015
 *  Print the legend
2016
 *
2017
 *  Purpose:
2018
 *      This function is called to Print the legend
2019
 *
2020
 *  Parameters:
2021
 *      class       - Class currently being printed
2022
 *      full        - Display full legend
2023
 *
2024
 *  Returns:
2025
 *      Nothing
2026
 *
2027
 *========================================================================*/
2028
 
2029
void print_legend ( int teamclass, int full )
2030
{
2031
 
2032
    int         i;
2033
    char        line[201];
2034
    FILE       *adfile = NULL;
2035
    int         count;
2036
 
2037
    /*
2038
     * First the categories 
2039
     */
2040
    print( "\n\n" );
2041
    if ( report_html ) print_underline(TRUE);
2042
    print( "Category abbreviations" );
2043
    if ( report_html ) print_underline(FALSE);
2044
    print( "\n" );
2045
 
2046
 
2047
    for( i = 1, count = 0; i <= config.num_class; i++ )
2048
    {
2049
#if 0
2050
        /*
2051
        **  Skip any non-equestrian class in the legend
2052
        **  Don't want to tell the general user whats goes on, unless we actually
2053
        **  creating the non-equestrian report.
2054
        */
2055
        if ( class != config.nonequestrian_class  && i == config.nonequestrian_class )
2056
            continue;
2057
#endif
2058
        if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[i - 1].abr ,"html")) );
2059
        print( "%-*s", 3, config.team_class[i - 1].abr );
2060
        if ( report_html == html ) print( "</A>" );
2061
        print( " : %-*s  ", LEN_CLASS_NAME, config.team_class[i - 1].full_name );
2062
 
2063
        if( !( ++count % 5 ) )
2064
            print( "\n" );
2065
    }
2066
 
2067
    /*
2068
    **  Add link to the finish order report
2069
    */
2070
    if ( report_html == html )
2071
    {
2072
        print( "<A HREF=\"%s\">", url_encode(p_filename(filebase, "finish" ,"html")) );
2073
        print( "%-*s", 3, "All" );
2074
        print( "</A>" );
2075
        print( " : %-*s  ", LEN_CLASS_NAME, "Finishing Order" );
2076
    }
2077
 
2078
    /*
2079
    **  Country data - if countries have been defined
2080
    */
2081
    if( config.num_countries )
2082
    {
2083
        print( "\n\n" );
2084
        if ( report_html ) print_underline(TRUE);
2085
        print( "Country abbreviations" );
2086
        if ( report_html ) print_underline(FALSE);
2087
        print( "\n" );
2088
 
2089
        for( i = 0, count = 0; i < MAX_COUNTRY; i++ )
2090
        {
2091
            if( config.country_name[i].abr[0] )
2092
            {
2093
                print( "%-*s : %-*s  ", 4, config.country_name[i].abr,
2094
                       LEN_CNTRY_NAME, config.country_name[i].full_name );
2095
                if( !( ++count % 5 ) )
2096
                    print( "\n" );
2097
            }
2098
        }
2099
    }
2100
    print( "\n" );
2101
 
2102
    /*
2103
     * Other comments 
2104
     */
2105
    if ( full )
2106
    {
2107
        print( "\nPlace numbers (LP and EP)\n" );
2108
        print( "LP - Placing based on elapsed time within the leg.                Cat Plce - Placing within the category.\n" );
2109
        print( "EP - Placing based on accumulated times to the end of that leg.   Fin Plce - Overall placing within the event.\n" );
2110
        print( "U  - Placing not available.\n" );
2111
    }
2112
 
2113
    /*
2114
     *  Insert the contents of the config.addendum file
2115
     *  or a defualt message
2116
     */
2117
    if( config.addendum[0] )
199 david 2118
    {
2119
        const char *name = QmConfig::getAddendemFile(config.addendum);
2120
        if (name)
2121
            adfile = fopen( name, "rt" );  /* Open the file for reading */
2122
    }
91 - 2123
 
2124
    if( adfile )
2125
    {
2126
        while( fgets( line, sizeof(line)-1, adfile ) )
2127
            print( "%s", line );
2128
    }
2129
    else
2130
    {
2131
        print( "\nTiming and Results by\n" );
2132
        print( "Embedded Solutions\n" );
2133
    }
2134
}
2135
 
2136
/*========================================================================
2137
 *
2138
 *  Return place data
2139
 *
2140
 *  Purpose:
2141
 *      This function is called to return place data
2142
 *
2143
 *      This routine is called to fill a print team_buffer - to allow for
2144
 *      multiple calls to this function ( before the data is used ) a number
2145
 *      of static team_buffers are maintained
2146
 *
2147
 *  Parameters:
2148
 *      num         place - if not bad_times
2149
 *      disq        Disqualified flag
2150
 *      time        Time data is based on
2151
 *
2152
 *  Returns:
2153
 *      This function returns a pointer to the character string for the
2154
 *      number or a pointer to a bad_times string.
2155
 *
2156
 *========================================================================*/
2157
 
2158
const char       *pi_place( int num, int disq, time_t time )
2159
{
2160
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2161
    static int  i = 0;                           /* Current index into store */
2162
    static const char *dis = "D";                      /* Disqualified */
2163
    static const char *non = "-";                      /* Invalid time */
2164
 
2165
    if( disq )                                   /* Disqualified team */
2166
        return ( dis );
2167
    if( time <= 0 )                              /* Unknown time */
2168
        return ( non );
2169
 
2170
    i++;
2171
    if( i >= 2 )
2172
        i = 0;                                   /* Select next entry */
2173
    sprintf( store[i], "%4d", num );
2174
    return ( store[i] );
2175
 
2176
}
2177
 
2178
/*========================================================================
2179
 *
2180
 *  Return place data
2181
 *
2182
 *  Purpose:
2183
 *      This function is called to Return place data
2184
 *
2185
 *      This routine is called to fill a print team_buffer - to allow for
2186
 *      multiple calls to this function ( before the data is used ) a number
2187
 *      of static team_buffers are maintained
2188
 *
2189
 *  Parameters:
2190
 *      num         place - if not bad_times
2191
 *      disq        Disqualified flag
2192
 *
2193
 *  Returns:
2194
 *      This function returns a pointer to the character string for the
2195
 *      number or a pointer to a bad_times string.
2196
 *
2197
 *========================================================================*/
2198
 
2199
const char *pr_place( int num, int disq )
2200
{
2201
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2202
    static int  i = 0;                           /* Current index into store */
2203
    static const char *dis = "U";
2204
 
2205
    if( disq )
2206
        return ( dis );
2207
 
2208
    i++;
2209
    if( i >= 2 )
2210
        i = 0;                                   /* Select next entry */
2211
    sprintf( store[i], "%4d", num );
2212
    return ( store[i] );
2213
 
2214
}
2215
 
2216
/*========================================================================
2217
 *
2218
 *  Return place data
2219
 *
2220
 *  Purpose:
2221
 *      This function is called to Return place data
2222
 *
2223
 *      This routine is called to fill a print team_buffer - to allow for
2224
 *      multiple calls to this function ( before the data is used ) a number
2225
 *      of static team_buffers are maintained
2226
 *
2227
 *  Parameters:
2228
 *      num         place - if not bad_times
2229
 *      disq        Disqualified flag
2230
 *      ne          Non Equestrian Flag
2231
 *
2232
 *  Returns:
2233
 *      This function returns a pointer to the character string for the
2234
 *      number or a pointer to a bad_times string.
2235
 *
2236
 *========================================================================*/
2237
 
2238
const char *pr_place_ne( int num, int disq, int ne )
2239
{
2240
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2241
    static int  i = 0;                           /* Current index into store */
2242
    static const char *dis = "U";
2243
 
2244
    if( disq && ! ne )
2245
         return ( dis );
2246
 
2247
    if( ++i >= 2 )
2248
        i = 0;                                   /* Select next entry */
2249
    sprintf( store[i], "%4d", num );
2250
    return ( store[i] );
2251
 
2252
}
2253
 
2254
/*========================================================================
2255
 *
2256
 *  Check data for bad times
2257
 *
2258
 *  Purpose:
2259
 *      This function is called to Check data for bad times
2260
 *      Scan the sort data structure and locate entries that have incorrect
2261
 *      times.
2262
 *      
2263
 *      Entries that have invalid leg times are displayed to the operator
2264
 *      and the report process can be aborted
2265
 *
2266
 *  Parameters:
189 - 2267
 *      leg             Leg to test
91 - 2268
 *      mode            Either end or elapsed times to be tested
2269
 *
2270
 *  Returns:
2271
 *      Returns FALSE if the report operation is to be aborted
2272
 *
2273
 *========================================================================*/
2274
 
2275
bool ck_data( int leg, int mode )
2276
{
2277
    ty_s_data  *ptr;
2278
    unsigned    i;
2279
    int         bad = 0;
189 - 2280
    int         k;
2281
    int         bad_leg;
91 - 2282
    time_t     *t;                               /* An array of times */
2283
 
2284
    ptr = sort_data;
2285
    for( i = 1; i <= sort_num; i++, ptr++ )
2286
    {
2287
        bad_leg = 0;
2288
        if( mode == C_DISQUAL )
2289
        {
2290
            ptr->flags.bad_times = ptr->flags.disqualified;
2291
            continue;
2292
        }
2293
 
2294
        if( mode == C_ELAPSED )
2295
            t = ptr->lege;
2296
        else
2297
            t = ptr->leg;
2298
 
2299
 
2300
        if ( leg >= 0 )
2301
        {
2302
            ptr->flags.bad_times = (ptr->flags.disqualified && ! ptr->flags.non_equestrian);
2303
        }
2304
        else
2305
        {
2306
            ptr->flags.bad_times = ptr->flags.disqualified;
2307
        }
2308
 
2309
        if( ! ptr->flags.bad_times )
2310
        {
2311
            if( leg <= 0 )
2312
            {
2313
                for( k = 0; k <= config.num_legs; k++ )
2314
                {
2315
                    if ( !(config.equestrian_leg && ptr->flags.non_equestrian && config.equestrian_leg == k  ))
2316
                        bad_leg |= ( t[k] <= 0 );
2317
                }
2318
            }
2319
            else
2320
            {
2321
                bad_leg = t[leg] <= 0;
2322
            }
2323
 
2324
            if( bad_leg )
2325
            {
2326
                ptr->flags.bad_times = TRUE;
2327
 
2328
                if ( ! report_all )
2329
                {
189 - 2330
                    printf( "Team with incorrect time information: %d", ptr->team  );
91 - 2331
                    bad++;
2332
                }
2333
            }
2334
        }
2335
    }
2336
 
2337
    if( bad )
2338
    {
189 - 2339
        printf( "%d teams with incorrect times. These have been flagged as unplaced", bad );
91 - 2340
    }
2341
    return ( FALSE );
2342
}
2343
 
2344
/*========================================================================
2345
 *
2346
 *  Update placing information
2347
 *
2348
 *  Purpose:
2349
 *      This function is called to Update placing information
2350
 *
2351
 *      This routine will rip through the data generating the team placing in
2352
 *      a) Within a leg
2353
 *      b) At the end of a leg
2354
 *      c) Within a leg by  class
2355
 *      d) At the end of a leg by class
2356
 *
2357
 *      This function is provided to allow the display routines to
2358
 *      be accessed and updated without the need to run a report
2359
 *
2360
 *  Parameters:
2361
 *      xxxx        a ptr to the xxxx stuff
2362
 *
2363
 *  Returns:
2364
 *      Nothing
2365
 *
2366
 *========================================================================*/
2367
 
2368
void srt_place(void)
2369
{
2370
    if( ck_data( -1, C_ELAPSED ) )
2371
        return;
2372
    do_big_sort();
2373
 
2374
    /*
195 david 2375
     * Generate the stats
91 - 2376
     */
2377
    gen_stats();
2378
}
2379
 
2380
/*========================================================================
2381
 *
2382
 *  Calculate summary information
2383
 *
2384
 *  Purpose:
2385
 *      This function is called to calculate summary information
2386
 *
2387
 *  Parameters:
2388
 *      ptr         - Address of a summary structure to fill in
2389
 *
2390
 *  Returns:
2391
 *      Nothing
2392
 *
2393
 *========================================================================*/
2394
 
2395
void calc_class_summary( t_class_summary * ptr )
2396
{
2397
    int i;
170 - 2398
    team_type   team_buf;
91 - 2399
 
2400
    /*
2401
    **  Reset the data
2402
    */
2403
    memset ( ptr, 0, sizeof (*ptr ));
2404
 
2405
    /*
2406
     * Extract the required data from the data base
2407
     * Only save that information required for the operation
2408
     */
2409
 
2410
    for( i = config.min_team; i <= config.max_team; i++ )
2411
    {
2412
        if( valid_field( i ) && g_record( i, &team_buf ) )
2413
        {
2414
            ptr->total.total++;
2415
            ptr->teamclass[team_buf.teamclass].total++;
2416
 
2417
            if ( team_buf.flags.disqualified )
2418
            {
2419
                ptr->teamclass[team_buf.teamclass].disqualified++;
2420
                ptr->total.disqualified++;
2421
            }
2422
 
2423
            if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
2424
            {
2425
                ptr->teamclass[team_buf.teamclass].non_equestrian++;
2426
                ptr->total.non_equestrian++;
2427
            }
2428
        }
2429
    }
2430
 
2431
    /*
2432
    **  Fix up the totals for the non equestrians
2433
    **  This is not a real category but a summary of the others.
2434
    */
2435
    if ( config.nonequestrian_class  )
2436
    {
2437
        ptr->teamclass[config.nonequestrian_class].total += ptr->total.non_equestrian;
2438
    }
2439
}
2440
 
2441
/*========================================================================
2442
 *
2443
 *  Print summary information
2444
 *
2445
 *  Purpose:
2446
 *      This function is called to print summary information
2447
 *
2448
 *  Parameters:
2449
 *      None
2450
 *
2451
 *  Returns:
2452
 *      Nothing
2453
 *
2454
 *========================================================================*/
2455
 
2456
void pri_summary (void)
2457
{
2458
 
2459
    t_class_summary data;
2460
    int         i;
2461
 
2462
    if( !open_printer( "", "summary", 132, text, "Summary Information" ) )
2463
        return;
2464
 
2465
    calc_class_summary( & data );
2466
 
2467
 
2468
    /*
2469
    **  Display summary stats
2470
    */
2471
 
2472
    print( "%*s : %-7s %-7s%-7s\n", LEN_CLASS_NAME,
2473
                                      "Category",
2474
                                      "Total",
2475
                                      "Disq",
2476
                                      "NonEq" );
2477
 
2478
    for( i = 0; i < config.num_class; i++ )
2479
    {
2480
        /*
2481
        **  The non-equestrian leg does not have any data
2482
        **  Supress the display
2483
        */
2484
        if ( config.nonequestrian_class == i+1  )
2485
            continue;
2486
 
2487
        print( "%*s : %-7d %-7d %-7d\n", LEN_CLASS_NAME,
2488
                                          config.team_class[i].full_name,
2489
                                          data.teamclass[i+1].total,
2490
                                          data.teamclass[i+1].disqualified,
2491
                                          data.teamclass[i+1].non_equestrian );
2492
    }
2493
 
2494
    print( "\n" );
2495
    print( "%*s : %-7d %-7d %-7d\n", LEN_CLASS_NAME,
2496
                                      "Totals",
2497
                                      data.total.total,
2498
                                      data.total.disqualified,
2499
                                      data.total.non_equestrian );
2500
 
2501
    close_printer();
2502
}
2503
 
2504
 
2505
 
2506
/*========================================================================
2507
 *
2508
 *  Main sort routine for final data
2509
 *
2510
 *  Purpose:
2511
 *      This function is called to do the report sort routine for final data
2512
 *      This routine will fill all the gaps in the sort_aux structure
2513
 *
2514
 *  Parameters:
2515
 *      None
2516
 *
2517
 *  Returns:
2518
 *      Nothing
2519
 *
2520
 *========================================================================*/
2521
 
2522
void do_big_sort(void)
2523
{
2524
    int         i, k;                            /* Looooopy things */
2525
    unsigned    j;
2526
    ty_s_data  *ptr;                             /* Pointer to sort data */
2527
    int         teamclass;                           /* Current class */
170 - 2528
    team_type   team_buf;
91 - 2529
 
2530
    for( i = 0; i <= config.num_legs; i++ )
2531
    {
2532
        /*
2533
        **  Sort on leg elapsed time
2534
        **  Then save the teams elapsed place in each leg
2535
        */
2536
        sort_team_data( i, S_L );
2537
        for( j = 1, k = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
2538
        {
2539
            if ( ptr->teamclass == config.nonequestrian_class )
2540
                continue;
2541
 
2542
            sort_aux[ptr->team].l_place[i] = k++;
2543
            sort_aux[ptr->team].team = ptr->team;
2544
        }
2545
 
2546
        /*
2547
        **  Sort on leg end time
2548
        **  Then save the teams place at the end of each leg
2549
        */
2550
        sort_team_data( i, S_LE );
2551
        for( j = 1, k = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
2552
        {
2553
            if ( ptr->teamclass == config.nonequestrian_class )
2554
                continue;
2555
 
2556
            sort_aux[ptr->team].le_place[i] = k++;
2557
        }
2558
 
2559
        /*
2560
        **  Sort on elapsed time per class
2561
        **  The save the teams elapsed place in each leg per class
2562
        */
2563
        sort_team_data( i, S_LC );
2564
        teamclass = -1;
2565
        for( k = 1, j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
2566
        {
2567
            if( teamclass != ptr->teamclass )
2568
            {
2569
                k = 1;
2570
                teamclass = ptr->teamclass;
2571
            }
2572
            sort_aux[ptr->team].lc_place[i] = k++;
2573
        }
2574
 
2575
        /*
2576
        **  Sort on end time per class
2577
        **  Then save the teams place at the end of each leg per class
2578
        */
2579
        sort_team_data( i, S_LEC );
2580
        teamclass = -1;
2581
        for( k = 1, j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
2582
        {
2583
            if( teamclass != ptr->teamclass )
2584
            {
2585
                k = 1;
2586
                teamclass = ptr->teamclass;
2587
            }
2588
            sort_aux[ptr->team].lec_place[i] = k++;
2589
        }
2590
    }
2591
 
2592
    /*
2593
    **  Write the place information back to disk for use in the displays
2594
    */
2595
    for( i = config.min_team; i <= config.max_team; i++ )
2596
    {
2597
        if( sort_aux[i].team && valid_field( i ) && g_record( i, &team_buf ) )
2598
        {
2599
            for( k = 0; k <= config.num_legs; k++ )
2600
            {
2601
                team_buf.leg[k].l_place = sort_aux[i].l_place[k];
2602
                team_buf.leg[k].le_place = sort_aux[i].le_place[k];
2603
                team_buf.leg[k].lc_place = sort_aux[i].lc_place[k];
2604
                team_buf.leg[k].lec_place = sort_aux[i].lec_place[k];
2605
            }
2606
            put_team_record( i, &team_buf );
2607
        }
2608
    }
2609
}
2610
 
2611
/*========================================================================
2612
 *
2613
 *  Sort in memory buffer
2614
 *
2615
 *  Purpose:
2616
 *      This function is called to Sort in memory buffer
2617
 *
2618
 *  Parameters:
2619
 *      leg         Requested leg
2620
 *      mode        Defines the sort mode
2621
 *
2622
 *  Returns:
2623
 *      Nothing
2624
 *
2625
 *========================================================================*/
2626
 
2627
void sort_team_data( int leg, int mode )
2628
{
2629
 
2630
    unsigned    j;
2631
    ty_s_data  *ptr;
2632
 
2633
    sort_leg = leg;                              /* Leg is global for the comparison function */
2634
    sort_mode = mode;                            /* Mode is global for compare function */
2635
 
2636
    qsort( ( char * ) sort_data, sort_num, sizeof( ty_s_data ), sort_comp );
2637
 
2638
    /*
2639
     * Insert "place data" into the sorted data
2640
     * This simply the index into the array of data - after its been
2641
     * sorted.
2642
     */
2643
    ptr = sort_data;
2644
    for( j = 1; j <= sort_num; j++, ptr++ )
2645
        ptr->place = j;
2646
 
2647
}
2648
 
2649
/*========================================================================
2650
 *
2651
 *  qsort comparison function
2652
 *
2653
 *  Purpose:
2654
 *      This function is called by qsort as a Sort comparison function
2655
 *
2656
 *  Parameters:
2657
 *      a       - 1st record
2658
 *      b       - 2nd record
2659
 *
2660
 *  Returns:
2661
 *      value to qsort
2662
 *
2663
 *========================================================================*/
2664
 
2665
int sort_comp( const void * aa, const void * bb )
2666
{
2667
    const ty_s_data * a = (ty_s_data *)aa;
2668
    const ty_s_data * b = (ty_s_data *)bb;
2669
 
2670
 
2671
    int         i;                               /* One of those */
2672
    time_t      ta, tb;                          /* Leg times */
2673
    int         na, nb;                          /* Number of valid legs */
2674
    time_t      tta, ttb;                        /* Temp times */
2675
 
2676
    /*
2677
    **  Sorting on Team Number
2678
    */
2679
    if( sort_mode == S_TEAM )
2680
        return ( a->team - b->team );
2681
 
2682
    /*
2683
    **  Sorting on Class and Team Number
2684
    */
2685
    if( sort_mode == S_CLASS )
2686
    {
2687
        if( a->teamclass != b->teamclass )
2688
            return ( a->teamclass - b->teamclass );
2689
        else
2690
            return ( a->team - b->team );
2691
    }
2692
 
2693
    /*
2694
    **  Sorting within a class
2695
    **  First sort on the class
2696
    */
2697
    if( sort_mode == S_LEC || sort_mode == S_LC )   /* Sort within a class */
2698
        if( a->teamclass != b->teamclass )
2699
            return ( a->teamclass - b->teamclass );
2700
 
2701
#if 1
2702
    /*
2703
    **  Always put the nonequestrian_class at the end
2704
    **  Simplifies the creation of ordered lists as these
2705
    **  Entries are not present in the main body
2706
    */
2707
    if ( a->teamclass == config.nonequestrian_class ||  b->teamclass == config.nonequestrian_class )
2708
    {
2709
        if ( a->teamclass == config.nonequestrian_class &&  b->teamclass == config.nonequestrian_class )
2710
        {
2711
        }
2712
        else
2713
        {
2714
            return ( a->teamclass == config.nonequestrian_class ? 1 : -1 );
2715
        }
2716
 
2717
    }
2718
#endif
2719
 
2720
    /*
2721
    **  Now we need to examine the times as we have sorted
2722
    **  on every thing else.
2723
    **
2724
    **  If one of the teams has bad_times, then that team is placed
2725
    **  lower in the sorting order. If both teams have bad times
2726
    **  then sort on team number. ie: Unplaced teams are sorted on
2727
    **  team number
2728
    **
2729
    **  If not sorting within a class (ie Overall), then Non_Equestrian
2730
    **  is better than a bad time. Places NE before disqualified
2731
    **
2732
    **
2733
    **  Note: NE also have bad_times set
2734
    */
2735
#if 1
2736
    if( a->flags.bad_times || b->flags.bad_times )  /* Valid data has precedence */
2737
    {
2738
        if( a->flags.bad_times && b->flags.bad_times )
2739
        {
2740
            if( a->flags.non_equestrian || b->flags.non_equestrian )
2741
            {
2742
                if( a->flags.non_equestrian && b->flags.non_equestrian )
2743
                {
2744
                    /*
2745
                    **  Both are non equestrian
2746
                    **  Let the time sort operate ...
2747
                    */
2748
                    //return ( a->team - b->team );
2749
                }
2750
                else
2751
                {
2752
                    /* One is equestrian */
2753
                    /* Good times better than NE */
2754
                    return ( a->flags.non_equestrian ? -1 : 1 );
2755
                }
2756
            }
2757
            else
2758
            {
2759
                /* Neither is equestrian */
2760
                return ( a->team - b->team );
2761
            }
2762
        }
2763
        else
2764
            return ( a->flags.bad_times ? 1 : -1 );
2765
    }
2766
#else
2767
    if( a->flags.bad_times || b->flags.bad_times )  /* Valid data has precedence */
2768
    {
2769
        if( a->flags.bad_times && b->flags.bad_times )
2770
            return ( a->team - b->team );
2771
        else
2772
            return ( a->flags.bad_times ? 1 : -1 );
2773
    }
2774
 
2775
    /*
2776
    **  Not sorting within a class ie: Overall
2777
    **  Non-Equestrian is at the end, so that it doesn't get counted in the
2778
    **  finishing order
2779
    */
2780
    if( sort_mode == S_LE || sort_mode == S_L )     /* Sort NOT within a class */
2781
    {
2782
        if( a->flags.non_equestrian || b->flags.non_equestrian )
2783
        {
2784
            if( a->flags.non_equestrian && b->flags.non_equestrian )
2785
                return ( a->team - b->team );
2786
            else
2787
                return ( a->flags.non_equestrian ? 1 : -1 );
2788
        }
2789
    }
2790
#endif
2791
 
2792
    /*
2793
    **  Before we sort on times we must determine which time to
2794
    **  use. Finish time, Leg end times, Leg Elapsed times.
2795
    */
2796
 
2797
    switch ( sort_mode )
2798
    {
2799
      /*
2800
      **    Sort on finish times
2801
      */
2802
      case S_FIN:
2803
        ta = a->leg[sort_leg];
2804
        tb = b->leg[sort_leg];
2805
        break;
2806
 
2807
 
2808
      /*
2809
      **    Sort on accumulated leg times
2810
      */
2811
      case S_LE:
2812
      case S_LEC:
2813
        if( sort_leg )
2814
        {
2815
            /*
2816
            **  Calculate accumulated time up to the desired leg
2817
            **  If the two teams have a different number of valid
2818
            **  leg times then order by the team that has completed
2819
            **  more legs.
2820
            */
2821
            ta = tb = ( time_t ) 0;
2822
            na = nb = 0;
2823
            for( i = 1; i <= sort_leg; i++ )
2824
            {
2825
                tta = a->lege[i];
2826
                ttb = b->lege[i];
2827
                if( tta > 0 )
2828
                {
2829
                    na++;
2830
                    ta += tta;
2831
                }
2832
                if( ttb > 0 )
2833
                {
2834
                    nb++;
2835
                    tb += ttb;
2836
                }
2837
            }
2838
            if( na != nb )
2839
                return ( nb - na );
2840
        }
2841
        else
2842
        {
2843
            ta = a->leg[sort_leg] - a->start;
2844
            tb = b->leg[sort_leg] - b->start;
2845
        }
2846
        break;
2847
 
2848
      /*
2849
      **    Sort on Elapsed times
2850
      */
2851
      case S_LC:
2852
      case S_L:
2853
        ta = a->lege[sort_leg];
2854
        tb = b->lege[sort_leg];
2855
        break;
2856
 
2857
      /*
2858
      **    Just to be sure ...
2859
      */
2860
      default:
2861
        return ( 0 );
2862
    }
2863
 
2864
    /*
2865
    **  Finally. Compare the required team times
2866
    */
2867
    if( ta == tb )
2868
        return ( a->team - b->team );
2869
    if( ( ta > 0 ) && ( tb > 0 ) )
2870
        return ( ( int ) ( ta - tb ) );
2871
    return ( ( ta > 0 ) ? -1 : 1 );
2872
}
2873
 
2874
/*========================================================================
2875
 *
2876
 *  qsort comparison function - competitor names
2877
 *
2878
 *  Purpose:
2879
 *      This function is called by qsort as a Sort comparison function
2880
 *
2881
 *  Parameters:
2882
 *      a       - 1st record
2883
 *      b       - 2nd record
2884
 *
2885
 *  Returns:
2886
 *      value to qsort
2887
 *
2888
 *========================================================================*/
2889
 
2890
int sort_comp_cname( const void * aa, const void * bb )
2891
{
2892
    ty_s_namedata * a = (ty_s_namedata *)aa;
2893
    ty_s_namedata * b = (ty_s_namedata *)bb;
2894
 
2895
 
2896
    int         i;                               /* One of those */
2897
 
2898
    /*
2899
    **  Sort by name
2900
    */
2901
    i = strcmp ( a->name, b->name );
2902
    if ( i )
2903
        return ( i );
2904
    a->multi=1;
2905
    b->multi=1;
2906
 
2907
    /*
2908
    **  Sort by Leg
2909
    */
2910
    i = a->leg - b->leg;
2911
    if ( i )
2912
        return ( i );
2913
 
2914
    /*
2915
    **  Sorting on Team Number
2916
    */
2917
    return ( a->team - b->team );
2918
 
2919
}
2920
 
2921
/*========================================================================
2922
 *
2923
 *  load report data into memory
2924
 *
2925
 *  Purpose:
2926
 *      This routine will pull all the data into memory 
2927
 *      Not all the team data is loaded. Only that essential for the
2928
 *      operation of the sort routine is loaded
2929
 *
2930
 *  Parameters:
2931
 *      None
2932
 *
2933
 *  Returns:
2934
 *      TRUE - All is well
2935
 *
2936
 *========================================================================*/
2937
 
2938
bool load_report_data(void)
2939
{
2940
 
2941
    ty_s_data  *ptr;                             /* pointer to sort data type */
2942
    ty_s_data  *last;
2943
    int         j;
2944
    unsigned    num;
170 - 2945
    team_type   team_buf;
91 - 2946
 
2947
    /*
2948
     * Fetch memory for the data store
2949
     */
2950
 
2951
    if( sort_data )
2952
        free( ( char * ) sort_data );
2953
    sort_data = 0;
2954
 
2955
    if( sort_aux )
2956
        free( ( char * ) sort_aux );
2957
    sort_aux = 0;
2958
    sort_num = 0;                                /* Counter of records in the array */
2959
 
2960
    /*
2961
    **  Allocate memory for the data structures
2962
    **  This will be free'd
2963
    */
2964
    num = config.max_team - config.min_team + 1 ;
2965
 
2966
    /*
2967
    **  Allow for non-equestrian teams - since some of the data is loaded twice
2968
    **  Take a guess that at most 1/2 the teams will be non-equestrian
2969
    */
2970
    num = num * 3 / 2;
2971
 
2972
    sort_data = ( ty_s_data * ) calloc ( num , sizeof( ty_s_data ) );
2973
    sort_aux = ( ty_s_aux * ) calloc( num + 1, sizeof( ty_s_aux ) );
2974
 
2975
    if( sort_data == 0 || sort_aux == 0 )
2976
    {
170 - 2977
        MainWindow::showMessage("Error in allocating memory");
91 - 2978
        return ( FALSE );
2979
    }
2980
 
2981
    /*
2982
    **  Load data into the memory based data structure
2983
    **  Only load data for valid-teams
2984
    **  Load essential data
2985
    **      Team Number, class and flags
2986
    **      Leg end and elapsed times
2987
    */
2988
    ptr = sort_data;
170 - 2989
    for( int team = config.min_team; team <= config.max_team; team++ )
91 - 2990
    {
2991
        if( valid_field( team ) )
2992
        {
2993
            g_record( team, &team_buf );
2994
            if( team_buf.flags.valid )
2995
            {
2996
                last = ptr;
2997
                ptr->team = team;
2998
                for( j = 0; j < MAX_LEGS + 1; j++ )
2999
                {
3000
                    ptr->lege[j] = team_buf.leg[j].elapsed;
3001
                    ptr->leg[j] = team_buf.leg[j].end;
3002
                }
3003
                ptr->start = team_buf.leg[0].start;
3004
                ptr->teamclass = team_buf.teamclass;
3005
 
3006
                ptr->flags = team_buf.flags;
3007
                ptr++;
3008
                sort_num++;
3009
 
3010
                /*
3011
                **  If non-equestrian support is enabled then
3012
                **  duplicate and modify data for the non-equestrian teams
3013
                **      - Change the class
3014
                **      - Modify the leg time
3015
                */
3016
#if 1
3017
                if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
3018
                {
3019
                    ptr->team = team;
3020
                    ptr->lege[0] = 0;
3021
 
3022
                    for( j = 0; j < MAX_LEGS + 1; j++ )
3023
                    {
3024
                        if ( j == config.equestrian_leg )
3025
                        {
3026
                            last->lege[j] = ptr->lege[j] = 0;
3027
                            if ( config.equestrian_leg > 1 )
3028
                                last->leg[j] = ptr->leg[j] = ptr->leg[j-1];
3029
                        }
3030
                        else
3031
                        {
3032
                            if ( j )
3033
                                last-> lege[j] = ptr->lege[j] = team_buf.leg[j].elapsed;
3034
                            last->leg[j] = ptr->leg[j] = team_buf.leg[j].end;
3035
                            ptr->lege[0] += ptr->lege[j] ;
3036
                            last->lege[0] = ptr->lege[0] ;
3037
                        }
3038
                    }
3039
 
3040
                    last->start = ptr->start = team_buf.leg[0].start;
3041
                    ptr->teamclass = config.nonequestrian_class;
3042
                    ptr->flags = team_buf.flags;
3043
                    ptr->flags.disqualified = 0;
3044
 
3045
 
3046
                    ptr++;
3047
                    sort_num++;
3048
                }
3049
#endif
3050
            }
3051
        }
3052
    }
3053
    return ( TRUE );
3054
}
3055
 
3056
/*========================================================================
3057
 *
3058
 *  Generate all the stats
3059
 *
3060
 *  Purpose:
3061
 *      This function is called to Generate all the stats
3062
 *
3063
 *  Parameters:
3064
 *      None
3065
 *
3066
 *  Returns:
3067
 *      Nothing
3068
 *
3069
 *========================================================================*/
3070
 
3071
void gen_stats(void)
3072
{
3073
    ty_s_data  *ptr;
3074
    unsigned    i;
3075
    int         j;
3076
    int         k;
3077
 
3078
    /*
3079
     * Init all the stats 
3080
     */
3081
    for( i = 0; i <= MAX_LEGS; i++ )
3082
    {
3083
        for( j = 0; j <= MAX_CLASS; j++ )
3084
        {
3085
            stats.team[i][j] = 0;
3086
            stats.fast.team[i][j] = 0;
3087
            stats.fast.time[i][j] = ( time_t ) - 1;
3088
            stats.average[i][j] = 0;
3089
        }
3090
    }
3091
 
3092
    for( i = 1, ptr = sort_data; i <= sort_num; i++, ptr++ )
3093
    {
3094
        /*
3095
        **  If there is any bad times in the team record, then none
3096
        **  of the data can be trusted.
3097
        **
3098
        **  If the team has been disqualified, then don't use any
3099
        **  of the data.
3100
        */
3101
        if( ptr->flags.bad_times || ptr->flags.disqualified )
3102
            continue;
3103
 
3104
        for( j = 0; j <= config.num_legs; j++ )
3105
        {
3106
            if( ptr->lege[j] <= 0 )              /* Ignore bad data */
3107
                continue;
3108
 
3109
            /*
3110
            **  Determine fastest team : overall
3111
            **  Ignore the non-equestrian data as this is in the list twice
3112
            */
3113
            if( ( ptr->lege[j] < stats.fast.time[j][0] )
3114
                || ( stats.fast.time[j][0] < 0 ) )
3115
            {
3116
                if ( ptr->teamclass != config.nonequestrian_class )
3117
                {
3118
                    stats.fast.team[j][0] = ptr->team;
3119
                    stats.fast.time[j][0] = ptr->lege[j];
3120
                }
3121
            }
3122
 
3123
            /*
3124
            **  Determine fastest team : within a class
3125
            */
3126
            if( ( ptr->lege[j] < stats.fast.time[j][ptr->teamclass] )
3127
                || stats.fast.time[j][ptr->teamclass] < 0 )
3128
            {
3129
                stats.fast.team[j][ptr->teamclass] = ptr->team;
3130
                stats.fast.time[j][ptr->teamclass] = ptr->lege[j];
3131
            }
3132
 
3133
            /*
3134
            **  Sum the end times : overall
3135
            */
3136
            if ( ptr->teamclass != config.nonequestrian_class )
3137
            {
3138
                stats.average[j][0] += ptr->lege[j];
3139
                stats.team[j][0]++;
3140
            }
3141
 
3142
 
3143
            /*
3144
            **  Sum the end times : within a class
3145
            */
3146
            stats.average[j][ptr->teamclass] += ptr->lege[j];
3147
            stats.team[j][ptr->teamclass]++;
3148
        }
3149
    }
3150
 
3151
    /*
3152
     * Calculate the averages
3153
     */
3154
    for( k = 0; k <= config.num_legs; k++ )
3155
    {
3156
        for( j = 0; j <= config.num_class; j++ )
3157
        {
3158
            if( stats.team[k][j] )
3159
                stats.average[k][j] /= stats.team[k][j];
3160
            else
3161
                stats.average[k][j] = ( time_t ) - 1;
3162
        }
3163
    }
3164
}
3165
 
3166
/********************************* EOF ***********************************/