Rev 381 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include "qmteamdata.h"#include <QMessageBox>#include <QDebug>#include "mainwindow.h"#include "consts.h"#include "structs.h"#include "proto.h"static QmTeamData *instance = NULL;QmTeamData::QmTeamData(){if (instance){qFatal("Multiple instances of TeamData not allowed");}instance = this;writable = false;}void QmTeamData::initTeamData( const QString &filen){// Default: Base filename on the config nameif (filen.isEmpty()){fileName = filepath;fileName.append(datfile);}else{fileName = filen;}// Open the filefile.setFileName(fileName);if ( ! file.exists() ){/** file does not exist - create it*/if (QMessageBox::Cancel == QMessageBox::question ( 0,"Data File Load Error","Cannot load or read Team Data file.\n""If you continue a new (empty) file will be created\n""If you cancel then the application will terminate.",QMessageBox::Ok | QMessageBox::Cancel) ){qFatal("Cancel to bad team data");}}writable = false;if ( ! file.open(QIODevice::ReadWrite|QIODevice::Unbuffered) ){qDebug ("Cannot create/open team data file for writing");if ( ! file.open(QIODevice::ReadOnly|QIODevice::Unbuffered) ){qDebug ("Cannot create/open team data file at all");}else{MainWindow::showMessage("Data File is Read Only");}}else{writable = true;}}void QmTeamData::closeTeamData(void){file.close();}/*========================================================================** Read a team record from disk** Purpose:* This function is called to Read a team record from disk* Locate the team record in the team data file* if the record does not exist create a blank record** The function will return FALSE if the record was not on disk* otherwise TRUE** Parameters:* team Team record required* data Area to return record** Returns:* FALSE : No data available**========================================================================*/bool g_record( int team, team_type * data ){int len; /* length of record read in */long pos; /* File pointer */if (!instance){qFatal("Attempt to read team data before initialised");}pos = team;pos *= sizeof( team_type );if (instance->file.seek( pos )){len = instance->file.read( ( char * ) data, sizeof( team_type ) );if( ( len == sizeof( team_type ) ) && ( team == data->numb ) ){return ( data->flags.valid );}}else{qDebug("Bad Seek for team:%d", team);}clr_team( team, data );return ( FALSE );}/*========================================================================** Clear team data from file** Purpose:* This function is called to Clear team data from file* This routine will set the team data to INVALID** Parameters:* tm Team number* data Pointer to the data** Returns:* Nothing**========================================================================*/void clr_team( int tm, team_type * data ){int len;memset( ( char * ) data, 0, ( int ) sizeof( team_type ) );data->flags.valid = FALSE;data->flags.bad_times = TRUE;data->numb = tm;for( len = 0; len < MAX_LEGS + 1; len++ ){data->leg[len].start = -1;data->leg[len].end = -1;data->leg[len].elapsed = -1;data->leg[len].l_place = 0;data->leg[len].le_place = 0;data->leg[len].lec_place = 0;data->leg[len].lc_place = 0;data->leg[len].manual = 0;}}/*========================================================================** Save team record to disk** Purpose:* This function is called to save team record to disk** Parameters:* team Team number* data Pointer to the data** Returns:* FALSE : Error in save**========================================================================*/bool put_team_record( int team, team_type * data ){int len; /* length of record read in */long pos; /* file pointer */if (!instance){qFatal("Attempt to read team data before initialised");}if ( !instance->writable){qDebug("Attempt to write ReadOnly data:%d", team);MainWindow::showMessage("Cannot save data. Team Data is read only");return false;}data->numb = team;pos = team;pos *= sizeof( team_type );if (instance->file.seek( pos )){len = instance->file.write( ( char * ) data, sizeof( team_type ) );if( len != sizeof( team_type ) ){qDebug("Error writing team data:%d", team);MainWindow::showMessage("Error Team record write");return false;}return true;}qDebug("Bad Seek for writing team:%d", team);return false;}