Subversion Repositories svn1

Rev

Rev 315 | Rev 332 | 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
    {
313 david 987
        if ( class_done[j] || !config.team_class[j-1].abr[0] )
91 - 988
        {
989
            continue;
990
        }
991
        report_title = tprintf( "Category results for : %-*s", LEN_CLASS_NAME, config.team_class[j - 1].full_name );
992
 
993
        if( !open_printer( "", config.team_class[j - 1].abr, 132, report_html, report_title ) )
994
            continue;
995
        print_class_header( j-1, TRUE );
996
        print( "\nThere were no competitors in this class\n" );
997
        print_legend(j,1);
998
        close_printer();
999
    }
1000
 
1001
 
1002
    /*
1003
    **  If we are generating an HTML report then we need to create the file
1004
    **  that contains all the team names - the assumption is that this data
1005
    **  is available
1006
    */
291 david 1007
    if ( report_html == html || report_html == printed)
91 - 1008
    {
1009
        pri_team();
1010
    }
1011
 
1012
    /*
1013
    **  Generate the awards report.
1014
    **  This is only available as an HTML report
1015
    */
291 david 1016
    if ( report_html == html || report_html == printed)
91 - 1017
    {
1018
        pri_awards_html();
1019
    }
1020
 
1021
    /*
1022
    **  Generate the master index page
1023
    */
272 david 1024
    if ( report_html == html )
91 - 1025
    {
1026
        pri_master_index();
1027
    }
1028
 
1029
    pri_name_index_body();
1030
}
1031
 
1032
/*========================================================================
1033
 *
1034
 *  Place to text
1035
 *
1036
 *  Purpose:
1037
 *      This function is called to convert a place to text
1038
 *
1039
 *  Parameters:
1040
 *      place
1041
 *
1042
 *  Returns:
1043
 *      text
1044
 *
1045
 *========================================================================*/
1046
 
1047
char *placing ( int place )
1048
{
1049
    if ( place > MAX_PLACE )
1050
    {
1051
        return tprintf( "Place: %d", place);
1052
    }
1053
    return tprintf ("%s Place", place_text[place]);
1054
}
1055
 
1056
/*========================================================================
1057
 *
1058
 *  Print award results
1059
 *
1060
 *  Purpose:
1061
 *      This function is called to Print award results
1062
 *      Keep the page to 80 cols, so that it can be pronted on A4
1063
 *
1064
 *  Parameters:
1065
 *      None
1066
 *
1067
 *  Returns:
1068
 *      Nothing
1069
 *
1070
 *========================================================================*/
1071
void pri_awards_html(void)
1072
{
1073
    report_type saved = report_html;
1074
    /*
1075
    **  Generate ALL results with HTML tags
1076
    */
1077
    report_html = html;
1078
    pri_awards();
291 david 1079
    report_html = printed;
1080
    pri_awards();
91 - 1081
    report_html = saved;
1082
}
1083
 
1084
/*========================================================================
1085
 *
1086
 *  Print award results
1087
 *
1088
 *  Purpose:
1089
 *      This function is called to Print award results
1090
 *      Keep the page to 80 cols, so that it can be pronted on A4
1091
 *
1092
 *  Parameters:
1093
 *      None
1094
 *
1095
 *  Returns:
1096
 *      Nothing
1097
 *
1098
 *========================================================================*/
1099
void pri_awards(void)
1100
{
1101
    int j;
1102
    int i;
1103
    int k;
1104
    int windex;
1105
    int winmax;
1106
    ty_s_data  *ptr;
170 - 1107
    team_type   team_buf;
91 - 1108
    int         last_class;
1109
    char    solid_line[100];
284 david 1110
    bool header_done = false;
91 - 1111
 
1112
    /*
1113
    **  Calculate Summary information
1114
    **  Should cache the data
1115
    */
1116
    t_class_summary sdata;
1117
    calc_class_summary( & sdata );
1118
 
1119
 
1120
    if( !open_printer( "", "awards", 80, report_html, "Prizes and Awards" ) )
1121
        return;
1122
 
1123
    memset ( solid_line, 0, sizeof( solid_line ));
1124
    memset ( solid_line, '-', 80 );
1125
 
1126
    /*
1127
    **  Generate an index for this page
1128
    */
1129
    print( "\n");
1130
    if ( report_html == html )
1131
    {
1132
        print( "<hr>" );
1133
        print( "<A NAME=\"%s\"></A>",url_encode("INDEX"));
1134
    }
284 david 1135
    print( "Award Categories - Full Event");
91 - 1136
 
1137
    for( j = 1; j <= config.num_class; j++ )
1138
    {
1139
        /*
1140
        **  Header for the class
1141
        */
313 david 1142
        if (!config.team_class[j - 1].abr[0])
91 - 1143
            continue;
313 david 1144
        if (config.class_winners[j - 1] <= 0) 
1145
            continue;
91 - 1146
 
1147
        winmax = config.class_winners[j-1];
1148
        {
284 david 1149
            int valid =   sdata.teamclass[j].valid_ev;
91 - 1150
            if ( valid < winmax )
1151
                winmax = valid;
1152
        }
1153
 
1154
        print( "\n");
1155
        print( "    ");
1156
        if ( report_html == html ) print( "<A HREF=\"#%s\">",url_encode(config.team_class[j-1].full_name));
1157
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[j-1].full_name ));
1158
        if ( report_html == html ) print( "</A>" );
1159
        print( "  %3d Awards", winmax );
1160
        if ( config.class_winners[j-1] != winmax )
1161
            print( " from a maximum of %3d", config.class_winners[j-1] );
284 david 1162
    }
91 - 1163
 
284 david 1164
    /*
1165
    **  NE Award Categories
1166
    */
1167
    if ( config.class_ne_winners_by_class )
1168
    {
1169
        print( "\n");
1170
        print( "Award Categories - Non Equestrian");
1171
 
1172
        for( j = 1; j <= config.num_class; j++ )
1173
        {
1174
            /*
1175
            **  Header for the class
1176
            */
313 david 1177
            if (!config.team_class[j - 1].abr[0])
1178
                continue;
284 david 1179
            if ( config.class_ne_winners[j-1] <= 0 )
1180
                continue;
1181
 
1182
            winmax = config.class_ne_winners[j-1];
1183
            {
1184
                int valid =   sdata.teamclass[j].valid_ne;
1185
                if ( valid < winmax )
1186
                    winmax = valid;
1187
            }
1188
 
1189
            print( "\n");
1190
            print( "    ");
1191
            if ( report_html == html ) print( "<A HREF=\"#%s_NE\">",url_encode(config.team_class[j-1].full_name));
1192
            print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[j-1].full_name ));
1193
            if ( report_html == html ) print( "</A>" );
1194
            print( "  %3d Awards", winmax );
1195
            if ( config.class_ne_winners[j-1] != winmax )
1196
                print( " from a maximum of %3d", config.class_ne_winners[j-1] );
1197
        }
91 - 1198
    }
1199
 
1200
    /*
1201
    **  Manual entries
1202
    */
284 david 1203
    print( "\n");
1204
    print( "Miscellaneous");
1205
 
1206
        print( "\n");
1207
        print( "    ");
1208
        if ( report_html == html ) print( "<A HREF=\"#%s_by_cat\">",url_encode("Full Event"));
1209
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,"Full Event"));
1210
        if ( report_html == html ) print( "</A>" );
1211
        print (" by Category");
1212
 
227 - 1213
    if ( config.class_ne_winners_by_class )
1214
    {
1215
        print( "\n");
1216
        print( "    ");
1217
        if ( report_html == html ) print( "<A HREF=\"#%s_by_cat\">",url_encode(config.team_class[config.nonequestrian_class-1].full_name));
1218
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[config.nonequestrian_class-1].full_name ));
1219
        if ( report_html == html ) print( "</A>" );
1220
        print (" by Category");
1221
    }
1222
 
1223
 
91 - 1224
    print( "\n");
1225
    print( "    ");
1226
    if ( report_html == html ) print( "<A HREF=\"#%s\">",url_encode("Hall Of Fame"));
1227
    print( "%s",  "Hall Of Fame" );
1228
    if ( report_html == html ) print( "</A>" );
1229
 
1230
    print( "\n");
1231
    print( "    ");
1232
    if ( report_html == html ) print( "<A HREF=\"#%s\">",url_encode("FASTEST"));
1233
    print( "%s",  "FASTEST" );
1234
    if ( report_html == html ) print( "</A>" );
1235
 
1236
    /*
1237
    **  Sort the data by class
1238
    */
227 - 1239
    sort_team_data( 0, S_LC );     /* Generate class placement data */
91 - 1240
    last_class = -1;                /* Invalid class to start with */
1241
 
1242
    /*
1243
    **  Process each category
1244
    */
1245
    print( "\n");
1246
    for( j = 1; ; j++ )
1247
    {
313 david 1248
        if (!config.team_class[j - 1].abr[0])
1249
            continue;
1250
 
91 - 1251
        /*
1252
        **  Tail for previous entry
1253
        */
227 - 1254
        //if ( config.class_ne_winners_by_class && j == config.nonequestrian_class )
1255
        //    continue;
1256
 
91 - 1257
        if ( j != 1 )
1258
            if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1259
 
1260
        if ( j > config.num_class  )
1261
            break;
1262
 
1263
        /*
1264
        **  Header for the class
1265
        */
1266
        print( "\n");
1267
        if ( report_html == html )
1268
        {
1269
            print( "<hr>" );
284 david 1270
 
1271
            if ( !header_done )
1272
            {
1273
                header_done = true;
1274
                if ( report_html == html )
1275
                {
1276
                    print( "<A name=\"%s_by_cat\"></A>",url_encode("Full Event"));
1277
                }
1278
            }
1279
 
91 - 1280
            print( "<A name=\"%s\"></A>",url_encode(config.team_class[j-1].full_name));
1281
        }
1282
        else
1283
        {
1284
            print( "%s\n", solid_line);
1285
        }
1286
        print( "Category: ");
1287
        if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[j - 1].abr ,"html")));
1288
        print( "%s",  config.team_class[j-1].full_name );
1289
        if ( report_html == html ) print( "</A>" );
1290
 
1291
        if ( config.class_winners[j-1] <= 0 )
1292
        {
1293
            print( "\n");
1294
            print( "No winners awarded" );
284 david 1295
            if ( report_html == html ) print("<br>");
91 - 1296
            continue;
1297
        }
1298
 
1299
        /*
1300
        **  Enties for 'n' the best teams as configured
1301
        */
1302
        windex = 0;                     /* Winners done */
1303
        unsigned int i;
