Subversion Repositories svn1

Rev

Rev 389 | 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
380 david 23
*       px_place                - Return place data with NE and V indication
91 - 24
*       ck_data                 - Check data for bad times
25
*       srt_place               - Update placing information
26
*       do_big_sort             - Main sort routine for final data
27
*       sort                    - Sort in memory buffer
28
*       sort_comp               - qsort comparison function
29
*       load                    - load report data into memory
30
*       gen_stats               - Generate all the stats
31
*
32
* programmer: David Purdie
33
*
34
* revision  date        by      reason
35
*    e388   11-Oct-88           Option in the final printout to only
36
*                               produce the main result sheet. The leg
37
*                               placing printout is not produced.
38
*
39
*                               Changes to the display format of unknown
40
*                               times of disqualified teams. Only affects
41
*                               the leg-printouts.
42
*                               Disqualified teams show as -- -- --
43
*                               Otherwise they show as     ** ** **
44
*                               or a valid time.
45
*
46
*   e339    31-Oct-88   DDP     Added the "Interim Report" facility
47
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
48
*   00.1    06-sep-02   DDP     Added support for HTML report generation
49
*
50
**************************************************************************/
51
 
52
#include    <stdio.h>
53
#include    "consts.h"
54
#include    "structs.h"
55
#include    "proto.h"
170 - 56
#include    "mainwindow.h"
91 - 57
 
58
void pri_awards_html(void);
380 david 59
void pri_summary_html(void);
91 - 60
void pri_awards(void);
61
void pri_master_index(void);
62
char *placing ( int place );
63
void pri_name_index(void);
64
void pri_name_index_body( void );
65
void pri_all_reports (void );
66
void pri_leg_body(int leg);
67
void pri_eleg_body(int leg);
68
void pri_csv_data ( void );
69
 
70
 
71
#define MAX_PLACE 11
72
const char * place_text[] =
73
{
74
    "Zero'th",
75
    "First",
76
    "Second",
77
    "Third",
78
    "Fourth",
79
    "Fifth",
80
    "Sixth",
81
    "Seventh",
82
    "Eighth",
83
    "Ninth",
84
    "Tenth",
85
    "Eleventh"
86
};
87
 
88
int         sort_leg;
89
int         sort_mode;
380 david 90
bool        sort_withEquestrian;
91
bool        sort_afterEquestrianLeg;
91 - 92
report_type report_html = text;
93
bool        report_all = FALSE;
94
 
95
/* Parameters used by the sort routine to govern its actions */
96
 
97
#define S_L     1                                /* Elasped times */
98
#define S_LE    2                                /* Leg end time */
99
#define S_LC    3                                /* Elapsed times per class */
100
#define S_LEC   4                                /* Leg end time per class */
101
#define S_FIN   5                                /* Sort on finish time at given leg */
102
#define S_TEAM  6                                /* Sort on team order */
103
#define S_CLASS 7                                /* Sort on class/team order */
227 - 104
#define S_LC_NE 8                                /* Elapsed times per class, with NE sorted by real class */
380 david 105
#define S_IFIN  9                                /* Sort on elapsed time at given leg */
91 - 106
 
380 david 107
 
91 - 108
/*
109
**  Various checking modes
110
*/
111
#define C_ELAPSED   1                            /* Check elapsed times */
112
#define C_END       2                            /* Check end times */
113
#define C_DISQUAL   3                            /* Check disqualified teams */
114
 
115
/*
116
**  Data
117
*/
118
ty_s_data  *sort_data = 0;                       /* pointer to memory */
119
ty_s_aux   *sort_aux = 0;                        /* pointer to aux sort info */
195 david 120
ty_s_namedata *sort_name_data = 0;               /* pointer to name info */
121
unsigned    int sort_num;                        /* Number in the array */
122
unsigned    int sort_num_data;                   /* Number in the array */
91 - 123
ty_stats        stats;                           /* Holds statistics */
124
 
125
/*========================================================================
126
 *
127
 *  Print in team order
128
 *
129
 *  Purpose:
130
 *      This function is called to Print in team order
131
 *      This function may also be used to create an HTML suite of files
132
 *      within the result set
133
 *
134
 *  Parameters:
135
 *      None
136
 *
137
 *  Returns:
138
 *      Nothing
139
 *
140
 *========================================================================*/
141
 
142
void pri_team(void)
143
{
144
    int         i, k;
170 - 145
    team_type   team_buf;
91 - 146
 
147
    if( !open_printer( "", "name", 132, report_html, "Team Names" ) )
148
        return;
149
 
150
    /*
151
     * Print out the data 
152
     * Print out the header
153
     */
154
 
155
    print( "\n" );
156
 
157
    print_underline( TRUE );
158
    print( "%-*s %-*s %-*s", MAX_TM_NAME + 5, "Entry number and name",
159
               LEN_CLASS_NAME, "Category",
160
               config.num_countries == 0 ? 1 : LEN_CNTRY_NAME,
161
               config.num_countries == 0 ? "" : "Country" );
162
    for( k = 0; k < MAX_MEMB; k++ )
163
    {
164
        print( " %-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
165
    }
166
    print_underline( FALSE ) ;
167
    print( "\n" );
168
 
169
    for( i = config.min_team; i <= config.max_team; i++ )
170
    {
171
        if( valid_field( i ) && g_record( i, &team_buf ) )
172
        {
173
            /*
174
            **  If printing an HTML report then we need to mark
175
            **  the entry with a reference so that we can link to it
176
            */
177
            if ( report_html == html )
178
            {
179
                print( "<A NAME=\"Team_%04d\">",team_buf.numb );
180
                print( "</A>" );
181
            }
182
 
183
            /*
184
            **  Basic information
185
            **      - Team number - with Xref back to full result
186
            **      - Full team name
187
            **      - Full categoray name - with Xref to category results
188
            **      - Country name
189
            */
389 david 190
            if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "finish" ,"html")), team_buf.numb );
91 - 191
            print( "%4d",       team_buf.numb );
192
 
193
            print( " %-*s ",     MAX_TM_NAME, team_buf.name );
194
 
389 david 195
            if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">",url_encode(p_filename(FileBase, config.team_class[team_buf.teamclass - 1].abr ,"html")), team_buf.numb );
91 - 196
            print( "%-*s",     LEN_CLASS_NAME, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].full_name );
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 );
380 david 306
    print( "%-*s %*s %*s %-*s", MAX_TM_NAME + 5, "Competitor name",
91 - 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
 
389 david 320
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "finish" ,"html")), ptr->team );
380 david 321
        print( "%*d", 6, ptr->leg + 1 );
91 - 322
        print( " " );
323
 
389 david 324
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), ptr->team );
380 david 325
        print( "%*d", 5, ptr->team );
91 - 326
        print( " " );
327
 
389 david 328
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">",url_encode(p_filename(FileBase, config.team_class[ptr->teamclass - 1].abr ,"html")), ptr->team );
91 - 329
        print( "%-*s", LEN_CLASS_NAME, ptr->teamclass == 0 ? "" : config.team_class[ptr->teamclass - 1].abr );
330
        print( " " );
331
 
332
        if ( ptr->multi ) print( "* ");
333
        print( "\n" );
334
    }
335
 
336
    print_legend( -1, 0 );
337
    close_printer();
338
 
339
}
340
 
341
/*========================================================================
342
 *
343
 *  Print in given leg finishing time
344
 *
345
 *  Purpose:
346
 *      This function is called to Print in given leg finishing time
347
 *
348
 *  Parameters:
349
 *      None
350
 *
351
 *  Returns:
352
 *      Nothing
353
 *
354
 *========================================================================*/
355
 
356
void pri_leg_body(int leg)    
357
{
358
    ty_s_data  *ptr;
359
    int         i, k;
195 david 360
 
91 - 361
    /*
362
     * Sort the data in finishing order 
363
     */
364
 
380 david 365
    ck_data( leg, C_END );                      /* Check data for this leg */
366
    sort_team_data( leg, S_FIN, true );         /* Sort the data */
91 - 367
 
368
 
369
    /*
370
     * Now print the data on the printer 
371
     */
372
    if( !open_printer( "",
373
                        tprintf("lg%1.1d", leg ),
374
                        80,
375
                        report_html,
376
                        leg ? tprintf ("Finish order for Leg %d.", leg)
377
                            : "Final team finish order"
378
                     ) )
379
    {
380
        return;
381
    }
382
 
383
    /*
384
     * Print out the data 
385
     */
386
    print( "PRELIMINARY RESULTS ONLY\n\n" );
387
 
388
    print_underline( TRUE );
389
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
390
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
391
    print( "|" );
392
    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
393
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
394
    print_underline( FALSE );
395
 
396
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
397
    {
398
        p_place( ptr++, leg, k++ );
399
        p_place( ptr++, leg, k++ );
380 david 400
 
91 - 401
        print( "|" );
402
        while( i <= config.max_team && !valid_field( i ) )
403
            i++;
404
        p_team( i++, leg );
405
        while( i <= config.max_team && !valid_field( i ) )
406
            i++;
407
        p_team( i++, leg );
408
        print( "\n" );
409
    }
410
 
411
    print_underline( TRUE );
412
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
413
//    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
414
    print( "|" );
415
//    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
416
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
417
    print_underline( FALSE );
418
 
419
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
420
    {
421
        p_place( ptr++, leg, k++ );
422
//        p_place( ptr++, leg, k++ );
423
        print( "|" );
424
        while( i <= config.max_team && !valid_field( i ) )
425
            i++;
426
        p_team( i++, leg );
427
//        while( i <= config.max_team && !valid_field( i ) )
428
//            i++;
429
//        p_team( i++, leg );
430
        print( "\n" );
431
    }
432
 
433
 
434
    /*
435
     * Insert the leg statistics 
436
     */
437
 
438
    gen_stats();                               /* Generate all stats */
439
    print( "\nLeg statistics\n" );
440
    print( "Fastest team: %4d time : %s.",
441
           stats.fast.team[leg][0], time_a( stats.fast.time[leg][0] ) );
442
    print( " Average time: %s\n", time_a( stats.average[leg][0] ) );
443
 
444
    close_printer();
445
}
446
 
447
/*========================================================================
448
 *
449
 *  Print place information
450
 *
451
 *  Purpose:
452
 *      This helper function is called to Print place information
453
 *      in a 20-character field
454
 *
455
 *  Parameters:
456
 *      ptr         Address of the place data
457
 *      leg         Leg to print
458
 *      k           Current index into sorted array. Simply
459
 *                  used to determine if the entry is valid
460
 *                  or if it should be space-filled
461
 *
462
 *  Returns:
463
 *      Nothing
464
 *
465
 *========================================================================*/
466
 
467
void p_place( ty_s_data * ptr, int leg, unsigned k )
468
{
469
    if( k < sort_num )
470
    {
471
        print( "%4.4s %4d %8s  ",
380 david 472
               px_place(-1, ptr->place, false, false, ptr->flags ),
91 - 473
               ptr->team, time_fa( ptr->leg[leg], ptr->flags.disqualified ) );
474
    }
475
    else
476
    {
477
        print( "%20s", "" );
478
    }
479
}
480
 
481
/*========================================================================
482
 *
483
 *  Print team information
484
 *
485
 *  Purpose:
486
 *      This helper function is called to Print team information
487
 *      in a 20-character field
488
 *
489
 *  Parameters:
490
 *      i           team to print
491
 *      leg         Leg to print
492
 *
493
 *  Returns:
494
 *      Nothing
495
 *
496
 *========================================================================*/
497
 
498
void p_team( int i, int leg )
499
{
500
    ty_s_data  *ptra;                            /* Pointer to sort data */
501
    int         found = FALSE;
502
    unsigned    j;
503
 
504
    if( valid_field( i ) )
505
    {
506
        ptra = sort_data;
507
        for( j = 1; j <= sort_num; j++, ptra++ )
508
            if( i == ptra->team )
509
            {
510
                found = TRUE;
511
                break;
512
            }
513
    }
514
    if( found )
515
    {
516
        print( "%4d %4.4s %8s  ",
517
               ptra->team,
380 david 518
               px_place( -1, ptra->place, false, false, ptra->flags ),
91 - 519
               time_fa( ptra->leg[leg], ptra->flags.disqualified ) );
520
    }
521
    else
522
    {
523
        print( "%20s", "" );
524
    }
525
}
526
 
527
/*========================================================================
528
 *
529
 *  Print on elapsed times for given leg
530
 *
531
 *  Purpose:
532
 *      This function is called to Print on elapsed times for given leg
533
 *
534
 *  Parameters:
195 david 535
 *      leg             - Leg number to print
91 - 536
 *
537
 *  Returns:
538
 *      Nothing
539
 *
540
 *========================================================================*/
541
 
542
void pri_eleg_body( int leg)
543
{
544
    ty_s_data  *ptr;
545
    int         i, k;
546
 
547
    /*
158 david 548
     * Sort the data in finishing order
91 - 549
     */
550
 
380 david 551
    ck_data( leg, C_ELAPSED );                      /* Check data for this leg */
552
    sort_team_data( leg, S_IFIN, true );            /* Sort the data on elapsed time */
91 - 553
 
554
 
555
    /*
158 david 556
     * Now print the data on the printer
91 - 557
     */
558
    if( !open_printer( "", tprintf( "le%1.1d", leg ),
559
                       80,
560
                       report_html,
561
                       leg ? tprintf( "Elapsed time order for Leg %d.", leg )
562
                           : tprintf( "Final elapsed team finishing order." )
563
                     ) )
564
    {
565
        return;
566
    }
567
 
568
    /*
158 david 569
     * Print out the data
91 - 570
     */
571
    print( "PRELIMINARY RESULTS ONLY\n\n" );
572
 
573
    print_underline( TRUE );
574
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
575
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
576
    print( "|" );
577
    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
578
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
579
    print_underline( FALSE );
580
 
581
 
582
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
583
    {
584
        pe_place( ptr++, leg, k++ );
585
        pe_place( ptr++, leg, k++ );
586
        print( "|" );
587
        while( i <= config.max_team && !valid_field( i ) )
588
            i++;
589
        pe_team( i++, leg );
590
        while( i <= config.max_team && !valid_field( i ) )
591
            i++;
592
        pe_team( i++, leg );
593
        print( "\n" );
594
    }
595
 
596
    print( "\n\n" );
597
    print_underline( TRUE );
598
    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
599
//    print( "%4s %4s %-8s  ", "Plce", "Team", "Time" );
600
    print( "|" );
601
//    print( "%4s %4s %-8s  ", "Team", "Plce", "Time" );
602
    print( "%4s %4s %-8s\n", "Team", "Plce", "Time" );
603
    print_underline( FALSE );
604
 
605
 
606
    for( ptr = sort_data, k = 0, i = config.min_team; i <= config.max_team; )
607
    {
608
        pe_place( ptr++, leg, k++ );
609
//        pe_place( ptr++, leg, k++ );
610
        print( "|" );
611
        while( i <= config.max_team && !valid_field( i ) )
612
            i++;
613
        pe_team( i++, leg );
614
//        while( i <= config.max_team && !valid_field( i ) )
615
//            i++;
616
//        pe_team( i++, leg );
617
        print( "\n" );
618
    }
619
 
620
 
621
    /*
158 david 622
     * Insert the leg statistics
91 - 623
     */
624
 
625
    gen_stats();                               /* Generate all stats */
626
    print( "\nLeg statistics\n" );
627
    print( "Fastest team: %4d time : %s.",
628
           stats.fast.team[leg][0], time_a( stats.fast.time[leg][0] ) );
629
    print( " Average time: %s\n", time_a( stats.average[leg][0] ) );
630
 
631
    close_printer();
632
}
633
 
158 david 634
///*========================================================================
635
// *
636
// *  Print place information
637
// *
638
// *  Purpose:
639
// *      This helper function is called to Print place and elapsed information
380 david 640
// *      in a 20-character field: Place:Team:ElapsedTime
158 david 641
// *
642
// *  Parameters:
643
// *      ptr         Address of the place data
644
// *      leg         Leg to print
645
// *      k           Current index into sorted array. Simply
646
// *                  used to determine if the entry is valid
647
// *                  or if it should be space-filled
648
// *
649
// *  Returns:
650
// *      Nothing
651
// *
652
// *========================================================================*/
91 - 653
 
654
void pe_place( ty_s_data * ptr, int leg, unsigned k )
655
{
656
    if( k < sort_num )
657
    {
658
        print( "%4.4s %4d %8s  ",
380 david 659
               px_place( -1, ptr->place, false, false, ptr->flags ),
91 - 660
               ptr->team,
661
               time_fa( ptr->lege[leg], ptr->flags.disqualified ) );
662
    }
663
    else
664
    {
665
        print( "%20s", "" );
666
    }
667
}
668
 
669
/*========================================================================
670
 *
671
 *  Print team information
672
 *
673
 *  Purpose:
674
 *      This helper function is called to Print team and elapsed time
195 david 675
 *      information in a 20-character field
91 - 676
 *
677
 *  Parameters:
678
 *      i           Team to print
679
 *      leg         Leg to print
680
 *
681
 *  Returns:
682
 *      Nothing
683
 *
684
 *========================================================================*/
685
 
686
void pe_team( int i, int leg )
687
{
688
    ty_s_data  *ptra;                            /* Pointer to sort data */
689
    int         found = FALSE;
690
    unsigned    j;
691
 
692
    if( valid_field( i ) )
693
    {
694
        ptra = sort_data;
695
        for( j = 1; j <= sort_num; j++, ptra++ )
696
            if( i == ptra->team )
697
            {
698
                found = TRUE;
699
                break;
700
            }
701
    }
702
    if( found )
703
    {
704
        print( "%4d %4.4s %8s  ",
705
               ptra->team,
380 david 706
               px_place( -1, ptra->place, false, false, ptra->flags ),
91 - 707
               time_fa( ptra->lege[leg], ptra->flags.disqualified ) );
708
    }
709
    else
710
    {
711
        print( "%20s", "" );
712
    }
713
}
714
 
