Subversion Repositories svn1

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
91 - 1
/*************************************************************************
2
*           Copyright (C) 1995 Embedded Solutions
3
*                       All rights reserved
4
*
5
* file:     src\teamupd.c
6
*
7
* purpose:  Team data edit and display
8
*           This module contains all the functions required to define and
9
*           alter the team definitions
10
*
11
* functions
12
*       team_update             - Update team data on the screen
13
*       team_display            - Display team data on the screen
14
*       leg_mods                - Modify team leg times
15
*       d_display               - Display all team information
16
*       d_leg                   - Display / Update leg time information
17
*       d_field                 - Display / Update field on the screen
18
*       g_tnum                  - Get a team number from the operator
19
*       g_record                - Read a team record from disk
20
*       clr_team                - Clear team data from file
21
*       put_team_record         - Save team record to disk
22
*       init_team_data          - Initialize the team data file
23
*       fix_team_data           - Close team data file
24
*
25
* programmer: David Purdie
26
*
27
* revision  date        by      reason
28
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
29
*
30
**************************************************************************/
174 - 31
#include    <QMessageBox>
91 - 32
#include    <fcntl.h>
33
#include    "consts.h"
34
#include    "structs.h"
35
#include    "proto.h"
170 - 36
#include    "mainwindow.h"
37
 
91 - 38
int         team_fd;                             /* Team data file descriptor */
39
 
40
 
41
/*========================================================================
42
 *
43
 *  Read a team record from disk
44
 *
45
 *  Purpose:
46
 *      This function is called to Read a team record from disk
47
 *      Locate the team record in the team data file
48
 *      if the record does not exist create a blank record
49
 *
50
 *      The function will return FALSE if the record was not on disk
51
 *      otherwise TRUE
52
 *
53
 *  Parameters:
54
 *      int_team        Team record required
55
 *      data            Area to return record
56
 *
57
 *  Returns:
58
 *      FALSE : No data available
59
 *
60
 *========================================================================*/
61
bool g_record( int _team, team_type * data )
62
{
63
    int         len;                             /* length of record read in */
64
    long        pos;                             /* File pointer */
65
 
66
    pos = _team;
67
    pos *= sizeof( team_type );
68
    len = lseek( team_fd, pos, 0 );
69
    len = read( team_fd, ( char * ) data, sizeof( team_type ) );
70
    if( ( len == sizeof( team_type ) ) && ( _team == data->numb ) )
71
        return ( data->flags.valid );
72
    else
73
    {
74
        clr_team( _team, data );
75
        return ( FALSE );
76
    }
77
}
78
 
79
/*========================================================================
80
 *
81
 *  Clear team data from file
82
 *
83
 *  Purpose:
84
 *      This function is called to Clear team data from file
85
 *      This routine will set the team data to INVALID
86
 *
87
 *  Parameters:
88
 *      tm          Team number
89
 *      data        Pointer to the data
90
 *
91
 *  Returns:
92
 *      Nothing
93
 *
94
 *========================================================================*/
95
void clr_team( int tm, team_type * data )
96
{
97
    int         len;
98
 
99
    memset( ( char * ) data, 0, ( int ) sizeof( team_type ) );
100
    data->flags.valid = FALSE;
101
    data->flags.bad_times = TRUE;
102
    data->numb = tm;
103
    for( len = 0; len < MAX_LEGS + 1; len++ )
104
    {
105
        data->leg[len].start = -1;
106
        data->leg[len].end = -1;
107
        data->leg[len].elapsed = -1;
108
        data->leg[len].l_place = 0;
109
        data->leg[len].le_place = 0;
110
        data->leg[len].lec_place = 0;
111
        data->leg[len].lc_place = 0;
112
        data->leg[len].manual = 0;
113
    }
114
}
115
 
116
/*========================================================================
117
 *
118
 *  Save team record to disk
119
 *
120
 *  Purpose:
121
 *      This function is called to save team record to disk
122
 *
123
 *  Parameters:
124
 *      _team       Team number
125
 *      data        Pointer to the data
126
 *
127
 *  Returns:
128
 *      FALSE : Error in save
129
 *
130
 *========================================================================*/
131
bool put_team_record( int _team, team_type * data )
132
{
133
    int         len;                             /* length of record read in */
134
    long        pos;                             /* file pointer */
135
 
136
    pos = _team;
137
    pos *= sizeof( team_type );
138
    lseek( team_fd, pos, 0 );
139
    len = write( team_fd, ( char * ) data, sizeof( team_type ) );
140
    if( len != sizeof( team_type ) )
141
    {
170 - 142
        //perror( "Team record write" );
143
        MainWindow::showMessage("Error Team record write");
91 - 144
    }
145
    return ( len == sizeof( team_type ) );
146
}
147
 
148
/*========================================================================
149
 *
150
 *  Initialize the team data file
151
 *
152
 *  Purpose:
153
 *      This function is called to Initialize the team data file and
154
 *      prepare it for processing
155
 *
156
 *  Parameters:
157
 *      None
158
 *
159
 *  Returns:
160
 *      FALSE : Error encountered
161
 *
162
 *========================================================================*/
163
bool init_team_data(void)
164
{
174 - 165
    QString dataFileName(filepath);
166
    dataFileName.append(datfile);
91 - 167
 
174 - 168
    team_fd = open( qPrintable(dataFileName), OPEN_RW, 0 );
91 - 169
    if( team_fd < 0 )
170
    {
171
        /*
172
         * file does not exist - create it 
173
         */
174 - 174
        if (QMessageBox::Cancel == QMessageBox::question ( 0,
175
                                                       "Data File Load Error",
176
                                                       "Cannot load or read Team Data file.\n"
177
                                                       "If you continue a new (empty) file will be created\n"
178
                                                       "If you cancel then the application will terminate.",
179
                                                       QMessageBox::Ok | QMessageBox::Cancel
180
                                                       ) )
181
        {
182
            qDebug("Cancel to bad team data");
183
            exit(2);
184
        }
185
        else
186
        {
187
            qDebug("Create empty Team Data file");
188
            team_fd = creat( qPrintable(dataFileName), 0664 );
189
            if( team_fd > 0 )
190
            {
191
                close( team_fd );                /* Close the file */
192
                team_fd = open( qPrintable(dataFileName), OPEN_RW, 644 );
193
                if( team_fd < 0 )
194
                {
195
                    perror( "Team datafile" );
196
                    qFatal( "Program aborted. Team Data error" );
197
                }
198
            }
199
        }
91 - 200
    }
201
    return ( team_fd >= 0 );
202
}
203
 
204
/*========================================================================
205
 *
206
 *  Close team data file
207
 *
208
 *  Purpose:
209
 *      This function is called to close the team data file
210
 *
211
 *  Parameters:
212
 *      None
213
 *
214
 *  Returns:
215
 *      Nothing
216
 *
217
 *========================================================================*/
218
void fix_team_data(void)
219
{
220
    if( team_fd > 0 )
221
        close( team_fd );
222
}
223
 
224
/********************************* EOF ***********************************/