1304
        for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1305
        {
1306
            if ( ptr->teamclass != j )
1307
            {
1308
                continue;
1309
            }
1310
 
1311
            /*
1312
            **  Now read in the team record
1313
            */
1314
            if( valid_field( ptr->team ) && g_record( ptr->team, &team_buf ) )
1315
            {
1316
                /*
1317
                **  Ensure we have a valid team
1318
                **  Can't award disqualified teams
1319
                **  Can't award NE teams unless its a NE award
1320
                */
1321
                if ( ptr->flags.bad_times )
1322
                    break;
1323
 
1324
                if ( ptr->teamclass != config.nonequestrian_class  && ptr->flags.non_equestrian )
1325
                    break;
1326
 
1327
                /*
1328
                **  Count the entry
1329
                */
1330
                windex++;
1331
 
1332
                /*
1333
                **  If printing an HTML report then we need to mark
1334
                **  the entry with a reference so that we can link to it
1335
                */
1336
                print( "\n");
1337
                if ( report_html == html )
1338
                {
1339
                    print( "<A NAME=\"Team_%04d\">",team_buf.numb );
1340
                    print( "</A>" );
1341
                }
1342
 
1343
                /*
1344
                **  Basic information
1345
                **      - Team number - with Xref back to full result
1346
                **      - Full team name
1347
                **      - Full categoray name
1348
                */
1349
                print( "%s", placing(windex) );
1350
 
1351
                print( "  Team Name: ");
1352
                if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1353
                print( "%-*s ",     MAX_TM_NAME, team_buf.name );
1354
                    if ( report_html == html ) print( "</A>" );
1355
 
1356
                print( "  Number: ");
1357
                if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
1358
                print( "%4d",       team_buf.numb );
1359
                if ( report_html == html ) print( "</A>" );
1360
 
1361
 
1362
                for( k = 0; k < MAX_MEMB; k++ )
1363
                {
1364
 
1365
                    /*
1366
                    **  Skip equestrian leg in the non-equestion display
1367
                    */
1368
                    if ( k + 1 == config.equestrian_leg && ptr->teamclass == config.nonequestrian_class)
1369
                        continue;
1370
 
1371
                    print( "\n");
1372
                    print( "    ");
1373
                    print( "%-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
1374
                    print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
1375
 
1376
                    print( "  %-8s", time_a( team_buf.leg[k+1].elapsed ) );
1377
                }
1378
 
1379
                print( "\n");
1380
                print( "    ");
1381
                print_bold( TRUE );
1382
                print( "%-*s %-*s  %-8s", MAX_PERSON_NAME , "Total" ,MAX_PERSON_NAME, "",time_a( team_buf.leg[0].elapsed ) );
1383
                print_bold( FALSE );
1384
                print( "\n" );
1385
            }
1386
 
1387
 
1388
            /*
1389
            **  More to do
1390
            */
1391
            if ( windex >= config.class_winners[j-1] )
1392
            {
1393
                break;
1394
            }
1395
        }
1396
    }
1397
 
1398
    /*
227 - 1399
    ** Non Equestrian winners by category
1400
    */
1401
    if ( config.class_ne_winners_by_class)
1402
    {
284 david 1403
        header_done = false;
227 - 1404
 
1405
        /*
1406
        **  Sort the data by class with NE data sorted by real class
1407
        */
1408
        sort_team_data( 0, S_LC_NE );      /* Generate class placement data */
1409
        last_class = -1;                   /* Invalid class to start with */
1410
 
1411
        /*
1412
        **  Only process the Non Equestrian teams in this pass
1413
        */
1414
        print( "\n");
1415
        for( j = 1; ; j++ )
1416
        {
313 david 1417
            if (!config.team_class[j - 1].abr[0])
1418
                continue;
1419
 
227 - 1420
            /*
1421
            **  Tail for previous entry
1422
            */
1423
            if ( j == config.nonequestrian_class)
1424
                continue;
1425
 
1426
            if ( j != 1 )
1427
                if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1428
 
1429
            if ( j > config.num_class  )
1430
                break;
1431
 
1432
            /*
1433
            **  Header for the (sub) class
1434
            */
1435
            if ( !header_done )
1436
            {
1437
                header_done = true;
1438
                if ( report_html == html )
1439
                {
1440
                    print( "<A name=\"%s_by_cat\"></A>",url_encode(config.team_class[config.nonequestrian_class-1].full_name));
1441
                }
1442
            }
1443
 
1444
            print( "\n");
1445
            if ( report_html == html )
1446
            {
1447
                print( "<hr>" );
284 david 1448
                print( "<A name=\"%s_NE\"></A>",url_encode(config.team_class[j-1].full_name));
227 - 1449
            }
1450
            else
1451
            {
1452
                print( "%s\n", solid_line);
1453
            }
1454
            print( "Category: ");
1455
            if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[config.nonequestrian_class - 1].abr ,"html")));
1456
            print( "%s",  config.team_class[config.nonequestrian_class-1].full_name );
1457
            if ( report_html == html ) print( "</A>" );
1458
 
1459
            print (" :: ");
1460
 
1461
            if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[j - 1].abr ,"html")));
1462
            print( "%s",  config.team_class[j-1].full_name );
1463
            if ( report_html == html ) print( "</A>" );
1464
 
1465
            if ( config.class_ne_winners[j-1] <= 0 )
1466
            {
1467
                print( "\n");
1468
                print( "No winners awarded" );
1469
                if ( report_html == html ) print("<br>");
1470
                continue;
1471
            }
1472
 
1473
            /*
1474
            **  Enties for 'n' the best teams as configured
1475
            */
1476
            windex = 0;                     /* Winners done */
1477
            unsigned int i;
1478
            for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1479
            {
1480
                if ( ptr->teamclass != config.nonequestrian_class ||  ptr->real_teamclass != j )
1481
                {
1482
                    continue;
1483
                }
1484
 
1485
                /*
1486
                **  Now read in the team record
1487
                */
1488
                if( valid_field( ptr->team ) && g_record( ptr->team, &team_buf ) )
1489
                {
1490
                    /*
1491
                    **  Ensure we have a valid team
1492
                    **  Can't award disqualified teams
1493
                    **  Can't award NE teams unless its a NE award
1494
                    */
1495
                    if ( ptr->flags.bad_times )
1496
                        break;
1497
 
1498
                    if ( ptr->teamclass != config.nonequestrian_class  && ptr->flags.non_equestrian )
1499
                        break;
1500
 
1501
                    /*
1502
                    **  Count the entry
1503
                    */
1504
                    windex++;
1505
 
1506
                    /*
1507
                    **  If printing an HTML report then we need to mark
1508
                    **  the entry with a reference so that we can link to it
1509
                    */
1510
                    print( "\n");
1511
                    if ( report_html == html )
1512
                    {
1513
                        print( "<A NAME=\"Team_%04d\">",team_buf.numb );
1514
                        print( "</A>" );
1515
                    }
1516
 
1517
                    /*
1518
                    **  Basic information
1519
                    **      - Team number - with Xref back to full result
1520
                    **      - Full team name
1521
                    **      - Full categoray name
1522
                    */
1523
                    print( "%s", placing(windex) );
1524
 
1525
                    print( "  Team Name: ");
1526
                    if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1527
                    print( "%-*s ",     MAX_TM_NAME, team_buf.name );
1528
                        if ( report_html == html ) print( "</A>" );
1529
 
1530
                    print( "  Number: ");
1531
                    if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
1532
                    print( "%4d",       team_buf.numb );
1533
                    if ( report_html == html ) print( "</A>" );
1534
 
1535
 
1536
                    for( k = 0; k < MAX_MEMB; k++ )
1537
                    {
1538
 
1539
                        /*
1540
                        **  Skip equestrian leg in the non-equestion display
1541
                        */
1542
                        if ( k + 1 == config.equestrian_leg && ptr->teamclass == config.nonequestrian_class)
1543
                            continue;
1544
 
1545
                        print( "\n");
1546
                        print( "    ");
1547
                        print( "%-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
1548
                        print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
1549
 
1550
                        print( "  %-8s", time_a( team_buf.leg[k+1].elapsed ) );
1551
                    }
1552
 
1553
                    print( "\n");
1554
                    print( "    ");
1555
                    print_bold( TRUE );
1556
                    print( "%-*s %-*s  %-8s", MAX_PERSON_NAME , "Total" ,MAX_PERSON_NAME, "",time_a( team_buf.leg[0].elapsed ) );
1557
                    print_bold( FALSE );
1558
                    print( "\n" );
1559
                }
1560
 
1561
 
1562
                /*
1563
                **  More to do
1564
                */
1565
                if ( windex >= config.class_ne_winners[j-1] )
1566
                {
1567
                    break;
1568
                }
1569
            }
1570
        }
1571
    }
1572
 
1573
    /*
91 - 1574
    **  Generate the Hall of Fame information
1575
    */
1576
    print( "\n");
1577
    if ( report_html == html )
1578
    {
1579
        print( "<hr>" );
1580
        print( "<A name=\"%s\"></A>",url_encode("Hall Of Fame"));
1581
    }
1582
    else
1583
    {
1584
        print( "%s\n", solid_line);
1585
    }
1586
    print( "%s",  "Hall of Fame" );
1587
 
1588
    if ( config.num_fame  )
1589
    {
1590
        for( i = 1; i <= config.num_fame; i++ )
1591
        {
1592
            print( "\n");
1593
            print( "    %-*s", MAX_PERSON_NAME, config.hall_fame[i-1] );
1594
        }
1595
    }
1596
    else
1597
    {
195 david 1598
        qDebug( "There are no new stars for the Hall of Fame");
91 - 1599
    }
1600
    if ( report_html == html ) print( "\n");
1601
    if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1602
 
1603
    /*
1604
    **  Generate the FASTEST information
1605
    */
1606
    print( "\n" );
1607
    print( "\n");
1608
    if ( report_html == html )
1609
    {
1610
        print( "<hr>" );
1611
        print( "<A name=\"%s\"></A>",url_encode("FASTEST"));
1612
    }
1613
    else
1614
    {
1615
        print( "%s\n", solid_line);
1616
    }
1617
    print( "%s",  "FASTEST" );
1618
 
1619
    /*
1620
    **  Sort the data and then generate the stats - again
1621
    */
1622
    do_big_sort();
1623
    gen_stats();
1624
 
1625
    for( i = 1; i <= config.num_legs; i++ )
1626
    {
1627
        g_record( stats.fast.team[i][0], &team_buf );
1628
 
1629
        print( "\n");
1630
        print( "    %-13s ", config.leg_name[i - 1] );
1631
        print( "  Name: ");
1632
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1633
        print( "%-*s", MAX_PERSON_NAME, team_buf.members[i-1].name );
1634
        if ( report_html == html ) print( "</A>" );
242 - 1635
        print( "  Team:");
91 - 1636
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
1637
        print( "%4d" , stats.fast.team[i][0] );
1638
        if ( report_html == html ) print( "</A> " );
242 - 1639
        print( "  Time:%s ", time_a( stats.fast.time[i][0] ) );
91 - 1640
 
1641
    }
