Subversion Repositories svn1

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
            */
68 - 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
 
68 - 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
 
68 - 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
 
68 - 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
 
68 - 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 ) );
68 - 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
 
68 - 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 ) );
68 - 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
 
68 - 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;
1276
    ty_s_data  *ptr;
1277
    int         last_class;
1278
 
27 - 1279
    if( !open_printer( "", "awards", 80, report_html, "Prizes and Awards" ) )
1 root 1280
        return;
1281
 
1282
    /*
1283
    **  Generate an index for this page
1284
    */
1285
    print( "\n");
61 - 1286
    if ( report_html )
1287
    {
1288
        print( "<hr>" );
68 - 1289
        print( "<A NAME=\"%s\"></A>",url_encode("INDEX"));
61 - 1290
    }
1 root 1291
    print( "Award Categories");
1292
 
1293
    for( j = 1; j <= config.num_class; j++ )
1294
    {
1295
        /*
1296
        **  Header for the class
1297
        */
1298
        if ( config.class_winners[j-1] <= 0 )
1299
            continue;
1300
 
1301
        print( "\n");
61 - 1302
        print( "    ");
68 - 1303
        if ( report_html ) print( "<A HREF=\"#%s\">",url_encode(config.team_class[j-1].full_name));
1 root 1304
        print( "%s",  config.team_class[j-1].full_name );
61 - 1305
        if ( report_html ) print( "</A>" );
1 root 1306
    }
1307
 
1308
    /*
1309
    **  Manual entries
1310
    */
1311
    print( "\n");
61 - 1312
    print( "    ");
68 - 1313
    if ( report_html ) print( "<A HREF=\"#%s\">",url_encode("Hall Of Fame"));
1 root 1314
    print( "%s",  "Hall Of Fame" );
61 - 1315
    if ( report_html ) print( "</A>" );
1 root 1316
 
1317
    print( "\n");
61 - 1318
    print( "    ");
68 - 1319
    if ( report_html ) print( "<A HREF=\"#%s\">",url_encode("FASTEST"));
1 root 1320
    print( "%s",  "FASTEST" );
61 - 1321
    if ( report_html ) print( "</A>" );
1 root 1322
 
1323
    /*
1324
    **  Sort the data by class
1325
    */
1326
    sort_team_data( 0, S_LC );      /* Generate class placement data */
1327
    last_class = -1;                /* Invalid class to start with */
1328
 
1329
    /*
1330
    **  Process each category
1331
    */
1332
    for( j = 1; ; j++ )
1333
    {
1334
        /*
1335
        **  Tail for previous entry
1336
        */
1337
        if ( j != 1 )
68 - 1338
            if ( report_html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1 root 1339
 
1340
        if ( j > config.num_class  )
1341
            break;
1342
 
1343
        /*
1344
        **  Header for the class
1345
        */
1346
        print( "\n");
61 - 1347
        if ( report_html )
1348
        {
1349
            print( "<hr>" );
68 - 1350
            print( "<A name=\"%s\"></A>",url_encode(config.team_class[j-1].full_name));
61 - 1351
        }
1352
        else
1353
        {
1354
            print( "\n");
1355
        }
1356
        print( "Category: ");
68 - 1357
        if ( report_html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[j - 1].abr ,"html")));
1 root 1358
        print( "%s",  config.team_class[j-1].full_name );
61 - 1359
        if ( report_html ) print( "</A>" );
1 root 1360
 
61 - 1361
        if ( config.class_winners[j-1] <= 0 )
1 root 1362
        {
1363
            print( "\n");
1364
            print( "No winners awarded" );
1365
            continue;
1366
        }
1367
 
1368
        /*
1369
        **  Enties for 'n' the best teams as configured
1370
        */
1371
        windex = 0;                     /* Winners done */
1372
        for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1373
        {
1374
            if ( ptr->class != j )
1375
            {
1376
                continue;
1377
            }
1378
 
1379
            /*
1380
            **  Now read in the team record
1381
            */
1382
            if( valid_field( ptr->team ) && g_record( ptr->team, &team_buf ) )
1383
            {
1384
                windex++;
1385
 
1386
                /*
1387
                **  If printing an HTML report then we need to mark
1388
                **  the entry with a reference so that we can link to it
1389
                */
1390
                print( "\n");
61 - 1391
                if ( report_html )
1392
                {
1393
                    print( "<A NAME=\"Team_%04d\">",team_buf.numb );
1394
                    print( "</A>" );
1395
                }
1 root 1396
 
1397
                /*
1398
                **  Basic information
1399
                **      - Team number - with Xref back to full result
1400
                **      - Full team name
1401
                **      - Full categoray name
1402
                */
1403
                print( "%s", placing(windex) );
1404
 
61 - 1405
                print( "  Team Name: ");
68 - 1406
                if ( report_html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1 root 1407
                print( "%-*s ",     MAX_TM_NAME, team_buf.name );
61 - 1408
                    if ( report_html ) print( "</A>" );
1 root 1409
 
61 - 1410
                print( "  Number: ");
68 - 1411
                if ( report_html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
1 root 1412
                print( "%4d",       team_buf.numb );
61 - 1413
                if ( report_html ) print( "</A>" );
1 root 1414
 
1415
 
1416
                for( k = 0; k < MAX_MEMB; k++ )
1417
                {
1418
 
1419
                    /*
1420
                    **  Skip equestrian leg in the non-equestion display
1421
                    */
1422
                    if ( k + 1 == config.equestrian_leg && ptr->class == config.nonequestrian_class)
1423
                        continue;
1424
 
1425
                    print( "\n");
1426
                    print( "    ");
1427
                    print( "%-*s", MAX_PERSON_NAME, config.leg_name[k] ? config.leg_name[k] : "Competitor"  );
1428
                    print( " %-*s", MAX_PERSON_NAME, team_buf.members[k].name );
1429
 
1430
                    print( "  %-8s", time_a( team_buf.leg[k+1].elapsed ) );
1431
                }
1432
 
1433
                print( "\n");
1434
                print( "    ");
1435
                print_bold( TRUE );
1436
                print( "%-*s %-*s  %-8s", MAX_PERSON_NAME , "Total" ,MAX_PERSON_NAME, "",time_a( team_buf.leg[0].elapsed ) );
1437
                print_bold( FALSE );
1438
                print( "\n" );
1439
            }
1440
 
1441
 
1442
            /*
1443
            **  More to do
1444
            */
1445
            if ( windex >= config.class_winners[j-1] )
1446
            {
1447
                break;
1448
            }
1449
        }
1450
    }
1451
 
1452
    /*
1453
    **  Generate the Hall of Fame information
1454
    */
1455
    print( "\n");
61 - 1456
    if ( report_html )
1457
    {
1458
        print( "<hr>" );
68 - 1459
        print( "<A name=\"%s\"></A>",url_encode("Hall Of Fame"));
61 - 1460
    }
1 root 1461
    print( "%s",  "Hall of Fame" );
1462
 
1463
    if ( config.num_fame  )
1464
    {
1465
        for( i = 1; i <= config.num_fame; i++ )
1466
        {
61 - 1467
            print( "\n");
1468
            print( "    %-*s", MAX_PERSON_NAME, config.hall_fame[i-1] );
1 root 1469
        }
1470
    }
1471
    else
1472
    {
1473
        printf( "\n    There are no new stars for the Hall of Fame");
1474
    }
68 - 1475
    if ( report_html ) print( "\n");
1476
    if ( report_html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
1 root 1477
 
1478
    /*
1479
    **  Generate the FASTEST information
1480
    */
61 - 1481
    print( "\n" );
1482
    if ( report_html )
1483
    {
1484
        print( "<hr>" );
68 - 1485
        print( "<A name=\"%s\"></A>",url_encode("FASTEST"));
61 - 1486
    }
1487
    else
1488
    {
1489
        print( "\n" );
1490
    }
1 root 1491
    print( "%s",  "FASTEST" );
1492
 
1493
    /*
1494
    **  Sort the data and then generate the stats - again
1495
    */
1496
    do_big_sort();
1497
    gen_stats();
1498
 
1499
    for( i = 1; i <= config.num_legs; i++ )
1500
    {
1501
        g_record( stats.fast.team[i][0], &team_buf );
1502
 
1503
        print( "\n");
1504
        print( "    %-13s ", config.leg_name[i - 1] );
61 - 1505
        print( "  Name: ");
68 - 1506
        if ( report_html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), team_buf.numb );
1 root 1507
        print( "%-*s", MAX_PERSON_NAME, team_buf.members[i-1].name );
61 - 1508
        if ( report_html ) print( "</A>" );
1509
        print( "  Team :");
68 - 1510
        if ( report_html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "finish" ,"html")), team_buf.numb );
27 - 1511
        print( "%4d" , stats.fast.team[i][0] );
61 - 1512
        if ( report_html ) print( "</A> " );
27 - 1513
        print( "Time:%s ", time_a( stats.fast.time[i][0] ) );
1 root 1514
 
1515
    }
1516
 
68 - 1517
    if ( report_html ) print( "\n");
1518
    if ( report_html ) print( "<A HREF=\"#%s\">Awards Index</A>",url_encode("INDEX"));
61 - 1519
    print( "\n");
1 root 1520
    close_printer();
1521
}
1522
 