715
/*========================================================================
716
 *
717
 *  Print final results in HTML
718
 *
719
 *  Purpose:
720
 *      This function is called to Print final results with HTML formatting
721
 *      All result files are created
722
 *
723
 *  Parameters:
724
 *      None
725
 *
726
 *  Returns:
727
 *      Nothing
728
 *
729
 *========================================================================*/
730
 
731
void pri_final_html(void)
732
{
733
    /*
734
    **  Generate ALL results with HTML tags
735
    */
736
    report_html = html;
737
    pri_final();
380 david 738
    pri_final_teamOrder();
739
 
91 - 740
    report_html = printed;
741
    pri_final();
380 david 742
    pri_final_teamOrder();
91 - 743
    report_html = text;
744
}
745
 
746
 
747
/*========================================================================
748
 *
749
 *  Print final results
750
 *
751
 *  Purpose:
752
 *      This function is called to Print final results
753
 *
754
 *  Parameters:
755
 *      None
756
 *
757
 *  Returns:
758
 *      Nothing
759
 *
760
 *========================================================================*/
761
 
762
void pri_final(void)
763
{
764
    ty_s_data  *ptr;
170 - 765
    team_type   team_buf;
91 - 766
    unsigned    i;
767
    int         j, last_class;
768
    char        *report_title;
769
    bool        class_done[MAX_CLASS+1];
770
    int         lcount;
380 david 771
    bool        isNeClass = false;
91 - 772
 
380 david 773
    ck_data( -1, C_ELAPSED );
774
 
91 - 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
     */
380 david 791
    print_class_header( -1, TRUE );                     /* Print the header */
792
    sort_team_data( 0, S_L, true );                     /* Re-sort on elapsed time */
91 - 793
    lcount = 0;
794
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
795
    {
380 david 796
        if ( ptr->isNeData )
91 - 797
            continue;
798
 
799
        g_record( ptr->team, &team_buf );
800
 
801
        /*
380 david 802
        ** If this is a NE team then dummy up some of the data that hasn't been stored in team_buf
803
        */
804
        if (ptr->flags.non_equestrian)
805
        {
806
            team_buf.leg[0].l_place = sort_aux[ptr->team].leq_place[0];
807
            team_buf.leg[0].lc_place = sort_aux[ptr->team].lq_place[0];
808
        }
809
 
810
        /*
91 - 811
        **  If printing an HTML report then we need to mark
812
        **  the entry with a reference so that we can link to it
813
        */
814
        if ( report_html == html )
815
        {
816
            print( "<A NAME=\"Team_%04d\"></A>",team_buf.numb );
817
        }
818
 
819
        /*
820
        **  Print the basics (Finishing order)
821
        **      - Place within complete field
822
        **      - Team number - with HTML reference to team file
823
        **      - Class
824
        */
380 david 825
        if ( report_html == printed && lcount % 5 == 4 ) print_underline( TRUE );
826
        print( "%4.4s ", px_place( 0, team_buf.leg[0].l_place, false, true, ptr->flags ) );
389 david 827
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), team_buf.numb );
91 - 828
        print( "%4d",  team_buf.numb );
829
 
830
        print( " %-*s", 3, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
831
 
832
        /*
833
        **  Print the per-leg data
834
        **      - Time
835
        **      - Leg place
836
        **      - End place
837
        */
838
        for( j = 1; j <= config.num_legs; j++ )
839
        {
380 david 840
            bool isEquestrianLeg = (j == config.equestrian_leg && team_buf.flags.non_equestrian);
841
 
91 - 842
            /*
843
            **  Ensure that non-equestrian leg data is not displayed
844
            */
380 david 845
            if ( isEquestrianLeg )
91 - 846
            {
847
                print( "  %-8s %4.4s %4.4s", "-- NE --", "NE","NE");
848
            }
849
            else
850
            {
380 david 851
                print( "  %-8s", time_a( team_buf.leg[j].elapsed ));
852
                if ( config.num_legs != 1 ) print( " %4.4s",      px_place( j, team_buf.leg[j].l_place, false, false, ptr->flags ));
853
                if ( config.num_legs != 1 ) print( " %4.4s",   px_place( j, team_buf.leg[j].le_place,true , true, ptr->flags ));
854
                if ( config.num_legs == 1 ) print( " %-*s ",  MAX_TM_NAME, team_buf.name );
91 - 855
            }
856
        }
857
 
858
       /*
859
        **  Print the trailer (Finishing order)
860
        **      - Total time
861
        **      - Category place - with reference to category file
862
        */
380 david 863
        if ( config.num_legs != 1 ) print( "  %-8s ", time_a( ptr->lege[0] ) );
91 - 864
 
389 david 865
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">",url_encode(p_filename(FileBase, config.team_class[team_buf.teamclass - 1].abr ,"html")), team_buf.numb );
380 david 866
        print( "%4.4s", px_place( 0, team_buf.leg[0].lc_place, false, false, ptr->flags) );
91 - 867
        if ( report_html == printed && lcount %5 == 4 ) print_underline( FALSE );
868
        lcount++;
869
        print( "\n" );
870
    }
871
 
872
    print_class_stats( -1, TRUE );              /* Print statistics */
873
    print_legend(-1, 1 );                       /* Print the legend */
874
    close_printer();                            /* Close the printer */
875
 
380 david 876
    /* ------------------ Class Reports
91 - 877
     * Now produce a breakdown on a class by class basis 
878
     * Now print out the class placement information
879
     */
880
 
380 david 881
    sort_team_data( 0, S_LC, true );           /* Generate class placement data */
91 - 882
    last_class = -1;                           /* Invalid class to start with */
883
    memset ( class_done, 0, sizeof(class_done));
884
 
885
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
886
    {
380 david 887
        short teamClass = ptr->teamclass;
888
        isNeClass = false;
889
 
91 - 890
        /*
380 david 891
        **  Dummy up the psuedo NE data a little bit
892
        */
893
        if (ptr->isNeData) {
894
            isNeClass = true;
895
            teamClass = config.nonequestrian_class;
896
        }
897
 
898
//qDebug() << "NEtm:" << ptr->team << "," << isNeClass << "," << teamClass;
899
 
900
        /*
91 - 901
        **  Detect a change in the "class"
902
        **  All data is within the one array of data
903
        **  Use the in-memory class as this MAY differ from that stored
904
        **  The non-equestrian class does this.
905
        */
380 david 906
        if( last_class != teamClass )
91 - 907
        {
908
            if( last_class >= 0 )
909
            {
910
                print_class_stats( last_class, TRUE );
911
                print_legend( last_class, 1 );
912
                close_printer();
380 david 913
if (class_done[teamClass]) {
914
    qDebug() << "ERROR class already processed - order error";
915
}
916
                last_class = 0;
91 - 917
            }
918
 
380 david 919
            report_title = tprintf( "Category results for : %-*s", LEN_CLASS_NAME, teamClass == 0 ? "" : config.team_class[teamClass - 1].full_name );
91 - 920
 
380 david 921
            if( !open_printer( "", config.team_class[teamClass - 1].abr, 132, report_html, report_title ) )
91 - 922
                continue;
380 david 923
            print_class_header( last_class = teamClass, TRUE );
91 - 924
 
925
            /*
926
            **  Mark the class as done
927
            */
380 david 928
            class_done[teamClass] = TRUE;
91 - 929
            lcount = 0;
380 david 930
            last_class = teamClass;
91 - 931
        }
932
 
933
        /*
934
        **  Now read in the team record
935
        */
936
        g_record( ptr->team, &team_buf );
937
 
938
        /*
380 david 939
        ** Dummy up the data for the dummy nonEquestrian Class
940
        ** Its not stored in the team_buf for backwards compatability
941
        */
942
        if( isNeClass)
943
        {
944
            for( j = 0; j <= config.num_legs; j++ )
945
            {
946
                team_buf.leg[j].lc_place = sort_aux[ptr->team].lq_place[j];
947
                team_buf.leg[j].lec_place = sort_aux[ptr->team].leq_place[j];
948
            }
949
        }
950
        else
951
        {
952
            if (ptr->flags.non_equestrian)
953
            {
954
                //team_buf.leg[0].lc_place = sort_aux[ptr->team].lcq_place[0];
955
                //team_buf.leg[0].lc_place = sort_aux[ptr->team].lcq_place[0];
956
                team_buf.leg[0].lec_place = sort_aux[ptr->team].leq_place[0];
957
            }
958
        }
959
 
960
 
961
        /*
91 - 962
        **  If printing an HTML report then we need to mark
963
        **  the entry with a reference so that we can link to it
964
        */
965
        if ( report_html == html )
966
            print( "<A NAME=\"Team_%04d\"></A>",team_buf.numb );
967
 
968
        /*
969
        **  Print the basics
970
        **      - Place within the class
971
        **      - Team number - with HTML reference to team file
972
        **      - Class
973
        */
974
 
975
        if ( report_html == printed && lcount %5 == 4 ) print_underline( TRUE );
380 david 976
        print( "%4.4s ", px_place( 0, team_buf.leg[0].lc_place, false, false, ptr->flags) );
389 david 977
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), team_buf.numb );
91 - 978
        print( "%4d",  team_buf.numb );
979
        print( " %-*s", 3, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
980
 
981
        for( j = 1; j <= config.num_legs; j++ )
982
        {
380 david 983
            bool isEquestrianLeg = (j == config.equestrian_leg && (ptr->isNeData || ptr->flags.non_equestrian));
984
 
91 - 985
            /*
986
            **  Ensure that non-equestrian leg data is not displayed
987
            */
380 david 988
            if ( isEquestrianLeg )
91 - 989
            {
990
                print( "  %-8s %4.4s %4.4s", "-- NE --", "NE","NE");
991
            }
992
            else
993
            {
380 david 994
                print( "  %-8s", time_a( team_buf.leg[j].elapsed ));
995
                if ( config.num_legs != 1 ) print( " %4.4s", px_place( j, team_buf.leg[j].lc_place,  false, false, ptr->flags ));
996
                if ( config.num_legs != 1 ) print( " %4.4s", px_place( j, team_buf.leg[j].lec_place, true , true, ptr->flags ) );
997
                if ( config.num_legs == 1 ) print( " %-*s ", MAX_TM_NAME, team_buf.name );
91 - 998
            }
999
        }
380 david 1000
#if 0
1001
        // zzz
1002
        print (" --");
91 - 1003
 
380 david 1004
        print (",%4.4d", sort_aux[ptr->team].l_place[0]);
1005
        print (" %4.4d", sort_aux[ptr->team].l_place[1]);
1006
        print (" %4.4d", sort_aux[ptr->team].l_place[2]);
1007
        print (" %4.4d", sort_aux[ptr->team].l_place[3]);
1008
        print (" %4.4d", sort_aux[ptr->team].l_place[4]);
1009
        print (" %4.4d", sort_aux[ptr->team].l_place[5]);
1010
 
1011
        print (",%4.4d", sort_aux[ptr->team].le_place[0]);
1012
        print (" %4.4d", sort_aux[ptr->team].le_place[1]);
1013
        print (" %4.4d", sort_aux[ptr->team].le_place[2]);
1014
        print (" %4.4d", sort_aux[ptr->team].le_place[3]);
1015
        print (" %4.4d", sort_aux[ptr->team].le_place[4]);
1016
        print (" %4.4d", sort_aux[ptr->team].le_place[5]);
1017
 
1018
        print (",%4.4d", sort_aux[ptr->team].lc_place[0]);
1019
        print (" %4.4d", sort_aux[ptr->team].lc_place[1]);
1020
        print (" %4.4d", sort_aux[ptr->team].lc_place[2]);
1021
        print (" %4.4d", sort_aux[ptr->team].lc_place[3]);
1022
        print (" %4.4d", sort_aux[ptr->team].lc_place[4]);
1023
        print (" %4.4d", sort_aux[ptr->team].lc_place[5]);
1024
 
1025
        print (",%4.4d", sort_aux[ptr->team].lec_place[0]);
1026
        print (" %4.4d", sort_aux[ptr->team].lec_place[1]);
1027
        print (" %4.4d", sort_aux[ptr->team].lec_place[2]);
1028
        print (" %4.4d", sort_aux[ptr->team].lec_place[3]);
1029
        print (" %4.4d", sort_aux[ptr->team].lec_place[4]);
1030
        print (" %4.4d", sort_aux[ptr->team].lec_place[5]);
1031
 
1032
        print (",%4.4d", sort_aux[ptr->team].lq_place[0]);
1033
        print (" %4.4d", sort_aux[ptr->team].lq_place[1]);
1034
        print (" %4.4d", sort_aux[ptr->team].lq_place[2]);
1035
        print (" %4.4d", sort_aux[ptr->team].lq_place[3]);
1036
        print (" %4.4d", sort_aux[ptr->team].lq_place[4]);
1037
        print (" %4.4d", sort_aux[ptr->team].lq_place[5]);
1038
 
1039
        print (",%4.4d", sort_aux[ptr->team].leq_place[0]);
1040
        print (" %4.4d", sort_aux[ptr->team].leq_place[1]);
1041
        print (" %4.4d", sort_aux[ptr->team].leq_place[2]);
1042
        print (" %4.4d", sort_aux[ptr->team].leq_place[3]);
1043
        print (" %4.4d", sort_aux[ptr->team].leq_place[4]);
1044
        print (" %4.4d", sort_aux[ptr->team].leq_place[5]);
1045
 
1046
        print (",[%4.4d", sort_aux[ptr->team].lcq_place[0]);
1047
        print (" %4.4d", sort_aux[ptr->team].lcq_place[1]);
1048
        print (" %4.4d", sort_aux[ptr->team].lcq_place[2]);
1049
        print (" %4.4d", sort_aux[ptr->team].lcq_place[3]);
1050
        print (" %4.4d", sort_aux[ptr->team].lcq_place[4]);
1051
        print (" %4.4d]", sort_aux[ptr->team].lcq_place[5]);
1052
 
1053
        print (",%4.4d", sort_aux[ptr->team].lecq_place[0]);
1054
        print (" %4.4d", sort_aux[ptr->team].lecq_place[1]);
1055
        print (" %4.4d", sort_aux[ptr->team].lecq_place[2]);
1056
        print (" %4.4d", sort_aux[ptr->team].lecq_place[3]);
1057
        print (" %4.4d", sort_aux[ptr->team].lecq_place[4]);
1058
        print (" %4.4d", sort_aux[ptr->team].lecq_place[5]);
1059
#endif
1060
 
91 - 1061
        /*
1062
        **  Print the trailer
1063
        **      - Total time
1064
        **      - Overall place - with reference to overall place file
1065
        */
380 david 1066
        if ( config.num_legs != 1 ) print( "  %-8s ", time_a( ptr->lege[0] ) );
91 - 1067
 
389 david 1068
        if ( report_html == html )  setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "finish" ,"html")), team_buf.numb );
380 david 1069
        print( "%4.4s", px_place( 0, team_buf.leg[0].l_place, false, true, ptr->flags));
91 - 1070
 
1071
        if ( report_html == printed && lcount %5 == 4 ) print_underline( FALSE );
1072
        lcount++;
1073
        print( "\n" );
1074
    }
1075
 
1076
    print_class_stats( last_class, TRUE );
1077
    print_legend(last_class,1);
1078
    close_printer();
1079
 
1080
    /*
1081
    **  Pickup missed classes and create a report
1082
    */
1083
    for( j = 1; j <= config.num_class; j++ )
1084
    {
380 david 1085
        if ( class_done[j] || !config.team_class[j-1].abr[0] )
91 - 1086
        {
1087
            continue;
1088
        }
1089
        report_title = tprintf( "Category results for : %-*s", LEN_CLASS_NAME, config.team_class[j - 1].full_name );
1090
 
1091
        if( !open_printer( "", config.team_class[j - 1].abr, 132, report_html, report_title ) )
1092
            continue;
1093
        print_class_header( j-1, TRUE );
1094
        print( "\nThere were no competitors in this class\n" );
1095
        print_legend(j,1);
1096
        close_printer();
1097
    }
1098
 
1099
 
1100
    /*
1101
    **  If we are generating an HTML report then we need to create the file
1102
    **  that contains all the team names - the assumption is that this data
1103
    **  is available
1104
    */
380 david 1105
    if ( report_html == html || report_html == printed)
91 - 1106
    {
1107
        pri_team();
1108
    }
1109
 
1110
    /*
1111
    **  Generate the awards report.
1112
    **  This is only available as an HTML report
1113
    */
380 david 1114
    if ( report_html == html || report_html == printed)
91 - 1115
    {
1116
        pri_awards_html();
1117
    }
1118
 
1119
    /*
1120
    **  Generate the master index page
1121
    */
380 david 1122
    if ( report_html == html )
91 - 1123
    {
1124
        pri_master_index();
1125
    }
1126
 
1127
    pri_name_index_body();
1128
}
1129
 
1130
/*========================================================================
1131
 *
380 david 1132
 *  Print final results in Team Order
1133
 *
1134
 *  Purpose:
1135
 *      This function is called to Print final results in Team Order
1136
 *
1137
 *  Parameters:
1138
 *      None
1139
 *
1140
 *  Returns:
1141
 *      Nothing
1142
 *
1143
 *========================================================================*/
1144
 