1642
 
1643
    if ( report_html == html ) print( "\n");
1644
    if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1645
    print( "\n");
1646
    close_printer();
1647
}
1648
 
1649
/*========================================================================
1650
 *
1651
 *  pri_master_index
1652
 *
1653
 *  Purpose:
1654
 *      This function is called to create an HTML page that references all
1655
 *      the other pages that have been generated
1656
 *
1657
 *      Assume that they are in the same directory
1658
 *
1659
 *  Parameters:
1660
 *      None
1661
 *
1662
 *  Returns:
1663
 *      Nothing
1664
 *
1665
 *========================================================================*/
1666
void pri_master_index_entry(const char *name, const char *text)
1667
{
1668
    print( "<tr><td>");
1669
    print ("<A HREF=\"%s\">%s</A>\n", url_encode(p_filename(filebase, name ,"html")), text );
1670
}
1671
 
1672
 
1673
void pri_master_index(void)
1674
{
1675
    int j;
1676
 
1677
    report_html = html;
1678
    if( !open_printer( "", "index", 132, report_html, "Master Index" ) )
1679
        return;
1680
 
1681
    /*
1682
    **  Names
1683
    */
1684
    print( "<TABLE border=0 align=center>" );
1685
    pri_master_index_entry( "name", "Team list" );
1686
    pri_master_index_entry( "competitor", "Competitor list" );
1687
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
1688
    pri_master_index_entry( "awards", "Prizes and Awards" );
1689
    print( "<tr><td>\n" );
1690
 
1691
    print( "\n" );
1692
    for( j = 1; j <= config.num_class; j++ )
1693
    {
313 david 1694
        if (!config.team_class[j - 1].abr[0])
1695
            continue;
91 - 1696
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
1697
    }
1698
 
1699
    print( "</TABLE>" );
1700
 
1701
    close_printer();
1702
 
1703
    /*
1704
    **  A small page to hold the Leg End displays
1705
    */
1706
 
1707
    if( !open_printer( "", "legindex", 132, report_html, "Master Index with trace data" ) )
1708
        return;
1709
 
1710
    /*
1711
    **  Names
1712
    */
1713
    print( "<TABLE border=0 align=center>" );
1714
#if 1
1715
    pri_master_index_entry( "name", "Team list" );
1716
    pri_master_index_entry( "competitor", "Competitor list" );
1717
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
1718
    pri_master_index_entry( "awards", "Prizes and Awards" );
1719
    print( "<tr><td>\n" );
1720
 
1721
    print( "\n" );
254 - 1722
    pri_master_index_entry( "summary", "Category Summary" );
91 - 1723
    for( j = 1; j <= config.num_class; j++ )
1724
    {
313 david 1725
        if (!config.team_class[j - 1].abr[0])
1726
            continue;
91 - 1727
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
1728
    }
1729
#endif
1730
    print( "<tr><td>\n" );
1731
 
1732
    print( "\n" );
170 - 1733
    for ( int leg = 1; leg <= config.num_legs; leg ++ )
91 - 1734
    {
1735
        pri_master_index_entry( tprintf("lg%1.1d", leg), tprintf("Leg End Results for: %d", leg) );    
1736
    }
1737
 
1738
    print( "<tr><td>\n" );
1739
    print( "\n" );
1740
 
170 - 1741
    for ( int leg = 1; leg <= config.num_legs; leg ++ )
91 - 1742
    {
1743
        pri_master_index_entry( tprintf("le%1.1d", leg), tprintf("Leg Elapsed Time Results for: %d", leg) );
1744
    }    
1745
 
1746
 
1747
    print( "</TABLE>" );
1748
 
1749
    close_printer();
203 - 1750
    /*
1751
     ** Tell the main system about this new report
1752
     */
272 david 1753
    MainWindow::registerReport(getPrinterFile(), "Master Leg Index");
91 - 1754
 
1755
}
1756
 
1757
 
1758
/*========================================================================
1759
 *
1760
 *  Print interim results
1761
 *
1762
 *  Purpose:
1763
 *      This function is called to Print interim results
1764
 *
1765
 *  Parameters:
1766
 *      None
1767
 *
1768
 *  Returns:
1769
 *      Nothing
1770
 *
1771
 *========================================================================*/
1772
 
1773
void pri_interim(void)
1774
{
1775
    ty_s_data  *ptr;
170 - 1776
    team_type   team_buf;
91 - 1777
    unsigned    i;
1778
    int         j, last_class;
1779
    char       *report_title;
1780
 
1781
    if ( ! report_all )
1782
    {
195 david 1783
        ck_data( -1, C_DISQUAL );              /* Check the data - dummy check */
91 - 1784
    }
1785
    do_big_sort();                             /* Sort on every thing */
1786
    gen_stats();                               /* Generate the stats too */
1787
 
1788
    /*
1789
     * Now print the data on the printer 
1790
     */
1791
 
1792
    if( !open_printer( "", "int", 132, report_html, "Interim Results" ) )
1793
        return;
1794
 
1795
    /*
1796
     * Print out the data 
1797
     */
1798
    print_class_header( -1, FALSE );                     /* Print the header */
1799
 
1800
    ptr = sort_data;
1801
    sort_team_data( 0, S_TEAM );                   /* Re-sort on team number */
1802
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1803
    {
1804
        if ( ptr->teamclass == config.nonequestrian_class )
1805
            continue;
1806
 
1807
        g_record( ptr->team, &team_buf );
1808
 
1809
        print( "%4d %4.4s %-*s",
1810
               team_buf.numb,
1811
               pi_place( team_buf.leg[config.num_legs].le_place,
1812
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ),
1813
               3,
1814
               team_buf.teamclass ==
1815
 
1816
        for( j = 1; j <= config.num_legs; j++ )
1817
        {
1818
            print( "  %-8s %4.4s %4.4s",
1819
                   time_fa( team_buf.leg[j].elapsed,
1820
                            ptr->flags.disqualified ),
1821
                   pi_place( team_buf.leg[j].l_place, ptr->flags.disqualified,
1822
                             team_buf.leg[j].elapsed ),
1823
                   pi_place( team_buf.leg[j].le_place,
1824
                             ptr->flags.disqualified,
1825
                             team_buf.leg[j].elapsed ) );
1826
        }
1827
        print( "  %-8s %4.4s\n",
1828
               time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ),
1829
               pi_place( team_buf.leg[config.num_legs].lec_place,
1830
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ) );
1831
    }
1832
 
1833
    print_class_stats( -1, FALSE );             /* Print statistics */
1834
    print_legend(-1, 1);                        /* Print the legend */
1835
    close_printer();                            /* Close the printer */
1836
 
1837
 
1838
    /*
1839
     * Now produce a breakdown on a class by class basis 
1840
     * Now print out the class placement information
1841
     */
1842
 
1843
    sort_team_data( 0, S_CLASS );              /* Generate class placement data */
1844
    last_class = -1;                             /* Invalid class to start with */
1845
 
1846
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1847
    {
1848
        /*
1849
        **  Detect a change in the "class"
1850
        **  All data is within the one array of data
1851
        **  Use the in-memory class as this MAY differ from that stored
1852
        **  The non-equestrian class does this.
1853
        */
1854
        if( last_class != ptr->teamclass )
1855
        {
1856
            if( last_class >= 0 )
1857
            {
1858
                print_class_stats( last_class, TRUE );
1859
                print_legend(last_class, 1);
1860
                close_printer();
1861
            }
1862
 
1863
            report_title = tprintf( "Interim Category results for : %-*s", LEN_CLASS_NAME, team_buf.teamclass == 0 ? "" : config.team_class[ptr->teamclass - 1].full_name );
1864
 
1865
            if( !open_printer( "", tprintf( "i%2s", config.team_class[ptr->teamclass - 1].abr ), 132, report_html, report_title ) )
1866
                continue;
1867
            print_class_header( last_class = ptr->teamclass, FALSE );
1868
        }
1869
 
1870
        /*
1871
        **  Now read in the team record
1872
        */
1873
        g_record( ptr->team, &team_buf );
1874
        print( "%4d %4.4s %-*s",
1875
               team_buf.numb,
1876
               pi_place( team_buf.leg[config.num_legs].lec_place,
1877
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ),
1878
               3,
1879
               team_buf.teamclass ==
1880
 
1881
        for( j = 1; j <= config.num_legs; j++ )
1882
        {
1883
            print( "  %-8s %4.4s %4.4s",
1884
                   time_fa( team_buf.leg[j].elapsed,
1885
                            ptr->flags.disqualified ),
1886
                   pi_place( team_buf.leg[j].lc_place,
1887
                             ptr->flags.disqualified,
1888
                             team_buf.leg[j].elapsed ),
1889
                   pi_place( team_buf.leg[j].lec_place,
1890
                             ptr->flags.disqualified,
1891
                             team_buf.leg[j].elapsed ) );
1892
        }
1893
        print( "  %-8s %4.4s\n",
1894
               time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ),
1895
               pi_place( team_buf.leg[config.num_legs].le_place,
1896
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ) );
1897
    }
1898
 
1899
    print_class_stats( last_class, FALSE );
1900
    print_legend(last_class, 1);
1901
    close_printer();
1902
 
1903
}
1904
 
1905
/*----------------------------------------------------------------------------
1906
** FUNCTION           : pri_csv_data
1907
**
1908
** DESCRIPTION        : Generate a CSV file of all the report data
1909
**                      It can then be used to play with the data externally
1910
**
1911
**
1912
** INPUTS             : None
1913
**
1914
** RETURNS            : Yes it does
1915
**
1916
----------------------------------------------------------------------------*/
1917
 
1918
 
