Subversion Repositories svn1

Rev

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

Rev Author Line No. Line
1 root 1
/*************************************************************************
2
*           Copyright Xdel Technology Pty. Ltd. 1987
3
*           Copyright (C) 1995 Embedded Solutions
4
*                       All rights reserved
5
*
6
* file:     src\upload.c
7
*
8
* purpose:  Data upload functions
9
*           This module contains all the functions required to upload
10
*           data from the handheld calculators and insert the data
11
*           into the data base as well as functions and a menu to load
12
*           and unload specific data into the data base
13
*
14
* functions
15
*       upload                  - Get Leg to upload
16
*       ins_data                - Insert time info into data base
17
*       supload                 - Extended upload functions menu
18
*       tupload                 - Read in text team information
19
*       t_parse_number          - Parse a number from a CSV line of team text
20
*       t_parse_text            - Parse string from a CSV line of team text
21
*       p_del                   - Test for a delimiter.
22
*       tdnload                 - Generate team name file
23
*       dnload                  - Generate leg timing file
24
*       getfname                - Get a filename from the user
25
*
26
* programmer: David Purdie
27
*
28
* revision  date        by      reason
29
*           May 1990    Margherita Veroni
30
*                               The functions to upload data have been modified
31
*                               to allow leg start times to also be uploaded for
32
*                               any leg. Data being uploaded which contain errors
33
*                               are written to an error file
34
*
35
*   00.0    1-Apr-94    DDP     Changed tdnload() to produce a comma seperated
36
*                               file. This allows external users to see the
37
*                               field delimters better than tabs
38
*
39
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
40
*
41
**************************************************************************/
42
 
43
#include    <stdio.h>
44
 
45
#include    "consts.h"
46
#include    "structs.h"
47
#include    "proto.h"
48
 
49
/* Variable storage */
50
 
51
char        ufilename[40];                       /* Name of upload file */
52
FILE       *ufile;                               /* Upload file stream */
53
FILE       *efile;                               /* Error file stream */
54
char        line[300];                           /* Input line */
55
char        line_text[133];                      /* Output text */
56
 
57
unsigned char manstart;                          /* Manual start time entry */
58
 
59
 
60
menu_table  sup_menu[] = {
61
    { '1', "Load team information from external file", tupload },
62
    { '2', "Create external team information file", tdnload },
63
    { '3', "Create external leg data file", dnload },
64
    { '4', "Upload time information", upload },
65
    { 'q', "Return to main menu", 0 },
66
    { '\0' }
67
};
68
 
69
/*========================================================================
70
 *
71
 *  Get Leg to upload
72
 *
73
 *  Purpose:
74
 *      This function is called to Get Leg to upload
75
 *          Prompt user for leg number and start / end of leg
76
 *          Open disc file
77
 *          Read and parse text. Enter data into team information
78
 *          Maintain error file of errors.
79
 *
80
 *  Parameters:
81
 *      None
82
 *
83
 *  Returns:
84
 *      Nothing
85
 *
86
 *========================================================================*/
87
 
