Subversion Repositories svn1-original

Rev

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

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