1145
void pri_final_teamOrder(void)
1146
{
1147
    ty_s_data  *ptr;
1148
    team_type   team_buf;
1149
    unsigned    i;
1150
    int         j;
1151
    int         lcount;
1152
 
1153
    ck_data( -1, C_ELAPSED );
1154
 
1155
    /*
1156
    **  Sort on every thing
1157
    **  Then generate all the stats too
1158
    */
1159
    do_big_sort();
1160
    gen_stats();
1161
 
1162
    /*
1163
     * Now print the data on the printer 
1164
     */
1165
    if( !open_printer( "", "team_order", 132, report_html, "Team Order" ) )
1166
        return;
1167
 
1168
    /*
1169
     * Print out the data 
1170
     */
1171
    print_class_header( -1, TRUE );                      /* Print the header */
1172
 
1173
    ptr = sort_data;
1174
    sort_team_data( 0, S_TEAM, true );                   /* Re-sort on team number */
1175
    lcount = 0;
1176
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1177
    {
1178
        if ( ptr->isNeData )
1179
            continue;
1180
 
1181
        g_record( ptr->team, &team_buf );
1182
 
1183
        /*
1184
        ** If this is a NE team then dummy up some of the data that hasn't been stored in team_buf
1185
        */
1186
        if (ptr->flags.non_equestrian)
1187
        {
1188
            team_buf.leg[0].l_place = sort_aux[ptr->team].leq_place[0];
1189
            //team_buf.leg[0].lc_place = sort_aux[ptr->team].lcq_place[0];
1190
        }
1191
 
1192
        /*
1193
        **  If printing an HTML report then we need to mark
1194
        **  the entry with a reference so that we can link to it
1195
        */
1196
        if ( report_html == html )
1197
        {
1198
            print( "<A NAME=\"Team_%04d\"></A>",team_buf.numb );
1199
        }
1200
 
1201
        /*
1202
        **  Print the basics (Finishing order)
1203
        **      - Place within complete field
1204
        **      - Team number - with HTML reference to team file
1205
        **      - Class
1206
        */
1207
        if ( report_html == printed && lcount %5 == 4 ) print_underline( TRUE );
1208
        print( "%4.4s ", px_place( 0, team_buf.leg[0].l_place, false, true, ptr->flags ) );
389 david 1209
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), team_buf.numb );
380 david 1210
        print( "%4d",  team_buf.numb );
1211
 
1212
        print( " %-*s", 3, team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
1213
 
1214
        /*
1215
        **  Print the per-leg data
1216
        **      - Time
1217
        **      - Leg place
1218
        **      - End place
1219
        */
1220
        for( j = 1; j <= config.num_legs; j++ )
1221
        {
1222
            bool isEquestrianLeg = (j == config.equestrian_leg && team_buf.flags.non_equestrian);
1223
            /*
1224
            **  Ensure that non-equestrian leg data is not displayed
1225
            */
1226
            if ( isEquestrianLeg )
1227
            {
1228
                print( "  %-8s %4.4s %4.4s", "-- NE --", "NE","NE");
1229
            }
1230
            else
1231
            {
1232
                print( "  %-8s",                     time_a( team_buf.leg[j].elapsed ));
1233
                if ( config.num_legs != 1 ) print( " %4.4s", px_place(j, team_buf.leg[j].l_place, false, false, ptr->flags ));
1234
                if ( config.num_legs != 1 ) print( " %4.4s", px_place(j, team_buf.leg[j].le_place,true,  true, ptr->flags ) );
1235
                if ( config.num_legs == 1 ) print( " %-*s ", MAX_TM_NAME, team_buf.name );
1236
            }
1237
        }
1238
 
1239
 
1240
       /*
1241
        **  Print the trailer (Finishing order)
1242
        **      - Total time
1243
        **      - Category place - with reference to category file
1244
        */
1245
       if ( config.num_legs != 1 ) print( "  %-8s ", time_a( ptr->lege[0] ) );
1246
 
389 david 1247
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">",url_encode(p_filename(FileBase, config.team_class[team_buf.teamclass - 1].abr ,"html")), team_buf.numb );
380 david 1248
        print( "%4.4s", px_place( 0, team_buf.leg[0].lc_place, false, false, ptr->flags ) );
1249
 
1250
//print (" --");
1251
//print (" %4.4d", sort_aux[ptr->team].lq_place[0]);
1252
//print (" %4.4d", sort_aux[ptr->team].leq_place[0]);
1253
//print (" %4.4d", sort_aux[ptr->team].lcq_place[0]);
1254
//print (" ,%4.4d", sort_aux[ptr->team].lec_place[0]);
1255
//print (" %4.4d", sort_aux[ptr->team].lec_place[1]);
1256
//print (" %4.4d", sort_aux[ptr->team].lec_place[2]);
1257
//print (" %4.4d", sort_aux[ptr->team].lec_place[3]);
1258
//print (" %4.4d", sort_aux[ptr->team].lec_place[4]);
1259
//print (" %4.4d", sort_aux[ptr->team].lec_place[5]);
1260
//print (" ,%4.4d", sort_aux[ptr->team].lecq_place[0]);
1261
//print (" %4.4d", sort_aux[ptr->team].lecq_place[1]);
1262
//print (" %4.4d", sort_aux[ptr->team].lecq_place[2]);
1263
//print (" %4.4d", sort_aux[ptr->team].lecq_place[3]);
1264
//print (" %4.4d", sort_aux[ptr->team].lecq_place[4]);
1265
//print (" %4.4d", sort_aux[ptr->team].lecq_place[5]);
1266
 
1267
 
1268
        if ( report_html == printed && lcount %5 == 4 ) print_underline( FALSE );
1269
        lcount++;
1270
        print( "\n" );
1271
    }
1272
 
1273
    print_class_stats( -1, TRUE );              /* Print statistics */
1274
    print_legend(-1, 1 );                       /* Print the legend */
1275
    close_printer();                            /* Close the printer */
1276
 
1277
}
1278
 
1279
/*========================================================================
1280
 *
91 - 1281
 *  Place to text
1282
 *
1283
 *  Purpose:
1284
 *      This function is called to convert a place to text
1285
 *
1286
 *  Parameters:
1287
 *      place
1288
 *
1289
 *  Returns:
1290
 *      text
1291
 *
1292
 *========================================================================*/
1293
 
1294
char *placing ( int place )
1295
{
1296
    if ( place > MAX_PLACE )
1297
    {
1298
        return tprintf( "Place: %d", place);
1299
    }
1300
    return tprintf ("%s Place", place_text[place]);
1301
}
1302
 
1303
/*========================================================================
1304
 *
1305
 *  Print award results
1306
 *
1307
 *  Purpose:
1308
 *      This function is called to Print award results
1309
 *      Keep the page to 80 cols, so that it can be pronted on A4
1310
 *
1311
 *  Parameters:
1312
 *      None
1313
 *
1314
 *  Returns:
1315
 *      Nothing
1316
 *
1317
 *========================================================================*/
1318
void pri_awards_html(void)
1319
{
1320
    report_type saved = report_html;
1321
    /*
1322
    **  Generate ALL results with HTML tags
1323
    */
1324
    report_html = html;
1325
    pri_awards();
380 david 1326
    report_html = printed;
1327
    pri_awards();
91 - 1328
    report_html = saved;
1329
}
1330
 
1331
/*========================================================================
1332
 *
1333
 *  Print award results
1334
 *
1335
 *  Purpose:
1336
 *      This function is called to Print award results
1337
 *      Keep the page to 80 cols, so that it can be pronted on A4
1338
 *
1339
 *  Parameters:
1340
 *      None
1341
 *
1342
 *  Returns:
1343
 *      Nothing
1344
 *
1345
 *========================================================================*/
1346
void pri_awards(void)
1347
{
1348
    int j;
1349
    int i;
1350
    int k;
1351
    int windex;
1352
    int winmax;
1353
    ty_s_data  *ptr;
170 - 1354
    team_type   team_buf;
91 - 1355
    char    solid_line[100];
380 david 1356
    bool header_done = false;
1357
    bool entryFound;
91 - 1358
 
380 david 1359
 
91 - 1360
    /*
1361
    **  Calculate Summary information
1362
    **  Should cache the data
1363
    */
1364
    t_class_summary sdata;
1365
    calc_class_summary( & sdata );
1366
 
1367
 
1368
    if( !open_printer( "", "awards", 80, report_html, "Prizes and Awards" ) )
1369
        return;
1370
 
1371
    memset ( solid_line, 0, sizeof( solid_line ));
1372
    memset ( solid_line, '-', 80 );
1373
 
1374
    /*
1375
    **  Generate an index for this page
1376
    */
1377
    print( "\n");
1378
    if ( report_html == html )
1379
    {
1380
        print( "<hr>" );
1381
        print( "<A NAME=\"%s\"></A>",url_encode("INDEX"));
1382
    }
380 david 1383
    print( "Award Categories - Full Event");
91 - 1384
 
1385
    for( j = 1; j <= config.num_class; j++ )
1386
    {
1387
        /*
1388
        **  Header for the class
1389
        */
380 david 1390
        if (!config.team_class[j - 1].abr[0])
91 - 1391
            continue;
380 david 1392
        if (config.class_winners[j - 1] <= 0) 
1393
            continue;
91 - 1394
 
1395
        winmax = config.class_winners[j-1];
1396
        {
380 david 1397
            int valid =   sdata.teamclass[j].valid_ev;
91 - 1398
            if ( valid < winmax )
1399
                winmax = valid;
1400
        }
1401
 
1402
        print( "\n");
1403
        print( "    ");
380 david 1404
        if ( report_html == html ) setHref( "<A HREF=\"#%s\">",url_encode(config.team_class[j-1].full_name));
91 - 1405
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[j-1].full_name ));
1406
        print( "  %3d Awards", winmax );
1407
        if ( config.class_winners[j-1] != winmax )
1408
            print( " from a maximum of %3d", config.class_winners[j-1] );
380 david 1409
    }
91 - 1410
 
380 david 1411
    /*
1412
    **  NE Award Categories
1413
    */
1414
    if ( config.class_ne_winners_by_class )
1415
    {
1416
        print( "\n");
1417
        print( "Award Categories - Non Equestrian");
1418
 
1419
        for( j = 1; j <= config.num_class; j++ )
1420
        {
1421
            /*
1422
            **  Header for the class
1423
            */
1424
            if (!config.team_class[j - 1].abr[0])
1425
                continue;
1426
            if ( config.class_ne_winners[j-1] <= 0 )
1427
                continue;
1428
 
1429
            winmax = config.class_ne_winners[j-1];
1430
            {
1431
                int valid =   sdata.teamclass[j].valid_ne;
1432
                if ( valid < winmax )
1433
                    winmax = valid;
1434
            }
1435
 
1436
            print( "\n");
1437
            print( "    ");
1438
            if ( report_html == html ) setHref( "<A HREF=\"#%s_NE\">",url_encode(config.team_class[j-1].full_name));
1439
            print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[j-1].full_name ));
1440
            print( "  %3d Awards", winmax );
1441
            if ( config.class_ne_winners[j-1] != winmax )
1442
                print( " from a maximum of %3d", config.class_ne_winners[j-1] );
1443
        }
91 - 1444
    }
1445
 
1446
    /*
1447
    **  Manual entries
1448
    */
380 david 1449
    print( "\n");
1450
    print( "Miscellaneous");
1451
 
1452
        print( "\n");
1453
        print( "    ");
1454
        if ( report_html == html ) setHref( "<A HREF=\"#%s_by_cat\">",url_encode("Full Event"));
1455
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,"Full Event"));
1456
        print (" by Category");
1457
 
227 - 1458
    if ( config.class_ne_winners_by_class )
1459
    {
1460
        print( "\n");
1461
        print( "    ");
380 david 1462
        if ( report_html == html ) setHref( "<A HREF=\"#%s_by_cat\">",url_encode(config.team_class[config.nonequestrian_class-1].full_name));
227 - 1463
        print( "%s",  tprintf( "%-*s", LEN_CLASS_NAME ,config.team_class[config.nonequestrian_class-1].full_name ));
1464
        print (" by Category");
1465
    }
1466
 
1467
 
91 - 1468
    print( "\n");
1469
    print( "    ");
380 david 1470
    if ( report_html == html ) setHref( "<A HREF=\"#%s\">",url_encode("Hall Of Fame"));
91 - 1471
    print( "%s",  "Hall Of Fame" );
1472
 
380 david 1473
    if (config.awardsfilename[0])
1474
    {
1475
        print( "\n");
1476
        print( "    ");
1477
        if ( report_html == html ) setHref( "<A HREF=\"#%s\">",url_encode("Additional Awards"));
1478
        print( "%s",  "Additional Awards" );
1479
    }
1480
 
91 - 1481
    print( "\n");
1482
    print( "    ");
380 david 1483
    if ( report_html == html ) setHref( "<A HREF=\"#%s\">",url_encode("FASTEST"));
91 - 1484
    print( "%s",  "FASTEST" );
1485
 
380 david 1486
    /*  ---------------------------- Awards for Teams ----
91 - 1487
    **  Sort the data by class
1488
    */
380 david 1489
    sort_team_data( 0, S_LC, true );     /* Generate class placement data */
91 - 1490
 
1491
    /*
1492
    **  Process each category
1493
    */
1494
    print( "\n");
380 david 1495
    entryFound = false;
91 - 1496
    for( j = 1; ; j++ )
1497
    {
380 david 1498
        if (!config.team_class[j - 1].abr[0])
1499
            continue;
1500
 
91 - 1501
        /*
1502
        **  Tail for previous entry
1503
        */
227 - 1504
        //if ( config.class_ne_winners_by_class && j == config.nonequestrian_class )
1505
        //    continue;
1506
 
380 david 1507
        if ( j != 1 ) {
1508
            if (!entryFound) {
1509
                print( "\n");
1510
                print( "No winners awarded. Non eligible." );
1511
                if ( report_html == html ) print("<br>");
1512
            }
91 - 1513
            if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
380 david 1514
        }
91 - 1515
 
1516
        if ( j > config.num_class  )
1517
            break;
1518
 
1519
        /*
1520
        **  Header for the class
1521
        */
1522
        print( "\n");
1523
        if ( report_html == html )
1524
        {
1525
            print( "<hr>" );
380 david 1526
 
1527
            if ( !header_done )
1528
            {
1529
                header_done = true;
1530
                if ( report_html == html )
1531
                {
1532
                    print( "<A name=\"%s_by_cat\"></A>",url_encode("Full Event"));
1533
                }
1534
            }
1535
 
91 - 1536
            print( "<A name=\"%s\"></A>",url_encode(config.team_class[j-1].full_name));
1537
        }
1538
        else
1539
        {
1540
            print( "%s\n", solid_line);
1541
        }
1542
        print( "Category: ");
389 david 1543
        if ( report_html == html ) setHref( "<A HREF=\"%s\">",url_encode(p_filename(FileBase, config.team_class[j - 1].abr ,"html")));
91 - 1544
        print( "%s",  config.team_class[j-1].full_name );
1545
 
1546
        if ( config.class_winners[j-1] <= 0 )
1547
        {
1548
            print( "\n");
1549
            print( "No winners awarded" );
380 david 1550
            if ( report_html == html ) print("<br>");
91 - 1551
            continue;
1552
        }
1553
 
1554
        /*
1555
        **  Enties for 'n' the best teams as configured
1556
        */
1557
        windex = 0;                     /* Winners done */
380 david 1558
        entryFound = false;
91 - 1559
        unsigned int i;
1560
        for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1561
        {
1562
            if ( ptr->teamclass != j )
1563
            {
1564
                continue;
1565
            }
1566
 
1567
            /*
1568
            **  Now read in the team record
1569
            */
1570
            if( valid_field( ptr->team ) && g_record( ptr->team, &team_buf ) )
1571
            {
1572
                /*
1573
                **  Ensure we have a valid team
1574
                **  Can't award disqualified teams
1575
                **  Can't award NE teams unless its a NE award
1576
                */
380 david 1577
                if ( ptr->flags.bad_times || ptr->flags.disqualified || ptr->flags.vet_check || !ptr->flags.valid )
91 - 1578
                    break;
1579
 
380 david 1580
                if ( !ptr->isNeData && ptr->flags.non_equestrian )
91 - 1581
                    break;
1582
 
1583
                /*
1584
                **  Count the entry
1585
                */
1586
                windex++;
380 david 1587
                entryFound = true;
91 - 1588
 
1589
                /*
1590
                **  If printing an HTML report then we need to mark
1591
                **  the entry with a reference so that we can link to it
1592
                */
1593
                print( "\n");
1594
                if ( report_html == html )
1595
                {
1596
                    print( "<A NAME=\"Team_%04d\">",team_buf.numb );
1597
                    print( "</A>" );
1598
                }
1599
 
1600
                /*
1601
                **  Basic information
1602
                **      - Team number - with Xref back to full result
1603
                **      - Full team name
1604
                **      - Full categoray name
1605
                */
1606
                print( "%s", placing(windex) );
1607
 
1608
                print( "  Team Name: ");
389 david 1609
                if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), team_buf.numb );
91 - 1610
                print( "%-*s ",     MAX_TM_NAME, team_buf.name );
1611
 
1612
                print( "  Number: ");
389 david 1613
                if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "finish" ,"html")), team_buf.numb );
91 - 1614
                print( "%4d",       team_buf.numb );
1615
 
1616
 
1617
                for( k = 0; k < MAX_MEMB; k++ )
1618
                {
1619
 
1620
                    /*
1621
                    **  Skip equestrian leg in the non-equestion display
1622
                    */
380 david 1623
                    if ( k + 1 == config.equestrian_leg && ptr->isNeData)
91 - 1624
                        continue;
1625
 
1626
                    print( "\n");
1627
                    print( "    ");
1628
                    print( "%-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
1629
                    print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
1630
 
1631
                    print( "  %-8s", time_a( team_buf.leg[k+1].elapsed ) );
1632
                }
1633
 
1634
                print( "\n");
1635
                print( "    ");
1636
                print_bold( TRUE );
1637
                print( "%-*s %-*s  %-8s", MAX_PERSON_NAME , "Total" ,MAX_PERSON_NAME, "",time_a( team_buf.leg[0].elapsed ) );
1638
                print_bold( FALSE );
1639
                print( "\n" );
1640
            }
1641
 
1642
 
1643
            /*
1644
            **  More to do
1645
            */
1646
            if ( windex >= config.class_winners[j-1] )
1647
            {
1648
                break;
1649
            }
1650
        }
1651
    }
1652
 
1653
    /*
227 - 1654
    ** Non Equestrian winners by category
1655
    */
1656
    if ( config.class_ne_winners_by_class)