88
void upload(void)
89
{
90
    int         hh, mm, ss;                      /* Team times */
91
    int         error = 0;                       /* Count of errors */
92
    int         ntms = 0;                        /* Number of teams uploaded */
93
    char        stend[] = "E";                   /* Start or End time */
94
    char       *filename;                        /* Name of file to open */
95
    char       *err_filename;                    /* Name of an error file to open */
96
 
97
    cur( 0, 5 );
98
    while( TRUE )
99
    {
100
        leg = 0;
101
        d_field( 0, 6, "Enter leg to upload :", D_NUMBER, 1, ( char * ) &leg,
102
                 TRUE, M_UPDATE );
103
        if( leg == 0 )
104
            return;                              /* Null leg - just exit */
105
        if( leg <= config.num_legs )             /* Valid leg number - Exit loop */
106
            break;
107
        beep();                                  /* Make a noise and wait for valid number */
108
    }
109
 
110
 
111
    /*
112
     * Find out if Start or End of leg times to be uploaded 
113
     */
114
 
115
    do
116
    {
117
        d_field( 0, 7, "Start or End of leg to upload :", D_USTRING, 1, stend,
118
                 TRUE, M_UPDATE );
119
    } while( ( stend[0] != 'S' ) && ( stend[0] != 'E' ) );
120
    manstart = ( ( stend[0] == 'S' ) ? TRUE : FALSE );
121
    printf( "\n" );
122
 
123
    /*
124
     * Locate the required data file and prepare for processing 
125
     */
126
    filename = tprintf( "%s%d" , manstart ? "Sleg" : "leg" , leg );
127
    ufile = fopen( filename, "rt" );
128
    if( ufile == 0 )
129
    {
130
        printf( "Cannot locate data file - %s\n", filename );
131
        beep();
132
        sleep( 5 );
133
        return;
134
    }
135
 
136
    /*
137
     * create an error file for this leg data 
138
     * duplicate times will stored here
139
     */
140
    err_filename = tprintf( "%s%d.err" , manstart ? "Sleg" : "leg" , leg );
141
    efile = fopen( err_filename, "at" );
142
 
143
    /*
144
     * Process each entry in the file 
145
     */
146
 
147
    if( leg > config.num_legs )
148
        printf( "\nUploading leg%d start information\n", leg );
149
 
150
    while( fgets( line, 101, ufile ) )
151
    {
152
        if( sscanf( line, "%d %d:%d:%d", &team, &hh, &mm, &ss ) != 4 )
153
        {
154
            printf( "Upload error - %s", line );
155
            error++;
156
        }
157
        else
158
        {
159
            if( !ins_data( team, hh, mm, ss ) )
160
                error++;
161
            ntms++;
162
        }
163
    }
164
 
165
    printf( "%d errors detected. %d teams uploaded. Any key to continue ", error, ntms );
166
    getinp();
167
 
168
    fclose( ufile );
169
    fclose( efile );
170
}
171
 
172
/*========================================================================
173
 *
174
 *  Insert time info into data base
175
 *
176
 *  Purpose:
177
 *      This helper function is called to Insert time info into data base
178
 *          Read record
179
 *          Read and convert time
180
 *          Write record
181
 *              Maintain error file
182
 *          Display errors on screen
183
 *
184
 *  Parameters:
185
 *      tm          Team
186
 *      hh          hours
187
 *      mm          minutes
188
 *      ss          seconds
189
 *
190
 *  Returns:
191
 *      Nothing
192
 *
193
 *========================================================================*/
194
 
195
int ins_data( int tm, int hh, int mm, int ss )
196
{
197
    time_t      l_time;                          /* Leg time */
198
    int         ok = TRUE;
199
 
200
    /*
201
     * Calculate the time for the team 
202
     */
203
 
204
    l_time = conv_time( hh, mm, ss );
205
 
206
    /*
207
     * If an error is found - invalid team, team not found or dual time for team
208
     * a message is output to the screen and the data is written to error file
209
     * FALSE is returned
210
     */
211
 
212
    if( !valid_field( tm ) )
213
    {
214
        printf( "Invalid team - %d %2.2d:%2.2d:%2.2d\n", tm, hh, mm, ss );
215
        fprintf( efile, "Invalid team - %d %2.2d:%2.2d:%2.2d\n", tm, hh, mm,
216
                 ss );
217
        ok = FALSE;
218
        return ( ok );
219
    }
220
    if( !g_record( tm, &team_buf ) )
221
    {
222
        printf( "Team not found -% d %2.2d:%2.2d:%2.2d\n", tm, hh, mm, ss );
223
        fprintf( efile, "Team not found - %d %2.2d:%2.2d:%2.2d\n", tm, hh, mm, ss );
224
        ok = FALSE;
225
    }
226
    if( !manstart )
227
    {                                            /* Normal upload */
228
        if( team_buf.leg[leg].end > 0 && team_buf.leg[leg].end != l_time )
229
        {
230
            printf( "Dual time for %d - %2.2d:%2.2d:%2.2d and %s\n", tm, hh,
231
                    mm, ss, time_a( team_buf.leg[leg].end ) );
232
            fprintf( efile, "Dual time for %d - %2.2d:%2.2d:%2.2d and %s\n",
233
                        tm, hh, mm, ss, time_a( team_buf.leg[leg].end )
234
                    ); /* write  duplicate time to error file */
235
            ok = FALSE;
236
            return ( ok );                       /* keep time already in database */
237
        }
238
        team_buf.leg[leg].end = l_time;
239
    }
240
    else
241
    {                                            /* Uplaod start time */
242
        team_buf.leg[leg].start = l_time;
243
        team_buf.leg[leg].manual = TRUE;
244
    }
245
    set_times( &team_buf );                      /* Calc start of next leg */
246
    ( void ) test_times( &team_buf, 0 );         /* Calc elapsed times etc */
247
    put_team_record( tm, &team_buf );
248
    return ( ok );
249
}
250
 