1919
void pri_csv_data ( void )
1920
{
1921
    int i;
1922
    int j;
1923
    int age_sum;
170 - 1924
    team_type   team_buf;
91 - 1925
 
1926
    /*
1927
    **  Sort on every thing
1928
    **  Then generate all the stats too
1929
    */
1930
    do_big_sort();
1931
    gen_stats();
1932
 
1933
    /*
1934
     * Now print the data on the printer 
1935
     */
1936
 
1937
    if( !open_printer( "full_data", "csv", 2000, text, NULL ) )
1938
        return;
1939
 
1940
    /*
1941
    **  Print headings
1942
    */
1943
    csv_print( "%s",   "Team Number" );
1944
    csv_print( "%s",   "Team Name" );
1945
 
1946
    csv_print( "%s",    "Class Full");
1947
    csv_print( "%s",    "Class Abr");
1948
    csv_print( "%s",    "Class Start Time");
1949
    csv_print( "%s",    "Class Start Time Number");
1950
 
1951
    csv_print( "%s",    "Team Country");
1952
 
1953
    for( j = 1; j <= config.num_legs; j++ )
1954
    {
1955
        csv_print( "%s", "Leg Number" );
1956
        csv_print( "%s", "Leg Name");
1957
        csv_print( "%s", "Competitor Name");
1958
        csv_print( "%s", "Sex" );
1959
        csv_print( "%s", "Age");
1960
        csv_print( "%s", "Start Time");
1961
        csv_print( "%s", "Start Time Number");
1962
        csv_print( "%s", "End Time" );
1963
        csv_print( "%s", "End Time Number" );
1964
        csv_print( "%s", "Elapsed Time");
1965
        csv_print( "%s", "Elapsed Time Number");
1966
        csv_print( "%s", "Leg Place");
1967
        csv_print( "%s", "Leg End Place");
1968
        csv_print( "%s", "Leg Class Place");
1969
        csv_print( "%s", "Leg End Class Place");
1970
        csv_print( "%s", "Manual");
1971
    }
1972
 
1973
    j = 0;
1974
    csv_print( "%s", "Team Start Time");
1975
    csv_print( "%s", "Team Start Time Number");
1976
    csv_print( "%s", "Team End Time");
1977
    csv_print( "%s", "Team End Time Number");
1978
    csv_print( "%s", "Team Elapsed Time");
1979
    csv_print( "%s", "Team Elapsed Time Number");
1980
//            csv_print( "%s", team_buf.leg[j].l_place );
1981
    csv_print( "%s", "Team Leg End Place");
1982
//            csv_print( "%s", team_buf.leg[j].lc_place );
1983
    csv_print( "%s", "Team Leg Class Place");
1984
//            csv_print( "%s", team_buf.leg[j].manual );
1985
 
1986
    csv_print( "%s", "Total Team Age");
1987
    csv_print( "%s", "Flag:valid Team");
1988
    csv_print( "%s", "Flag:bad_times" );
1989
    csv_print( "%s", "Flag:disqualified" );
1990
    csv_print( "%s", "Flag:non_equestrian" );
246 - 1991
    csv_print( "%s", "Flag:vet_check" );
91 - 1992
    csv_print("\n");
1993
 
1994
 
1995
    for( i = config.min_team; i <= config.max_team; i++ )
1996
    {
1997
        if( valid_field( i ) && g_record( i, &team_buf ) )
1998
        {
1999
            /*
2000
            **  Basic information
2001
            **      - Team number - with Xref back to full result
2002
            **      - Full team name
2003
            **      - Full categoray name - with Xref to category results
2004
            **      - Country name
2005
            */
2006
            csv_print( "%d",   team_buf.numb );
2007
            csv_print( "%s",   team_buf.name );
2008
 
2009
            csv_print( "%s",    team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].full_name );
2010
            csv_print( "%s",    team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
2011
            csv_print( "%s",    time_a (team_buf.teamclass == 0 ? 0 : config.team_class[team_buf.teamclass - 1].start ));
2012
            csv_print( "%d",    team_buf.teamclass == 0 ? 0 : config.team_class[team_buf.teamclass - 1].start );
2013
 
2014
            csv_print( "%s",    config.num_countries == 0
2015
                                || team_buf.country ==
2016
 
2017
 
2018
            age_sum = 0;
2019
            for( j = 1; j <= config.num_legs; j++ )
2020
            {
2021
                csv_print( "%d", j );
2022
                csv_print( "%s", config.leg_name[j - 1] );
2023
                csv_print( "%s", team_buf.members[j-1].name );
2024
                csv_print( "%s", ( team_buf.members[j-1].sex == male ) ? "Male" : "Female" );
2025
                csv_print( "%d", team_buf.members[j-1].age );
2026
                if ( age_sum >= 0 )
2027
                {
2028
                    ushort age = team_buf.members[j-1].age;
2029
                    if ( age > 0 && age < 255 )
2030
                    {
2031
                        age_sum += age;
2032
                    }
2033
                    else
2034
                    {
2035
                        age_sum = -1;
2036
                    }
2037
                }
2038
 
2039
 
2040
                csv_print( "%s", time_a(team_buf.leg[j].start ));
2041
                csv_print( "%d", team_buf.leg[j].start );
2042
                csv_print( "%s", time_a(team_buf.leg[j].end ));
2043
                csv_print( "%d", team_buf.leg[j].end );
2044
                csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
2045
                csv_print( "%d", team_buf.leg[j].elapsed );
2046
                csv_print( "%d", team_buf.leg[j].l_place );
2047
                csv_print( "%d", team_buf.leg[j].le_place );
2048
                csv_print( "%d", team_buf.leg[j].lc_place );
2049
                csv_print( "%d", team_buf.leg[j].lec_place );
2050
                csv_print( "%d", team_buf.leg[j].manual );
2051
            }
2052
 
2053
            j = 0;
2054
            csv_print( "%s", time_a(team_buf.leg[j].start ));
2055
            csv_print( "%d", team_buf.leg[j].start );
2056
            csv_print( "%s", time_a(team_buf.leg[j].end ));
2057
            csv_print( "%d", team_buf.leg[j].end );
2058
            csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
2059
            csv_print( "%d", team_buf.leg[j].elapsed );
2060
//            csv_print( "%d", team_buf.leg[j].l_place );
2061
            csv_print( "%d", team_buf.leg[j].le_place );
2062
//            csv_print( "%d", team_buf.leg[j].lc_place );
2063
            csv_print( "%d", team_buf.leg[j].lec_place );
2064
//            csv_print( "%d", team_buf.leg[j].manual );
2065
 
2066
            csv_print( "%d", age_sum );
2067
            csv_print( "%d", team_buf.flags.valid );
2068
            csv_print( "%d", team_buf.flags.bad_times );
2069
            csv_print( "%d", team_buf.flags.disqualified );
2070
            csv_print( "%d", team_buf.flags.non_equestrian );
246 - 2071
            csv_print( "%d", team_buf.flags.vet_check );
91 - 2072
 
2073
//How about class placings
2074
 
2075
 
2076
            csv_print( "\n" );
2077
        }
2078
    }
2079
 
2080
    close_printer();
2081
}
2082
 
2083
 
2084
/*========================================================================
2085
 *
2086
 *  Print all reports at once
2087
 *  Its all so fast, these days ...
2088
 *
2089
 *  Purpose:
2090
 *      This function is called to print all reports at once
2091
 *
2092
 *  Parameters:
2093
 *      None
2094
 *
2095
 *  Returns:
2096
 *      Nothing
2097
 *
2098
 *========================================================================*/
2099
 
2100
void pri_all_reports ( void )
2101
{
2102
    int leg;
2103
    report_all = TRUE;
2104
 
2105
    pri_team();
2106
 
2107
    for ( leg = 1; leg <= config.num_legs; leg ++ )
2108
    {
2109
        pri_leg_body ( leg );
2110
        pri_eleg_body ( leg );
2111
 
2112
        report_html = html;
2113
 
2114
        pri_leg_body ( leg );
291 david 2115
        pri_eleg_body ( leg );
2116
 
2117
        //report_html = printed;
2118
        //
2119
        //pri_leg_body ( leg );
2120
        //pri_eleg_body ( leg );        
91 - 2121
 
2122
        report_html = text;
2123
    }
2124
 
2125
    pri_final();
2126
    pri_final_html();
2127
    pri_csv_data();
254 - 2128
    pri_summary_html();
91 - 2129
    pri_summary();
2130
    pri_awards_html();
2131
    pri_awards();
2132
    pri_master_index();
2133
 
168 david 2134
    pri_interim();
2135
 
91 - 2136
    report_all = FALSE;
2137
}
2138
 
2139
 
2140
/*========================================================================
2141
 *
2142
 *  Print a class header
2143
 *
2144
 *  Purpose:
2145
 *      This function is called to print a class header
2146
 *
2147
 *  Parameters:
2148
 *      class           Name of this class
2149
 *      final           False - prelim results
2150
 *
2151
 *  Returns:
2152
 *      Nothing
2153
 *
2154
 *========================================================================*/
2155
 
2156
void print_class_header( int teamclass, int final )
2157
{
2158
    int         j;
2159
 
2160
    /*
2161
    **  Give a clear indication that the report is preliminary
2162
    */
2163
 
2164
    if( !final )
2165
        print( "PRELIMINARY RESULTS ONLY\n\n" );
2166
 
2167
    /*
2168
    **  Now printout the column headings
2169
    **  This is a two line display
2170
    **
2171
    **  Line-1  Leg names
2172
    */
2173
    print( "%-*s %-*s %-*s", 4, "", 4, "", 3, "" );
2174
    for( j = 1; j <= config.num_legs; j++ )
2175
    {
2176
        print_bold( TRUE );
2177
        print( "  %-*s", 18, config.leg_name[j - 1] );
2178
        print_bold( FALSE );
2179
    }
2180
    print( "  %-8s %-4s\n", "Total", ( teamclass < 0 ) ? "Cat" : "Fin" );
2181
 
2182
 
2183
    /*
2184
    **  Line-2  Details
2185
    */
2186
    print_underline( TRUE );
2187
    print( "%-*s %*s %-*s", 4, final ? "Plce" : "Team",
2188
                            4, final ? "Team" : "Plce",
2189
                            3, "Cat" );
2190
 
2191
    for( j = 1; j <= config.num_legs; j++ )
2192
        print( "  %-8s %-4s %-4s", "Time", " LP", " EP" );
2193
 
2194
    print( "  %-8s %-4s\n", "Time", "Plce" );
2195
    print_underline( FALSE );
2196
}
2197
 
2198
/*========================================================================
2199
 *
2200
 *  Generate the class stats
2201
 *
2202
 *  Purpose:
2203
 *      This function is called to Generate the class stats
2204
 *
2205
 *  Parameters:
2206
 *      c           Class to print
2207
 *      final       TRUE: Final data
2208
 *                  FALSE: Interim data
2209
 *
2210
 *  Returns:
2211
 *      Nothing
2212
 *
2213
 *========================================================================*/
2214
 
2215
void print_class_stats( int c, int final )
2216
{
2217
    int         i, j;
2218
    const char        *title;
2219
 
2220
    if( c < 0 )
2221
    {
2222
        title = "Event";
2223
        c = 0;
2224
    }
2225
    else
2226
    {
2227
        title = "Category";
2228
    }
2229
 
2230
    print( "\n" );
2231
    if ( report_html ) print_underline(TRUE);
2232
    print( "%s statistics", title );
2233
    if ( report_html ) print_underline(FALSE);
2234
    print( "\n" );
2235
 
2236
 
2237
    /*
2238
    **  Print the names of the different legs
2239
    */
2240
    print( "%-*s       ", LEN_CLASS_NAME, "" );
2241
    for( i = 1; i <= config.num_legs; i++ )
2242
    {
2243
        print_bold( TRUE );
2244
        print( "%-13s  ", config.leg_name[i - 1] );
2245
        print_bold( FALSE );
2246
    }
2247
    print( "%-13s  \n", final ? "Total" : "" );
2248
 
2249
    /*
2250
    **  Print the fastest teams for each leg and overall
2251
    **  Add cross references to the team names for the fastest teams
2252
    */
2253
    print( "%*s : ", LEN_CLASS_NAME, "Fastest" );
2254
    for( i = 0; i <= config.num_legs; i++ )
2255
    {
2256
        j = i + 1;
2257
        if( i >= config.num_legs )
2258
        {
2259
            if( final )
2260
                j = 0;                           /* Leg-0 last */
2261
            else
2262
                break;
2263
        }
2264
 
2265
        if ( report_html == html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), stats.fast.team[j][c] );
2266
        print( "%4d",  stats.fast.team[j][c] );
2267
        if ( report_html == html ) print( "</A>" );
2268
        print( " %s  ", time_a( stats.fast.time[j][c] ) );
2269
    }
2270
    print( "\n" );
2271
 
2272
    /*
2273
    **  Print the average time for each leg
2274
    */
2275
    print( "%*s : ", LEN_CLASS_NAME, "Average" );
2276
    for( i = 0; i <= config.num_legs; i++ )
2277
    {
2278
        j = i + 1;
2279
        if( i >= config.num_legs )
2280
        {
2281
            if( final )
2282
                j = 0;                           /* Leg-0 last */
2283
            else
2284
                break;
2285
        }
2286
        print( "     %s  ", time_a( stats.average[j][c] ) );
2287
    }
2288
}
2289
 
