Subversion Repositories svn1

Rev

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