251
/*========================================================================
252
 *
253
 *  Extended upload functions menu
254
 *
255
 *  Purpose:
256
 *      This function is called to Extended upload functions
257
 *
258
 *  Parameters:
259
 *      None
260
 *
261
 *  Returns:
262
 *      Nothing
263
 *
264
 *========================================================================*/
265
 
266
void supload(void)
267
{
268
    do_menu( "Extended data manipulation", "Select option", sup_menu ); /* Call a menu to do it */
269
}
270
 
271
/*========================================================================
272
 *
273
 *  Read in text team information
274
 *
275
 *  Purpose:
276
 *      This function is called to do Read in text team information
277
 *      from a file
278
 *
279
 *      The source file is a comma seperated file and may contain the
280
 *      folowing items
281
 *
282
 *      Team Number     - Mandatory
283
 *      Team Name       - Mandatory
284
 *      Team Catagory   - Mandatory
285
 *
286
 *      Team Names ...  - Optional
287
 *
288
 *  Parameters:
289
 *      None
290
 *
291
 *  Returns:
292
 *      Nothing
293
 *
294
 *========================================================================*/
295
 
296
void tupload(void)
297
{
298
    int         error = 0;
299
    int         i;
300
    char       *linep;
301
    int         class_count[MAX_CLASS];
302
    int         total;
303
 
304
    cur( 0, 5 );
305
    printf( "Read text file of team information" );
306
 
307
    if( !getfname( "Enter name of the file to read :", ".csv" ) )
308
        return;
309
    printf( "\n" );
310
 
311
    ufile = fopen( ufilename, "rt" );             /* Open the file for reading */
312
    if( ufile == 0 )
313
    {
314
        printf( "Cannot locate data file - %s\n", ufilename );
315
        beep();
316
        sleep( 5 );
317
        return;
318
    }
319
 
320
    memset ( class_count, 0, sizeof( class_count ));
321
 
322
    /*
323
    **  Get the data from the file
324
    **  Read in lines one by one
325
    */
326
    while( fgets( line, sizeof(line) - 10 , ufile ) )
327
    {
328
        linep = line;
329
        /*
330
        **  Skip blank lines
331
        **  Skip leading white space
332
        */
333
        for ( linep = line; isspace( *linep ); linep++ )
334
        {
335
        }
336
        if ( ! *linep )
337
            continue;
338
 
339
        /*
340
        **  The first entry on the line should be team number
341
        **  If it is not a valid team number then skip
342
        */
343
 
344
        if( ! t_parse_number( &linep, &team ) )
345
        {
346
            printf( "No team number: %-30s.\n", line );
347
            error++;
348
        }
349
        else if( ! valid_field( team ) )
350
        {
351
            printf ( "Invalid team number: %d\n", team );
352
            error++;
353
        }
354
        else
355
        {
356
            g_record( team, &team_buf );
357
 
358
            /*
359
            **  Extract a team information from the CSV file
360
            **  These fields will be
361
            **      - Team Name
362
            **      - Category
363
            **      - Member names
364
            */
365
            if ( t_parse_text( &linep, line_text ) )
366
            {
367
                strncpy (team_buf.name,line_text,MAX_TM_NAME);
368
            }
369
 
370
            if ( t_parse_text( &linep, line_text ) )
371
            {
372
                int cat_found = 0;
373
                team_buf.class = lookup_class( line_text, NULL );
374
                if ( team_buf.class > 0)
375
                {
376
                    /*
377
                    **  The team has a category
378
                    **  Now flag the team as valid - it has ALL
379
                    **  the basic information
380
                    */
381
                    team_buf.flags.valid = TRUE;
382
                    cat_found =1;
383
 
384
                    if ( team_buf.class < MAX_CLASS )
385
                        class_count[team_buf.class]++;
386
                }
387
 
388
                if ( !cat_found && *line_text )
389
                {
390
                    printf( "Team: %d - Invalid category:%s\n", team,line_text );
391
                    error++;
392
                }
393
            }
394
 
395
            for( i = 0; i < MAX_MEMB; i++ )
396
            {
397
                if ( t_parse_text( &linep, line_text ) )
398
                {
399
                    strncpy (team_buf.members[i].name,line_text,MAX_PERSON_NAME);
400
                }
401
            }
402
 
403
            put_team_record( team, &team_buf );
404
        }
405
 
406
 
407
        /*
408
        **  printf( ">>>:%s\n", line );
409
        **  if ( 'q' == getinp() ) break;
410
        */
411
 
412
    }
413
 
414
    /*
415
    **  Display a few upload stats
416
    */
417
    total = 0;
418
    for( i = 0; i < config.num_class; i++ )
419
    {
420
        printf( "%*s : %d\n", LEN_CLASS_NAME, config.team_class[i].full_name, class_count[i+1] );
421
        total += class_count[i+1];
422
    }
423
 
424
    printf( "\n%*s : %d\n", LEN_CLASS_NAME, "Total Uploaded", total );
425
    printf( "%*s : %d\n", LEN_CLASS_NAME, "Errors", error );
426
 
427
 
428
    printf( "\nAny key to continue " );
429
    getinp();
430
 
431
    fclose( ufile );
432
}
433
 
