Subversion Repositories svn1-original

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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