1523
/*========================================================================
1524
 *
1525
 *  pri_master_index
1526
 *
1527
 *  Purpose:
1528
 *      This function is called to create an HTML page that references all
1529
 *      the other pages that have been generated
1530
 *
1531
 *      Assume that they are in the same directory
1532
 *
1533
 *  Parameters:
1534
 *      None
1535
 *
1536
 *  Returns:
1537
 *      Nothing
1538
 *
1539
 *========================================================================*/
1540
void pri_master_index_entry(char *name, char *text)
1541
{
1542
    print( "<tr><td>");
68 - 1543
    print ("<A HREF=\"%s\">%s</A>\n", url_encode(p_filename(filebase, name ,"html")), text );
1 root 1544
}
1545
 
1546
 
1547
void pri_master_index(void)
1548
{
1549
    int j;
1550
 
1551
    report_html = 1;
1552
    if( !open_printer( "", "index", 132, report_html, "Master Index" ) )
1553
        return;
1554
 
1555
    /*
1556
    **  Names
1557
    */
1558
    print( "<TABLE border=0 align=center>" );
18 david 1559
    pri_master_index_entry( "name", "Team list" );
1560
    pri_master_index_entry( "competitor", "Competitor list" );
1 root 1561
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
1562
    pri_master_index_entry( "awards", "Prizes and Awards" );
1563
    print( "<tr><td>\n" );
1564
 
1565
    print( "\n" );
1566
    for( j = 1; j <= config.num_class; j++ )
1567
    {
1568
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
1569
    }
32 - 1570
 
1 root 1571
    print( "</TABLE>" );
1572
 
1573
    close_printer();
32 - 1574
 
1575
    /*
1576
    **  A small page to hold the Leg End displays
1577
    */
1578
 
1579
    if( !open_printer( "", "legindex", 132, report_html, "Master Index with trace data" ) )
1580
        return;
1581
 
1582
    /*
1583
    **  Names
1584
    */
1585
    print( "<TABLE border=0 align=center>" );
1586
#if 1
1587
    pri_master_index_entry( "name", "Team list" );
1588
    pri_master_index_entry( "competitor", "Competitor list" );
1589
    pri_master_index_entry( "finish", "Finishing Order for all Teams" );
1590
    pri_master_index_entry( "awards", "Prizes and Awards" );
1591
    print( "<tr><td>\n" );
1592
 
1593
    print( "\n" );
1594
    for( j = 1; j <= config.num_class; j++ )
1595
    {
1596
        pri_master_index_entry( config.team_class[j - 1].abr, tprintf("Category Results for: %s", config.team_class[j-1].full_name) );
1597
    }
1598
#endif
1599
    print( "<tr><td>\n" );
1600
 
1601
    print( "\n" );
1602
    for ( leg = 1; leg <= config.num_legs; leg ++ )
1603
    {
1604
        pri_master_index_entry( tprintf("lg%1.1d", leg), tprintf("Leg End Results for: %d", leg) );    
1605
    }
1606
 
1607
    print( "<tr><td>\n" );
1608
    print( "\n" );
1609
 
1610
    for ( leg = 1; leg <= config.num_legs; leg ++ )
1611
    {
1612
        pri_master_index_entry( tprintf("le%1.1d", leg), tprintf("Leg Elapsed Time Results for: %d", leg) );
1613
    }    
1614
 
1615
 
1616
    print( "</TABLE>" );
1617
 
1618
    close_printer();
1619
 
1 root 1620
}
1621
 
1622
 
1623
/*========================================================================
1624
 *
1625
 *  Print interim results
1626
 *
1627
 *  Purpose:
1628
 *      This function is called to Print interim results
1629
 *
1630
 *  Parameters:
1631
 *      None
1632
 *
1633
 *  Returns:
1634
 *      Nothing
1635
 *
1636
 *========================================================================*/
1637
 
