Subversion Repositories svn1-original

Rev

Rev 95 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
95 - 1
/*************************************************************************
2
*           Copyright (C) 1992 Embedded Solutions.
3
*                       All rights reserved
4
*
5
* file:         noname.c
6
*
7
* purpose:      Create marathon time record in a manner
8
*               compatible with the BRMR program, as per the GETX07
9
*               program.
10
*
11
* functions:
12
*       main                    - Program entry point
13
*       get_team_number         - Get a team number from the user
14
*       read_field_data         - Read in existing team info
15
*       display_help            - Display help to the user
16
*
17
* programmer: David Purdie (DDP)
18
*
19
* revision  date        by      reason
20
*   00.0    19-Mar-94   DDP     created
21
*
22
**************************************************************************/
23
 
24
#include    <stdio.h>
25
#include    <stdlib.h>
26
#include    <ctype.h>
27
#include    <time.h>
28
 
29
#define TRUE    1
30
#define FALSE   0
31
 
32
/*
33
**  Global variables
34
*/
35
int         leg = 0;
36
char        ans;
37
FILE       *file;
38
char        filename[12];
39
char       *leg_table[] = {
40
    "Run",
41
    "Canoe",
42
    "Swim",
43
    "Horse",
44
    "Cycle",
45
};
46
 
47
time_t      teamtime[1000];
48
 
49
 
50
/*
51
**  Forward prototypes
52
*/
53
void        read_field_data( FILE * file );
54
void        display_help( int );
55
int get_team_number(void);
56
 
57
 
58
/*========================================================================
59
 *
60
 *  Program entry point
61
 *
62
 *  Purpose:
63
 *      This function is called start the program
64
 *
65
 *  Parameters:
66
 *      argc            - Number of arguments
67
 *      argv            - Address of argument pointers
68
 *
69
 *  Returns:
70
 *      Nothing
71
 *      Exit with valid exit codes
72
 *
73
 *========================================================================*/
74
 