434
 
435
/*========================================================================
436
 *
437
 *  Parse a number from a CSV text file
438
 *
439
 *  Purpose:
440
 *      This helper function is called to Parse line of team text
441
 *
442
 *  Parameters:
443
 *      linep           Current input source pointer
444
 *                      Will be updated to point to end of the field
445
 *      number          Number extracted
446
 *
447
 *  Returns:
448
 *      TRUE - Number extracted OK
449
 *      FALSE - No number extracted
450
 *
451
 *========================================================================*/
452
 
453
bool t_parse_number( char **linep, int *number )
454
{
455
    long    lnumber;
456
 
457
    /*
458
    **  Expecting a number
459
    **  strtol will remove leading white space
460
    */
461
    lnumber = strtol( *linep, linep, 10 );
462
 
463
    /*
464
    **  Skip the trailing delimiter(s)
465
    */
466
    while ( p_del(*linep) )
467
    {
468
        (*linep)++;
469
    }
470
 
471
    /*
472
    **  A valid number ?
473
    */
474
    if ( lnumber == 0 )
475
        return FALSE;
476
 
477
    *number = (int) lnumber;
478
    return TRUE;
479
}
480
 
481
/*========================================================================
482
 *
483
 *  Parse a text field from a CSV text file
484
 *
485
 *  Purpose:
486
 *      This helper function is called to Parse line of team text
487
 *
488
 *  Parameters:
489
 *      linep           Current input source pointer
490
 *                      Will be updated to point to end of the field
491
 *      number          Address of buffer to insert text into
492
 *
493
 *  Returns:
494
 *      TRUE - Field extracted OK
495
 *      FALSE - No field extracted
496
 *
497
 *========================================================================*/
