Subversion Repositories svn1-original

Rev

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

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