75
int main( int argc, char *argv[] )
76
{
77
    char        input[30];
78
    int         team;
79
    time_t     *teamptr;
80
    time_t      curtime;
81
    char        data_entered = FALSE;
82
 
83
    /*
84
     **  Display HELP
85
     */
86
    display_help( 0 );
87
 
88
 
89
    /*
90
     **  Determine the leg to get data for
91
     */
92
    if( argc >= 2 )
93
        leg = atoi( argv[1] );
94
 
95
/*
96
**  Initialise the team data base
97
*/
98
    for( team = 0, teamptr = teamtime; team < 999; team++, teamptr++ )
99
    {
100
        *teamptr = 0;
101
    }
102
 
103
 
104
    do
105
    {
106
        while( leg == 0 )
107
        {
108
            printf( "Which leg to collect data for : " );
109
            scanf( "%d", &leg );
110
            fflush( stdin );
111
        }
112
 
113
        if( ( leg == 0 )
114
            || ( leg > sizeof( leg_table ) / sizeof( leg_table[0] ) ) )
115
        {
116
            printf( "Invalid leg\n" );
117
            leg = 0;
118
            continue;
119
        }
120
 
121
        sprintf( filename, "LEG%1.1d", leg );
122
        file = fopen( filename, "at+" );
123
        if( file == NULL )
124
        {
125
            printf( "Error opening file\n" );
126
            exit( 1 );
127
        }
128
 
129
        fseek( file, 0, SEEK_END );
130
        if( ftell( file ) != 0 )
131
        {
132
            printf( "Leg data already exists. Append new data ? " );
133
            scanf( "%c", &ans );
134
            fflush( stdin );
135
            if( toupper( ans ) != 'Y' )
136
            {
137
                fclose( file );
138
                leg = 0;
139
                continue;
140
            }
141
            read_field_data( file );
142
        }
143
    } while( leg == 0 );
144
 
145
/*
146
**  Main loop: Collect team numbers from the uer
147
*/
148
    while( 1 )
149
    {
150
        /*
151
         ** Get the team number
152
         */
153
        printf( "Enter team > " );
154
        team = get_team_number();
155
        teamptr = &teamtime[team % 1000];
156
 
157
        /*
158
         **  Team-999 Exit
159
         */
160
        if( team == 999 )
161
            break;
162
 
163
        /*
164
         **  Just display the time
165
         */
166
        else if( team == 0 )
167
        {
168
            time( &curtime );
169
            printf( " %8.8s\n", ctime( &curtime ) + 11 );
170
            continue;
171
        }
172
 
173
        /*
174
         **  Save data to disk
175
         */
176
        else if( team == -1 )
177
        {
178
            if( data_entered )
179
            {
180
                fflush( file );
181
                fclose( file );
182
                file = fopen( filename, "at+" );
183
                if( file == NULL )
184
                {
185
                    printf( " Panic Cannot re-open data file\n" );
186
                    exit( 1 );
187
                }
188
                else
189
                    printf( " Data saved to disk\n" );
190
                data_entered = FALSE;
191
            }
192
            else
193
                printf( "\r" );
194
            continue;
195
        }
196
 
197
        else if( team == -2 )
198
            display_help( 1 );
199
 
200
        /*
201
         **  Determine operation
202
         **      1..998      Enter team data
203
         */
204
        else if( team < 999 )
205
        {
206
            if( *teamptr )
207
            {
208
                printf( " Already entered at %8.8s\a\n",
209
                        ctime( teamptr ) + 11 );
210
            }
211
            else
212
            {
213
                time( teamptr );
214
                printf( " %8.8s\n", ctime( teamptr ) + 11 );
215
                fprintf( file, "%3.3d %8.8s\n", team, ctime( teamptr ) + 11 );
216
                data_entered = TRUE;
217
            }
218
        }
219
 
220
        /*
221
         **  Overwrite team information
222
         **      1001..1998
223
         */
224
        else if( team < 1999 )
225
        {
226
            if( *teamptr )
227
            {
228
                printf( " Was %8.8s. ", ctime( teamptr ) + 11 );
229
                time( teamptr );
230
                printf( " Now %8.8s\a\n", ctime( teamptr ) + 11 );
231
                fprintf( file, "%3.3d %8.8s\n", team, ctime( teamptr ) + 11 );
232
                data_entered = TRUE;
233
            }
234
        }
235
 
236
        /*
237
         **  Display team time
238
         **      2001..2998
239
         */
240
        else if( team < 2999 )
241
        {
242
            if( *teamptr )
243
            {
244
                printf( " Entered as %8.8s\a\n", ctime( teamptr ) + 11 );
245
            }
246
            else
247
                printf( " Has not been entered\n" );
248
        }
249
 
250
        /*
251
         **  Invalid team number
252
         */
253
        else
254
        {
255
            printf( " Invalid team number\a\n" );
256
        }
257
 
258
    }
259
 
260
    /*
261
     **  Happy ending. Save the data and exit
262
     **  As some data may be duplicated on disk we should re-write the
263
     **  Entire file
264
     */
265
    fclose( file );
266
    printf( "\nCreating ordered data file\n" );
267
 
268
    file = fopen( "LEG.WRK", "wtt" );
269
    if( file == NULL )
270
    {
271
        printf( "Error creating linear data record of times\n" );
272
        exit( 1 );
273
    }
274
    for( team = 1, teamptr = &teamtime[1]; team < 999; team++, teamptr++ )
275
    {
276
        if( *teamptr )
277
            fprintf( file, "%3.3d %8.8s\n", team, ctime( teamptr ) + 11 );
278
    }
279
    fclose( file );
280
    remove( filename );
281
    if( rename( "LEG.WRK", filename ) != 0 )
282
    {
283
        perror( "Error renaming work file" );
284
        exit( 1 );
285
    }
286
 
287
    exit( 0 );
288
}
289
 