1638
void pri_interim(void)
1639
{
1640
    ty_s_data  *ptr;
1641
    unsigned    i;
1642
    int         j, last_class;
29 - 1643
    char        suppress_classes = FALSE;                /* Boolean. Printout class files too */
1 root 1644
    char       *report_title;
1645
 
29 - 1646
    if ( ! report_all )
1647
    {
1648
        cur( 0, 5 );
1649
        printf( "Generate interim result printouts\n" );
1650
        ck_data( -1, C_DISQUAL );                    /* Check the data - dummy check */
1651
        suppress_classes = getyes( "Do you want to suppress class printouts" );
1652
        printf( "\nSorting the data\n" );
1653
    }
1 root 1654
    do_big_sort();                             /* Sort on every thing */
1655
    gen_stats();                               /* Generate the stats too */
1656
 
1657
    printf( "\nGenerating the printed output\n" );
1658
 
1659
    /*
1660
     * Now print the data on the printer 
1661
     */
1662
 
1663
    if( !open_printer( "", "int", 132, report_html, "Interim Results" ) )
1664
        return;
1665
 
1666
    /*
1667
     * Print out the data 
1668
     */
1669
    print_class_header( -1, FALSE );                     /* Print the header */
1670
 
1671
    ptr = sort_data;
1672
    sort_team_data( 0, S_TEAM );                   /* Re-sort on team number */
1673
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1674
    {
1675
        if ( ptr->class == config.nonequestrian_class )
1676
            continue;
1677
 
1678
        g_record( ptr->team, &team_buf );
1679
 
1680
        print( "%4d %4.4s %-*s",
1681
               team_buf.numb,
1682
               pi_place( team_buf.leg[config.num_legs].le_place,
1683
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ),
1684
               3,
1685
               team_buf.class ==
1686
 
1687
        for( j = 1; j <= config.num_legs; j++ )
1688
        {
1689
            print( "  %-8s %4.4s %4.4s",
1690
                   time_fa( team_buf.leg[j].elapsed,
1691
                            ptr->flags.disqualified ),
1692
                   pi_place( team_buf.leg[j].l_place, ptr->flags.disqualified,
1693
                             team_buf.leg[j].elapsed ),
1694
                   pi_place( team_buf.leg[j].le_place,
1695
                             ptr->flags.disqualified,
1696
                             team_buf.leg[j].elapsed ) );
1697
        }
1698
        print( "  %-8s %4.4s\n",
1699
               time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ),
1700
               pi_place( team_buf.leg[config.num_legs].lec_place,
1701
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ) );
1702
    }
1703
 
1704
    print_class_stats( -1, FALSE );             /* Print statistics */
18 david 1705
    print_legend(-1, 1);                        /* Print the legend */
1 root 1706
    close_printer();                            /* Close the printer */
1707
 
1708
 
1709
    /*
1710
     * Now produce a breakdown on a class by class basis 
1711
     * Now print out the class placement information
1712
     */
1713
 
1714
    if( suppress_classes )
1715
    {
1716
        printf( "WARNING: Class printouts suppressed\n" );
1717
        return;
1718
    }
1719
 
1720
    sort_team_data( 0, S_CLASS );              /* Generate class placement data */
1721
    last_class = -1;                             /* Invalid class to start with */
1722
 
1723
    for( ptr = sort_data, i = 0; i < sort_num; i++, ptr++ )
1724
    {
1725
        /*
1726
        **  Detect a change in the "class"
1727
        **  All data is within the one array of data
1728
        **  Use the in-memory class as this MAY differ from that stored
1729
        **  The non-equestrian class does this.
1730
        */
1731
        if( last_class != ptr->class )
1732
        {
1733
            if( last_class >= 0 )
1734
            {
1735
                print_class_stats( last_class, TRUE );
18 david 1736
                print_legend(last_class, 1);
1 root 1737
                close_printer();
1738
            }
1739
 
1740
            report_title = tprintf( "Interim Category results for : %-*s", LEN_CLASS_NAME, team_buf.class == 0 ? "" : config.team_class[ptr->class - 1].full_name );
1741
 
1742
            if( !open_printer( "", tprintf( "i%2s", config.team_class[ptr->class - 1].abr ), 132, report_html, report_title ) )
1743
                continue;
1744
            print_class_header( last_class = ptr->class, FALSE );
1745
        }
1746
 
1747
        /*
1748
        **  Now read in the team record
1749
        */
1750
        g_record( ptr->team, &team_buf );
1751
        print( "%4d %4.4s %-*s",
1752
               team_buf.numb,
1753
               pi_place( team_buf.leg[config.num_legs].lec_place,
1754
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ),
1755
               3,
1756
               team_buf.class ==
1757
 
1758
        for( j = 1; j <= config.num_legs; j++ )
1759
        {
1760
            print( "  %-8s %4.4s %4.4s",
1761
                   time_fa( team_buf.leg[j].elapsed,
1762
                            ptr->flags.disqualified ),
1763
                   pi_place( team_buf.leg[j].lc_place,
1764
                             ptr->flags.disqualified,
1765
                             team_buf.leg[j].elapsed ),
1766
                   pi_place( team_buf.leg[j].lec_place,
1767
                             ptr->flags.disqualified,
1768
                             team_buf.leg[j].elapsed ) );
1769
        }
1770
        print( "  %-8s %4.4s\n",
1771
               time_fa( team_buf.leg[0].elapsed, ptr->flags.disqualified ),
1772
               pi_place( team_buf.leg[config.num_legs].le_place,
1773
                         ptr->flags.disqualified, team_buf.leg[0].elapsed ) );
1774
    }
1775
 
1776
    print_class_stats( last_class, FALSE );
18 david 1777
    print_legend(last_class, 1);
1 root 1778
    close_printer();
1779
 
1780
}
1781
 
61 - 1782
/*----------------------------------------------------------------------------
1783
** FUNCTION           : pri_csv_data
1784
**
1785
** DESCRIPTION        : Generate a CSV file of all the report data
1786
**                      It can then be used to play with the data externally
1787
**
1788
**
1789
** INPUTS             : None
1790
**
1791
** RETURNS            : Yes it does
1792
**
1793
----------------------------------------------------------------------------*/
1794
 
1795
 
1796
void pri_csv_data ( void )
1797
{
1798
    int i;
1799
    int j;
1800
    int age_sum;
1801
 
1802
    /*
1803
    **  Sort on every thing
1804
    **  Then generate all the stats too
1805
    */
1806
    printf( "\nSorting the data\n" );
1807
    do_big_sort();
1808
    gen_stats();
1809
 
1810
    /*
1811
     * Now print the data on the printer 
1812
     */
1813
 
1814
    if( !open_printer( "full_data", "csv", 2000, FALSE, NULL ) )
1815
        return;
1816
 
1817
    printf( "\nGenerating the printed output\n" );
1818
 
1819
    /*
1820
    **  Print headings
1821
    */
1822
    csv_print( "%s",   "Team Number" );
1823
    csv_print( "%s",   "Team Name" );
1824
 
1825
    csv_print( "%s",    "Class Full");
1826
    csv_print( "%s",    "Class Abr");
1827
    csv_print( "%s",    "Class Start Time");
1828
    csv_print( "%s",    "Class Start Time Number");
1829
 
1830
    csv_print( "%s",    "Team Country");
1831
 
1832
    for( j = 1; j <= config.num_legs; j++ )
1833
    {
1834
        csv_print( "%s", "Leg Number" );
1835
        csv_print( "%s", "Leg Name");
1836
        csv_print( "%s", "Competitor Name");
1837
        csv_print( "%s", "Sex" );
1838
        csv_print( "%s", "Age");
1839
        csv_print( "%s", "Start Time");
1840
        csv_print( "%s", "Start Time Number");
1841
        csv_print( "%s", "End Time" );
1842
        csv_print( "%s", "End Time Number" );
1843
        csv_print( "%s", "Elapsed Time");
1844
        csv_print( "%s", "Elapsed Time Number");
1845
        csv_print( "%s", "Leg Place");
1846
        csv_print( "%s", "Leg End Place");
1847
        csv_print( "%s", "Leg Class Place");
1848
        csv_print( "%s", "Leg End Class Place");
1849
        csv_print( "%s", "Manual");
1850
    }
1851
 
1852
    j = 0;
1853
    csv_print( "%s", "Team Start Time");
1854
    csv_print( "%s", "Team Start Time Number");
1855
    csv_print( "%s", "Team End Time");
1856
    csv_print( "%s", "Team End Time Number");
1857
    csv_print( "%s", "Team Elapsed Time");
1858
    csv_print( "%s", "Team Elapsed Time Number");
1859
//            csv_print( "%s", team_buf.leg[j].l_place );
1860
    csv_print( "%s", "Team Leg End Place");
1861
//            csv_print( "%s", team_buf.leg[j].lc_place );
1862
    csv_print( "%s", "Team Leg Class Place");
1863
//            csv_print( "%s", team_buf.leg[j].manual );
1864
 
1865
    csv_print( "%s", "Total Team Age");
1866
    csv_print( "%s", "Flag:valid Team");
1867
    csv_print( "%s", "Flag:bad_times" );
1868
    csv_print( "%s", "Flag:disqualified" );
1869
    csv_print( "%s", "Flag:non_equestrian" );
1870
    csv_print("\n");
1871
 
1872
 
1873
    for( i = config.min_team; i <= config.max_team; i++ )
1874
    {
1875
        if( valid_field( i ) && g_record( i, &team_buf ) )
1876
        {
1877
            /*
1878
            **  Basic information
1879
            **      - Team number - with Xref back to full result
1880
            **      - Full team name
1881
            **      - Full categoray name - with Xref to category results
1882
            **      - Country name
1883
            */
1884
            csv_print( "%d",   team_buf.numb );
1885
            csv_print( "%s",   team_buf.name );
1886
 
1887
            csv_print( "%s",    team_buf.class == 0 ? "" : config.team_class[team_buf.class - 1].full_name );
1888
            csv_print( "%s",    team_buf.class == 0 ? "" : config.team_class[team_buf.class - 1].abr );
1889
            csv_print( "%s",    time_a (team_buf.class == 0 ? 0 : config.team_class[team_buf.class - 1].start ));
1890
            csv_print( "%d",    team_buf.class == 0 ? 0 : config.team_class[team_buf.class - 1].start );
1891
 
1892
            csv_print( "%s",    config.num_countries == 0
1893
                                || team_buf.country ==
1894
 
1895
 
1896
            age_sum = 0;
1897
            for( j = 1; j <= config.num_legs; j++ )
1898
            {
1899
                csv_print( "%d", j );
1900
                csv_print( "%s", config.leg_name[j - 1] );
1901
                csv_print( "%s", team_buf.members[j-1].name );
1902
                csv_print( "%s", ( team_buf.members[j-1].sex == male ) ? "Male" : "Female" );
1903
                csv_print( "%d", team_buf.members[j-1].age );
1904
                if ( age_sum >= 0 )
1905
                {
1906
                    ushort age = team_buf.members[j-1].age;
1907
                    if ( age > 0 && age < 255 )
1908
                    {
1909
                        age_sum += age;
1910
                    }
1911
                    else
1912
                    {
1913
                        age_sum = -1;
1914
                    }
1915
                }
1916
 
1917
 
1918
                csv_print( "%s", time_a(team_buf.leg[j].start ));
1919
                csv_print( "%d", team_buf.leg[j].start );
1920
                csv_print( "%s", time_a(team_buf.leg[j].end ));
1921
                csv_print( "%d", team_buf.leg[j].end );
1922
                csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
1923
                csv_print( "%d", team_buf.leg[j].elapsed );
1924
                csv_print( "%d", team_buf.leg[j].l_place );
1925
                csv_print( "%d", team_buf.leg[j].le_place );
1926
                csv_print( "%d", team_buf.leg[j].lc_place );
1927
                csv_print( "%d", team_buf.leg[j].lec_place );
1928
                csv_print( "%d", team_buf.leg[j].manual );
1929
            }
1930
 
1931
            j = 0;
1932
            csv_print( "%s", time_a(team_buf.leg[j].start ));
1933
            csv_print( "%d", team_buf.leg[j].start );
1934
            csv_print( "%s", time_a(team_buf.leg[j].end ));
1935
            csv_print( "%d", team_buf.leg[j].end );
1936
            csv_print( "%s", time_a(team_buf.leg[j].elapsed ));
1937
            csv_print( "%d", team_buf.leg[j].elapsed );
1938
//            csv_print( "%d", team_buf.leg[j].l_place );
1939
            csv_print( "%d", team_buf.leg[j].le_place );
1940
//            csv_print( "%d", team_buf.leg[j].lc_place );
1941
            csv_print( "%d", team_buf.leg[j].lec_place );
1942
//            csv_print( "%d", team_buf.leg[j].manual );
1943
 
1944
            csv_print( "%d", age_sum );
1945
            csv_print( "%d", team_buf.flags.valid );
1946
            csv_print( "%d", team_buf.flags.bad_times );
1947
            csv_print( "%d", team_buf.flags.disqualified );
1948
            csv_print( "%d", team_buf.flags.non_equestrian );
1949
 
1950
//How about class placings
1951
 
1952
 
1953
            csv_print( "\n" );
1954
        }
1955
    }
1956
 
1957
    close_printer();
1958
}
1959
 
1960
 
1 root 1961
/*========================================================================
1962
 *
29 - 1963
 *  Print all reports at once
61 - 1964
 *  Its all so fast, these days ...
29 - 1965
 *
1966
 *  Purpose:
1967
 *      This function is called to print all reports at once
1968
 *
1969
 *  Parameters:
1970
 *      None
1971
 *
1972
 *  Returns:
1973
 *      Nothing
1974
 *
1975
 *========================================================================*/
1976
 
1977
void pri_all_reports ( void )
1978
{
1979
    int leg;
1980
    report_all = TRUE;
1981
 
1982
    pri_team();
1983
 
1984
    for ( leg = 1; leg <= config.num_legs; leg ++ )
1985
    {
1986
        pri_leg_body ( leg );
1987
        pri_eleg_body ( leg );
32 - 1988
 
1989
        report_html = TRUE;
1990
 
1991
        pri_leg_body ( leg );
1992
        pri_eleg_body ( leg );        
1993
 
1994
        report_html = FALSE;
29 - 1995
    }
1996
 
1997
    pri_final();
1998
    pri_final_html();
61 - 1999
    pri_csv_data();
29 - 2000
    pri_summary();
61 - 2001
    pri_awards_html();
29 - 2002
    pri_awards();
2003
    pri_master_index();
2004
 
2005
    report_all = FALSE;
2006
}
2007
 
2008
 
2009
/*========================================================================
2010
 *
1 root 2011
 *  Print a class header
2012
 *
2013
 *  Purpose:
2014
 *      This function is called to print a class header
2015
 *
2016
 *  Parameters:
2017
 *      class           Name of this class
2018
 *      final           False - prelim results
2019
 *
2020
 *  Returns:
2021
 *      Nothing
2022
 *
2023
 *========================================================================*/
2024
 
2025
void print_class_header( int class, int final )
2026
{
2027
    int         j;
2028
 
2029
    /*
2030
    **  Give a clear indication that the report is preliminary
2031
    */
2032
 
2033
    if( !final )
2034
        print( "PRELIMINARY RESULTS ONLY\n\n" );
2035
 
2036
    /*
2037
    **  Now printout the column headings
2038
    **  This is a two line display
2039
    **
2040
    **  Line-1  Leg names
2041
    */
2042
    print( "%-*s %-*s %-*s", 4, "", 4, "", 3, "" );
2043
    for( j = 1; j <= config.num_legs; j++ )
2044
    {
2045
        print_bold( TRUE );
2046
        print( "  %-*s", 18, config.leg_name[j - 1] );
2047
        print_bold( FALSE );
2048
    }
2049
    print( "  %-8s %-4s\n", "Total", ( class < 0 ) ? "Cat" : "Fin" );
2050
 
2051
 
2052
    /*
2053
    **  Line-2  Details
2054
    */
2055
    print_underline( TRUE );
2056
    print( "%-*s %*s %-*s", 4, final ? "Plce" : "Team",
2057
                            4, final ? "Team" : "Plce",
2058
                            3, "Cat" );
2059
 
2060
    for( j = 1; j <= config.num_legs; j++ )
2061
        print( "  %-8s %-4s %-4s", "Time", " LP", " EP" );
2062
 
2063
    print( "  %-8s %-4s\n", "Time", "Plce" );
2064
    print_underline( FALSE );
2065
}
2066
 
2067
/*========================================================================
2068
 *
2069
 *  Generate the class stats
2070
 *
2071
 *  Purpose:
2072
 *      This function is called to Generate the class stats
2073
 *
2074
 *  Parameters:
2075
 *      c           Class to print
2076
 *      final       TRUE: Final data
2077
 *                  FALSE: Interim data
2078
 *
2079
 *  Returns:
2080
 *      Nothing
2081
 *
2082
 *========================================================================*/
2083
 
2084
void print_class_stats( int c, int final )
2085
{
2086
    int         i, j;
2087
    char        *title;
2088
 
2089
    if( c < 0 )
2090
    {
2091
        title = "Event";
2092
        c = 0;
2093
    }
2094
    else
2095
    {
2096
        title = "Category";
2097
    }
2098
 
2099
    print( "\n" );
2100
    if ( report_html ) print_underline(TRUE);
2101
    print( "%s statistics", title );
2102
    if ( report_html ) print_underline(FALSE);
2103
    print( "\n" );
2104
 
2105
 
2106
    /*
2107
    **  Print the names of the different legs
2108
    */
2109
    print( "%-*s       ", LEN_CLASS_NAME, "" );
2110
    for( i = 1; i <= config.num_legs; i++ )
2111
    {
2112
        print_bold( TRUE );
2113
        print( "%-13s  ", config.leg_name[i - 1] );
2114
        print_bold( FALSE );
2115
    }
2116
    print( "%-13s  \n", final ? "Total" : "" );
2117
 
2118
    /*
2119
    **  Print the fastest teams for each leg and overall
2120
    **  Add cross references to the team names for the fastest teams
2121
    */
2122
    print( "%*s : ", LEN_CLASS_NAME, "Fastest" );
2123
    for( i = 0; i <= config.num_legs; i++ )
2124
    {
2125
        j = i + 1;
2126
        if( i >= config.num_legs )
2127
        {
2128
            if( final )
2129
                j = 0;                           /* Leg-0 last */
2130
            else
2131
                break;
2132
        }
2133
 
68 - 2134
        if ( report_html ) print( "<A HREF=\"%s#Team_%04d\">", url_encode(p_filename(filebase, "name" ,"html")), stats.fast.team[j][c] );
1 root 2135
        print( "%4d",  stats.fast.team[j][c] );
2136
        if ( report_html ) print( "</A>" );
2137
        print( " %s  ", time_a( stats.fast.time[j][c] ) );
2138
    }
2139
    print( "\n" );
2140
 
2141
    /*
2142
    **  Print the average time for each leg
2143
    */
2144
    print( "%*s : ", LEN_CLASS_NAME, "Average" );
2145
    for( i = 0; i <= config.num_legs; i++ )
2146
    {
2147
        j = i + 1;
2148
        if( i >= config.num_legs )
2149
        {
2150
            if( final )
2151
                j = 0;                           /* Leg-0 last */
2152
            else
2153
                break;
2154
        }
2155
        print( "     %s  ", time_a( stats.average[j][c] ) );
2156
    }
2157
}
2158
 
2159
/*========================================================================
2160
 *
2161
 *  Print the legend
2162
 *
2163
 *  Purpose:
2164
 *      This function is called to Print the legend
2165
 *
2166
 *  Parameters:
2167
 *      class       - Class currently being printed
18 david 2168
 *      full        - Display full legend
1 root 2169
 *
2170
 *  Returns:
2171
 *      Nothing
2172
 *
2173
 *========================================================================*/
2174
 
18 david 2175
void print_legend ( int class, int full )
1 root 2176
{
2177
 
2178
    int         i;
2179
    char        line[201];
2180
    FILE       *adfile = NULL;
2181
    int         count;
2182
 
2183
    /*
2184
     * First the categories 
2185
     */
2186
    print( "\n\n" );
2187
    if ( report_html ) print_underline(TRUE);
2188
    print( "Category abbreviations" );
2189
    if ( report_html ) print_underline(FALSE);
2190
    print( "\n" );
2191
 
2192
 
2193
    for( i = 1, count = 0; i <= config.num_class; i++ )
2194
    {
2195
#if 0
2196
        /*
2197
        **  Skip any non-equestrian class in the legend
2198
        **  Don't want to tell the general user whats goes on, unless we actually
2199
        **  creating the non-equestrian report.
2200
        */
2201
        if ( class != config.nonequestrian_class  && i == config.nonequestrian_class )
2202
            continue;
2203
#endif
68 - 2204
        if ( report_html ) print( "<A HREF=\"%s\">",url_encode(p_filename(filebase, config.team_class[i - 1].abr ,"html")) );
1 root 2205
        print( "%-*s", 3, config.team_class[i - 1].abr );
2206
        if ( report_html ) print( "</A>" );
2207
        print( " : %-*s  ", LEN_CLASS_NAME, config.team_class[i - 1].full_name );
2208
 
2209
        if( !( ++count % 5 ) )
2210
            print( "\n" );
2211
    }
2212
 
2213
    /*
2214
    **  Add link to the finish order report
2215
    */
2216
    if ( report_html )
2217
    {
68 - 2218
        print( "<A HREF=\"%s\">", url_encode(p_filename(filebase, "finish" ,"html")) );
1 root 2219
        print( "%-*s", 3, "All" );
2220
        print( "</A>" );
2221
        print( " : %-*s  ", LEN_CLASS_NAME, "Finishing Order" );
2222
    }
2223
 
2224
    /*
2225
    **  Country data - if countries have been defined
2226
    */
2227
    if( config.num_countries )
2228
    {
2229
        print( "\n\n" );
2230
        if ( report_html ) print_underline(TRUE);
2231
        print( "Country abbreviations" );
2232
        if ( report_html ) print_underline(FALSE);
2233
        print( "\n" );
2234
 
2235
        for( i = 0, count = 0; i < MAX_COUNTRY; i++ )
2236
        {
2237
            if( config.country_name[i].abr[0] )
2238
            {
2239
                print( "%-*s : %-*s  ", 4, config.country_name[i].abr,
2240
                       LEN_CNTRY_NAME, config.country_name[i].full_name );
2241
                if( !( ++count % 5 ) )
2242
                    print( "\n" );
2243
            }
2244
        }
2245
    }
18 david 2246
    print( "\n" );
1 root 2247
 
2248
    /*
2249
     * Other comments 
2250
     */
18 david 2251
    if ( full )
2252
    {
2253
        print( "\nPlace numbers (LP and EP)\n" );
2254
        print( "LP - Placing based on elapsed time within the leg.                Cat Plce - Placing within the category.\n" );
2255
        print( "EP - Placing based on accumulated times to the end of that leg.   Fin Plce - Overall placing within the event.\n" );
61 - 2256
        print( "U  - Placing not available.\n" );
18 david 2257
    }
1 root 2258
 
2259
    /*
2260
     *  Insert the contents of the config.addendum file
2261
     *  or a defualt message
2262
     */
2263
    if( config.addendum[0] )
2264
        adfile = fopen( config.addendum, "rt" );  /* Open the file for reading */
2265
 
2266
    if( adfile )
2267
    {
2268
        while( fgets( line, sizeof(line)-1, adfile ) )
2269
            print( "%s", line );
2270
    }
2271
    else
2272
    {
2273
        print( "\nTiming and Results by\n" );
2274
        print( "Embedded Solutions\n" );
2275
    }
2276
}
2277
 
2278
/*========================================================================
2279
 *
2280
 *  Return place data
2281
 *
2282
 *  Purpose:
2283
 *      This function is called to return place data
2284
 *
2285
 *      This routine is called to fill a print team_buffer - to allow for
2286
 *      multiple calls to this function ( before the data is used ) a number
2287
 *      of static team_buffers are maintained
2288
 *
2289
 *  Parameters:
2290
 *      num         place - if not bad_times
2291
 *      disq        Disqualified flag
2292
 *      time        Time data is based on
2293
 *
2294
 *  Returns:
2295
 *      This function returns a pointer to the character string for the
2296
 *      number or a pointer to a bad_times string.
2297
 *
2298
 *========================================================================*/
2299
 
2300
char       *pi_place( int num, int disq, time_t time )
2301
{
2302
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2303
    static int  i = 0;                           /* Current index into store */
2304
    static char *dis = "D";                      /* Disqualified */
2305
    static char *non = "-";                      /* Invalid time */
2306
 
2307
    if( disq )                                   /* Disqualified team */
2308
        return ( dis );
2309
    if( time <= 0 )                              /* Unknown time */
2310
        return ( non );
2311
 
2312
    i++;
2313
    if( i >= 2 )
2314
        i = 0;                                   /* Select next entry */
2315
    sprintf( store[i], "%4d", num );
2316
    return ( store[i] );
2317
 
2318
}
2319
 
2320
/*========================================================================
2321
 *
2322
 *  Return place data
2323
 *
2324
 *  Purpose:
2325
 *      This function is called to Return place data
2326
 *
2327
 *      This routine is called to fill a print team_buffer - to allow for
2328
 *      multiple calls to this function ( before the data is used ) a number
2329
 *      of static team_buffers are maintained
2330
 *
2331
 *  Parameters:
2332
 *      num         place - if not bad_times
2333
 *      disq        Disqualified flag
2334
 *
2335
 *  Returns:
2336
 *      This function returns a pointer to the character string for the
2337
 *      number or a pointer to a bad_times string.
2338
 *
2339
 *========================================================================*/
2340
 
2341
char *pr_place( int num, int disq )
2342
{
2343
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2344
    static int  i = 0;                           /* Current index into store */
2345
    static char *dis = "U";
2346
 
2347
    if( disq )
2348
        return ( dis );
2349
 
2350
    i++;
2351
    if( i >= 2 )
2352
        i = 0;                                   /* Select next entry */
2353
    sprintf( store[i], "%4d", num );
2354
    return ( store[i] );
2355
 
2356
}
2357
 
2358
/*========================================================================
2359
 *
61 - 2360
 *  Return place data
2361
 *
2362
 *  Purpose:
2363
 *      This function is called to Return place data
2364
 *
2365
 *      This routine is called to fill a print team_buffer - to allow for
2366
 *      multiple calls to this function ( before the data is used ) a number
2367
 *      of static team_buffers are maintained
2368
 *
2369
 *  Parameters:
2370
 *      num         place - if not bad_times
2371
 *      disq        Disqualified flag
2372
 *      ne          Non Equestrian Flag
2373
 *
2374
 *  Returns:
2375
 *      This function returns a pointer to the character string for the
2376
 *      number or a pointer to a bad_times string.
2377
 *
2378
 *========================================================================*/
2379
 
2380
char *pr_place_ne( int num, int disq, int ne )
2381
{
2382
    static char store[2][5];                     /* 2 stores for 4 digit numbers */
2383
    static int  i = 0;                           /* Current index into store */
2384
    static char *dis = "U";
2385
 
2386
    if( disq && ! ne )
2387
         return ( dis );
2388
 
2389
    if( ++i >= 2 )
2390
        i = 0;                                   /* Select next entry */
2391
    sprintf( store[i], "%4d", num );
2392
    return ( store[i] );
2393
 
2394
}
2395
 
2396
/*========================================================================
2397
 *
1 root 2398
 *  Check data for bad times
2399
 *
2400
 *  Purpose:
2401
 *      This function is called to Check data for bad times
2402
 *      Scan the sort data structure and locate entries that have incorrect
2403
 *      times.
2404
 *      
2405
 *      Entries that have invalid leg times are displayed to the operator
2406
 *      and the report process can be aborted
2407
 *
2408
 *  Parameters:
2409
 *      leg         Leg to test
2410
 *      mode            Either end or elapsed times to be tested
2411
 *
2412
 *  Returns:
2413
 *      Returns FALSE if the report operation is to be aborted
2414
 *
2415
 *========================================================================*/
2416
 
2417
bool ck_data( int leg, int mode )
2418
{
2419
    ty_s_data  *ptr;
2420
    unsigned    i;
2421
    int         bad = 0;
2422
    int         j = 0;
2423
    int         k, bad_leg;
2424
    time_t     *t;                               /* An array of times */
2425
 
29 - 2426
 
2427
 
1 root 2428
    ptr = sort_data;
2429
    for( i = 1; i <= sort_num; i++, ptr++ )
2430
    {
2431
        bad_leg = 0;
2432
        if( mode == C_DISQUAL )
2433
        {
2434
            ptr->flags.bad_times = ptr->flags.disqualified;
2435
            continue;
2436
        }
2437
 
2438
        if( mode == C_ELAPSED )
2439
            t = ptr->lege;
2440
        else
2441
            t = ptr->leg;
2442
 
61 - 2443
 
2444
        if ( leg >= 0 )
1 root 2445
        {
61 - 2446
            ptr->flags.bad_times = (ptr->flags.disqualified && ! ptr->flags.non_equestrian);
2447
        }
2448
        else
2449
        {
2450
            ptr->flags.bad_times = ptr->flags.disqualified;
2451
        }
2452
 
2453
        if( ! ptr->flags.bad_times )
2454
        {
1 root 2455
            if( leg <= 0 )
2456
            {
2457
                for( k = 0; k <= config.num_legs; k++ )
2458
                {
2459
                    if ( !(config.equestrian_leg && ptr->flags.non_equestrian && config.equestrian_leg == k  ))
2460
                        bad_leg |= ( t[k] <= 0 );
2461
                }
2462
            }
2463
            else
2464
            {
2465
                bad_leg = t[leg] <= 0;
2466
            }
2467
 
2468
            if( bad_leg )
2469
            {
2470
                ptr->flags.bad_times = TRUE;
29 - 2471
 
2472
                if ( ! report_all )
1 root 2473
                {
29 - 2474
                    if( !bad )
2475
                        printf( "Team with incorrect time information\n" );
2476
                    if( ++j > 15 )
2477
                    {
2478
                        printf( "\n" );
2479
                        j = 0;
2480
                    }
2481
                    printf( "%4d ", ptr->team );
2482
                    bad++;
1 root 2483
                }
2484
            }
2485
        }
2486
    }
2487
 
2488
    if( bad )
2489
    {
2490
        printf( "\n%d teams with incorrect times.\n", bad );
2491
        return ( !getyes
2492
                 ( "These have been flagged as unplaced - continue report" ) );
2493
    }
2494
    return ( FALSE );
2495
}
2496
 
2497
/*========================================================================
2498
 *
2499
 *  Update placing information
2500
 *
2501
 *  Purpose:
2502
 *      This function is called to Update placing information
2503
 *
2504
 *      This routine will rip through the data generating the team placing in
2505
 *      a) Within a leg
2506
 *      b) At the end of a leg
2507
 *      c) Within a leg by  class
2508
 *      d) At the end of a leg by class
2509
 *
2510
 *      This function is provided to allow the display routines to
2511
 *      be accessed and updated without the need to run a report
2512
 *
2513
 *  Parameters:
2514
 *      xxxx        a ptr to the xxxx stuff
2515
 *
2516
 *  Returns:
2517
 *      Nothing
2518
 *
2519
 *========================================================================*/
2520
 
2521
void srt_place(void)
2522
{
2523
    int         i, j;
2524
 
2525
    cur( 0, 5 );
2526
    printf( "Update the team placings to the data base\n" );
2527
    flush_out();
2528
    if( ck_data( -1, C_ELAPSED ) )
2529
        return;
2530
    do_big_sort();
2531
 
2532
    /*
2533
     * Generate the stats and display them on the screen for interest
2534
     * This operation will not hurt - so why not
2535
     */
2536
 
2537
    gen_stats();
2538
    printf( "\nEvent statistics\n\n" );
2539
    printf( "%-*s   %-13s ", LEN_CLASS_NAME, "", "Overall" );
2540
    for( i = 1; i <= config.num_legs; i++ )
2541
        printf( "%-13s ", config.leg_name[i - 1] );
2542
 
2543
    for( j = 0; j <= config.num_class; j++ )
2544
    {
2545
        printf( "\n%-*s : ", LEN_CLASS_NAME,
2546
                j ? config.team_class[j - 1].full_name : "Overall" );
2547
        for( i = 0; i <= config.num_legs; i++ )
2548
        {
2549
            printf( "%4d ", stats.fast.team[i][j] );
2550
            printf( "%s ", time_a( stats.fast.time[i][j] ) );
2551
        }
2552
        printf( "\n%*s : ", LEN_CLASS_NAME, "Average" );
2553
        for( i = 0; i <= config.num_legs; i++ )
2554
        {
2555
            printf( "     %s ", time_a( stats.average[i][j] ) );
2556
        }
2557
    }
2558
    printf( "\nAny key to continue" );
2559
    ( void ) getinp();
2560
}
2561
 
2562
/*========================================================================
2563
 *
2564
 *  Calculate summary information
2565
 *
2566
 *  Purpose:
2567
 *      This function is called to calculate summary information
2568
 *
2569
 *  Parameters:
2570
 *      ptr         - Address of a summary structure to fill in
2571
 *
2572
 *  Returns:
2573
 *      Nothing
2574
 *
2575
 *========================================================================*/
2576
 
2577
void calc_class_summary( t_class_summary * ptr )
2578
{
2579
    int i;
2580
 
2581
    /*
2582
    **  Reset the data
2583
    */
2584
    memset ( ptr, 0, sizeof (*ptr ));
2585
 
2586
    /*
2587
     * Extract the required data from the data base
2588
     * Only save that information required for the operation
2589
     */
2590
 
2591
    for( i = config.min_team; i <= config.max_team; i++ )
2592
    {
2593
        if( valid_field( i ) && g_record( i, &team_buf ) )
2594
        {
2595
            ptr->total.total++;
2596
            ptr->class[team_buf.class].total++;
2597
 
2598
            if ( team_buf.flags.disqualified )
2599
            {
2600
                ptr->class[team_buf.class].disqualified++;
2601
                ptr->total.disqualified++;
2602
            }
2603
 
2604
            if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
2605
            {
2606
                ptr->class[team_buf.class].non_equestrian++;
2607
                ptr->total.non_equestrian++;
2608
            }
2609
        }
2610
    }
13 david 2611
 
2612
    /*
2613
    **  Fix up the totals for the non equestrians
2614
    **  This is not a real category but a summary of the others.
2615
    */
2616
    if ( config.nonequestrian_class  )
2617
    {
2618
        ptr->class[config.nonequestrian_class].total += ptr->total.non_equestrian;
2619
    }
1 root 2620
}
2621
 
2622
/*========================================================================
2623
 *
2624
 *  Display summary information
2625
 *
2626
 *  Purpose:
2627
 *      This function is called to display summary information
2628
 *
2629
 *  Parameters:
2630
 *      None
2631
 *
2632
 *  Returns:
2633
 *      Nothing
2634
 *
2635
 *========================================================================*/
2636
 
2637
void display_summary (void)
2638
{
2639
 
2640
    t_class_summary data;
2641
    int         i;
2642
    int         line = 2;
2643
 
2644
    cur( 0, line );
2645
    printf( "Generating Summary Information" );
2646
    calc_class_summary( & data );
2647
 
2648
 
2649
    /*
2650
    **  Display summary stats
2651
    */
2652
 
2653
    cur( 0, line );
2654
    console_clreol();
2655
    printf( "Summary Information\n\n" );
2656
    printf( "%*s : %-7s %-7s%-7s\n", LEN_CLASS_NAME,
2657
                                      "Category",
2658
                                      "Total",
2659
                                      "Disq",
2660
                                      "NonEq" );
2661
 
2662
    for( i = 0; i < config.num_class; i++ )
2663
    {
2664
        /*
2665
        **  The non-equestrian leg does not have any data
2666
        **  Supress the display
2667
        */
2668
        if ( config.nonequestrian_class == i+1  )
2669
            continue;
2670
 
2671
        printf( "%*s : %-7d %-7d %-7d\n", LEN_CLASS_NAME,
2672
                                          config.team_class[i].full_name,
2673
                                          data.class[i+1].total,
2674
                                          data.class[i+1].disqualified,
2675
                                          data.class[i+1].non_equestrian );
2676
    }
2677
 
2678
    printf( "\n" );
2679
    printf( "%*s : %-7d %-7d %-7d\n", LEN_CLASS_NAME,
2680
                                      "Totals",
2681
                                      data.total.total,
2682
                                      data.total.disqualified,
2683
                                      data.total.non_equestrian );
2684
 
2685
    printf( "\nAny key to continue " );
2686
    getinp();
2687
}
2688
 
13 david 2689
/*========================================================================
2690
 *
2691
 *  Print summary information
2692
 *
2693
 *  Purpose:
2694
 *      This function is called to print summary information
2695
 *
2696
 *  Parameters:
2697
 *      None
2698
 *
2699
 *  Returns:
2700
 *      Nothing
2701
 *
2702
 *========================================================================*/
1 root 2703
 
13 david 2704
void pri_summary (void)
2705
{
2706
 
2707
    t_class_summary data;
2708
    int         i;
2709
    int         line = 2;
2710
 
2711
 
2712
    if( !open_printer( "", "summary", 132, 0, "Summary Information" ) )
2713
        return;
2714
 
2715
    cur( 0, line );
2716
    printf( "Generating Summary Information" );
2717
    calc_class_summary( & data );
2718
 
2719
 
2720
    /*
2721
    **  Display summary stats
2722
    */
2723
 
2724
    print( "%*s : %-7s %-7s%-7s\n", LEN_CLASS_NAME,
2725
                                      "Category",
2726
                                      "Total",
2727
                                      "Disq",
2728
                                      "NonEq" );
2729
 
2730
    for( i = 0; i < config.num_class; i++ )
2731
    {
2732
        /*
2733
        **  The non-equestrian leg does not have any data
2734
        **  Supress the display
2735
        */
2736
        if ( config.nonequestrian_class == i+1  )
2737
            continue;
2738
 
2739
        print( "%*s : %-7d %-7d %-7d\n", LEN_CLASS_NAME,
2740
                                          config.team_class[i].full_name,
2741
                                          data.class[i+1].total,
2742
                                          data.class[i+1].disqualified,
2743
                                          data.class[i+1].non_equestrian );
2744
    }
2745
 
2746
    print( "\n" );
2747
    print( "%*s : %-7d %-7d %-7d\n", LEN_CLASS_NAME,
2748
                                      "Totals",
2749
                                      data.total.total,
2750
                                      data.total.disqualified,
2751
                                      data.total.non_equestrian );
2752
 
2753
    close_printer();
29 - 2754
    if ( !report_all )
2755
    {
2756
        printf( "\nAny key to continue " );
2757
        getinp();
2758
    }
13 david 2759
}
2760
 
2761
 
2762
 
1 root 2763
/*========================================================================
2764
 *
2765
 *  Main sort routine for final data
2766
 *
2767
 *  Purpose:
2768
 *      This function is called to do the report sort routine for final data
2769
 *      This routine will fill all the gaps in the sort_aux structure
2770
 *
2771
 *  Parameters:
2772
 *      None
2773
 *
2774
 *  Returns:
2775
 *      Nothing
2776
 *
2777
 *========================================================================*/
2778
 
2779
void do_big_sort(void)
2780
{
2781
    int         i, k;                            /* Looooopy things */
2782
    unsigned    j;
2783
    ty_s_data  *ptr;                             /* Pointer to sort data */
2784
    int         class;                           /* Current class */
2785
 
2786
    printf( "Sorting it ALL out" );
2787
    flush_out();
2788
 
2789
    for( i = 0; i <= config.num_legs; i++ )
2790
    {
2791
        /*
2792
        **  Sort on leg elapsed time
61 - 2793
        **  Then save the teams elapsed place in each leg
1 root 2794
        */
2795
        sort_team_data( i, S_L );
61 - 2796
        for( j = 1, k = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
1 root 2797
        {
61 - 2798
            if ( ptr->class == config.nonequestrian_class )
2799
                continue;
2800
 
2801
            sort_aux[ptr->team].l_place[i] = k++;
1 root 2802
            sort_aux[ptr->team].team = ptr->team;
2803
        }
2804
 
2805
        /*
2806
        **  Sort on leg end time
2807
        **  Then save the teams place at the end of each leg
2808
        */
2809
        sort_team_data( i, S_LE );
61 - 2810
        for( j = 1, k = 1, ptr = sort_data; j <= sort_num; ptr++, j++ )
2811
        {
2812
            if ( ptr->class == config.nonequestrian_class )
2813
                continue;
2814
 
2815
            sort_aux[ptr->team].le_place[i] = k++;
2816
        }
1 root 2817
 
2818
        /*
2819
        **  Sort on elapsed time per class
2820
        **  The save the teams elapsed place in each leg per class
2821
        */
2822
        sort_team_data( i, S_LC );
2823
        class = -1;
2824
        for( k = 1, j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
2825
        {
2826
            if( class != ptr->class )
2827
            {
2828
                k = 1;
2829
                class = ptr->class;
2830
            }
2831
            sort_aux[ptr->team].lc_place[i] = k++;
2832
        }
2833
 
2834
        /*
2835
        **  Sort on end time per class
2836
        **  Then save the teams place at the end of each leg per class
2837
        */
2838
        sort_team_data( i, S_LEC );
2839
        class = -1;
2840
        for( k = 1, j = 1, ptr = sort_data; j <= sort_num; j++, ptr++ )
2841
        {
2842
            if( class != ptr->class )
2843
            {
2844
                k = 1;
2845
                class = ptr->class;
2846
            }
2847
            sort_aux[ptr->team].lec_place[i] = k++;
2848
        }
2849
    }
2850
 
2851
    /*
2852
    **  Write the place information back to disk for use in the displays
2853
    */
2854
    printf( "\nWriting it all back to the disk file" );
2855
    flush_out();
2856
 
2857
    for( i = config.min_team; i <= config.max_team; i++ )
2858
    {
2859
        if( sort_aux[i].team && valid_field( i ) && g_record( i, &team_buf ) )
2860
        {
2861
            for( k = 0; k <= config.num_legs; k++ )
2862
            {
2863
                team_buf.leg[k].l_place = sort_aux[i].l_place[k];
2864
                team_buf.leg[k].le_place = sort_aux[i].le_place[k];
2865
                team_buf.leg[k].lc_place = sort_aux[i].lc_place[k];
2866
                team_buf.leg[k].lec_place = sort_aux[i].lec_place[k];
2867
            }
2868
            put_team_record( i, &team_buf );
2869
        }
2870
    }
2871
}
2872
 
2873
/*========================================================================
2874
 *
2875
 *  Sort in memory buffer
2876
 *
2877
 *  Purpose:
2878
 *      This function is called to Sort in memory buffer
2879
 *
2880
 *  Parameters:
2881
 *      leg         Requested leg
2882
 *      mode        Defines the sort mode
2883
 *
2884
 *  Returns:
2885
 *      Nothing
2886
 *
2887
 *========================================================================*/
2888
 
2889
void sort_team_data( int leg, int mode )
2890
{
2891
 
2892
    unsigned    j;
2893
    ty_s_data  *ptr;
2894
 
2895
    sort_leg = leg;                              /* Leg is global for the comparison function */
2896
    sort_mode = mode;                            /* Mode is global for compare function */
2897
 
2898
    qsort( ( char * ) sort_data, sort_num, sizeof( ty_s_data ), sort_comp );
2899
 
2900
    /*
2901
     * Insert "place data" into the sorted data
2902
     * This simply the index into the array of data - after its been
2903
     * sorted.
2904
     */
2905
    ptr = sort_data;
2906
    for( j = 1; j <= sort_num; j++, ptr++ )
2907
        ptr->place = j;
2908
 
2909
}
2910
 
2911
/*========================================================================
2912
 *
2913
 *  qsort comparison function
2914
 *
2915
 *  Purpose:
2916
 *      This function is called by qsort as a Sort comparison function
2917
 *
2918
 *  Parameters:
2919
 *      a       - 1st record
2920
 *      b       - 2nd record
2921
 *
2922
 *  Returns:
2923
 *      value to qsort
2924
 *
2925
 *========================================================================*/
2926
 
2927
int sort_comp( const void * aa, const void * bb )
2928
{
2929
    const ty_s_data * a = aa;
2930
    const ty_s_data * b = bb;
2931
 
2932
 
2933
    int         i;                               /* One of those */
2934
    time_t      ta, tb;                          /* Leg times */
2935
    int         na, nb;                          /* Number of valid legs */
2936
    time_t      tta, ttb;                        /* Temp times */
2937
 
2938
    /*
2939
    **  Sorting on Team Number
2940
    */
2941
    if( sort_mode == S_TEAM )
2942
        return ( a->team - b->team );
2943
 
2944
    /*
2945
    **  Sorting on Class and Team Number
2946
    */
2947
    if( sort_mode == S_CLASS )
2948
    {
2949
        if( a->class != b->class )
2950
            return ( a->class - b->class );
2951
        else
2952
            return ( a->team - b->team );
2953
    }
2954
 
2955
    /*
2956
    **  Sorting within a class
2957
    **  First sort on the class
2958
    */
2959
    if( sort_mode == S_LEC || sort_mode == S_LC )   /* Sort within a class */
2960
        if( a->class != b->class )
2961
            return ( a->class - b->class );
2962
 
61 - 2963
#if 1
1 root 2964
    /*
61 - 2965
    **  Always put the nonequestrian_class at the end
2966
    **  Simplifies the creation of ordered lists as these
2967
    **  Entries are not present in the main body
2968
    */
2969
    if ( a->class == config.nonequestrian_class ||  b->class == config.nonequestrian_class )
2970
    {
2971
        if ( a->class == config.nonequestrian_class &&  b->class == config.nonequestrian_class )
2972
        {
2973
        }
2974
        else
2975
        {
2976
            return ( a->class == config.nonequestrian_class ? 1 : -1 );
2977
        }
2978
 
2979
    }
2980
#endif
2981
 
2982
    /*
1 root 2983
    **  Now we need to examine the times as we have sorted
2984
    **  on every thing else.
2985
    **
2986
    **  If one of the teams has bad_times, then that team is placed
2987
    **  lower in the sorting order. If both teams have bad times
2988
    **  then sort on team number. ie: Unplaced teams are sorted on
2989
    **  team number
61 - 2990
    **
2991
    **  If not sorting within a class (ie Overall), then Non_Equestrian
2992
    **  is better than a bad time. Places NE before disqualified
2993
    **
2994
    **
2995
    **  Note: NE also have bad_times set
1 root 2996
    */
61 - 2997
#if 1
1 root 2998
    if( a->flags.bad_times || b->flags.bad_times )  /* Valid data has precedence */
2999
    {
3000
        if( a->flags.bad_times && b->flags.bad_times )
61 - 3001
        {
3002
            if( a->flags.non_equestrian || b->flags.non_equestrian )
3003
            {
3004
                if( a->flags.non_equestrian && b->flags.non_equestrian )
3005
                {
3006
                    /*
3007
                    **  Both are non equestrian
3008
                    **  Let the time sort operate ...
3009
                    */
3010
                    //return ( a->team - b->team );
3011
                }
3012
                else
3013
                {
3014
                    /* One is equestrian */
3015
                    /* Good times better than NE */
3016
                    return ( a->flags.non_equestrian ? -1 : 1 );
3017
                }
3018
            }
3019
            else
3020
            {
3021
                /* Neither is equestrian */
3022
                return ( a->team - b->team );
3023
            }
3024
        }
3025
        else
3026
            return ( a->flags.bad_times ? 1 : -1 );
3027
    }
3028
#else
3029
    if( a->flags.bad_times || b->flags.bad_times )  /* Valid data has precedence */
3030
    {
3031
        if( a->flags.bad_times && b->flags.bad_times )
1 root 3032
            return ( a->team - b->team );
3033
        else
3034
            return ( a->flags.bad_times ? 1 : -1 );
3035
    }
3036
 
3037
    /*
3038
    **  Not sorting within a class ie: Overall
3039
    **  Non-Equestrian is at the end, so that it doesn't get counted in the
3040
    **  finishing order
3041
    */
3042
    if( sort_mode == S_LE || sort_mode == S_L )     /* Sort NOT within a class */
3043
    {
3044
        if( a->flags.non_equestrian || b->flags.non_equestrian )
3045
        {
3046
            if( a->flags.non_equestrian && b->flags.non_equestrian )
3047
                return ( a->team - b->team );
3048
            else
3049
                return ( a->flags.non_equestrian ? 1 : -1 );
3050
        }
3051
    }
61 - 3052
#endif
1 root 3053
 
3054
    /*
3055
    **  Before we sort on times we must determine which time to
3056
    **  use. Finish time, Leg end times, Leg Elapsed times.
3057
    */
3058
 
3059
    switch ( sort_mode )
3060
    {
3061
      /*
3062
      **    Sort on finish times
3063
      */
3064
      case S_FIN:
3065
        ta = a->leg[sort_leg];
3066
        tb = b->leg[sort_leg];
3067
        break;
3068
 
3069
 
3070
      /*
3071
      **    Sort on accumulated leg times
3072
      */
3073
      case S_LE:
3074
      case S_LEC:
3075
        if( sort_leg )
3076
        {
3077
            /*
3078
            **  Calculate accumulated time up to the desired leg
3079
            **  If the two teams have a different number of valid
3080
            **  leg times then order by the team that has completed
3081
            **  more legs.
3082
            */
3083
            ta = tb = ( time_t ) 0;
3084
            na = nb = 0;
3085
            for( i = 1; i <= sort_leg; i++ )
3086
            {
3087
                tta = a->lege[i];
3088
                ttb = b->lege[i];
3089
                if( tta > 0 )
3090
                {
3091
                    na++;
3092
                    ta += tta;
3093
                }
3094
                if( ttb > 0 )
3095
                {
3096
                    nb++;
3097
                    tb += ttb;
3098
                }
3099
            }
3100
            if( na != nb )
3101
                return ( nb - na );
3102
        }
3103
        else
3104
        {
3105
            ta = a->leg[sort_leg] - a->start;
3106
            tb = b->leg[sort_leg] - b->start;
3107
        }
3108
        break;
3109
 
3110
      /*
3111
      **    Sort on Elapsed times
3112
      */
3113
      case S_LC:
3114
      case S_L:
3115
        ta = a->lege[sort_leg];
3116
        tb = b->lege[sort_leg];
3117
        break;
3118
 
3119
      /*
3120
      **    Just to be sure ...
3121
      */
3122
      default:
3123
        return ( 0 );
3124
    }
3125
 
3126
    /*
3127
    **  Finally. Compare the required team times
3128
    */
3129
    if( ta == tb )
3130
        return ( a->team - b->team );
3131
    if( ( ta > 0 ) && ( tb > 0 ) )
3132
        return ( ( int ) ( ta - tb ) );
3133
    return ( ( ta > 0 ) ? -1 : 1 );
3134
}
3135
 
3136
/*========================================================================
3137
 *
18 david 3138
 *  qsort comparison function - competitor names
3139
 *
3140
 *  Purpose:
3141
 *      This function is called by qsort as a Sort comparison function
3142
 *
3143
 *  Parameters:
3144
 *      a       - 1st record
3145
 *      b       - 2nd record
3146
 *
3147
 *  Returns:
3148
 *      value to qsort
3149
 *
3150
 *========================================================================*/
3151
 
3152
int sort_comp_cname( const void * aa, const void * bb )
3153
{
25 - 3154
    ty_s_namedata * a = (ty_s_namedata *)aa;
3155
    ty_s_namedata * b = (ty_s_namedata *)bb;
18 david 3156
 
3157
 
3158
    int         i;                               /* One of those */
3159
 
3160
    /*
3161
    **  Sort by name
3162
    */
3163
    i = strcmp ( a->name, b->name );
3164
    if ( i )
3165
        return ( i );
3166
    a->multi=1;
3167
    b->multi=1;
3168
 
3169
    /*
3170
    **  Sort by Leg
3171
    */
3172
    i = a->leg - b->leg;
3173
    if ( i )
3174
        return ( i );
3175
 
3176
    /*
3177
    **  Sorting on Team Number
3178
    */
3179
    return ( a->team - b->team );
3180
 
3181
}
3182
 
3183
/*========================================================================
3184
 *
1 root 3185
 *  load report data into memory
3186
 *
3187
 *  Purpose:
3188
 *      This routine will pull all the data into memory 
3189
 *      Not all the team data is loaded. Only that essential for the
3190
 *      operation of the sort routine is loaded
3191
 *
3192
 *  Parameters:
3193
 *      None
3194
 *
3195
 *  Returns:
3196
 *      TRUE - All is well
3197
 *
3198
 *========================================================================*/
3199
 
3200
bool load_report_data(void)
3201
{
3202
 
3203
    ty_s_data  *ptr;                             /* pointer to sort data type */
61 - 3204
    ty_s_data  *last;
1 root 3205
    int         j;
3206
    unsigned    num;
3207
 
3208
    printf( "Loading team information - This will not hurt" );
3209
    flush_out();
3210
 
3211
    /*
3212
     * Fetch memory for the data store
3213
     */
3214
 
3215
    if( sort_data )
3216
        free( ( char * ) sort_data );
3217
    sort_data = 0;
3218
 
3219
    if( sort_aux )
3220
        free( ( char * ) sort_aux );
3221
    sort_aux = 0;
3222
    sort_num = 0;                                /* Counter of records in the array */
3223
 
3224
    /*
3225
    **  Allocate memory for the data structures
3226
    **  This will be free'd
3227
    */
3228
    num = config.max_team - config.min_team + 1 ;
3229
 
3230
    /*
3231
    **  Allow for non-equestrian teams - since some of the data is loaded twice
3232
    **  Take a guess that at most 1/2 the teams will be non-equestrian
3233
    */
3234
    num = num * 3 / 2;
3235
 
3236
    sort_data = ( ty_s_data * ) calloc ( num , sizeof( ty_s_data ) );
3237
    sort_aux = ( ty_s_aux * ) calloc( num + 1, sizeof( ty_s_aux ) );
3238
 
3239
    if( sort_data == 0 || sort_aux == 0 )
3240
    {
3241
        printf( "\n\nError in allocating memory\n" );
3242
        sleep( 5 );
3243
        return ( FALSE );
3244
    }
3245
 
3246
    /*
3247
    **  Load data into the memory based data structure
3248
    **  Only load data for valid-teams
3249
    **  Load essential data
3250
    **      Team Number, class and flags
3251
    **      Leg end and elapsed times
3252
    */
3253
    ptr = sort_data;
3254
    for( team = config.min_team; team <= config.max_team; team++ )
3255
    {
3256
        if( valid_field( team ) )
3257
        {
3258
            g_record( team, &team_buf );
3259
            if( team_buf.flags.valid )
3260
            {
61 - 3261
                last = ptr;
1 root 3262
                ptr->team = team;
3263
                for( j = 0; j < MAX_LEGS + 1; j++ )
3264
                {
3265
                    ptr->lege[j] = team_buf.leg[j].elapsed;
3266
                    ptr->leg[j] = team_buf.leg[j].end;
3267
                }
3268
                ptr->start = team_buf.leg[0].start;
3269
                ptr->class = team_buf.class;
3270
 
3271
                ptr->flags = team_buf.flags;
3272
                ptr++;
3273
                sort_num++;
3274
 
3275
                /*
3276
                **  If non-equestrian support is enabled then
3277
                **  duplicate and modify data for the non-equestrian teams
3278
                **      - Change the class
3279
                **      - Modify the leg time
3280
                */
61 - 3281
#if 1
1 root 3282
                if ( config.nonequestrian_class && team_buf.flags.non_equestrian )
3283
                {
3284
                    ptr->team = team;
3285
                    ptr->lege[0] = 0;
3286
 
3287
                    for( j = 0; j < MAX_LEGS + 1; j++ )
3288
                    {
3289
                        if ( j == config.equestrian_leg )
3290
                        {
61 - 3291
                            last->lege[j] = ptr->lege[j] = 0;
1 root 3292
                            if ( config.equestrian_leg > 1 )
61 - 3293
                                last->leg[j] = ptr->leg[j] = ptr->leg[j-1];
1 root 3294
                        }
3295
                        else
3296
                        {
3297
                            if ( j )
61 - 3298
                                last-> lege[j] = ptr->lege[j] = team_buf.leg[j].elapsed;
3299
                            last->leg[j] = ptr->leg[j] = team_buf.leg[j].end;
1 root 3300
                            ptr->lege[0] += ptr->lege[j] ;
61 - 3301
                            last->lege[0] = ptr->lege[0] ;
1 root 3302
                        }
3303
                    }
3304
 
61 - 3305
                    last->start = ptr->start = team_buf.leg[0].start;
1 root 3306
                    ptr->class = config.nonequestrian_class;
3307
                    ptr->flags = team_buf.flags;
3308
                    ptr->flags.disqualified = 0;
61 - 3309
 
3310
 
1 root 3311
                    ptr++;
3312
                    sort_num++;
3313
                }
61 - 3314
#endif
1 root 3315
            }
3316
        }
3317
    }
3318
    return ( TRUE );
3319
}
3320
 
3321
/*========================================================================
3322
 *
3323
 *  Generate all the stats
3324
 *
3325
 *  Purpose:
3326
 *      This function is called to Generate all the stats
3327
 *
3328
 *  Parameters:
3329
 *      None
3330
 *
3331
 *  Returns:
3332
 *      Nothing
3333
 *
3334
 *========================================================================*/
3335
 
3336
void gen_stats(void)
3337
{
3338
    ty_s_data  *ptr;
3339
    unsigned    i;
3340
    int         j;
3341
    int         k;
3342
 
3343
    /*
3344
     * Init all the stats 
3345
     */
3346
    for( i = 0; i <= MAX_LEGS; i++ )
3347
    {
3348
        for( j = 0; j <= MAX_CLASS; j++ )
3349
        {
3350
            stats.team[i][j] = 0;
3351
            stats.fast.team[i][j] = 0;
3352
            stats.fast.time[i][j] = ( time_t ) - 1;
3353
            stats.average[i][j] = 0;
3354
        }
3355
    }
3356
 
3357
    for( i = 1, ptr = sort_data; i <= sort_num; i++, ptr++ )
3358
    {
3359
        /*
3360
        **  If there is any bad times in the team record, then none
3361
        **  of the data can be trusted.
3362
        **
3363
        **  If the team has been disqualified, then don't use any
3364
        **  of the data.
3365
        */
3366
        if( ptr->flags.bad_times || ptr->flags.disqualified )
3367
            continue;
3368
 
3369
        for( j = 0; j <= config.num_legs; j++ )
3370
        {
3371
            if( ptr->lege[j] <= 0 )              /* Ignore bad data */
3372
                continue;
3373
 
3374
            /*
3375
            **  Determine fastest team : overall
3376
            **  Ignore the non-equestrian data as this is in the list twice
3377
            */
3378
            if( ( ptr->lege[j] < stats.fast.time[j][0] )
3379
                || ( stats.fast.time[j][0] < 0 ) )
3380
            {
3381
                if ( ptr->class != config.nonequestrian_class )
3382
                {
3383
                    stats.fast.team[j][0] = ptr->team;
3384
                    stats.fast.time[j][0] = ptr->lege[j];
3385
                }
3386
            }
3387
 
3388
            /*
3389
            **  Determine fastest team : within a class
3390
            */
3391
            if( ( ptr->lege[j] < stats.fast.time[j][ptr->class] )
3392
                || stats.fast.time[j][ptr->class] < 0 )
3393
            {
3394
                stats.fast.team[j][ptr->class] = ptr->team;
3395
                stats.fast.time[j][ptr->class] = ptr->lege[j];
3396
            }
3397
 
3398
            /*
3399
            **  Sum the end times : overall
3400
            */
3401
            if ( ptr->class != config.nonequestrian_class )
3402
            {
3403
                stats.average[j][0] += ptr->lege[j];
3404
                stats.team[j][0]++;
3405
            }
3406
 
3407
 
3408
            /*
3409
            **  Sum the end times : within a class
3410
            */
3411
            stats.average[j][ptr->class] += ptr->lege[j];
3412
            stats.team[j][ptr->class]++;
3413
        }
3414
    }
3415
 
3416
    /*
3417
     * Calculate the averages
3418
     */
3419
    for( k = 0; k <= config.num_legs; k++ )
3420
    {
3421
        for( j = 0; j <= config.num_class; j++ )
3422
        {
3423
            if( stats.team[k][j] )
3424
                stats.average[k][j] /= stats.team[k][j];
3425
            else
3426
                stats.average[k][j] = ( time_t ) - 1;
3427
        }
3428
    }
3429
}
3430
 
3431
/********************************* EOF ***********************************/