Subversion Repositories svn1

Rev

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

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