Subversion Repositories svn1

Rev

Rev 170 | Blame | Last modification | View Log | RSS feed

/*************************************************************************
*           Copyright (C) 1995 Embedded Solutions
*                       All rights reserved
*
* file:     src\teamupd.c
*
* purpose:  Team data edit and display
*           This module contains all the functions required to define and
*           alter the team definitions
*
* functions
*       team_update             - Update team data on the screen
*       team_display            - Display team data on the screen
*       leg_mods                - Modify team leg times
*       d_display               - Display all team information
*       d_leg                   - Display / Update leg time information
*       d_field                 - Display / Update field on the screen
*       g_tnum                  - Get a team number from the operator
*       g_record                - Read a team record from disk
*       clr_team                - Clear team data from file
*       put_team_record         - Save team record to disk
*       init_team_data          - Initialize the team data file
*       fix_team_data           - Close team data file
*
* programmer: David Purdie
*
* revision  date        by      reason
*   00.0    27/01/95    DDP     Tidies up the program and formatted the file
*
**************************************************************************/
#include    <QMessageBox>
#include    <fcntl.h>
#include    "consts.h"
#include    "structs.h"
#include    "proto.h"
#include    "mainwindow.h"

int         team_fd;                             /* Team data file descriptor */


/*========================================================================
 *
 *  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:
 *      int_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 */

    pos = _team;
    pos *= sizeof( team_type );
    len = lseek( team_fd, pos, 0 );
    len = read( team_fd, ( char * ) data, sizeof( team_type ) );
    if( ( len == sizeof( team_type ) ) && ( _team == data->numb ) )
        return ( data->flags.valid );
    else
    {
        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 */

    pos = _team;
    pos *= sizeof( team_type );
    lseek( team_fd, pos, 0 );
    len = write( team_fd, ( char * ) data, sizeof( team_type ) );
    if( len != sizeof( team_type ) )
    {
        //perror( "Team record write" );
        MainWindow::showMessage("Error Team record write");
    }
    return ( len == sizeof( team_type ) );
}

/*========================================================================
 *
 *  Initialize the team data file
 *
 *  Purpose:
 *      This function is called to Initialize the team data file and
 *      prepare it for processing
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      FALSE : Error encountered
 *
 *========================================================================*/
bool init_team_data(void)
{
    QString dataFileName(filepath);
    dataFileName.append(datfile);

    team_fd = open( qPrintable(dataFileName), OPEN_RW, 0 );
    if( team_fd < 0 )
    {
        /*
         * 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
                                                       ) )
        {
            qDebug("Cancel to bad team data");
            exit(2);
        }
        else
        {
            qDebug("Create empty Team Data file");
            team_fd = creat( qPrintable(dataFileName), 0664 );
            if( team_fd > 0 )
            {
                close( team_fd );                /* Close the file */
                team_fd = open( qPrintable(dataFileName), OPEN_RW, 644 );
                if( team_fd < 0 )
                {
                    perror( "Team datafile" );
                    qFatal( "Program aborted. Team Data error" );
                }
            }
        }
    }
    return ( team_fd >= 0 );
}

/*========================================================================
 *
 *  Close team data file
 *
 *  Purpose:
 *      This function is called to close the team data file
 *
 *  Parameters:
 *      None
 *
 *  Returns:
 *      Nothing
 *
 *========================================================================*/
void fix_team_data(void)
{
    if( team_fd > 0 )
        close( team_fd );
}

/********************************* EOF ***********************************/