2290
/*========================================================================
2291
 *
2292
 *  Print the legend
2293
 *
2294
 *  Purpose:
2295
 *      This function is called to Print the legend
2296
 *
2297
 *  Parameters:
2298
 *      class       - Class currently being printed
2299
 *      full        - Display full legend
2300
 *
2301
 *  Returns:
2302
 *      Nothing
2303
 *
2304
 *========================================================================*/
2305
 
2306
void print_legend ( int teamclass, int full )
2307
{
2308
 
2309
    int         i;
2310
    char        line[201];
2311
    FILE       *adfile = NULL;
2312
    int         count;
2313
 
2314
    /*
2315
     * First the categories 
2316
     */
2317
    print( "\n\n" );
2318
    if ( report_html ) print_underline(TRUE);
2319
    print( "Category abbreviations" );
2320
    if ( report_html ) print_underline(FALSE);
2321
    print( "\n" );
2322
 
2323
 
2324
    for( i = 1, count = 0; i <= config.num_class; i++ )
2325
    {
313 david 2326
        if (!config.team_class[i - 1].abr[0])
2327
            continue;
91 - 2328
#if 0
2329
        /*
2330
        **  Skip any non-equestrian class in the legend
2331
        **  Don't want to tell the general user whats goes on, unless we actually
2332
        **  creating the non-equestrian report.
2333
        */
2334
        if ( class != config.nonequestrian_class  && i == config.nonequestrian_class )
2335
            continue;
2336
#endif
2337
        if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[i - 1].abr ,"html")) );
2338
        print( "%-*s", 3, config.team_class[i - 1].abr );
2339
        if ( report_html == html ) print( "</A>" );
2340
        print( " : %-*s  ", LEN_CLASS_NAME, config.team_class[i - 1].full_name );
2341
 
2342
        if( !( ++count % 5 ) )
2343
            print( "\n" );
2344
    }
2345
 
2346
    /*
2347
    **  Add link to the finish order report
2348
    */
2349
    if ( report_html == html )
2350
    {
2351
        print( "<A HREF=\"%s\">", url_encode(p_filename(filebase, "finish" ,"html")) );
2352
        print( "%-*s", 3, "All" );
2353
        print( "</A>" );
2354
        print( " : %-*s  ", LEN_CLASS_NAME, "Finishing Order" );
2355
    }
2356
 
2357
    /*
2358
    **  Country data - if countries have been defined
2359
    */
2360
    if( config.num_countries )
2361
    {
2362
        print( "\n\n" );
2363
        if ( report_html ) print_underline(TRUE);
2364
        print( "Country abbreviations" );
2365
        if ( report_html ) print_underline(FALSE);
2366
        print( "\n" );
2367
 
2368
        for( i = 0, count = 0; i < MAX_COUNTRY; i++ )
2369
        {
2370
            if( config.country_name[i].abr[0] )
2371
            {
2372
                print( "%-*s : %-*s  ", 4, config.country_name[i].abr,
2373
                       LEN_CNTRY_NAME, config.country_name[i].full_name );
2374
                if( !( ++count % 5 ) )
2375
                    print( "\n" );
2376
            }
2377
        }
2378
    }
2379
    print( "\n" );
2380
 
2381
    /*
2382
     * Other comments 
2383
     */
2384
    if ( full )
2385
    {
2386
        print( "\nPlace numbers (LP and EP)\n" );
2387
        print( "LP - Placing based on elapsed time within the leg.                Cat Plce - Placing within the category.\n" );
2388
        print( "EP - Placing based on accumulated times to the end of that leg.   Fin Plce - Overall placing within the event.\n" );
2389
        print( "U  - Placing not available.\n" );
2390
    }
2391
 
2392
    /*
2393
     *  Insert the contents of the config.addendum file
2394
     *  or a defualt message
2395
     */
2396
    if( config.addendum[0] )
199 david 2397
    {
315 david 2398
        QString name = QmConfig::getAddendemFile(config.addendum);
2399
        if (! name.isEmpty())
2400
        {
2401
            adfile = fopen( qPrintable(name), "rt" );  /* Open the file for reading */
2402
        }
199 david 2403
    }
91 - 2404
 
2405
    if( adfile )
2406
    {
2407
        while( fgets( line, sizeof(line)-1, adfile ) )
2408
            print( "%s", line );
2409
    }
2410
    else
2411
    {
2412
        print( "\nTiming and Results by\n" );
2413
        print( "Embedded Solutions\n" );
2414
    }
2415
}
2416
 
2417
/*========================================================================
2418
 *
2419
 *  Return place data
2420
 *
2421
 *  Purpose:
2422
 *      This function is called to return place data
2423
 *
2424
 *      This routine is called to fill a print team_buffer - to allow for
2425
 *      multiple calls to this function ( before the data is used ) a number
2426
 *      of static team_buffers are maintained
2427
 *
2428
 *  Parameters:
2429
 *      num         place - if not bad_times
2430
 *      disq        Disqualified flag
2431
 *      time        Time data is based on
2432
 *
2433
 *  Returns:
2434
 *      This function returns a pointer to the character string for the
2435
 *      number or a pointer to a bad_times string.
2436
 *
2437
 *========================================================================*/
2438
 
2439
const char       *pi_place( int num, int disq, time_t time )
2440
{
2441
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2442
    static int  i = 0;                           /* Current index into store */
2443
    static const char *dis = "D";                      /* Disqualified */
2444
    static const char *non = "-";                      /* Invalid time */
2445
 
2446
    if( disq )                                   /* Disqualified team */
2447
        return ( dis );
2448
    if( time <= 0 )                              /* Unknown time */
2449
        return ( non );
2450
 
2451
    i++;
2452
    if( i >= 2 )
2453
        i = 0;                                   /* Select next entry */
2454
    sprintf( store[i], "%4d", num );
2455
    return ( store[i] );
2456
 
2457
}
2458
 
2459
/*========================================================================
2460
 *
2461
 *  Return place data
2462
 *
2463
 *  Purpose:
2464
 *      This function is called to Return place data
2465
 *
2466
 *      This routine is called to fill a print team_buffer - to allow for
2467
 *      multiple calls to this function ( before the data is used ) a number
2468
 *      of static team_buffers are maintained
2469
 *
2470
 *  Parameters:
2471
 *      num         place - if not bad_times
2472
 *      disq        Disqualified flag
2473
 *
2474
 *  Returns:
2475
 *      This function returns a pointer to the character string for the
2476
 *      number or a pointer to a bad_times string.
2477
 *
2478
 *========================================================================*/
2479
 
2480
const char *pr_place( int num, int disq )
2481
{
2482
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2483
    static int  i = 0;                           /* Current index into store */
2484
    static const char *dis = "U";
2485
 
2486
    if( disq )
2487
        return ( dis );
2488
 
2489
    i++;
2490
    if( i >= 2 )
2491
        i = 0;                                   /* Select next entry */
2492
    sprintf( store[i], "%4d", num );
2493
    return ( store[i] );
2494
 
2495
}
2496
 
2497
/*========================================================================
2498
 *
2499
 *  Check data for bad times
2500
 *
2501
 *  Purpose:
2502
 *      This function is called to Check data for bad times
2503
 *      Scan the sort data structure and locate entries that have incorrect
2504
 *      times.
2505
 *      
2506
 *      Entries that have invalid leg times are displayed to the operator
2507
 *      and the report process can be aborted
2508
 *
2509
 *  Parameters:
189 - 2510
 *      leg             Leg to test
91 - 2511
 *      mode            Either end or elapsed times to be tested
2512
 *
2513
 *  Returns:
2514
 *      Returns FALSE if the report operation is to be aborted
2515
 *
2516
 *========================================================================*/
2517
 
2518
bool ck_data( int leg, int mode )
2519
{
2520
    ty_s_data  *ptr;
2521
    unsigned    i;
2522
    int         bad = 0;
189 - 2523
    int         k;
2524
    int         bad_leg;
91 - 2525
    time_t     *t;                               /* An array of times */
2526
 
2527
    ptr = sort_data;
2528
    for( i = 1; i <= sort_num; i++, ptr++ )
2529
    {
2530
        bad_leg = 0;
2531
        if( mode == C_DISQUAL )
2532
        {
2533
            ptr->flags.bad_times = ptr->flags.disqualified;
2534
            continue;
2535
        }
2536
 
2537
        if( mode == C_ELAPSED )
2538
            t = ptr->lege;
2539
        else
2540
            t = ptr->leg;
2541
 
2542
 
246 - 2543
        ptr->flags.bad_times = ptr->flags.disqualified;
91 - 2544
        if( ! ptr->flags.bad_times )
2545
        {
2546
            if( leg <= 0 )
2547
            {
2548
                for( k = 0; k <= config.num_legs; k++ )
2549
                {
2550
                    if ( !(config.equestrian_leg && ptr->flags.non_equestrian && config.equestrian_leg == k  ))
2551
                        bad_leg |= ( t[k] <= 0 );
2552
                }
2553
            }
2554
            else
2555
            {
2556
                bad_leg = t[leg] <= 0;
2557
            }
2558
 
2559
            if( bad_leg )
2560
            {
2561
                ptr->flags.bad_times = TRUE;
2562
 
2563
                if ( ! report_all )
2564
                {
314 david 2565
                    qDebug( "Team with incorrect time information: %d", ptr->team  );
91 - 2566
                    bad++;
2567
                }
2568
            }
2569
        }
2570
    }
2571
 
2572
    if( bad )
2573
    {
314 david 2574
        qDebug( "%d teams with incorrect times. These have been flagged as unplaced", bad );
91 - 2575
    }
2576
    return ( FALSE );
2577
}
2578
 