290
/*========================================================================
291
 *
292
 *  Get a team number from the user
293
 *
294
 *  Purpose:
295
 *      This function is called to get a team number from the user
296
 *
297
 *  Parameters:
298
 *      None
299
 *
300
 *  Returns:
301
 *      Team number
302
 *          0               - Just display time
303
 *          -1              - Time to save data
304
 *          -2              - Display help
305
 *
306
 *===========================================================================*/
307
 
308
int get_team_number(void)
309
{
310
    int         team = 0;
311
    int         num_digits = 0;
312
    int         ch;
313
    time_t      dummy;
314
    time_t      last_time = time( &dummy );
315
 
316
    /*
317
     **  Collect a number
318
     */
319
    for( ;; )
320
    {
321
        if( kbhit() )
322
        {
323
            ch = getch();
324
 
325
            /*
326
             **  Process the character
327
             */
328
            if( ch == '?' && num_digits == 0 )
329
                return ( -2 );
330
 
331
            if( ( ch == '\r' ) || ( ch == '\n' ) || ( ch == ' ' )
332
                || ( ch == ',' ) || ( ch == 0x1b ) || ( ch == 0x03 ) )
333
                return ( team );
334
 
335
            else if( ( ch >= '0' && ch <= '9' ) && ( num_digits < 4 ) )
336
            {
337
                num_digits += 1;
338
                team = team * 10 + ch - '0';
339
                putch( ch );
340
            }
341
 
342
            else if( ( ch == '\b' || ch == 0x177 ) && ( num_digits ) )
343
            {
344
                num_digits -= 1;
345
                team = team / 10;
346
                cputs( "\b \b" );
347
            }
348
            else
349
                putch( '\a' );
350
        }
351
 
352
        /*
353
         **  No key available
354
         **  Check time and save data if required
355
         */
356
        else if( num_digits == 0 )
357
        {
358
            if( time( &dummy ) - last_time > 20 )
359
                return ( -1 );
360
        }
361
    }
362
}
363
 
364
 
365
/*========================================================================
366
 *
367
 *  Read in existing team info
368
 *
369
 *  Purpose:
370
 *      This function is called to Read in existing team info
371
 *
372
 *  Parameters:
373
 *      file    - File pointer
374
 *
375
 *  Returns:
376
 *      Nothing
377
 *
378
 *========================================================================*/
379
 
380
void read_field_data( FILE * file )
381
{
382
    int         cnt;
383
    int         team, hh, mm, ss;
384
 
385
    /*
386
     **  Start from the begining
387
     */
388
    fseek( file, 0, SEEK_SET );
389
    while( feof( file ) == 0 )
390
    {
391
        cnt = fscanf( file, "%d %d:%d:%d", &team, &hh, &mm, &ss );
392
        if( cnt == 4 && team > 0 && team < 9999 )
393
        {
394
            teamtime[team] =
395
                ( long ) ss + ( ( long ) mm * 60 ) +
396
                ( ( long ) hh * 60 * 60 );
397
        }
398
    }
399
    /*
400
     **  Ensure file is at end
401
     */
402
    fseek( file, 0, SEEK_END );
403
}
404
 
405
/*========================================================================
406
 *
407
 *  Display help to the user
408
 *
409
 *  Purpose:
410
 *      This function is called to Display help to the user
411
 *
412
 *  Parameters:
413
 *      type            1 - Short help
414
 *                        - Long help
415
 *
416
 *  Returns:
417
 *      Nothing
418
 *
419
 *========================================================================*/
420
 
421
void display_help( int type )
422
{
423
    printf( "\n" );
424
    if( type != 1 )
425
    {
426
        printf
427
            ( "This program will simulate marathon data collection on a PC\n" );
428
        printf( "This program will save data to disk periodically\n" );
429
        printf( "Commands are:\n" );
430
    }
431
    printf( " nnn    - Register team time\n" );
432
    printf( "1nnn    - Force team time, override any existing time\n" );
433
    printf( "2nnn    - Display team time\n" );
434
    printf( " 999    - Exit program (DO NOT USE CONTROL-C)\n" );
435
}
436
 
437
/********************************* EOF ***********************************/