Subversion Repositories svn1-original

Rev

Rev 354 | Rev 356 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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