2579
/*========================================================================
2580
 *
2581
 *  Update placing information
2582
 *
2583
 *  Purpose:
2584
 *      This function is called to Update placing information
2585
 *
2586
 *      This routine will rip through the data generating the team placing in
2587
 *      a) Within a leg
2588
 *      b) At the end of a leg
2589
 *      c) Within a leg by  class
2590
 *      d) At the end of a leg by class
2591
 *
2592
 *      This function is provided to allow the display routines to
2593
 *      be accessed and updated without the need to run a report
2594
 *
2595
 *  Parameters:
2596
 *      xxxx        a ptr to the xxxx stuff
2597
 *
2598
 *  Returns:
2599
 *      Nothing
2600
 *
2601
 *========================================================================*/
2602
 
2603
void srt_place(void)
2604
{
2605
    if( ck_data( -1, C_ELAPSED ) )
2606
        return;
2607
    do_big_sort();
2608
 
2609
    /*
195 david 2610
     * Generate the stats
91 - 2611
     */
2612
    gen_stats();
2613
}
2614
 
2615
/*========================================================================
2616
 *
2617
 *  Calculate summary information
2618
 *
2619
 *  Purpose:
2620
 *      This function is called to calculate summary information
2621
 *
2622
 *  Parameters:
2623
 *      ptr         - Address of a summary structure to fill in
2624
 *
2625
 *  Returns:
2626
 *      Nothing
2627
 *
2628
 *========================================================================*/
2629
 
2630
void calc_class_summary( t_class_summary * ptr )
2631
{
2632
    int i;
170 - 2633
    team_type   team_buf;
91 - 2634
 
2635
    /*
2636
    **  Reset the data
2637
    */
2638
    memset ( ptr, 0, sizeof (*ptr ));
2639
 
2640
    /*
2641
     * Extract the required data from the data base
2642
     * Only save that information required for the operation
2643
     */
2644
 
2645
    for( i = config.min_team; i <= config.max_team; i++ )
2646
    {
2647
        if( valid_field( i ) && g_record( i, &team_buf ) )
2648
        {
227 - 2649
            bool valid = true;
284 david 2650
            bool valid_ne = true;
91 - 2651
 
2652
            if ( team_buf.flags.disqualified )
2653
            {
2654
                ptr->teamclass[team_buf.teamclass].disqualified++;
2655
                ptr->total.disqualified++;
227 - 2656
                valid = false;
284 david 2657
                valid_ne = false;
91 - 2658
            }
2659
 
225 - 2660
            if ( team_buf.flags.vet_check )
2661
            {
2662
                ptr->teamclass[team_buf.teamclass].vet_check++;
2663
                ptr->total.vet_check++;
227 - 2664
                valid = false;
284 david 2665
                valid_ne = false;
225 - 2666
            }
2667
 
91 - 2668
            if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
2669
            {
2670
                ptr->teamclass[team_buf.teamclass].non_equestrian++;
2671
                ptr->total.non_equestrian++;
227 - 2672
                valid = false;
91 - 2673
            }
284 david 2674
            else
2675
            {
2676
                valid_ne = false;
2677
            }
227 - 2678
 
2679
            ptr->total.total++;
2680
            ptr->teamclass[team_buf.teamclass].total++;
2681
            if ( valid )
2682
            {
2683
                ptr->total.valid++;
2684
                ptr->teamclass[team_buf.teamclass].valid++;
284 david 2685
 
2686
                if ( ! team_buf.flags.bad_times )
2687
                {
2688
                    ptr->total.valid_ev++;
2689
                    ptr->teamclass[team_buf.teamclass].valid_ev++;
2690
                }
227 - 2691
            }
284 david 2692
 
2693
            if ( valid_ne )
2694
            {
2695
                if ( ! team_buf.flags.bad_times )
2696
                {
2697
                    ptr->total.valid_ne++;
2698
                    ptr->teamclass[team_buf.teamclass].valid_ne++;
2699
                }
2700
            }
91 - 2701
        }
2702
    }
2703
 
2704
    /*
2705
    **  Fix up the totals for the non equestrians
2706
    **  This is not a real category but a summary of the others.
2707
    */
2708
    if ( config.nonequestrian_class  )
2709
    {
2710
        ptr->teamclass[config.nonequestrian_class].total += ptr->total.non_equestrian;
227 - 2711
        ptr->teamclass[config.nonequestrian_class].valid += ptr->teamclass[config.nonequestrian_class].total;
91 - 2712
    }
2713
}
2714
 
2715
/*========================================================================
2716
 *
254 - 2717
 *  Print summary results
2718
 *
2719
 *  Purpose:
2720
 *      This function is called to Print summary results
2721
 *      Keep the page to 80 cols, so that it can be pronted on A4
2722
 *
2723
 *  Parameters:
2724
 *      None
2725
 *
2726
 *  Returns:
2727
 *      Nothing
2728
 *
2729
 *========================================================================*/
2730
void pri_summary_html(void)
2731
{
2732
    report_type saved = report_html;
2733
    /*
2734
    **  Generate ALL results with HTML tags
2735
    */
2736
    report_html = html;
2737
    pri_summary();
291 david 2738
    report_html = printed;
2739
    pri_summary();
254 - 2740
    report_html = saved;
2741
}
2742
 
2743
/*========================================================================
2744
 *
91 - 2745
 *  Print summary information
2746
 *
2747
 *  Purpose:
2748
 *      This function is called to print summary information
2749
 *
2750
 *  Parameters:
2751
 *      None
2752
 *
2753
 *  Returns:
2754
 *      Nothing
2755
 *
2756
 *========================================================================*/
2757
 
2758
void pri_summary (void)
2759
{
2760
 
2761
    t_class_summary data;
2762
    int         i;
2763
 
254 - 2764
    if( !open_printer( "", "summary", 80, report_html, "Summary Information" ) )
91 - 2765
        return;
2766
 
2767
    calc_class_summary( & data );
2768
 
2769
 
2770
    /*
2771
    **  Display summary stats
2772
    */
2773
 
284 david 2774
    print( "%*s : %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
254 - 2775
                                      LEN_CLASS_NAME, "Category",
91 - 2776
                                      "Total",
253 - 2777
                                      "Valid",
91 - 2778
                                      "Disq",
253 - 2779
                                      "NonEq",
284 david 2780
                                      "VetChk",
2781
                                      "CompEv",
2782
                                      "CompNe"
253 - 2783
                                       );
91 - 2784
 
2785
    for( i = 0; i < config.num_class; i++ )
2786
    {
313 david 2787
        if (!config.team_class[i].abr[0])
2788
            continue;
91 - 2789
        /*
2790
        **  The non-equestrian leg does not have any data
313 david 2791
        **  Suppress the display
91 - 2792
        */
2793
        if ( config.nonequestrian_class == i+1  )
2794
            continue;
2795
 
254 - 2796
        if ( report_html == html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[i].abr ,"html")) );
2797
        print( "%*s ", LEN_CLASS_NAME, config.team_class[i].full_name );
2798
        if ( report_html == html ) print( "</A>" );
2799
 
284 david 2800
        print( ": %-7d %-7d %-7d %-7d %-7d %-7d %-7d\n",
91 - 2801
                                          data.teamclass[i+1].total,
253 - 2802
                                          data.teamclass[i+1].valid,
91 - 2803
                                          data.teamclass[i+1].disqualified,
253 - 2804
                                          data.teamclass[i+1].non_equestrian,
284 david 2805
                                          data.teamclass[i+1].vet_check,
2806
                                          data.teamclass[i+1].valid_ev,
2807
                                          data.teamclass[i+1].valid_ne
253 - 2808
                                          );
91 - 2809
    }
2810
 
2811
    print( "\n" );
284 david 2812
    print( "%*s : %-7d %-7d %-7d%-7d %-7d %-7d %-7d\n",
254 - 2813
                                      LEN_CLASS_NAME, "Totals",
91 - 2814
                                      data.total.total,
253 - 2815
                                      data.total.valid,
91 - 2816
                                      data.total.disqualified,
253 - 2817
                                      data.total.non_equestrian,
284 david 2818
                                      data.total.vet_check,
2819
                                      data.total.valid_ev,
2820
                                      data.total.valid_ne
253 - 2821
                                      );
91 - 2822
 
2823
    close_printer();
2824
}
2825
 
2826
 
2827
 
2828
/*========================================================================
2829
 *
2830
 *  Main sort routine for final data
2831
 *
2832
 *  Purpose:
2833
 *      This function is called to do the report sort routine for final data
2834
 *      This routine will fill all the gaps in the sort_aux structure
2835
 *
2836
 *  Parameters:
2837
 *      None
2838
 *
2839
 *  Returns:
2840
 *      Nothing
2841
 *
2842
 *========================================================================*/
2843
 
