Subversion Repositories svn1

Rev

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

Rev Author Line No. Line
177 - 1
#include "qmteamdata.h"
2
#include <QMessageBox>
3
#include <QDebug>
4
#include "mainwindow.h"
5
 
6
#include    "consts.h"
7
#include    "structs.h"
8
#include    "proto.h"
9
 
10
static QmTeamData   *instance = NULL;
11
 
12
QmTeamData::QmTeamData()
13
{
14
    if (instance)
15
    {
16
        qFatal("Multiple instances of TeamData not allowed");
17
    }
18
    instance = this;
218 david 19
    writable = false;
177 - 20
}
21
 
22
void QmTeamData::initTeamData( const QString &filen)
23
{
24
    // Default: Base filename on the config name
25
    if (filen.isEmpty())
26
    {
27
        fileName = filepath;
28
        fileName.append(datfile);
29
    }
30
    else
31
    {
32
        fileName = filen;
33
    }
34
 
35
    // Open the file
36
    file.setFileName(fileName);
37
    if ( ! file.exists() )
38
    {
39
        /*
40
         * file does not exist - create it
41
         */
42
        if (QMessageBox::Cancel == QMessageBox::question ( 0,
43
                                                       "Data File Load Error",
44
                                                       "Cannot load or read Team Data file.\n"
45
                                                       "If you continue a new (empty) file will be created\n"
46
                                                       "If you cancel then the application will terminate.",
47
                                                       QMessageBox::Ok | QMessageBox::Cancel
48
                                                       ) )
49
        {
50
            qFatal("Cancel to bad team data");
51
        }
52
    }
53
 
218 david 54
    writable = false;
177 - 55
    if ( ! file.open(QIODevice::ReadWrite|QIODevice::Unbuffered) )
56
    {
218 david 57
        qDebug ("Cannot create/open team data file for writing");
58
        if ( ! file.open(QIODevice::ReadOnly|QIODevice::Unbuffered) )
59
        {
60
 
61
            qDebug ("Cannot create/open team data file at all");
62
        }
63
        else
64
        {
65
            MainWindow::showMessage("Data File is Read Only");
66
        }
177 - 67
    }
218 david 68
    else
69
    {
70
        writable = true;
71
    }
177 - 72
}
73
 
74
 
75
void QmTeamData::closeTeamData(void)
76
{
77
    file.close();
78
}
79
 
80
/*========================================================================
81
 *
82
 *  Read a team record from disk
83
 *
84
 *  Purpose:
85
 *      This function is called to Read a team record from disk
86
 *      Locate the team record in the team data file
87
 *      if the record does not exist create a blank record
88
 *
89
 *      The function will return FALSE if the record was not on disk
90
 *      otherwise TRUE
91
 *
92
 *  Parameters:
218 david 93
 *      team            Team record required
177 - 94
 *      data            Area to return record
95
 *
96
 *  Returns:
97
 *      FALSE : No data available
98
 *
99
 *========================================================================*/
218 david 100
bool g_record( int team, team_type * data )
177 - 101
{
102
    int         len;                             /* length of record read in */
103
    long        pos;                             /* File pointer */
104
 
105
    if (!instance)
106
    {
107
        qFatal("Attempt to read team data before initialised");
108
    }
109
 
218 david 110
    pos = team;
177 - 111
    pos *= sizeof( team_type );
112
    if (instance->file.seek( pos ))
113
    {
114
        len = instance->file.read( ( char * ) data, sizeof( team_type ) );
218 david 115
        if( ( len == sizeof( team_type ) ) && ( team == data->numb ) )
177 - 116
        {
117
            return ( data->flags.valid );
118
        }
119
    }
120
    else
121
    {
218 david 122
        qDebug("Bad Seek for team:%d", team);
177 - 123
    }
218 david 124
    clr_team( team, data );
177 - 125
    return ( FALSE );
126
}
127
 
128
/*========================================================================
129
 *
130
 *  Clear team data from file
131
 *
132
 *  Purpose:
133
 *      This function is called to Clear team data from file
134
 *      This routine will set the team data to INVALID
135
 *
136
 *  Parameters:
137
 *      tm          Team number
138
 *      data        Pointer to the data
139
 *
140
 *  Returns:
141
 *      Nothing
142
 *
143
 *========================================================================*/
144
void clr_team( int tm, team_type * data )
145
{
146
    int         len;
147
 
148
    memset( ( char * ) data, 0, ( int ) sizeof( team_type ) );
149
    data->flags.valid = FALSE;
150
    data->flags.bad_times = TRUE;
151
    data->numb = tm;
152
    for( len = 0; len < MAX_LEGS + 1; len++ )
153
    {
154
        data->leg[len].start = -1;
155
        data->leg[len].end = -1;
156
        data->leg[len].elapsed = -1;
157
        data->leg[len].l_place = 0;
158
        data->leg[len].le_place = 0;
159
        data->leg[len].lec_place = 0;
160
        data->leg[len].lc_place = 0;
161
        data->leg[len].manual = 0;
162
    }
163
}
164
 
165
/*========================================================================
166
 *
167
 *  Save team record to disk
168
 *
169
 *  Purpose:
170
 *      This function is called to save team record to disk
171
 *
172
 *  Parameters:
218 david 173
 *      team       Team number
177 - 174
 *      data        Pointer to the data
175
 *
176
 *  Returns:
177
 *      FALSE : Error in save
178
 *
179
 *========================================================================*/
218 david 180
bool put_team_record( int team, team_type * data )
177 - 181
{
182
    int         len;                             /* length of record read in */
183
    long        pos;                             /* file pointer */
184
 
185
    if (!instance)
186
    {
187
        qFatal("Attempt to read team data before initialised");
188
    }
189
 
218 david 190
    if ( !instance->writable)
191
    {
192
        qDebug("Attempt to write ReadOnly data:%d", team);
193
        MainWindow::showMessage("Cannot save data. Team Data is read only");
194
        return false;
195
    }
196
 
380 david 197
    data->numb = team;
198
 
218 david 199
    pos = team;
177 - 200
    pos *= sizeof( team_type );
201
    if (instance->file.seek( pos ))
202
    {
203
        len = instance->file.write( ( char * ) data, sizeof( team_type ) );
204
        if( len != sizeof( team_type ) )
205
        {
218 david 206
            qDebug("Error writing team data:%d", team);
177 - 207
            MainWindow::showMessage("Error Team record write");
208
            return false;
209
        }
210
        return true;
211
    }
212
 
218 david 213
    qDebug("Bad Seek for writing team:%d", team);
177 - 214
    return false;
215
}
216
 
217
 
218
 
219