Subversion Repositories svn1

Rev

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