2844
void do_big_sort(void)
2845
{
2846
    int         i, k;                            /* Looooopy things */
2847
    unsigned    j;
2848
    ty_s_data  *ptr;                             /* Pointer to sort data */
2849
    int         teamclass;                           /* Current class */
170 - 2850
    team_type   team_buf;
91 - 2851
 
2852
    for( i = 0; i <= config.num_legs; i++ )
2853
    {
2854
        /*
2855
        **  Sort on leg elapsed time
2856
        **  Then save the teams elapsed place in each leg
2857
        */
2858
        sort_team_data( i, S_L );
2859
        for( j = 1, k = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
2860
        {
2861
            if ( ptr->teamclass == config.nonequestrian_class )
2862
                continue;
2863
 
2864
            sort_aux[ptr->team].l_place[i] = k++;
2865
            sort_aux[ptr->team].team = ptr->team;
2866
        }
2867
 
2868
        /*
2869
        **  Sort on leg end time
2870
        **  Then save the teams place at the end of each leg
2871
        */
2872
        sort_team_data( i, S_LE );
2873
        for( j = 1, k = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
2874
        {
2875
            if ( ptr->teamclass == config.nonequestrian_class )
2876
                continue;
2877
 
2878
            sort_aux[ptr->team].le_place[i] = k++;
2879
        }
2880
 
2881
        /*
2882
        **  Sort on elapsed time per class
2883
        **  The save the teams elapsed place in each leg per class
2884
        */
2885
        sort_team_data( i, S_LC );
2886
        teamclass = -1;
2887
        for( k = 1, j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
2888
        {
2889
            if( teamclass != ptr->teamclass )
2890
            {
2891
                k = 1;
2892
                teamclass = ptr->teamclass;
2893
            }
2894
            sort_aux[ptr->team].lc_place[i] = k++;
2895
        }
2896
 
2897
        /*
2898
        **  Sort on end time per class
2899
        **  Then save the teams place at the end of each leg per class
2900
        */
2901
        sort_team_data( i, S_LEC );
2902
        teamclass = -1;
2903
        for( k = 1, j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
2904
        {
2905
            if( teamclass != ptr->teamclass )
2906
            {
2907
                k = 1;
2908
                teamclass = ptr->teamclass;
2909
            }
2910
            sort_aux[ptr->team].lec_place[i] = k++;
2911
        }
2912
    }
2913
 
2914
    /*
2915
    **  Write the place information back to disk for use in the displays
2916
    */
2917
    for( i = config.min_team; i <= config.max_team; i++ )
2918
    {
2919
        if( sort_aux[i].team && valid_field( i ) && g_record( i, &team_buf ) )
2920
        {
2921
            for( k = 0; k <= config.num_legs; k++ )
2922
            {
2923
                team_buf.leg[k].l_place = sort_aux[i].l_place[k];
2924
                team_buf.leg[k].le_place = sort_aux[i].le_place[k];
2925
                team_buf.leg[k].lc_place = sort_aux[i].lc_place[k];
2926
                team_buf.leg[k].lec_place = sort_aux[i].lec_place[k];
2927
            }
2928
            put_team_record( i, &team_buf );
2929
        }
2930
    }
2931
}
2932
 
2933
/*========================================================================
2934
 *
2935
 *  Sort in memory buffer
2936
 *
2937
 *  Purpose:
2938
 *      This function is called to Sort in memory buffer
2939
 *
2940
 *  Parameters:
2941
 *      leg         Requested leg
2942
 *      mode        Defines the sort mode
2943
 *
2944
 *  Returns:
2945
 *      Nothing
2946
 *
2947
 *========================================================================*/
2948
 
2949
void sort_team_data( int leg, int mode )
2950
{
2951
 
2952
    unsigned    j;
2953
    ty_s_data  *ptr;
2954
 
2955
    sort_leg = leg;                              /* Leg is global for the comparison function */
2956
    sort_mode = mode;                            /* Mode is global for compare function */
2957
 
2958
    qsort( ( char * ) sort_data, sort_num, sizeof( ty_s_data ), sort_comp );
2959
 
2960
    /*
2961
     * Insert "place data" into the sorted data
2962
     * This simply the index into the array of data - after its been
2963
     * sorted.
2964
     */
2965
    ptr = sort_data;
2966
    for( j = 1; j <= sort_num; j++, ptr++ )
246 - 2967
    {
91 - 2968
        ptr->place = j;
246 - 2969
    }
91 - 2970
 
2971
}
2972
 
2973
/*========================================================================
2974
 *
2975
 *  qsort comparison function
2976
 *
2977
 *  Purpose:
2978
 *      This function is called by qsort as a Sort comparison function
2979
 *
2980
 *  Parameters:
2981
 *      a       - 1st record
2982
 *      b       - 2nd record
2983
 *
2984
 *  Returns:
2985
 *      value to qsort
246 - 2986
 *      Return negative if 'a' is less than 'b',
2987
 *             zero if 'a' == 'b'
2988
 *             positive if 'a' > 'b'
91 - 2989
 *
2990
 *========================================================================*/
2991
 
2992
int sort_comp( const void * aa, const void * bb )
2993
{
2994
    const ty_s_data * a = (ty_s_data *)aa;
2995
    const ty_s_data * b = (ty_s_data *)bb;
2996
 
2997
 
2998
    int         i;                               /* One of those */
2999
    time_t      ta, tb;                          /* Leg times */
3000
    int         na, nb;                          /* Number of valid legs */
3001
    time_t      tta, ttb;                        /* Temp times */
3002
 
3003
    /*
3004
    **  Sorting on Team Number
3005
    */
3006
    if( sort_mode == S_TEAM )
3007
        return ( a->team - b->team );
3008
 
3009
    /*
3010
    **  Sorting on Class and Team Number
3011
    */
3012
    if( sort_mode == S_CLASS )
3013
    {
3014
        if( a->teamclass != b->teamclass )
3015
            return ( a->teamclass - b->teamclass );
3016
        else
3017
            return ( a->team - b->team );
3018
    }
3019
 
3020
    /*
3021
    **  Sorting within a class
3022
    **  First sort on the class
3023
    */
227 - 3024
    if( sort_mode == S_LEC || sort_mode == S_LC || sort_mode == S_LC_NE )   /* Sort within a class */
91 - 3025
        if( a->teamclass != b->teamclass )
3026
            return ( a->teamclass - b->teamclass );
3027
 
3028
    /*
3029
    **  Always put the nonequestrian_class at the end
3030
    **  Simplifies the creation of ordered lists as these
3031
    **  Entries are not present in the main body
3032
    */
3033
    if ( a->teamclass == config.nonequestrian_class ||  b->teamclass == config.nonequestrian_class )
3034
    {
3035
        if ( a->teamclass == config.nonequestrian_class &&  b->teamclass == config.nonequestrian_class )
3036
        {
246 - 3037
            /*
3038
            **  Both are non equestrian
3039
            **      S_LC_NE - Elapsed times per class, with NE sorted by real class
3040
            **                Only used in awards
3041
            */
227 - 3042
            if (sort_mode == S_LC_NE )
3043
            {
3044
                if ( a->real_teamclass != b->real_teamclass )
3045
                {
3046
                    return ( a->real_teamclass - b->real_teamclass );
3047
                }
3048
            }
91 - 3049
        }
3050
        else
3051
        {
246 - 3052
            /*
3053
            **  Only one in non-equestrian
3054
            **  Place Non-equestrian AFTER equestrian
3055
            */
91 - 3056
            return ( a->teamclass == config.nonequestrian_class ? 1 : -1 );
3057
        }
3058
 
3059
    }
3060
 
3061
    /*
3062
    **  Now we need to examine the times as we have sorted
3063
    **  on every thing else.
3064
    **
3065
    **  If one of the teams has bad_times, then that team is placed
3066
    **  lower in the sorting order. If both teams have bad times
3067
    **  then sort on team number. ie: Unplaced teams are sorted on
3068
    **  team number
3069
    **
3070
    **  If not sorting within a class (ie Overall), then Non_Equestrian
3071
    **  is better than a bad time. Places NE before disqualified
3072
    **
3073
    */
246 - 3074
    if( a->flags.bad_times || b->flags.bad_times || a->flags.non_equestrian || b->flags.non_equestrian)  /* Valid data has precedence */
91 - 3075
    {
3076
        if( a->flags.bad_times && b->flags.bad_times )
3077
        {
246 - 3078
            return ( a->team - b->team );
91 - 3079
        }
246 - 3080
        else if( a->flags.bad_times || b->flags.bad_times )
3081
        {
91 - 3082
            return ( a->flags.bad_times ? 1 : -1 );
246 - 3083
        }
3084
        if( sort_mode != S_FIN && sort_mode != S_IFIN )
91 - 3085
        {
3086
            if( a->flags.non_equestrian && b->flags.non_equestrian )
246 - 3087
            {
3088
                /*
3089
                **  Both are non equestrian
3090
                **  Let the time sort operate ...
3091
                */
3092
                //return ( a->team - b->team );
3093
            }
91 - 3094
            else
246 - 3095
            {
3096
                /* One is equestrian */
3097
                /* Good times better than NE */
91 - 3098
                return ( a->flags.non_equestrian ? 1 : -1 );
246 - 3099
            }
91 - 3100
        }
3101
    }
3102
 
3103
    /*
3104
    **  Before we sort on times we must determine which time to
3105
    **  use. Finish time, Leg end times, Leg Elapsed times.
3106
    */
3107
 
3108
    switch ( sort_mode )
3109
    {
3110
      /*
3111
      **    Sort on finish times
3112
      */
3113
      case S_FIN:
3114
        ta = a->leg[sort_leg];
3115
        tb = b->leg[sort_leg];
3116
        break;
3117
 
3118
 
3119
      /*
3120
      **    Sort on accumulated leg times
3121
      */
3122
      case S_LE:
3123
      case S_LEC:
3124
        if( sort_leg )
3125
        {
3126
            /*
3127
            **  Calculate accumulated time up to the desired leg
3128
            **  If the two teams have a different number of valid
3129
            **  leg times then order by the team that has completed
3130
            **  more legs.
3131
            */
3132
            ta = tb = ( time_t ) 0;
3133
            na = nb = 0;
3134
            for( i = 1; i <= sort_leg; i++ )
3135
            {
3136
                tta = a->lege[i];
3137
                ttb = b->lege[i];
3138
                if( tta > 0 )
3139
                {
3140
                    na++;
3141
                    ta += tta;
3142
                }
3143
                if( ttb > 0 )
3144
                {
3145
                    nb++;
3146
                    tb += ttb;
3147
                }
3148
            }
3149
            if( na != nb )
3150
                return ( nb - na );
3151
        }
3152
        else
3153
        {
3154
            ta = a->leg[sort_leg] - a->start;
3155
            tb = b->leg[sort_leg] - b->start;
3156
        }
3157
        break;
3158
 
3159
      /*
3160
      **    Sort on Elapsed times
3161
      */
227 - 3162
      case S_LC_NE:
91 - 3163
      case S_LC:
3164
      case S_L:
246 - 3165
      case S_IFIN:
91 - 3166
        ta = a->lege[sort_leg];
3167
        tb = b->lege[sort_leg];
3168
        break;
3169
 
3170
      /*
3171
      **    Just to be sure ...
3172
      */
3173
      default:
3174
        return ( 0 );
3175
    }
3176
 
3177
    /*
3178
    **  Finally. Compare the required team times
3179
    */
3180
    if( ta == tb )
3181
        return ( a->team - b->team );
3182
    if( ( ta > 0 ) && ( tb > 0 ) )
3183
        return ( ( int ) ( ta - tb ) );
3184
    return ( ( ta > 0 ) ? -1 : 1 );
3185
}
3186
 
3187
/*========================================================================
3188
 *
3189
 *  qsort comparison function - competitor names
3190
 *
3191
 *  Purpose:
3192
 *      This function is called by qsort as a Sort comparison function
3193
 *
3194
 *  Parameters:
3195
 *      a       - 1st record
3196
 *      b       - 2nd record
3197
 *
3198
 *  Returns:
3199
 *      value to qsort
3200
 *
3201
 *========================================================================*/
3202
 
3203
int sort_comp_cname( const void * aa, const void * bb )
3204
{
3205
    ty_s_namedata * a = (ty_s_namedata *)aa;
3206
    ty_s_namedata * b = (ty_s_namedata *)bb;
3207
 
3208
 
3209
    int         i;                               /* One of those */
3210
 
3211
    /*
3212
    **  Sort by name
3213
    */
3214
    i = strcmp ( a->name, b->name );
3215
    if ( i )
3216
        return ( i );
3217
    a->multi=1;
3218
    b->multi=1;
3219
 
3220
    /*
3221
    **  Sort by Leg
3222
    */
3223
    i = a->leg - b->leg;
3224
    if ( i )
3225
        return ( i );
3226
 
3227
    /*
3228
    **  Sorting on Team Number
3229
    */
3230
    return ( a->team - b->team );
3231
 
3232
}
3233
 
3234
/*========================================================================
3235
 *
3236
 *  load report data into memory
3237
 *
3238
 *  Purpose:
3239
 *      This routine will pull all the data into memory 
3240
 *      Not all the team data is loaded. Only that essential for the
3241
 *      operation of the sort routine is loaded
3242
 *
3243
 *  Parameters:
3244
 *      None
3245
 *
3246
 *  Returns:
3247
 *      TRUE - All is well
3248
 *
3249
 *========================================================================*/
3250
 
3251
bool load_report_data(void)
3252
{
3253
 
3254
    ty_s_data  *ptr;                             /* pointer to sort data type */
3255
    ty_s_data  *last;
3256
    int         j;
3257
    unsigned    num;
170 - 3258
    team_type   team_buf;
91 - 3259
 
3260
    /*
3261
     * Fetch memory for the data store
3262
     */
3263
 
3264
    if( sort_data )
3265
        free( ( char * ) sort_data );
3266
    sort_data = 0;
3267
 
3268
    if( sort_aux )
3269
        free( ( char * ) sort_aux );
3270
    sort_aux = 0;
3271
    sort_num = 0;                                /* Counter of records in the array */
3272
 
3273
    /*
3274
    **  Allocate memory for the data structures
3275
    **  This will be free'd
3276
    */
3277
    num = config.max_team - config.min_team + 1 ;
3278
 
3279
    /*
3280
    **  Allow for non-equestrian teams - since some of the data is loaded twice
313 david 3281
    **  OLD: Take a guess that at most 1/2 the teams will be non-equestrian
3282
    **  NEW: Memory is cheap. Assume all teams could be non-equestrian
91 - 3283
    */
313 david 3284
    // OLD: num = num * 3 / 2;
3285
    num = num * 2;
91 - 3286
 
3287
    sort_data = ( ty_s_data * ) calloc ( num , sizeof( ty_s_data ) );
3288
    sort_aux = ( ty_s_aux * ) calloc( num + 1, sizeof( ty_s_aux ) );
3289
 
3290
    if( sort_data == 0 || sort_aux == 0 )
3291
    {
170 - 3292
        MainWindow::showMessage("Error in allocating memory");
91 - 3293
        return ( FALSE );
3294
    }
3295
 
3296
    /*
3297
    **  Load data into the memory based data structure
3298
    **  Only load data for valid-teams
3299
    **  Load essential data
3300
    **      Team Number, class and flags
3301
    **      Leg end and elapsed times
3302
    */
3303
    ptr = sort_data;
170 - 3304
    for( int team = config.min_team; team <= config.max_team; team++ )
91 - 3305
    {
3306
        if( valid_field( team ) )
3307
        {
3308
            g_record( team, &team_buf );
3309
            if( team_buf.flags.valid )
3310
            {
3311
                last = ptr;
3312
                ptr->team = team;
3313
                for( j = 0; j < MAX_LEGS + 1; j++ )
3314
                {
3315
                    ptr->lege[j] = team_buf.leg[j].elapsed;
3316
                    ptr->leg[j] = team_buf.leg[j].end;
3317
                }
3318
                ptr->start = team_buf.leg[0].start;
3319
                ptr->teamclass = team_buf.teamclass;
227 - 3320
                ptr->real_teamclass = team_buf.teamclass;
91 - 3321
 
3322
                ptr->flags = team_buf.flags;
246 - 3323
//                if (ptr->flags.non_equestrian)
3324
//                    ptr->flags.disqualified = true;
225 - 3325
                if (ptr->flags.vet_check)
3326
                    ptr->flags.disqualified = true;
246 - 3327
 
91 - 3328
                ptr++;
3329
                sort_num++;
3330
 
246 - 3331
 
91 - 3332
                /*
3333
                **  If non-equestrian support is enabled then
3334
                **  duplicate and modify data for the non-equestrian teams
3335
                **      - Change the class
3336
                **      - Modify the leg time
3337
                */
3338
#if 1
3339
                if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
3340
                {
3341
                    ptr->team = team;
3342
                    ptr->lege[0] = 0;
3343
 
3344
                    for( j = 0; j < MAX_LEGS + 1; j++ )
3345
                    {
3346
                        if ( j == config.equestrian_leg )
3347
                        {
3348
                            last->lege[j] = ptr->lege[j] = 0;
3349
                            if ( config.equestrian_leg > 1 )
3350
                                last->leg[j] = ptr->leg[j] = ptr->leg[j-1];
3351
                        }
3352
                        else
3353
                        {
3354
                            if ( j )
3355
                                last-> lege[j] = ptr->lege[j] = team_buf.leg[j].elapsed;
3356
                            last->leg[j] = ptr->leg[j] = team_buf.leg[j].end;
3357
                            ptr->lege[0] += ptr->lege[j] ;
3358
                            last->lege[0] = ptr->lege[0] ;
3359
                        }
3360
                    }
3361
 
3362
                    last->start = ptr->start = team_buf.leg[0].start;
3363
                    ptr->teamclass = config.nonequestrian_class;
227 - 3364
                    ptr->real_teamclass = team_buf.teamclass;
91 - 3365
                    ptr->flags = team_buf.flags;
246 - 3366
//                    ptr->flags.disqualified = false;
91 - 3367
 
3368
                    ptr++;
3369
                    sort_num++;
3370
                }
3371
#endif
3372
            }
3373
        }
3374
    }
3375
    return ( TRUE );
3376
}
3377
 
3378
/*========================================================================
3379
 *
3380
 *  Generate all the stats
3381
 *
3382
 *  Purpose:
3383
 *      This function is called to Generate all the stats
3384
 *
3385
 *  Parameters:
3386
 *      None
3387
 *
3388
 *  Returns:
3389
 *      Nothing
3390
 *
3391
 *========================================================================*/
3392
 
3393
void gen_stats(void)
3394
{
3395
    ty_s_data  *ptr;
3396
    unsigned    i;
3397
    int         j;
3398
    int         k;
3399
 
3400
    /*
3401
     * Init all the stats 
3402
     */
3403
    for( i = 0; i <= MAX_LEGS; i++ )
3404
    {
3405
        for( j = 0; j <= MAX_CLASS; j++ )
3406
        {
3407
            stats.team[i][j] = 0;
3408
            stats.fast.team[i][j] = 0;
3409
            stats.fast.time[i][j] = ( time_t ) - 1;
3410
            stats.average[i][j] = 0;
3411
        }
3412
    }
3413
 
3414
    for( i = 1, ptr = sort_data; i <= sort_num; i++, ptr++ )
3415
    {
3416
        /*
3417
        **  If there is any bad times in the team record, then none
3418
        **  of the data can be trusted.
3419
        **
3420
        **  If the team has been disqualified, then don't use any
3421
        **  of the data.
3422
        */
246 - 3423
        if( ptr->flags.bad_times || ptr->flags.disqualified  )
91 - 3424
            continue;
3425
 
246 - 3426
        /*
3427
        **  Ignore non-Equestrian, unless we are processing the psuedo NE class
3428
        */
3429
        if ( ptr->teamclass != config.nonequestrian_class && ptr->flags.non_equestrian)
3430
            continue;
3431
 
3432
 
91 - 3433
        for( j = 0; j <= config.num_legs; j++ )
3434
        {
3435
            if( ptr->lege[j] <= 0 )              /* Ignore bad data */
3436
                continue;
3437
 
3438
            /*
3439
            **  Determine fastest team : overall
3440
            **  Ignore the non-equestrian data as this is in the list twice
3441
            */
3442
            if( ( ptr->lege[j] < stats.fast.time[j][0] )
3443
                || ( stats.fast.time[j][0] < 0 ) )
3444
            {
3445
                if ( ptr->teamclass != config.nonequestrian_class )
3446
                {
3447
                    stats.fast.team[j][0] = ptr->team;
3448
                    stats.fast.time[j][0] = ptr->lege[j];
3449
                }
3450
            }
3451
 
3452
            /*
3453
            **  Determine fastest team : within a class
3454
            */
3455
            if( ( ptr->lege[j] < stats.fast.time[j][ptr->teamclass] )
3456
                || stats.fast.time[j][ptr->teamclass] < 0 )
3457
            {
3458
                stats.fast.team[j][ptr->teamclass] = ptr->team;
3459
                stats.fast.time[j][ptr->teamclass] = ptr->lege[j];
3460
            }
3461
 
3462
            /*
3463
            **  Sum the end times : overall
3464
            */
3465
            if ( ptr->teamclass != config.nonequestrian_class )
3466
            {
3467
                stats.average[j][0] += ptr->lege[j];
3468
                stats.team[j][0]++;
3469
            }
3470
 
3471
 
3472
            /*
3473
            **  Sum the end times : within a class
3474
            */
3475
            stats.average[j][ptr->teamclass] += ptr->lege[j];
3476
            stats.team[j][ptr->teamclass]++;
3477
        }
3478
    }
3479
 
3480
    /*
313 david 3481
     * Calculate the averages 
3482
     *      Can ignore that fact that some categories don't exists
3483
     *      The results will be -1
91 - 3484
     */
3485
    for( k = 0; k <= config.num_legs; k++ )
3486
    {
3487
        for( j = 0; j <= config.num_class; j++ )
3488
        {
3489
            if( stats.team[k][j] )
3490
                stats.average[k][j] /= stats.team[k][j];
3491
            else
3492
                stats.average[k][j] = ( time_t ) - 1;
3493
        }
3494
    }
3495
}
3496
 
3497
/********************************* EOF ***********************************/