1657
    {
380 david 1658
        header_done = false;
227 - 1659
 
1660
        /*
1661
        **  Sort the data by class with NE data sorted by real class
1662
        */
380 david 1663
        sort_team_data( 0, S_LC_NE, true );     /* Generate class placement data */
227 - 1664
 
1665
        /*
1666
        **  Only process the Non Equestrian teams in this pass
1667
        */
1668
        print( "\n");
380 david 1669
        entryFound = false;
227 - 1670
        for( j = 1; ; j++ )
1671
        {
380 david 1672
            if (!config.team_class[j - 1].abr[0])
1673
                continue;
1674
 
227 - 1675
            /*
1676
            **  Tail for previous entry
1677
            */
1678
            if ( j == config.nonequestrian_class)
1679
                continue;
1680
 
380 david 1681
            if ( j != 1 ) {
1682
                if (!entryFound) {
1683
                    print( "\n");
1684
                    print( "No winners awarded. Non eligible." );
1685
                    if ( report_html == html ) print("<br>");
1686
                }
227 - 1687
                if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
380 david 1688
            }
227 - 1689
 
1690
            if ( j > config.num_class  )
1691
                break;
1692
 
1693
            /*
1694
            **  Header for the (sub) class
1695
            */
1696
            if ( !header_done )
1697
            {
1698
                header_done = true;
1699
                if ( report_html == html )
1700
                {
1701
                    print( "<A name=\"%s_by_cat\"></A>",url_encode(config.team_class[config.nonequestrian_class-1].full_name));
1702
                }
1703
            }
1704
 
1705
            print( "\n");
1706
            if ( report_html == html )
1707
            {
1708
                print( "<hr>" );
380 david 1709
                print( "<A name=\"%s_NE\"></A>",url_encode(config.team_class[j-1].full_name));
227 - 1710
            }
1711
            else
1712
            {
1713
                print( "%s\n", solid_line);
1714
            }
1715
            print( "Category: ");
389 david 1716
            if ( report_html == html ) setHref( "<A HREF=\"%s\">",url_encode(p_filename(FileBase, config.team_class[config.nonequestrian_class - 1].abr ,"html")));
227 - 1717
            print( "%s",  config.team_class[config.nonequestrian_class-1].full_name );
1718
 
1719
            print (" :: ");
1720
 
389 david 1721
            if ( report_html == html ) setHref( "<A HREF=\"%s\">",url_encode(p_filename(FileBase, config.team_class[j - 1].abr ,"html")));
227 - 1722
            print( "%s",  config.team_class[j-1].full_name );
1723
 
1724
            if ( config.class_ne_winners[j-1] <= 0 )
1725
            {
1726
                print( "\n");
1727
                print( "No winners awarded" );
1728
                if ( report_html == html ) print("<br>");
1729
                continue;
1730
            }
1731
 
1732
            /*
1733
            **  Enties for 'n' the best teams as configured
1734
            */
380 david 1735
            entryFound = false;
227 - 1736
            windex = 0;                     /* Winners done */
1737
            unsigned int i;
1738
            for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1739
            {
380 david 1740
                if ( !ptr->isNeData )
227 - 1741
                    continue;
380 david 1742
 
1743
                if ( ptr->teamclass != j ) {
1744
                    continue;
227 - 1745
                }
380 david 1746
                //qDebug() << "NeTeam0:" <<  ptr->team << "," << ptr->isNeData << "," << ptr->teamclass << "," << j;
227 - 1747
 
1748
                /*
1749
                **  Now read in the team record
1750
                */
1751
                if( valid_field( ptr->team ) && g_record( ptr->team, &team_buf ) )
1752
                {
1753
                    /*
1754
                    **  Ensure we have a valid team
1755
                    **  Can't award disqualified teams
1756
                    **  Can't award NE teams unless its a NE award
1757
                    */
380 david 1758
                    if ( ptr->flags.bad_times || ptr->flags.disqualified || ptr->flags.vet_check || !ptr->flags.valid )
227 - 1759
                        break;
1760
 
380 david 1761
                    if ( /*!ptr->isNeData &&*/ !ptr->flags.non_equestrian )
227 - 1762
                        break;
1763
 
1764
                    /*
1765
                    **  Count the entry
1766
                    */
1767
                    windex++;
380 david 1768
                    entryFound = true;
227 - 1769
 
1770
                    /*
1771
                    **  If printing an HTML report then we need to mark
1772
                    **  the entry with a reference so that we can link to it
1773
                    */
1774
                    print( "\n");
1775
                    if ( report_html == html )
1776
                    {
1777
                        print( "<A NAME=\"Team_%04d\">",team_buf.numb );
1778
                        print( "</A>" );
1779
                    }
1780
 
1781
                    /*
1782
                    **  Basic information
1783
                    **      - Team number - with Xref back to full result
1784
                    **      - Full team name
1785
                    **      - Full categoray name
1786
                    */
1787
                    print( "%s", placing(windex) );
1788
 
1789
                    print( "  Team Name: ");
389 david 1790
                    if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), team_buf.numb );
227 - 1791
                    print( "%-*s ",     MAX_TM_NAME, team_buf.name );
1792
 
1793
                    print( "  Number: ");
389 david 1794
                    if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "finish" ,"html")), team_buf.numb );
227 - 1795
                    print( "%4d",       team_buf.numb );
1796
 
1797
 
1798
                    for( k = 0; k < MAX_MEMB; k++ )
1799
                    {
1800
 
1801
                        /*
1802
                        **  Skip equestrian leg in the non-equestion display
1803
                        */
380 david 1804
                        if ( k + 1 == config.equestrian_leg && ptr->isNeData)
227 - 1805
                            continue;
1806
 
1807
                        print( "\n");
1808
                        print( "    ");
1809
                        print( "%-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
1810
                        print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
1811
 
1812
                        print( "  %-8s", time_a( team_buf.leg[k+1].elapsed ) );
1813
                    }
1814
 
1815
                    print( "\n");
1816
                    print( "    ");
1817
                    print_bold( TRUE );
1818
                    print( "%-*s %-*s  %-8s", MAX_PERSON_NAME , "Total" ,MAX_PERSON_NAME, "",time_a( team_buf.leg[0].elapsed ) );
1819
                    print_bold( FALSE );
1820
                    print( "\n" );
1821
                }
1822
 
1823
 
1824
                /*
1825
                **  More to do
1826
                */
1827
                if ( windex >= config.class_ne_winners[j-1] )
1828
                {
1829
                    break;
1830
                }
1831
            }
1832
        }
1833
    }
1834
 
1835
    /*
91 - 1836
    **  Generate the Hall of Fame information
1837
    */
1838
    print( "\n");
1839
    if ( report_html == html )
1840
    {
1841
        print( "<hr>" );
1842
        print( "<A name=\"%s\"></A>",url_encode("Hall Of Fame"));
1843
    }
1844
    else
1845
    {
1846
        print( "%s\n", solid_line);
1847
    }
1848
    print( "%s",  "Hall of Fame" );
1849
 
1850
    if ( config.num_fame  )
1851
    {
1852
        for( i = 1; i <= config.num_fame; i++ )
1853
        {
1854
            print( "\n");
1855
            print( "    %-*s", MAX_PERSON_NAME, config.hall_fame[i-1] );
1856
        }
1857
    }
1858
    else
1859
    {
195 david 1860
        qDebug( "There are no new stars for the Hall of Fame");
91 - 1861
    }
1862
    if ( report_html == html ) print( "\n");
1863
    if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1864
 
380 david 1865
 
1866
 
1867
 
91 - 1868
    /*
380 david 1869
    **  Insert additional Awards information
1870
    */
1871
    if (config.awardsfilename[0])
1872
    {
1873
        print( "\n");
1874
        if ( report_html == html )
1875
        {
1876
            print( "<hr>" );
1877
            print( "<A name=\"%s\"></A>",url_encode("Additional Awards"));
1878
        }
1879
        else
1880
        {
1881
            print( "%s\n", solid_line);
1882
        }
1883
        print( "%s",  "Additional Awards" );
1884
        print ("\n");
1885
 
1886
        /*
1887
        **  Read and process the named awards file
1888
        */
1889
        FILE       *adfile = NULL;
1890
        char        line[201];
1891
 
1892
        QString name = QmConfig::getAddendemFile(config.awardsfilename);
1893
        if (! name.isEmpty())
1894
        {
1895
            adfile = fopen( qPrintable(name), "rt" );  /* Open the file for reading */
1896
        }
1897
 
1898
 
1899
        if( adfile )
1900
        {
1901
            while( fgets( line, sizeof(line)-1, adfile ) ) {
1902
                //  Process each line
1903
                //  Attempt to perform some smart text replacements
1904
                //  ie: <TeamNumber:63> <TeamName:63><TeamLegName:1:63>
1905
                print( "%s", line );
1906
            }
1907
            fclose(adfile);
1908
        }
1909
        else
1910
        {
1911
            if ( report_html == html )  print ("<br>");
1912
            print( "\nThe awards file could not be found: %s\n",  adfile);
1913
        }
1914
 
1915
 
1916
        if ( report_html == html ) print( "\n");
1917
        if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1918
    }
1919
 
1920
    /*
91 - 1921
    **  Generate the FASTEST information
1922
    */
1923
    print( "\n" );
1924
    print( "\n");
1925
    if ( report_html == html )
1926
    {
1927
        print( "<hr>" );
1928
        print( "<A name=\"%s\"></A>",url_encode("FASTEST"));
1929
    }
1930
    else
1931
    {
1932
        print( "%s\n", solid_line);
1933
    }
1934
    print( "%s",  "FASTEST" );
1935
 
1936
    /*
1937
    **  Sort the data and then generate the stats - again
1938
    */
1939
    do_big_sort();
1940
    gen_stats();
1941
 
1942
    for( i = 1; i <= config.num_legs; i++ )
1943
    {
1944
        g_record( stats.fast.team[i][0], &team_buf );
1945
 
1946
        print( "\n");
1947
        print( "    %-13s ", config.leg_name[i - 1] );
1948
        print( "  Name: ");
389 david 1949
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), team_buf.numb );
91 - 1950
        print( "%-*s", MAX_PERSON_NAME, team_buf.members[i-1].name );
380 david 1951
        print( "  Team:");
389 david 1952
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "finish" ,"html")), team_buf.numb );
91 - 1953
        print( "%4d" , stats.fast.team[i][0] );
380 david 1954
        print( "  Time:%s ", time_a( stats.fast.time[i][0] ) );
91 - 1955
 
1956
    }
1957
 
1958
    if ( report_html == html ) print( "\n");
1959
    if ( report_html == html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1960
    print( "\n");
1961
    close_printer();
1962
}
1963
 
1964
/*========================================================================
1965
 *
1966
 *  pri_master_index
1967
 *
1968
 *  Purpose:
1969
 *      This function is called to create an HTML page that references all
1970
 *      the other pages that have been generated
1971
 *
1972
 *      Assume that they are in the same directory
1973
 *
1974
 *  Parameters:
1975
 *      None
1976
 *
1977
 *  Returns:
1978
 *      Nothing
1979
 *
1980
 *========================================================================*/
1981
void pri_master_index_entry(const char *name, const char *text)
1982
{
1983
    print( "<tr><td>");
389 david 1984
    print ("<A HREF=\"%s\">%s</A>\n", url_encode(p_filename(FileBase, name ,"html")), text );
91 - 1985
}
1986
 
1987
 
1988
void pri_master_index(void)
1989
{
1990
    int j;
1991
 
1992
    report_html = html;
1993
    if( !open_printer( "", "index", 132, report_html, "Master Index" ) )
1994
        return;
1995
 
1996
    /*
1997
    **  Names
1998
    */
1999
    print( "<TABLE border=0 align=center>" );
2000
    pri_master_index_entry( "name", "Team list" );
2001
    pri_master_index_entry( "competitor", "Competitor list" );
2002
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
380 david 2003
    pri_master_index_entry( "team_order", "All Teams with results" );
91 - 2004
    pri_master_index_entry( "awards", "Prizes and Awards" );
2005
    print( "<tr><td>\n" );
2006
 
2007
    print( "\n" );
2008
    for( j = 1; j <= config.num_class; j++ )
2009
    {
380 david 2010
        if (!config.team_class[j - 1].abr[0])
2011
            continue;
91 - 2012
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
2013
    }
2014
 
2015
    print( "</TABLE>" );
2016
 
2017
    close_printer();
2018
 
2019
    /*
2020
    **  A small page to hold the Leg End displays
2021
    */
2022
 
2023
    if( !open_printer( "", "legindex", 132, report_html, "Master Index with trace data" ) )
2024
        return;
2025
 
2026
    /*
2027
    **  Names
2028
    */
2029
    print( "<TABLE border=0 align=center>" );
2030
#if 1
2031
    pri_master_index_entry( "name", "Team list" );
2032
    pri_master_index_entry( "competitor", "Competitor list" );
2033
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
380 david 2034
    pri_master_index_entry( "team_order", "All Teams with results" );
91 - 2035
    pri_master_index_entry( "awards", "Prizes and Awards" );
2036
    print( "<tr><td>\n" );
2037
 
2038
    print( "\n" );
380 david 2039
    pri_master_index_entry( "summary", "Category Summary" );
91 - 2040
    for( j = 1; j <= config.num_class; j++ )
2041
    {
380 david 2042
        if (!config.team_class[j - 1].abr[0])
2043
            continue;
91 - 2044
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
2045
    }
2046
#endif
2047
    print( "<tr><td>\n" );
2048
 
2049
    print( "\n" );
170 - 2050
    for ( int leg = 1; leg <= config.num_legs; leg ++ )
91 - 2051
    {
2052
        pri_master_index_entry( tprintf("lg%1.1d", leg), tprintf("Leg End Results for: %d", leg) );    
2053
    }
2054
 
2055
    print( "<tr><td>\n" );
2056
    print( "\n" );
2057
 
170 - 2058
    for ( int leg = 1; leg <= config.num_legs; leg ++ )
91 - 2059
    {
2060
        pri_master_index_entry( tprintf("le%1.1d", leg), tprintf("Leg Elapsed Time Results for: %d", leg) );
2061
    }    
2062
 
2063
 
2064
    print( "</TABLE>" );
2065
 
2066
    close_printer();
203 - 2067
    /*
2068
     ** Tell the main system about this new report
2069
     */
380 david 2070
    MainWindow::registerReport(getPrinterFile(), "Master Leg Index");
91 - 2071
 
2072
}
2073
 
2074
 
2075
/*========================================================================
2076
 *
2077
 *  Print interim results
2078
 *
2079
 *  Purpose:
2080
 *      This function is called to Print interim results
2081
 *
2082
 *  Parameters:
2083
 *      None
2084
 *
2085
 *  Returns:
2086
 *      Nothing
2087
 *
2088
 *========================================================================*/
2089
 
2090
void pri_interim(void)
2091
{
2092
    ty_s_data  *ptr;
170 - 2093
    team_type   team_buf;
91 - 2094
    unsigned    i;
2095
    int         j, last_class;
2096
    char       *report_title;
380 david 2097
    short       pClass;
91 - 2098
 
2099
    if ( ! report_all )
2100
    {
195 david 2101
        ck_data( -1, C_DISQUAL );              /* Check the data - dummy check */
91 - 2102
    }
2103
    do_big_sort();                             /* Sort on every thing */
2104
    gen_stats();                               /* Generate the stats too */
2105
 
2106
    /*
2107
     * Now print the data on the printer 
2108
     */
2109
 
2110
    if( !open_printer( "", "int", 132, report_html, "Interim Results" ) )
2111
        return;
2112
 
2113
    /*
2114
     * Print out the data 
2115
     */
2116
    print_class_header( -1, FALSE );                     /* Print the header */
2117
 
2118
    ptr = sort_data;
380 david 2119
    sort_team_data( 0, S_TEAM, true );                   /* Re-sort on team number */
91 - 2120
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
2121
    {
380 david 2122
        if ( ptr->isNeData )
91 - 2123
            continue;
2124
 
2125
        g_record( ptr->team, &team_buf );
2126
 
2127
        print( "%4d %4.4s %-*s",
2128
               team_buf.numb,
380 david 2129
               px_place(-1, team_buf.leg[config.num_legs].le_place, false, false, ptr->flags ),
91 - 2130
               3,
380 david 2131
               team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
2132
 
91 - 2133
        for( j = 1; j <= config.num_legs; j++ )
2134
        {
380 david 2135
            print( "  %-8s", time_fa( team_buf.leg[j].elapsed, ptr->flags.disqualified ));
2136
            if ( config.num_legs != 1 ) print( " %4.4s", px_place(j, team_buf.leg[j].l_place, false, true, ptr->flags ));
2137
            if ( config.num_legs != 1 ) print( " %4.4s", px_place(j, team_buf.leg[j].le_place, false, true, ptr->flags ));
2138
            if ( config.num_legs == 1 ) print( " %-*s ",     MAX_TM_NAME, team_buf.name );
91 - 2139
        }
380 david 2140
 
2141
        if ( config.num_legs != 1 ) print( "  %-8s", time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ));
2142
        print( " %4.4s\n", px_place(0, team_buf.leg[config.num_legs].lec_place, false, false, ptr->flags ));
91 - 2143
    }
2144
 
2145
    print_class_stats( -1, FALSE );             /* Print statistics */
2146
    print_legend(-1, 1);                        /* Print the legend */
2147
    close_printer();                            /* Close the printer */
2148
 
2149
 
2150
    /*
2151
     * Now produce a breakdown on a class by class basis 
2152
     * Now print out the class placement information
2153
     */
2154
 
380 david 2155
    sort_team_data( 0, S_CLASS, true );             /* Generate class placement data */
2156
    last_class = -1;                                /* Invalid class to start with */
2157
    pClass = -1;
91 - 2158
 
2159
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
2160
    {
380 david 2161
        if (ptr->isNeData)
2162
            pClass = config.nonequestrian_class;
2163
        else
2164
            pClass = ptr->teamclass;
2165
 
91 - 2166
        /*
2167
        **  Detect a change in the "class"
2168
        **  All data is within the one array of data
2169
        **  Use the in-memory class as this MAY differ from that stored
2170
        **  The non-equestrian class does this.
2171
        */
380 david 2172
        if( last_class != pClass )
91 - 2173
        {
2174
            if( last_class >= 0 )
2175
            {
380 david 2176
                print_class_stats( last_class, FALSE );
91 - 2177
                print_legend(last_class, 1);
2178
                close_printer();
2179
            }
2180
 
380 david 2181
            report_title = tprintf( "Interim Category results for : %-*s", LEN_CLASS_NAME, pClass == 0 ? "" : config.team_class[pClass - 1].full_name );
91 - 2182
 
380 david 2183
            if( !open_printer( "", tprintf( "i%2s", config.team_class[pClass - 1].abr ), 132, report_html, report_title ) )
91 - 2184
                continue;
380 david 2185
            print_class_header( last_class = pClass, FALSE );
91 - 2186
        }
2187
 
2188
        /*
2189
        **  Now read in the team record
2190
        */
2191
        g_record( ptr->team, &team_buf );
2192
        print( "%4d %4.4s %-*s",
2193
               team_buf.numb,
380 david 2194
               px_place(-1, team_buf.leg[config.num_legs].lec_place, false, false, ptr->flags ),
91 - 2195
               3,
380 david 2196
               team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
2197
 
91 - 2198
        for( j = 1; j <= config.num_legs; j++ )
2199
        {
380 david 2200
            print( "  %-8s", time_fa( team_buf.leg[j].elapsed, ptr->flags.disqualified ));
2201
            if ( config.num_legs != 1 ) print( " %4.4s", px_place(j, team_buf.leg[j].lc_place,  false, true, ptr->flags ));
2202
            if ( config.num_legs != 1 ) print( " %4.4s", px_place(j, team_buf.leg[j].lec_place, false, true, ptr->flags ));
2203
            if ( config.num_legs == 1 ) print( " %-*s ", MAX_TM_NAME, team_buf.name );
91 - 2204
        }
380 david 2205
 
2206
 
2207
        if ( config.num_legs != 1 ) print( "  %-8s", time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ));
2208
        print( " %4.4s\n", px_place(0, team_buf.leg[config.num_legs].le_place, false, false, ptr->flags ) );
91 - 2209
    }
2210
 
2211
    print_class_stats( last_class, FALSE );
2212
    print_legend(last_class, 1);
2213
    close_printer();
2214
 
2215
}
2216
 
