Subversion Repositories svn1-original

Rev

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

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