498
 
499
bool t_parse_text( char **linep, char *text )
500
{
501
    char    uch;
502
    char    *textp = text;
503
    bool    quoted = FALSE;
504
 
505
    /*
506
    **  If we have already reached the end of the line tne indicate
507
    **  That there is no data
508
    */
509
    uch = **linep;
510
    if ( uch == '\n' || uch == '\r' || uch == '\0' )
511
        return ( FALSE );
512
 
513
    /*
514
    **  Extract the next record
515
    */
516
    while ( TRUE )
517
    {
518
        uch = **linep;
519
 
520
        /*
521
        **  End of the field
522
        */
523
        if ( uch == '\n' || uch == '\r' || uch == '\0' )
524
            break;
525
 
526
        (*linep)++;
527
        if ( !quoted && uch == ',' )
528
        {
529
            break;
530
        }
531
 
532
        /*
533
        **  An unquoted " will start scanning for a matching quote
534
        */
535
        if ( !quoted && uch == '"' )
536
        {
537
            quoted = TRUE;
538
            continue;
539
        }
540
 
541
 
542
        /*
543
        **  A quoted " may be en embedded quote or the end of a quote
544
        */
545
        if ( quoted && uch == '"' )
546
        {
547
            if ( **linep != '"' )
548
            {
549
                quoted = FALSE;
550
                continue;
551
            }
552
 
553
            /*
554
            **  Skip one " and pick up the next
555
            */
556
            (*linep)++;
557
 
558
        }
559
 
560
        /*
561
        **  Save this character
562
        */
563
        *textp++ = uch;
564
    }
565
 
566
    /*
567
    **  Clean up the extracted string
568
    */
569
    *textp = 0;
570
    compact ( text );
571
 
572
    return ( TRUE );
573
}
574
 
575
/*========================================================================
576
 *
577
 *  Test for a delimiter.
578
 *
579
 *  Purpose:
580
 *      This function is called to Test for a delimiter.
581
 *
582
 *  Parameters:
583
 *      c           Character to test
584
 *
585
 *  Returns:
586
 *      TRUE if a delimter (space, tab or comma)
587
 *
588
 *========================================================================*/
589
 
590
char p_del( char *c )
591
{
592
    return ( *c == ' ' || *c == '\t' || *c == ',' || *c == '\0' || *c == '\n' || *c == '\r');
593
}
594
 
595
char p_eol( char *c )
596
{
597
    return ( *c == '\0' || *c == '\n' || *c == '\r' );
598
}
599
 
600
/*========================================================================
601
 *
602
 *  Generate team name file
603
 *
604
 *  Purpose:
605
 *      This function is called to Generate team name file
606
 *
607
 *      The file contains team number,Team name,Team class
608
 *      The operator is prompted to enter the name of the file
609
 *
610
 *  Parameters:
611
 *      None
612
 *
613
 *  Returns:
614
 *      Nothing
615
 *
616
 *========================================================================*/
617
 