2217
/*----------------------------------------------------------------------------
2218
** FUNCTION           : pri_csv_data
2219
**
2220
** DESCRIPTION        : Generate a CSV file of all the report data
2221
**                      It can then be used to play with the data externally
2222
**
2223
**
2224
** INPUTS             : None
2225
**
2226
** RETURNS            : Yes it does
2227
**
2228
----------------------------------------------------------------------------*/
2229
 
2230
 
2231
void pri_csv_data ( void )
2232
{
2233
    int i;
2234
    int j;
2235
    int age_sum;
170 - 2236
    team_type   team_buf;
91 - 2237
 
2238
    /*
2239
    **  Sort on every thing
2240
    **  Then generate all the stats too
2241
    */
2242
    do_big_sort();
2243
    gen_stats();
2244
 
2245
    /*
2246
     * Now print the data on the printer 
2247
     */
2248
 
2249
    if( !open_printer( "full_data", "csv", 2000, text, NULL ) )
2250
        return;
2251
 
2252
    /*
2253
    **  Print headings
2254
    */
2255
    csv_print( "%s",   "Team Number" );
2256
    csv_print( "%s",   "Team Name" );
2257
 
2258
    csv_print( "%s",    "Class Full");
2259
    csv_print( "%s",    "Class Abr");
2260
    csv_print( "%s",    "Class Start Time");
2261
    csv_print( "%s",    "Class Start Time Number");
2262
 
2263
    csv_print( "%s",    "Team Country");
2264
 
2265
    for( j = 1; j <= config.num_legs; j++ )
2266
    {
2267
        csv_print( "%s", "Leg Number" );
2268
        csv_print( "%s", "Leg Name");
2269
        csv_print( "%s", "Competitor Name");
2270
        csv_print( "%s", "Sex" );
2271
        csv_print( "%s", "Age");
2272
        csv_print( "%s", "Start Time");
2273
        csv_print( "%s", "Start Time Number");
2274
        csv_print( "%s", "End Time" );
2275
        csv_print( "%s", "End Time Number" );
2276
        csv_print( "%s", "Elapsed Time");
2277
        csv_print( "%s", "Elapsed Time Number");
2278
        csv_print( "%s", "Leg Place");
2279
        csv_print( "%s", "Leg End Place");
2280
        csv_print( "%s", "Leg Class Place");
2281
        csv_print( "%s", "Leg End Class Place");
380 david 2282
 
2283
        csv_print( "%s", "NE Leg Place");
2284
        csv_print( "%s", "NE Leg End Place");
2285
        csv_print( "%s", "NE Leg Class Place");
2286
        csv_print( "%s", "NE Leg End Class Place");
2287
 
91 - 2288
        csv_print( "%s", "Manual");
2289
    }
2290
 
2291
    j = 0;
2292
    csv_print( "%s", "Team Start Time");
2293
    csv_print( "%s", "Team Start Time Number");
2294
    csv_print( "%s", "Team End Time");
2295
    csv_print( "%s", "Team End Time Number");
2296
    csv_print( "%s", "Team Elapsed Time");
2297
    csv_print( "%s", "Team Elapsed Time Number");
380 david 2298
 
91 - 2299
    csv_print( "%s", "Team Leg End Place");
380 david 2300
    csv_print( "%s", "Team Event Place");
91 - 2301
    csv_print( "%s", "Team Leg Class Place");
380 david 2302
    csv_print( "%s", "Team Event Class Place");
2303
 
2304
    csv_print( "%s", "NE Team Leg End Place");
2305
    csv_print( "%s", "NE Team Event Place");
2306
    csv_print( "%s", "NE Team Leg Class Place");
2307
    csv_print( "%s", "NE Team Event Class Place");
2308
 
91 - 2309
    csv_print( "%s", "Total Team Age");
2310
    csv_print( "%s", "Flag:valid Team");
2311
    csv_print( "%s", "Flag:bad_times" );
2312
    csv_print( "%s", "Flag:disqualified" );
2313
    csv_print( "%s", "Flag:non_equestrian" );
380 david 2314
    csv_print( "%s", "Flag:vet_check" );
2315
 
2316
    // Not available as the data is only in-memory
2317
    // csv_print( "%s", "Flag:notInFastest" );
2318
    // csv_print( "%s", "Flag:notInSort" );
2319
 
91 - 2320
    csv_print("\n");
2321
 
2322
 
2323
    for( i = config.min_team; i <= config.max_team; i++ )
2324
    {
2325
        if( valid_field( i ) && g_record( i, &team_buf ) )
2326
        {
2327
            /*
2328
            **  Basic information
2329
            **      - Team number - with Xref back to full result
2330
            **      - Full team name
2331
            **      - Full categoray name - with Xref to category results
2332
            **      - Country name
2333
            */
2334
            csv_print( "%d",   team_buf.numb );
2335
            csv_print( "%s",   team_buf.name );
2336
 
2337
            csv_print( "%s",    team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].full_name );
2338
            csv_print( "%s",    team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr );
2339
            csv_print( "%s",    time_a (team_buf.teamclass == 0 ? 0 : config.team_class[team_buf.teamclass - 1].start ));
2340
            csv_print( "%d",    team_buf.teamclass == 0 ? 0 : config.team_class[team_buf.teamclass - 1].start );
2341
 
2342
            csv_print( "%s",    config.num_countries == 0
2343
                                || team_buf.country ==
2344
 
2345
 
2346
            age_sum = 0;
2347
            for( j = 1; j <= config.num_legs; j++ )
2348
            {
2349
                csv_print( "%d", j );
2350
                csv_print( "%s", config.leg_name[j - 1] );
2351
                csv_print( "%s", team_buf.members[j-1].name );
2352
                csv_print( "%s", ( team_buf.members[j-1].sex == male ) ? "Male" : "Female" );
2353
                csv_print( "%d", team_buf.members[j-1].age );
2354
                if ( age_sum >= 0 )
2355
                {
2356
                    ushort age = team_buf.members[j-1].age;
2357
                    if ( age > 0 && age < 255 )
2358
                    {
2359
                        age_sum += age;
2360
                    }
2361
                    else
2362
                    {
2363
                        age_sum = -1;
2364
                    }
2365
                }
2366
 
2367
 
2368
                csv_print( "%s", time_a(team_buf.leg[j].start ));
2369
                csv_print( "%d", team_buf.leg[j].start );
2370
                csv_print( "%s", time_a(team_buf.leg[j].end ));
2371
                csv_print( "%d", team_buf.leg[j].end );
2372
                csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
2373
                csv_print( "%d", team_buf.leg[j].elapsed );
380 david 2374
 
91 - 2375
                csv_print( "%d", team_buf.leg[j].l_place );
2376
                csv_print( "%d", team_buf.leg[j].le_place );
2377
                csv_print( "%d", team_buf.leg[j].lc_place );
2378
                csv_print( "%d", team_buf.leg[j].lec_place );
380 david 2379
 
2380
                csv_print( "%d", sort_aux[team_buf.numb].lq_place[j] );
2381
                csv_print( "%d", sort_aux[team_buf.numb].leq_place[j] );
2382
                csv_print( "%d", sort_aux[team_buf.numb].lcq_place[j] );
2383
                csv_print( "%d", sort_aux[team_buf.numb].lecq_place[j] );
2384
 
2385
 
91 - 2386
                csv_print( "%d", team_buf.leg[j].manual );
2387
            }
2388
 
2389
            j = 0;
2390
            csv_print( "%s", time_a(team_buf.leg[j].start ));
2391
            csv_print( "%d", team_buf.leg[j].start );
2392
            csv_print( "%s", time_a(team_buf.leg[j].end ));
2393
            csv_print( "%d", team_buf.leg[j].end );
2394
            csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
2395
            csv_print( "%d", team_buf.leg[j].elapsed );
380 david 2396
 
2397
            csv_print( "%d", team_buf.leg[j].l_place );
91 - 2398
            csv_print( "%d", team_buf.leg[j].le_place );
380 david 2399
            csv_print( "%d", team_buf.leg[j].lc_place );
91 - 2400
            csv_print( "%d", team_buf.leg[j].lec_place );
380 david 2401
 
2402
            csv_print( "%d", sort_aux[team_buf.numb].lq_place[j] );
2403
            csv_print( "%d", sort_aux[team_buf.numb].leq_place[j] );
2404
            csv_print( "%d", sort_aux[team_buf.numb].lcq_place[j] );
2405
            csv_print( "%d", sort_aux[team_buf.numb].lecq_place[j] );
2406
 
91 - 2407
            csv_print( "%d", age_sum );
2408
            csv_print( "%d", team_buf.flags.valid );
2409
            csv_print( "%d", team_buf.flags.bad_times );
2410
            csv_print( "%d", team_buf.flags.disqualified );
2411
            csv_print( "%d", team_buf.flags.non_equestrian );
380 david 2412
            csv_print( "%d", team_buf.flags.vet_check );
2413
            // Not available as the data is only in-memory
2414
            // csv_print( "%d", team_buf.flags.notInFastest );
2415
            // csv_print( "%d", team_buf.flags.notInSort );
91 - 2416
 
2417
//How about class placings
2418
 
2419
 
2420
            csv_print( "\n" );
2421
        }
2422
    }
2423
 
2424
    close_printer();
2425
}
2426
 
2427
 
2428
/*========================================================================
2429
 *
2430
 *  Print all reports at once
2431
 *  Its all so fast, these days ...
2432
 *
2433
 *  Purpose:
2434
 *      This function is called to print all reports at once
2435
 *
2436
 *  Parameters:
2437
 *      None
2438
 *
2439
 *  Returns:
2440
 *      Nothing
2441
 *
2442
 *========================================================================*/
2443
 
2444
void pri_all_reports ( void )
2445
{
2446
    int leg;
2447
    report_all = TRUE;
2448
 
2449
    pri_team();
2450
 
2451
    for ( leg = 1; leg <= config.num_legs; leg ++ )
2452
    {
2453
        pri_leg_body ( leg );
2454
        pri_eleg_body ( leg );
2455
 
2456
        report_html = html;
2457
 
2458
        pri_leg_body ( leg );
380 david 2459
        pri_eleg_body ( leg );
2460
 
2461
        //report_html = printed;
2462
        //
2463
        //pri_leg_body ( leg );
2464
        //pri_eleg_body ( leg );        
91 - 2465
 
2466
        report_html = text;
2467
    }
2468
 
2469
    pri_final();
380 david 2470
    pri_final_teamOrder();
91 - 2471
    pri_final_html();
2472
    pri_csv_data();
380 david 2473
    pri_summary_html();
91 - 2474
    pri_summary();
2475
    pri_awards_html();
2476
    pri_awards();
2477
    pri_master_index();
2478
 
380 david 2479
    report_html = html;
168 david 2480
    pri_interim();
380 david 2481
    report_html = text;
2482
    pri_interim();
168 david 2483
 
91 - 2484
    report_all = FALSE;
2485
}
2486
 
2487
 
2488
/*========================================================================
2489
 *
2490
 *  Print a class header
2491
 *
2492
 *  Purpose:
2493
 *      This function is called to print a class header
2494
 *
2495
 *  Parameters:
2496
 *      class           Name of this class
2497
 *      final           False - prelim results
2498
 *
2499
 *  Returns:
2500
 *      Nothing
2501
 *
2502
 *========================================================================*/
2503
 
2504
void print_class_header( int teamclass, int final )
2505
{
2506
    int         j;
2507
 
2508
    /*
2509
    **  Give a clear indication that the report is preliminary
2510
    */
2511
 
2512
    if( !final )
2513
        print( "PRELIMINARY RESULTS ONLY\n\n" );
2514
 
2515
    /*
2516
    **  Now printout the column headings
2517
    **  This is a two line display
2518
    **
2519
    **  Line-1  Leg names
2520
    */
380 david 2521
 
2522
    if (config.num_legs == 1 ) {
2523
        print( "%-*s", 4, "" );
2524
        print( "  %-*s", 18, config.leg_name[0] );
2525
        print( " %-*s", MAX_TM_NAME + 1, "" );
2526
        print( "%-4s\n", ( teamclass < 0 ) ? "Cat" : "Fin" );
2527
 
2528
    } else {
2529
        print( "%-*s %-*s %-*s", 4, "", 4, "", 3, "" );
2530
        for( j = 1; j <= config.num_legs; j++ )
2531
        {
2532
            print_bold( TRUE );
2533
            print( "  %-*s", 18, config.leg_name[j - 1] );
2534
            print_bold( FALSE );
2535
        }
2536
        print( "  %-8s %-4s\n", "Total", ( teamclass < 0 ) ? "Cat" : "Fin" );
91 - 2537
    }
2538
 
2539
 
380 david 2540
 
2541
 
91 - 2542
    /*
2543
    **  Line-2  Details
2544
    */
2545
    print_underline( TRUE );
2546
    print( "%-*s %*s %-*s", 4, final ? "Plce" : "Team",
2547
                            4, final ? "Team" : "Plce",
2548
                            3, "Cat" );
2549
 
2550
 
380 david 2551
    if (config.num_legs == 1 ) {
2552
        print( "  %-8s", "Time" );
2553
        print( " %-*s", MAX_TM_NAME + 1, "Team Name" );
2554
        print( "%4s\n", "Plce" );
2555
 
2556
    } else {
2557
        for( j = 1; j <= config.num_legs; j++ )
2558
            print( "  %-8s %4s %4s", "Time", "LP", "EP" );
2559
        print( "  %-8s %4s\n", "Time", "Plce" );
2560
    }
2561
 
2562
 
91 - 2563
    print_underline( FALSE );
2564
}
2565
 
2566
/*========================================================================
2567
 *
2568
 *  Generate the class stats
2569
 *
2570
 *  Purpose:
2571
 *      This function is called to Generate the class stats
2572
 *
2573
 *  Parameters:
2574
 *      c           Class to print
2575
 *      final       TRUE: Final data
2576
 *                  FALSE: Interim data
2577
 *
2578
 *  Returns:
2579
 *      Nothing
2580
 *
2581
 *========================================================================*/
2582
 
2583
void print_class_stats( int c, int final )
2584
{
2585
    int         i, j;
2586
    const char        *title;
2587
 
2588
    if( c < 0 )
2589
    {
2590
        title = "Event";
2591
        c = 0;
2592
    }
2593
    else
2594
    {
2595
        title = "Category";
2596
    }
2597
 
2598
    print( "\n" );
2599
    if ( report_html ) print_underline(TRUE);
2600
    print( "%s statistics", title );
2601
    if ( report_html ) print_underline(FALSE);
2602
    print( "\n" );
2603
 
2604
 
2605
    /*
2606
    **  Print the names of the different legs
2607
    */
2608
    print( "%-*s       ", LEN_CLASS_NAME, "" );
2609
    for( i = 1; i <= config.num_legs; i++ )
2610
    {
2611
        print_bold( TRUE );
2612
        print( "%-13s  ", config.leg_name[i - 1] );
2613
        print_bold( FALSE );
2614
    }
380 david 2615
    if ( config.num_legs != 1 ) {
2616
        print_bold( TRUE );
2617
        print( "%-13s  ", final ? "Total" : "" );
2618
        print_bold( FALSE );
2619
    }
2620
    print( "\n" );
91 - 2621
 
2622
    /*
2623
    **  Print the fastest teams for each leg and overall
2624
    **  Add cross references to the team names for the fastest teams
2625
    */
2626
    print( "%*s : ", LEN_CLASS_NAME, "Fastest" );
2627
    for( i = 0; i <= config.num_legs; i++ )
2628
    {
2629
        j = i + 1;
2630
        if( i >= config.num_legs )
2631
        {
2632
            if( final )
2633
                j = 0;                           /* Leg-0 last */
2634
            else
2635
                break;
2636
        }
2637
 
389 david 2638
        if ( report_html == html ) setHref( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(FileBase, "name" ,"html")), stats.fast.team[j][c] );
91 - 2639
        print( "%4d",  stats.fast.team[j][c] );
2640
        print( " %s  ", time_a( stats.fast.time[j][c] ) );
380 david 2641
        if ( config.num_legs == 1 ) break;
91 - 2642
    }
