Subversion Repositories svn1-original

Rev

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

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