618
void tdnload(void)
619
{
620
    int         i;
621
 
622
    cur( 0, 5 );
623
    printf( "Create text file of team information" );
624
 
625
    if( !getfname( "Enter name of the file to create :", ".txt" ) )
626
        return;
627
    printf( "\n" );
628
 
629
    ufile = fopen( ufilename, "wt" );             /* Open the file for writing */
630
    if( ufile == 0 )
631
    {
632
        printf( "Cannot create data file - %s\n", ufilename );
633
        beep();
634
        sleep( 5 );
635
        return;
636
    }
637
 
638
    /*
639
     * Put the data into the file 
640
     */
641
 
642
    for( i = config.min_team; i <= config.max_team; i++ )
643
    {
644
        if( valid_field( i ) && g_record( i, &team_buf ) )
645
        {
646
            fprintf( ufile, "%-5d,%-30s,%-5s\n",
647
                     team_buf.numb,
648
                     team_buf.name,
649
                     team_buf.class >
650
 
651
        }
652
    }
653
    fclose( ufile );
654
}
655
 
656
/*========================================================================
657
 *
658
 *  Generate leg timing file
659
 *
660
 *  Purpose:
661
 *      This function is called to Generate leg timing file
662
 *
663
 *  Parameters:
664
 *      None
665
 *
666
 *  Returns:
667
 *      Nothing
668
 *
669
 *========================================================================*/
670
 
671
void dnload(void)
672
{
673
    int         i;
674
    char        stend[] = "E";
675
 
676
    cur( 0, 5 );
677
    printf( "Generate leg data files" );
678
    while( TRUE )
679
    {
680
        leg = 0;
681
        d_field( 0, 6, "Enter leg to save :", D_NUMBER, 1, ( char * ) &leg,
682
                 TRUE, M_UPDATE );
683
        if( leg == 0 )
684
            return;                              /* Null leg - just exit */
685
        if( leg <= config.num_legs )             /* Valid leg number - exit loop */
686
            break;
687
        beep();                                /* Make a noise and keep waiting for valid number */
688
 
689
    }
690
 
691
 
692
    /*
693
     * Find out if Start or End of leg times to be saved 
694
     */
695
 
696
    do
697
    {
698
        d_field( 0, 7, "Start of End of leg to save :", D_USTRING, 1, stend,
699
                 TRUE, M_UPDATE );
700
    } while( ( stend[0] != 'S' ) && ( stend[0] != 'E' ) );
701
    manstart = ( stend[0] == 'S' ? TRUE : FALSE );
702
 
703
    /*
704
     * Locate the required data file and prepare for processing 
705
     */
706
 
707
    printf( "\n" );
708
    sprintf( ufilename, ( manstart ? "Sleg%d" : "leg%d" ), leg );   /* Create the file name */
709
    ufile = fopen( ufilename, "wt" );             /* Open the file for writing */
710
    if( ufile == 0 )
711
    {
712
        printf( "Cannot create data file - %s\n", ufilename );
713
        beep();
714
        sleep( 5 );
715
        return;
716
    }
717
 
718
    /*
719
     * Write the data to the data file 
720
     */
721
 
722
    for( i = config.min_team; i <= config.max_team; i++ )
723
    {
724
        if( valid_field( i ) && g_record( i, &team_buf ) )
725
        {
726
            if( !manstart
727
                && ( leg <= config.num_legs && team_buf.leg[leg].end >= 0 ) )
728
                fprintf( ufile, "%d %s\n", i,
729
                         time_a( team_buf.leg[leg].end ) );
730
 
731
            if( manstart && team_buf.leg[leg].start >= 0 )
732
                fprintf( ufile, "%d %s\n", i,
733
                         time_a( team_buf.leg[leg].start ) );
734
        }
735
    }
736
    fclose( ufile );
737
}
738
 
739
/*========================================================================
740
 *
741
 *  Get a filename from the user
742
 *
743
 *  Purpose:
744
 *      This function is called to Get a filename from the user
745
 *
746
 *  Parameters:
747
 *      prompt      User prompt
748
 *      ext         File extension
749
 *
750
 *  Returns:
751
 *      TRUE: all is well
752
 *
753
 *========================================================================*/
754
 
755
char getfname( char *prompt, char *ext )
756
{
757
    /*
758
    **  Create a default name if non is present
759
    */
760
    if ( ! *ufilename )
761
    {
762
        sprintf( ufilename, "%s%s", filebase, ext );
763
    }
764
 
765
    d_field( 0, 6, prompt, D_STRING, 40, ufilename, TRUE, M_UPDATE );
766
    if( abort_flag )
767
        return ( FALSE );
768
 
769
    compact( ufilename );
770
    if( ufilename[0] )
771
        return ( TRUE );
772
    return ( FALSE );
773
}
774
 
775
/********************************* EOF ***********************************/