2643
    print( "\n" );
2644
 
2645
    /*
2646
    **  Print the average time for each leg
2647
    */
2648
    print( "%*s : ", LEN_CLASS_NAME, "Average" );
2649
    for( i = 0; i <= config.num_legs; i++ )
2650
    {
2651
        j = i + 1;
2652
        if( i >= config.num_legs )
2653
        {
2654
            if( final )
2655
                j = 0;                           /* Leg-0 last */
2656
            else
2657
                break;
2658
        }
2659
        print( "     %s  ", time_a( stats.average[j][c] ) );
380 david 2660
        if ( config.num_legs == 1 ) break;
91 - 2661
    }
2662
}
2663
 
2664
/*========================================================================
2665
 *
2666
 *  Print the legend
2667
 *
2668
 *  Purpose:
2669
 *      This function is called to Print the legend
2670
 *
2671
 *  Parameters:
2672
 *      class       - Class currently being printed
2673
 *      full        - Display full legend
2674
 *
2675
 *  Returns:
2676
 *      Nothing
2677
 *
2678
 *========================================================================*/
2679
 
2680
void print_legend ( int teamclass, int full )
2681
{
2682
 
2683
    int         i;
2684
    char        line[201];
2685
    FILE       *adfile = NULL;
2686
    int         count;
390 david 2687
    Q_UNUSED(teamclass);
91 - 2688
 
2689
    /*
2690
     * First the categories 
2691
     */
2692
    print( "\n\n" );
2693
    if ( report_html ) print_underline(TRUE);
2694
    print( "Category abbreviations" );
2695
    if ( report_html ) print_underline(FALSE);
2696
    print( "\n" );
2697
 
2698
 
2699
    for( i = 1, count = 0; i <= config.num_class; i++ )
2700
    {
380 david 2701
        if (!config.team_class[i - 1].abr[0])
91 - 2702
            continue;
380 david 2703
 
389 david 2704
        if ( report_html == html ) setHref( "<A HREF=\"%s\">",url_encode(p_filename(FileBase, config.team_class[i - 1].abr ,"html")) );
91 - 2705
        print( "%-*s", 3, config.team_class[i - 1].abr );
2706
        print( " : %-*s  ", LEN_CLASS_NAME, config.team_class[i - 1].full_name );
2707
 
2708
        if( !( ++count % 5 ) )
2709
            print( "\n" );
2710
    }
2711
 
2712
    /*
2713
    **  Add link to the finish order report
2714
    */
2715
    if ( report_html == html )
2716
    {
389 david 2717
        setHref( "<A HREF=\"%s\">", url_encode(p_filename(FileBase, "finish" ,"html")) );
91 - 2718
        print( "%-*s", 3, "All" );
2719
        print( " : %-*s  ", LEN_CLASS_NAME, "Finishing Order" );
2720
    }
2721
 
2722
    /*
2723
    **  Country data - if countries have been defined
2724
    */
2725
    if( config.num_countries )
2726
    {
2727
        print( "\n\n" );
2728
        if ( report_html ) print_underline(TRUE);
2729
        print( "Country abbreviations" );
2730
        if ( report_html ) print_underline(FALSE);
2731
        print( "\n" );
2732
 
2733
        for( i = 0, count = 0; i < MAX_COUNTRY; i++ )
2734
        {
2735
            if( config.country_name[i].abr[0] )
2736
            {
2737
                print( "%-*s : %-*s  ", 4, config.country_name[i].abr,
2738
                       LEN_CNTRY_NAME, config.country_name[i].full_name );
2739
                if( !( ++count % 5 ) )
2740
                    print( "\n" );
2741
            }
2742
        }
2743
    }
2744
    print( "\n" );
2745
 
2746
    /*
2747
     * Other comments 
2748
     */
2749
    if ( full )
2750
    {
380 david 2751
        if ( config.num_legs != 1 )  {
2752
            print( "\n" );
2753
            if ( report_html ) print_underline(TRUE);
2754
            print( "Place numbers (LP and EP)" );
2755
            if ( report_html ) print_underline(FALSE);
2756
            print( "\n" );
2757
            print( "LP - Placing based on elapsed time within the leg.                Cat Plce - Placing within the category.\n" );
2758
            print( "EP - Placing based on accumulated times to the end of that leg.   Fin Plce - Overall placing within the event.\n" );
2759
            print( "U  - Placing not available.\n" );
2760
        } else {
2761
            print( "\n" );
2762
            if ( report_html ) print_underline(TRUE);
2763
            print( "Table Legend" );
2764
            if ( report_html ) print_underline(FALSE);
2765
            print( "\n" );
2766
 
2767
            print( "Cat Plce - Placing within the category.\n");
2768
            print ("Fin Plce - Overall placing within the event.\n" );
2769
            print( "U        - Placing not available.\n" );
2770
        }
91 - 2771
    }
2772
 
2773
    /*
2774
     *  Insert the contents of the config.addendum file
2775
     *  or a defualt message
2776
     */
2777
    if( config.addendum[0] )
199 david 2778
    {
380 david 2779
        QString name = QmConfig::getAddendemFile(config.addendum);
2780
        if (! name.isEmpty())
2781
        {
2782
            adfile = fopen( qPrintable(name), "rt" );  /* Open the file for reading */
2783
        }
199 david 2784
    }
91 - 2785
 
2786
    if( adfile )
2787
    {
2788
        while( fgets( line, sizeof(line)-1, adfile ) )
2789
            print( "%s", line );
380 david 2790
        fclose (adfile);
91 - 2791
    }
2792
    else
2793
    {
2794
        print( "\nTiming and Results by\n" );
2795
        print( "Embedded Solutions\n" );
2796
    }
2797
}
2798
 
2799
 
2800
/*========================================================================
2801
 *
380 david 2802
 *  Return place data or NE flag
91 - 2803
 *
2804
 *  Purpose:
2805
 *      This function is called to Return place data
2806
 *
2807
 *      This routine is called to fill a print team_buffer - to allow for
2808
 *      multiple calls to this function ( before the data is used ) a number
2809
 *      of static team_buffers are maintained
2810
 *
2811
 *  Parameters:
380 david 2812
 *      leg         Leg number being processed or
2813
 *                  -1: Unplaced on any bad flag
2814
 *      num         place - if valid time
2815
 *      epMode      true:  Event Time Mode - Show V and NE flags
2816
 *                  false: Leg Time Mode -
2817
 *      neMode      true: Display NE for NE Teams
2818
 *                  false: Display data for NE teams
2819
 *      flags       Team flags
91 - 2820
 *
2821
 *  Returns:
2822
 *      This function returns a pointer to the character string for the
380 david 2823
 *      number or a pointer to a status string.
91 - 2824
 *
2825
 *========================================================================*/
380 david 2826
const char *px_place( int leg, int num, bool epMode, bool neMode, team_flags flags )
91 - 2827
{
2828
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2829
    static int  i = 0;                           /* Current index into store */
2830
    static const char *dis = "U";
380 david 2831
    static const char *alt = "NE";
2832
    static const char *vet = "U";
91 - 2833
 
380 david 2834
    if (flags.bad_times || flags.disqualified || !flags.valid)
2835
        return dis;
91 - 2836
 
380 david 2837
    if (leg == -1 && flags.vet_check )
2838
        return vet;
91 - 2839
 
380 david 2840
    if (neMode && flags.non_equestrian && (leg >= config.equestrian_leg || leg == 0))
2841
        return alt;
91 - 2842
 
380 david 2843
    if (epMode && flags.vet_check && leg >= config.equestrian_leg)
2844
        return vet;
91 - 2845
 
380 david 2846
    if ( flags.vet_check && (leg == config.equestrian_leg || leg == 0))
2847
        return vet;
91 - 2848
 
380 david 2849
    if (num <= 0)
2850
        return dis;
91 - 2851
 
380 david 2852
    i++;
2853
    if( i >= 2 )
91 - 2854
        i = 0;                                   /* Select next entry */
2855
    sprintf( store[i], "%4d", num );
2856
    return ( store[i] );
2857
 
2858
}
2859
 
2860
/*========================================================================
2861
 *
2862
 *  Check data for bad times
2863
 *
2864
 *  Purpose:
2865
 *      This function is called to Check data for bad times
2866
 *      Scan the sort data structure and locate entries that have incorrect
2867
 *      times.
2868
 *      
2869
 *      Entries that have invalid leg times are displayed to the operator
2870
 *      and the report process can be aborted
2871
 *
2872
 *  Parameters:
189 - 2873
 *      leg             Leg to test
91 - 2874
 *      mode            Either end or elapsed times to be tested
380 david 2875
 *                          C_END     - Printing in Leg Finishing Time
2876
 *                          C_DISQUAL - Interim results
2877
 *                          C_ELAPSED - Elapsed times
91 - 2878
 *
2879
 *
2880
 *========================================================================*/
2881
 
380 david 2882
void ck_data( int leg, int mode )
91 - 2883
{
2884
    ty_s_data  *ptr;
2885
    unsigned    i;
2886
    int         bad = 0;
189 - 2887
    int         k;
2888
    int         bad_leg;
384 david 2889
    maraTime_t     *t;                               /* An array of times */
91 - 2890
 
2891
    ptr = sort_data;
2892
    for( i = 1; i <= sort_num; i++, ptr++ )
2893
    {
2894
        bad_leg = 0;
2895
        if( mode == C_DISQUAL )
2896
        {
2897
            ptr->flags.bad_times = ptr->flags.disqualified;
2898
            continue;
2899
        }
2900
 
2901
        if( mode == C_ELAPSED )
2902
            t = ptr->lege;
2903
        else
2904
            t = ptr->leg;
2905
 
2906
 
380 david 2907
        // If the team is duff, then don;t include in Event or Leg Calcs
2908
        if( !ptr->flags.valid || ptr->flags.bad_times || ptr->flags.disqualified)
91 - 2909
        {
380 david 2910
            ptr->flags.notInLP = TRUE;
91 - 2911
        }
2912
        else
2913
        {
380 david 2914
            // If any of the leg times are duff, then don't include the leg Event Calcs
91 - 2915
 
2916
            if( leg <= 0 )
2917
            {
2918
                for( k = 0; k <= config.num_legs; k++ )
2919
                {
2920
                    if ( !(config.equestrian_leg && ptr->flags.non_equestrian && config.equestrian_leg == k  ))
2921
                        bad_leg |= ( t[k] <= 0 );
2922
                }
2923
            }
2924
            else
2925
            {
2926
                bad_leg = t[leg] <= 0;
2927
            }
2928
 
2929
            if( bad_leg )
2930
            {
2931
                if ( ! report_all )
2932
                {
380 david 2933
                    qDebug( "Team with incorrect time information: %d", ptr->team  );
91 - 2934
                    bad++;
2935
                }
2936
            }
2937
        }
2938
    }
2939
 
2940
    if( bad )
2941
    {
380 david 2942
        qDebug( "%d teams with incorrect times. These have been flagged as unplaced", bad );
91 - 2943
    }
2944
}
2945
 
2946
/*========================================================================
2947
 *
2948
 *  Update placing information
2949
 *
2950
 *  Purpose:
2951
 *      This function is called to Update placing information
2952
 *
2953
 *      This routine will rip through the data generating the team placing in
2954
 *      a) Within a leg
2955
 *      b) At the end of a leg
2956
 *      c) Within a leg by  class
2957
 *      d) At the end of a leg by class
2958
 *
2959
 *      This function is provided to allow the display routines to
2960
 *      be accessed and updated without the need to run a report
2961
 *
2962
 *  Parameters:
2963
 *      xxxx        a ptr to the xxxx stuff
2964
 *
2965
 *  Returns:
2966
 *      Nothing
2967
 *
2968
 *========================================================================*/
2969
 
2970
void srt_place(void)
2971
{
380 david 2972
    ck_data( -1, C_ELAPSED );
91 - 2973
    do_big_sort();
2974
 
2975
    /*
195 david 2976
     * Generate the stats
91 - 2977
     */
2978
    gen_stats();
2979
}
2980
 
2981
/*========================================================================
2982
 *
2983
 *  Calculate summary information
2984
 *
2985
 *  Purpose:
2986
 *      This function is called to calculate summary information
2987
 *
2988
 *  Parameters:
2989
 *      ptr         - Address of a summary structure to fill in
2990
 *
2991
 *  Returns:
2992
 *      Nothing
2993
 *
2994
 *========================================================================*/
2995
 
2996
void calc_class_summary( t_class_summary * ptr )
2997
{
2998
    int i;
170 - 2999
    team_type   team_buf;
91 - 3000
 
3001
    /*
3002
    **  Reset the data
3003
    */
3004
    memset ( ptr, 0, sizeof (*ptr ));
3005
 
3006
    /*
3007
     * Extract the required data from the data base
3008
     * Only save that information required for the operation
3009
     */
3010
 
3011
    for( i = config.min_team; i <= config.max_team; i++ )
3012
    {
3013
        if( valid_field( i ) && g_record( i, &team_buf ) )
3014
        {
227 - 3015
            bool valid = true;
380 david 3016
            bool valid_ne = true;
91 - 3017
 
3018
            if ( team_buf.flags.disqualified )
3019
            {
3020
                ptr->teamclass[team_buf.teamclass].disqualified++;
3021
                ptr->total.disqualified++;
227 - 3022
                valid = false;
380 david 3023
                valid_ne = false;
91 - 3024
            }
3025
 
225 - 3026
            if ( team_buf.flags.vet_check )
3027
            {
3028
                ptr->teamclass[team_buf.teamclass].vet_check++;
3029
                ptr->total.vet_check++;
227 - 3030
                valid = false;
380 david 3031
                valid_ne = false;
225 - 3032
            }
3033
 
91 - 3034
            if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
3035
            {
3036
                ptr->teamclass[team_buf.teamclass].non_equestrian++;
3037
                ptr->total.non_equestrian++;
227 - 3038
                valid = false;
91 - 3039
            }
380 david 3040
            else
3041
            {
3042
                valid_ne = false;
3043
            }
227 - 3044
 
3045
            ptr->total.total++;
3046
            ptr->teamclass[team_buf.teamclass].total++;
3047
            if ( valid )
3048
            {
3049
                ptr->total.valid++;
3050
                ptr->teamclass[team_buf.teamclass].valid++;
380 david 3051
 
3052
                if ( ! team_buf.flags.bad_times )
3053
                {
3054
                    ptr->total.valid_ev++;
3055
                    ptr->teamclass[team_buf.teamclass].valid_ev++;
3056
                }
227 - 3057
            }
380 david 3058
 
3059
            if ( valid_ne )
3060
            {
3061
                if ( ! team_buf.flags.bad_times )
3062
                {
3063
                    ptr->total.valid_ne++;
3064
                    ptr->teamclass[team_buf.teamclass].valid_ne++;
3065
                }
3066
            }
91 - 3067
        }
3068
    }
3069
 
3070
    /*
3071
    **  Fix up the totals for the non equestrians
3072
    **  This is not a real category but a summary of the others.
3073
    */
3074
    if ( config.nonequestrian_class  )
3075
    {
3076
        ptr->teamclass[config.nonequestrian_class].total += ptr->total.non_equestrian;
227 - 3077
        ptr->teamclass[config.nonequestrian_class].valid += ptr->teamclass[config.nonequestrian_class].total;
91 - 3078
    }
3079
}
3080
 
3081
/*========================================================================
3082
 *
380 david 3083
 *  Print summary results
3084
 *
3085
 *  Purpose:
3086
 *      This function is called to Print summary results
3087
 *      Keep the page to 80 cols, so that it can be pronted on A4
3088
 *
3089
 *  Parameters:
3090
 *      None
3091
 *
3092
 *  Returns:
3093
 *      Nothing
3094
 *
3095
 *========================================================================*/
3096
void pri_summary_html(void)
3097
{
3098
    report_type saved = report_html;
3099
    /*
3100
    **  Generate ALL results with HTML tags
3101
    */
3102
    report_html = html;
3103
    pri_summary();
3104
    report_html = printed;
3105
    pri_summary();
3106
    report_html = saved;
3107
}
3108
 
3109
/*========================================================================
3110
 *
91 - 3111
 *  Print summary information
3112
 *
3113
 *  Purpose:
3114
 *      This function is called to print summary information
3115
 *
3116
 *  Parameters:
3117
 *      None
3118
 *
3119
 *  Returns:
3120
 *      Nothing
3121
 *
3122
 *========================================================================*/
3123
 
3124
void pri_summary (void)
3125
{
3126
 
3127
    t_class_summary data;
3128
    int         i;
3129
 
380 david 3130
    if( !open_printer( "", "summary", 80, report_html, "Summary Information" ) )
91 - 3131
        return;
3132
 
3133
    calc_class_summary( & data );
3134
 
3135
 
3136
    /*
3137
    **  Display summary stats
3138
    */
3139
 
380 david 3140
    print( "%*s : %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n",
3141
                                      LEN_CLASS_NAME, "Category",
91 - 3142
                                      "Total",
380 david 3143
                                      "Valid",
91 - 3144
                                      "Disq",
380 david 3145
                                      "NonEq",
3146
                                      "VetChk",
3147
                                      "CompEv",
3148
                                      "CompNe"
3149
                                       );
91 - 3150
 
3151
    for( i = 0; i < config.num_class; i++ )
3152
    {
380 david 3153
        if (!config.team_class[i].abr[0])
3154
            continue;
91 - 3155
        /*
3156
        **  The non-equestrian leg does not have any data
380 david 3157
        **  Suppress the display
91 - 3158
        */
3159
        if ( config.nonequestrian_class == i+1  )
3160
            continue;
3161
 
389 david 3162
        if ( report_html == html ) setHref( "<A HREF=\"%s\">",url_encode(p_filename(FileBase, config.team_class[i].abr ,"html")) );
380 david 3163
        print( "%*s ", LEN_CLASS_NAME, config.team_class[i].full_name );
3164
 
3165
        print( ": %-7d %-7d %-7d %-7d %-7d %-7d %-7d\n",
91 - 3166
                                          data.teamclass[i+1].total,
380 david 3167
                                          data.teamclass[i+1].valid,
91 - 3168
                                          data.teamclass[i+1].disqualified,
380 david 3169
                                          data.teamclass[i+1].non_equestrian,
3170
                                          data.teamclass[i+1].vet_check,
3171
                                          data.teamclass[i+1].valid_ev,
3172
                                          data.teamclass[i+1].valid_ne
3173
                                          );
91 - 3174
    }
3175
 
3176
    print( "\n" );
380 david 3177
    print( "%*s : %-7d %-7d %-7d%-7d %-7d %-7d %-7d\n",
3178
                                      LEN_CLASS_NAME, "Totals",
91 - 3179
                                      data.total.total,
380 david 3180
                                      data.total.valid,
91 - 3181
                                      data.total.disqualified,
380 david 3182
                                      data.total.non_equestrian,
3183
                                      data.total.vet_check,
3184
                                      data.total.valid_ev,
3185
                                      data.total.valid_ne
3186
                                      );
91 - 3187
 
3188
    close_printer();
3189
}
3190
 
3191
 
3192
 
3193
/*========================================================================
3194
 *
3195
 *  Main sort routine for final data
3196
 *
3197
 *  Purpose:
3198
 *      This function is called to do the report sort routine for final data
3199
 *      This routine will fill all the gaps in the sort_aux structure
3200
 *
3201
 *  Parameters:
3202
 *      None
3203
 *
3204
 *  Returns:
3205
 *      Nothing
3206
 *
3207
 *========================================================================*/
3208
 
3209
void do_big_sort(void)
3210
{
380 david 3211
    int         i, k, q;                             /* Looooopy things */
91 - 3212
    unsigned    j;
380 david 3213
    ty_s_data  *ptr;                                 /* Pointer to sort data */
91 - 3214
    int         teamclass;                           /* Current class */
380 david 3215
    int         teamclassq;                          /* Current class */
170 - 3216
    team_type   team_buf;
91 - 3217
 
3218
    for( i = 0; i <= config.num_legs; i++ )
3219
    {
380 david 3220
        bool sortWithEq =  (i == 0);
3221
 
91 - 3222
        /*
3223
        **  Sort on leg elapsed time
3224
        **  Then save the teams elapsed place in each leg
3225
        */
380 david 3226
        sort_team_data( i, S_L, sortWithEq );
3227
        for( j = 1, k = 1, q = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
91 - 3228
        {
380 david 3229
            sort_aux[ptr->team].team = ptr->team;
3230
 
3231
            if (i == config.equestrian_leg || i == 0) {
3232
                if (ptr->flags.bad_times || ptr->flags.disqualified || ptr->flags.vet_check) {
3233
                    if (ptr->isNeData) {
3234
                        sort_aux[ptr->team].lq_place[i] = -1;
3235
                    } else {
3236
                        sort_aux[ptr->team].l_place[i] = -1;
3237
                    }
3238
                    continue;
3239
                }
3240
            }
3241
 
3242
            // Flag as unplaced NE teams on the EQ leg
3243
            // DO NOT Flag as unplaced in overall (leg:0) NE teams as this is used in the full report
3244
            if ( (i == config.equestrian_leg /*|| i == 0*/ ) && ptr->flags.non_equestrian ) {
3245
                if (ptr->isNeData) {
3246
                    sort_aux[ptr->team].lq_place[i] = -1;
3247
                } else {
3248
                    sort_aux[ptr->team].l_place[i] = -1;
3249
                }
91 - 3250
                continue;
380 david 3251
            }
3252
 
3253
            if (ptr->isNeData) {
3254
                sort_aux[ptr->team].lq_place[i] = q++;
3255
            } else {
3256
                sort_aux[ptr->team].l_place[i] = k++;
3257
            }
91 - 3258
        }
3259
 
3260
        /*
3261
        **  Sort on leg end time
3262
        **  Then save the teams place at the end of each leg
3263
        */
380 david 3264
        sort_team_data( i, S_LE, sortWithEq);
3265
        for( j = 1, k = 1, q = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
91 - 3266
        {
380 david 3267
            if (i == 0 || i == config.equestrian_leg || sort_afterEquestrianLeg ) {
3268
                if (ptr->flags.bad_times || ptr->flags.disqualified || ptr->flags.vet_check) {
3269
                    if (ptr->isNeData) {
3270
                        sort_aux[ptr->team].leq_place[i] = -1;
3271
                    } else {
3272
                        sort_aux[ptr->team].le_place[i] = -1;
3273
                    }
3274
                    continue;
3275
                }
3276
            }
3277
 
3278
            // Flag as unplaced NE teams on the EQ leg
3279
            // Flag as unplaced NE teams after the EQ leg
3280
            // Flag as unplaced in overall (leg:0) NE teams
3281
            if ( (i == config.equestrian_leg || sort_afterEquestrianLeg || i == 0 ) && ptr->flags.non_equestrian ) {
3282
                if (ptr->isNeData) {
3283
                    sort_aux[ptr->team].leq_place[i] = -1;
3284
                } else {
3285
                    sort_aux[ptr->team].le_place[i] = -1;
3286
                }
91 - 3287
                continue;
380 david 3288
            }
3289
 
3290
            if (ptr->isNeData) {
3291
                sort_aux[ptr->team].leq_place[i] = q++;
3292
            } else {
3293
                sort_aux[ptr->team].le_place[i] = k++;
3294
            }
91 - 3295
        }
3296
 
3297
        /*
3298
        **  Sort on elapsed time per class
380 david 3299
        **  Then save the teams elapsed place in each leg per class
91 - 3300
        */
380 david 3301
        sort_team_data( i, S_LC, sortWithEq );
91 - 3302
        teamclass = -1;
380 david 3303
        teamclassq = -1;
3304
        int ll = -1;
3305
        for( k = 1, j = 1, q = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
91 - 3306
        {
380 david 3307
            bool isNe = false;
3308
            if ( sortWithEq && ptr->isNeData  ) {
3309
                if ( !ptr->isNeData)
3310
                    isNe = true;
91 - 3311
            }
380 david 3312
            else {
3313
                if (ptr->isNeData)
3314
                    isNe = true;
3315
            }
3316
 
3317
            // Flag as unplaced NE teams on the EQ leg
3318
            if (i == 0 || i == config.equestrian_leg) {
3319
                if (ptr->flags.bad_times || ptr->flags.disqualified || ptr->flags.vet_check) {
3320
                    if (isNe) {
3321
                        sort_aux[ptr->team].lcq_place[i] = -1;
3322
                    } else {
3323
                        sort_aux[ptr->team].lc_place[i] = -1;
3324
                    }
3325
                    continue;
3326
                }
3327
            }
3328
 
3329
            if ( i == config.equestrian_leg && ptr->flags.non_equestrian ) {
3330
                if (ptr->isNeData) {
3331
                    sort_aux[ptr->team].lcq_place[i] = -1;
3332
                } else {
3333
                    sort_aux[ptr->team].lc_place[i] = -1;
3334
                }
3335
                continue;
3336
            }
3337
 
3338
            if (ptr->isNeData) {
3339
                if( teamclassq != ptr->teamclass )
3340
                {
3341
                    q = 1;
3342
                    teamclassq = ptr->teamclass;
3343
                }
3344
//qDebug() << "Team:" << ptr->team << ",Class:" << ptr->teamclass << "," << q;
3345
                sort_aux[ptr->team].lcq_place[i] = q++;
3346
 
3347
            }
3348
            else {
3349
 
3350
                if( teamclass != ptr->teamclass )
3351
                {
3352
                    k = 1;
3353
                    ll = 1;
3354
                    teamclass = ptr->teamclass;
3355
                }
3356
                if (ptr->flags.non_equestrian && sortWithEq)
3357
                    sort_aux[ptr->team].lc_place[i] = ll++;
3358
                else
3359
                    sort_aux[ptr->team].lc_place[i] = k++;
3360
            }
3361
 
91 - 3362
        }
3363
 
3364
        /*
3365
        **  Sort on end time per class
3366
        **  Then save the teams place at the end of each leg per class
380 david 3367
        **  Suuport NE teams as well Equestrian Teams
91 - 3368
        */
380 david 3369
        sort_team_data( i, S_LEC, sortWithEq );
91 - 3370
        teamclass = -1;
380 david 3371
        teamclassq = -1;
3372
        for( k = 1, j = 1, q = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
91 - 3373
        {
380 david 3374
 
3375
            // All teams with bad data
3376
            if (ptr->flags.bad_times || ptr->flags.disqualified ) {
3377
                sort_aux[ptr->team].lec_place[i] = -1;
3378
                sort_aux[ptr->team].lecq_place[i] = -1;
3379
                continue;
91 - 3380
            }
380 david 3381
 
3382
            // NE team - the full entry
3383
            // Flag as unplaced NE teams on the EQ leg
3384
            // Flag as unplaced NE teams after the EQ leg
3385
            // Flag as unplaced in overall (leg:0) NE teams
3386
            if (!ptr->isNeData  && ptr->flags.non_equestrian) {
3387
                if (i == 0 || i == config.equestrian_leg || sort_afterEquestrianLeg ) {
3388
                    sort_aux[ptr->team].lec_place[i] = -1;
3389
                    continue;
3390
 
3391
                }
3392
 
3393
            //  NE Team - the duplicate entry
3394
            // Flag as unplaced NE teams after the EQ leg
3395
            } else if ( ptr->isNeData ) {
3396
                if ( i == config.equestrian_leg) {
3397
                    sort_aux[ptr->team].lecq_place[i] = -1;
3398
                    continue;
3399
                }
3400
 
3401
                // EQ Team
3402
                // Flag as unplaced vetted teams on the EQ leg
3403
                // Flag as unplaced vetted teams after the EQ leg
3404
                // Flag as unplaced in overall (leg:0) vetted teams
3405
            } else {
3406
                if (i == 0 || i == config.equestrian_leg || sort_afterEquestrianLeg ) {
3407
                   if ( ptr->flags.vet_check) {
3408
                       sort_aux[ptr->team].lec_place[i] = -1;
3409
                       continue;
3410
                   }
3411
                }
3412
            }
3413
 
3414
 
3415
            if (ptr->isNeData) {
3416
                if( teamclassq != ptr->teamclass )
3417
                {
3418
                    q = 1;
3419
                    teamclassq = ptr->teamclass;
3420
                }
3421
                sort_aux[ptr->team].lecq_place[i] = q++;
3422
            }
3423
            else {
3424
                if( teamclass != ptr->teamclass )
3425
                {
3426
                    k = 1;
3427
                    teamclass = ptr->teamclass;
3428
                }
3429
                sort_aux[ptr->team].lec_place[i] = k++;
3430
            }
3431
 
91 - 3432
        }
3433
    }
3434
 
3435
    /*
3436
    **  Write the place information back to disk for use in the displays
380 david 3437
    **  For backwards compatability we havn't saved all the data.
3438
    **      The NE specific data has not been saved, but is held in memory for report generation
91 - 3439
    */
3440
    for( i = config.min_team; i <= config.max_team; i++ )
3441
    {
3442
        if( sort_aux[i].team && valid_field( i ) && g_record( i, &team_buf ) )
3443
        {
3444
            for( k = 0; k <= config.num_legs; k++ )
3445
            {
3446
                team_buf.leg[k].l_place = sort_aux[i].l_place[k];
3447
                team_buf.leg[k].le_place = sort_aux[i].le_place[k];
3448
                team_buf.leg[k].lc_place = sort_aux[i].lc_place[k];
3449
                team_buf.leg[k].lec_place = sort_aux[i].lec_place[k];
3450
            }
3451
            put_team_record( i, &team_buf );
3452
        }
3453
    }
3454
}
3455
 
3456
/*========================================================================
3457
 *
3458
 *  Sort in memory buffer
3459
 *
3460
 *  Purpose:
3461
 *      This function is called to Sort in memory buffer
3462
 *
3463
 *  Parameters:
3464
 *      leg         Requested leg
3465
 *      mode        Defines the sort mode
380 david 3466
 *      withEq      Sort with equestrian data (True=Normal)
91 - 3467
 *
3468
 *  Returns:
3469
 *      Nothing
3470
 *
3471
 *========================================================================*/
3472
 
380 david 3473
void sort_team_data( int leg, int mode, bool withEq )
91 - 3474
{
3475
 
3476
    unsigned    j;
3477
    ty_s_data  *ptr;
3478
 
3479
    sort_leg = leg;                              /* Leg is global for the comparison function */
3480
    sort_mode = mode;                            /* Mode is global for compare function */
380 david 3481
    sort_withEquestrian = withEq;                /* Mode is global for compare function */
3482
    sort_afterEquestrianLeg = ( config.equestrian_leg && leg > config.equestrian_leg);
91 - 3483
 
3484
    qsort( ( char * ) sort_data, sort_num, sizeof( ty_s_data ), sort_comp );
3485
 
3486
    /*
3487
     * Insert "place data" into the sorted data
3488
     * This simply the index into the array of data - after its been
3489
     * sorted.
3490
     */
3491
    ptr = sort_data;
3492
    for( j = 1; j <= sort_num; j++, ptr++ )
380 david 3493
    {
91 - 3494
        ptr->place = j;
380 david 3495
    }
91 - 3496
 
380 david 3497
    //qDebug() << "sort_team_data: Leg:" << leg << ",M:" << mode << ",E:" << withEq;
3498
 
3499
 
91 - 3500
}
3501
 
3502
/*========================================================================
3503
 *
3504
 *  qsort comparison function
3505
 *
3506
 *  Purpose:
3507
 *      This function is called by qsort as a Sort comparison function
3508
 *
3509
 *  Parameters:
3510
 *      a       - 1st record
3511
 *      b       - 2nd record
3512
 *
3513
 *  Returns:
3514
 *      value to qsort
380 david 3515
 *      Return negative if 'a' is less than 'b',
3516
 *             zero if 'a' == 'b'
3517
 *             positive if 'a' > 'b'
91 - 3518
 *
3519
 *========================================================================*/
3520
 
3521
int sort_comp( const void * aa, const void * bb )
3522
{
3523
    const ty_s_data * a = (ty_s_data *)aa;
3524
    const ty_s_data * b = (ty_s_data *)bb;
3525
 
3526
 
3527
    int         i;                               /* One of those */
384 david 3528
    maraTime_t      ta, tb;                          /* Leg times */
91 - 3529
    int         na, nb;                          /* Number of valid legs */
384 david 3530
    maraTime_t      tta, ttb;                        /* Temp times */
91 - 3531
 
3532
    /*
3533
    **  Sorting on Team Number
3534
    */
3535
    if( sort_mode == S_TEAM )
3536
        return ( a->team - b->team );
3537
 
3538
    /*
3539
    **  Sorting on Class and Team Number
380 david 3540
    **  Sort NE entries as a class of there own
91 - 3541
    */
3542
    if( sort_mode == S_CLASS )
3543
    {
380 david 3544
        if ( a->isNeData || b->isNeData) {
3545
            if ( a->isNeData && b->isNeData)
3546
                return ( a->team - b->team );
3547
            else
3548
                return ( a->isNeData ? 1 : -1 );
3549
        }
3550
 
91 - 3551
        if( a->teamclass != b->teamclass )
3552
            return ( a->teamclass - b->teamclass );
3553
        else
3554
            return ( a->team - b->team );
3555
    }
3556
 
3557
    /*
3558
    **  Sorting within a class
3559
    **  First sort on the class
380 david 3560
    **      Always put the NE entries last as this will simplify processing
3561
    **      Compare NE entries against NE entries and EQ entries againstEQ entries
91 - 3562
    */
380 david 3563
    if( sort_mode == S_LEC || sort_mode == S_LC /*|| sort_mode == S_LC_NE*/ ) {  /* Sort within a class */
3564
        if ( a->isNeData || b->isNeData) {
3565
            if ( !(a->isNeData && b->isNeData ) )
3566
                return ( a->isNeData ? 1 : -1 );
3567
        } else {
3568
            if (a->teamclass != b->teamclass) {
3569
                return ( a->teamclass - b->teamclass );
227 - 3570
            }
91 - 3571
        }
3572
    }
3573
 
3574
    /*
3575
    **  Now we need to examine the times as we have sorted
3576
    **  on every thing else.
3577
    **
3578
    **  If one of the teams has bad_times, then that team is placed
3579
    **  lower in the sorting order. If both teams have bad times
3580
    **  then sort on team number. ie: Unplaced teams are sorted on
3581
    **  team number
3582
    **
3583
    **  If not sorting within a class (ie Overall), then Non_Equestrian
3584
    **  is better than a bad time. Places NE before disqualified
3585
    **
3586
    */
380 david 3587
    bool aNoSort = false;
3588
    bool bNoSort = false;
3589
 
3590
    if ( sort_leg != 0 && (sort_mode == S_LC || sort_mode == S_L ) ) {
3591
        aNoSort = a->flags.notInLP;
3592
        bNoSort = b->flags.notInLP;
91 - 3593
    }
380 david 3594
 
3595
    if ( sort_withEquestrian && (sort_mode == S_LC || sort_mode == S_L ) ) {
3596
        if (a->flags.vet_check)
3597
            aNoSort = true;
3598
        if (b->flags.vet_check)
3599
            bNoSort = true;
91 - 3600
    }
3601
 
380 david 3602
    if (a->flags.bad_times || a->flags.disqualified || !a->flags.valid)
3603
        aNoSort = true;
3604
 
3605
    if (b->flags.bad_times || b->flags.disqualified || !b->flags.valid)
3606
        bNoSort = true;
3607
 
3608
    if ( sort_mode == S_FIN || sort_mode == S_IFIN ) {
3609
        if (a->flags.vet_check)
3610
            aNoSort = true;
3611
        if (b->flags.vet_check)
3612
            bNoSort = true;
3613
    }
3614
 
3615
    if (sort_mode == S_IFIN ) {
3616
        if (a->isNeData)
3617
            aNoSort = true;
3618
        if (b->isNeData)
3619
            bNoSort = true;
3620
    }
3621
 
3622
    // On the Equestrian Leg, vetted out teams are treated as disqualified
3623
    if (sort_leg == config.equestrian_leg || (sort_afterEquestrianLeg && ( sort_mode == S_LE || sort_mode == S_LEC))) {
3624
        if (a->flags.vet_check)
3625
            aNoSort = true;
3626
        if (b->flags.vet_check)
3627
            bNoSort = true;
3628
    }
3629
 
3630
    if (aNoSort && bNoSort)
3631
        return ( a->team - b->team );
3632
 
3633
    if (aNoSort || bNoSort)
3634
        return ( aNoSort ? 1 : -1 );
3635
 
3636
    if( sort_withEquestrian && sort_mode != S_FIN && sort_mode != S_IFIN )
91 - 3637
    {
380 david 3638
        if( a->flags.non_equestrian && b->flags.non_equestrian )
91 - 3639
        {
380 david 3640
            /*
3641
            **  Both are non equestrian
3642
            **  Let the time sort operate ...
3643
            */
3644
            //return ( a->team - b->team );
91 - 3645
        }
380 david 3646
        else if( a->flags.non_equestrian || b->flags.non_equestrian )
3647
        {
3648
            /* One is equestrian */
3649
            /* Good times better than NE */
3650
            return ( a->flags.non_equestrian ? 1 : -1 );
3651
        }
91 - 3652
    }
3653
 
3654
    /*
3655
    **  Before we sort on times we must determine which time to
3656
    **  use. Finish time, Leg end times, Leg Elapsed times.
3657
    */
3658
 
3659
    switch ( sort_mode )
3660
    {
3661
      /*
3662
      **    Sort on finish times
3663
      */
3664
      case S_FIN:
3665
        ta = a->leg[sort_leg];
3666
        tb = b->leg[sort_leg];
3667
        break;
3668
 
3669
 
3670
      /*
3671
      **    Sort on accumulated leg times
3672
      */
3673
      case S_LE:
3674
      case S_LEC:
3675
        if( sort_leg )
3676
        {
3677
            /*
3678
            **  Calculate accumulated time up to the desired leg
3679
            **  If the two teams have a different number of valid
3680
            **  leg times then order by the team that has completed
3681
            **  more legs.
3682
            */
384 david 3683
            ta = tb = ( maraTime_t ) 0;
91 - 3684
            na = nb = 0;
3685
            for( i = 1; i <= sort_leg; i++ )
3686
            {
3687
                tta = a->lege[i];
3688
                ttb = b->lege[i];
3689
                if( tta > 0 )
3690
                {
3691
                    na++;
3692
                    ta += tta;
3693
                }
3694
                if( ttb > 0 )
3695
                {
3696
                    nb++;
3697
                    tb += ttb;
3698
                }
3699
            }
3700
            if( na != nb )
3701
                return ( nb - na );
3702
        }
3703
        else
3704
        {
3705
            ta = a->leg[sort_leg] - a->start;
3706
            tb = b->leg[sort_leg] - b->start;
3707
        }
3708
        break;
3709
 
3710
      /*
3711
      **    Sort on Elapsed times
3712
      */
227 - 3713
      case S_LC_NE:
91 - 3714
      case S_LC:
3715
      case S_L:
380 david 3716
      case S_IFIN:
91 - 3717
        ta = a->lege[sort_leg];
3718
        tb = b->lege[sort_leg];
3719
        break;
3720
 
3721
      /*
3722
      **    Just to be sure ...
3723
      */
3724
      default:
3725
        return ( 0 );
3726
    }
3727
 
380 david 3728
    if (sort_mode != S_LC_NE) {
3729
        /*
3730
         ** If we are ignore Equestrian config then place the Equestrian data last
3731
        */
3732
        if (sort_withEquestrian) {
3733
            if (a->isNeData != b->isNeData)
3734
                return ( a->isNeData ? 1 : -1 );
3735
        }
3736
 
3737
        if ( a->team == b->team) {
3738
            return ( a->isNeData ? 1 : -1 );
3739
        }
3740
    }
3741
 
91 - 3742
    /*
3743
    **  Finally. Compare the required team times
3744
    */
3745
    if( ta == tb )
3746
        return ( a->team - b->team );
3747
    if( ( ta > 0 ) && ( tb > 0 ) )
3748
        return ( ( int ) ( ta - tb ) );
3749
    return ( ( ta > 0 ) ? -1 : 1 );
3750
}
3751
 
3752
/*========================================================================
3753
 *
3754
 *  qsort comparison function - competitor names
3755
 *
3756
 *  Purpose:
3757
 *      This function is called by qsort as a Sort comparison function
3758
 *
3759
 *  Parameters:
3760
 *      a       - 1st record
3761
 *      b       - 2nd record
3762
 *
3763
 *  Returns:
3764
 *      value to qsort
3765
 *
3766
 *========================================================================*/
3767
 
3768
int sort_comp_cname( const void * aa, const void * bb )
3769
{
3770
    ty_s_namedata * a = (ty_s_namedata *)aa;
3771
    ty_s_namedata * b = (ty_s_namedata *)bb;
3772
 
3773
 
3774
    int         i;                               /* One of those */
3775
 
3776
    /*
3777
    **  Sort by name
3778
    */
3779
    i = strcmp ( a->name, b->name );
3780
    if ( i )
3781
        return ( i );
3782
    a->multi=1;
3783
    b->multi=1;
3784
 
3785
    /*
3786
    **  Sort by Leg
3787
    */
3788
    i = a->leg - b->leg;
3789
    if ( i )
3790
        return ( i );
3791
 
3792
    /*
3793
    **  Sorting on Team Number
3794
    */
3795
    return ( a->team - b->team );
3796
 
3797
}
3798
 
3799
/*========================================================================
3800
 *
3801
 *  load report data into memory
3802
 *
3803
 *  Purpose:
3804
 *      This routine will pull all the data into memory 
3805
 *      Not all the team data is loaded. Only that essential for the
3806
 *      operation of the sort routine is loaded
3807
 *
3808
 *  Parameters:
3809
 *      None
3810
 *
3811
 *  Returns:
3812
 *      TRUE - All is well
3813
 *
3814
 *========================================================================*/
3815
 
3816
bool load_report_data(void)
3817
{
3818
 
3819
    ty_s_data  *ptr;                             /* pointer to sort data type */
3820
    ty_s_data  *last;
380 david 3821
    unsigned    j;
91 - 3822
    unsigned    num;
170 - 3823
    team_type   team_buf;
91 - 3824
 
3825
    /*
3826
     * Fetch memory for the data store
3827
     */
3828
 
3829
    if( sort_data )
3830
        free( ( char * ) sort_data );
3831
    sort_data = 0;
3832
 
3833
    if( sort_aux )
3834
        free( ( char * ) sort_aux );
3835
    sort_aux = 0;
3836
    sort_num = 0;                                /* Counter of records in the array */
3837
 
3838
    /*
3839
    **  Allocate memory for the data structures
3840
    **  This will be free'd
3841
    */
3842
    num = config.max_team - config.min_team + 1 ;
3843
 
3844
    /*
3845
    **  Allow for non-equestrian teams - since some of the data is loaded twice
380 david 3846
    **  OLD: Take a guess that at most 1/2 the teams will be non-equestrian
3847
    **  NEW: Memory is cheap. Assume all teams could be non-equestrian
91 - 3848
    */
380 david 3849
    // OLD: num = num * 3 / 2;
3850
    num = num * 2;
91 - 3851
 
3852
    sort_data = ( ty_s_data * ) calloc ( num , sizeof( ty_s_data ) );
3853
    sort_aux = ( ty_s_aux * ) calloc( num + 1, sizeof( ty_s_aux ) );
3854
 
3855
    if( sort_data == 0 || sort_aux == 0 )
3856
    {
170 - 3857
        MainWindow::showMessage("Error in allocating memory");
91 - 3858
        return ( FALSE );
3859
    }
3860
 
3861
    /*
3862
    **  Load data into the memory based data structure
3863
    **  Only load data for valid-teams
3864
    **  Load essential data
3865
    **      Team Number, class and flags
3866
    **      Leg end and elapsed times
3867
    */
3868
    ptr = sort_data;
170 - 3869
    for( int team = config.min_team; team <= config.max_team; team++ )
91 - 3870
    {
3871
        if( valid_field( team ) )
3872
        {
3873
            g_record( team, &team_buf );
3874
            if( team_buf.flags.valid )
3875
            {
3876
                last = ptr;
3877
                ptr->team = team;
380 david 3878
                ptr->isNeData = false;
91 - 3879
                for( j = 0; j < MAX_LEGS + 1; j++ )
3880
                {
3881
                    ptr->lege[j] = team_buf.leg[j].elapsed;
3882
                    ptr->leg[j] = team_buf.leg[j].end;
3883
                }
3884
                ptr->start = team_buf.leg[0].start;
3885
                ptr->teamclass = team_buf.teamclass;
380 david 3886
                ptr->flags = team_buf.flags;
91 - 3887
 
380 david 3888
                // Add a few flags to simplify processing
3889
                //  Vetted out teams to be included inthe fastest calcs - except for Equestian Leg
3890
                //  Bodgey teams are not sorted by place - only after all places and then by team number
3891
                ptr->flags.notInLP = 0;
3892
                ptr->flags.notInFastest = 0;
3893
 
3894
                if ( ptr->flags.bad_times || ptr->flags.disqualified || !ptr->flags.valid) {
3895
                    ptr->flags.notInLP = 1;
3896
                    ptr->flags.notInFastest = 1;
3897
                }
3898
 
91 - 3899
                ptr++;
3900
                sort_num++;
3901
 
380 david 3902
 
91 - 3903
                /*
3904
                **  If non-equestrian support is enabled then
3905
                **  duplicate and modify data for the non-equestrian teams
3906
                **      - Change the class
3907
                **      - Modify the leg time
3908
                */
3909
#if 1
3910
                if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
3911
                {
3912
                    ptr->team = team;
380 david 3913
                    ptr->isNeData = true;
91 - 3914
                    ptr->lege[0] = 0;
3915
 
3916
                    for( j = 0; j < MAX_LEGS + 1; j++ )
3917
                    {
380 david 3918
                        if ( (short)j == config.equestrian_leg )
91 - 3919
                        {
3920
                            last->lege[j] = ptr->lege[j] = 0;
3921
                            if ( config.equestrian_leg > 1 )
3922
                                last->leg[j] = ptr->leg[j] = ptr->leg[j-1];
3923
                        }
3924
                        else
3925
                        {
3926
                            if ( j )
3927
                                last-> lege[j] = ptr->lege[j] = team_buf.leg[j].elapsed;
3928
                            last->leg[j] = ptr->leg[j] = team_buf.leg[j].end;
3929
                            ptr->lege[0] += ptr->lege[j] ;
3930
                            last->lege[0] = ptr->lege[0] ;
3931
                        }
3932
                    }
3933
 
3934
                    last->start = ptr->start = team_buf.leg[0].start;
380 david 3935
                    ptr->teamclass = team_buf.teamclass;
91 - 3936
                    ptr->flags = team_buf.flags;
3937
 
3938
                    ptr++;
3939
                    sort_num++;
3940
                }
3941
#endif
3942
            }
3943
        }
3944
    }
380 david 3945
 
3946
#if 0
3947
    // Debug zzz
3948
    qDebug() << "--- Start ---";
3949
    sort_team_data( 0, S_LC, 1 );
3950
 
3951
    for( j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
3952
    {
3953
        qDebug() << "T:" << ptr->team
3954
                << "Class:" << ptr->teamclass
3955
                << "NEData:" << (ptr->isNeData? 1:0)
3956
                << "iLP:" << (ptr->flags.notInLP ? 1:0)
3957
                << "iF:" << (ptr->flags.notInFastest ? 1:0)
3958
                << "Valid:" << (ptr->flags.valid ? 1:0)
3959
                << "NE:" << (ptr->flags.non_equestrian? 1:0)
3960
                << "B:" << (ptr->flags.bad_times ? 1 : 0 )
3961
                << "D:" << (ptr->flags.disqualified ? 1:0 )
3962
                << "V:" << (ptr->flags.vet_check ? 1:0 )
3963
                << "LE0:" << ptr->lege[0]
3964
                << "L0:" << ptr->leg[0]
3965
                << "LE1:" << ptr->lege[3]
3966
                ;
3967
    }
3968
    qDebug() << "-----------------------------------End---";
3969
#endif
3970
 
91 - 3971
    return ( TRUE );
3972
}
3973
 
3974
/*========================================================================
3975
 *
3976
 *  Generate all the stats
3977
 *
3978
 *  Purpose:
3979
 *      This function is called to Generate all the stats
3980
 *
3981
 *  Parameters:
3982
 *      None
3983
 *
3984
 *  Returns:
3985
 *      Nothing
3986
 *
3987
 *========================================================================*/
3988
 
3989
void gen_stats(void)
3990
{
3991
    ty_s_data  *ptr;
3992
    unsigned    i;
3993
    int         j;
3994
    int         k;
3995
 
3996
    /*
3997
     * Init all the stats 
3998
     */
3999
    for( i = 0; i <= MAX_LEGS; i++ )
4000
    {
4001
        for( j = 0; j <= MAX_CLASS; j++ )
4002
        {
4003
            stats.team[i][j] = 0;
4004
            stats.fast.team[i][j] = 0;
384 david 4005
            stats.fast.time[i][j] = ( maraTime_t ) - 1;
91 - 4006
            stats.average[i][j] = 0;
4007
        }
4008
    }
4009
 
4010
    for( i = 1, ptr = sort_data; i <= sort_num; i++, ptr++ )
4011
    {
4012
        /*
4013
        **  If there is any bad times in the team record, then none
4014
        **  of the data can be trusted.
4015
        **
4016
        **  If the team has been disqualified, then don't use any
4017
        **  of the data.
4018
        */
380 david 4019
        if (ptr->flags.notInFastest) {
91 - 4020
            continue;
380 david 4021
        }
91 - 4022
 
380 david 4023
        short teamClass = ptr->teamclass;
4024
 
4025
 
4026
        /*  Don't include the psuedo NE data items in the calculations (unless processing NE class)
4027
        **  They are duplicates of other data
4028
        */
4029
        if (j == config.nonequestrian_class) {
4030
            if ( !ptr->isNeData)
4031
                continue;
4032
        }
4033
 
4034
        if (ptr->isNeData)
4035
            teamClass = config.nonequestrian_class;
4036
 
4037
 
91 - 4038
        for( j = 0; j <= config.num_legs; j++ )
4039
        {
4040
            if( ptr->lege[j] <= 0 )              /* Ignore bad data */
4041
                continue;
4042
 
4043
            /*
380 david 4044
            ** If this is the Equestrian Leg and the team has been vetted out, then this team cannot
4045
            ** be included into the stats for this leg
4046
            **
4047
            ** If team has been vetted out then don't consider in fastest overall
4048
            */
4049
            if ( ptr->flags.vet_check && (j == config.equestrian_leg || j== 0)) {
4050
                continue;
4051
            }
4052
 
4053
            /* If this is the Equestrian Leg and the team is a Non-Equestrian Team, then don't include
4054
            ** it into the stats.
4055
            **
4056
            ** If this is a NE team then don't consider it to be fastest overall
4057
            */
4058
            if ( !ptr->isNeData && ptr->flags.non_equestrian &&  (j == config.equestrian_leg || j== 0)) {
4059
                continue;
4060
            }
4061
 
4062
            /*
91 - 4063
            **  Determine fastest team : overall
4064
            **  Ignore the non-equestrian data as this is in the list twice
4065
            */
4066
            if( ( ptr->lege[j] < stats.fast.time[j][0] )
4067
                || ( stats.fast.time[j][0] < 0 ) )
4068
            {
380 david 4069
                if ( !ptr->isNeData )
91 - 4070
                {
4071
                    stats.fast.team[j][0] = ptr->team;
4072
                    stats.fast.time[j][0] = ptr->lege[j];
4073
                }
4074
            }
4075
 
4076
            /*
4077
            **  Determine fastest team : within a class
4078
            */
380 david 4079
            if( ( ptr->lege[j] < stats.fast.time[j][teamClass] )
4080
                || stats.fast.time[j][teamClass] < 0 )
91 - 4081
            {
380 david 4082
                stats.fast.team[j][teamClass] = ptr->team;
4083
                stats.fast.time[j][teamClass] = ptr->lege[j];
91 - 4084
            }
4085
 
4086
            /*
4087
            **  Sum the end times : overall
4088
            */
380 david 4089
            if ( !ptr->isNeData )
91 - 4090
            {
4091
                stats.average[j][0] += ptr->lege[j];
4092
                stats.team[j][0]++;
4093
            }
4094
 
4095
 
4096
            /*
4097
            **  Sum the end times : within a class
4098
            */
380 david 4099
            stats.average[j][teamClass] += ptr->lege[j];
4100
            stats.team[j][teamClass]++;
91 - 4101
        }
4102
    }
4103
 
4104
    /*
380 david 4105
     * Calculate the averages 
4106
     *      Can ignore that fact that some categories don't exists
4107
     *      The results will be -1
91 - 4108
     */
4109
    for( k = 0; k <= config.num_legs; k++ )
4110
    {
4111
        for( j = 0; j <= config.num_class; j++ )
4112
        {
4113
            if( stats.team[k][j] )
4114
                stats.average[k][j] /= stats.team[k][j];
4115
            else
384 david 4116
                stats.average[k][j] = ( maraTime_t ) - 1;
91 - 4117
        }
4118
    }
4119
}
4120
 
4121
/********************************* EOF ***********************************/