Subversion Repositories svn1-original

Rev

Rev 176 | Rev 179 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 176 Rev 178
Line 1... Line 1...
1
#include "qmconfig.h"
1
#include "qmconfig.h"
2
#include "mainwindow.h"
2
#include "mainwindow.h"
3
#include <QFileDialog>
3
#include <QFileDialog>
4
#include <QObject>
4
#include <QObject>
5
#include <QMessageBox>
5
#include <QMessageBox>
-
 
6
#include <QFileInfo>
-
 
7
#include <QFile>
6
 
8
 
7
// Global Data
9
// Global Data
8
MARA_CFG    config;
10
MARA_CFG    config;
-
 
11
QString     fileName;
9
 
12
 
-
 
13
/*
-
 
14
**  Local definitions
-
 
15
*/
-
 
16
char        confile[20];                         /* Name of the config file */
-
 
17
char        datfile[20];                         /* Name of the data file */
-
 
18
char        filebase[20];                        /* Event file name base */
-
 
19
char        filepath[100];
10
 
20
 
11
QmConfig::QmConfig(const QString &cnfFile)
21
QmConfig::QmConfig(const QString &cnfFile)
12
{
22
{
13
    fileName = cnfFile;
23
    fileName = cnfFile;
14
    if ( !fileName.endsWith(".cnf",Qt::CaseInsensitive))
24
    if ( !fileName.endsWith(".cnf",Qt::CaseInsensitive))
Line 16... Line 26...
16
        fileName.append(".cnf");
26
        fileName.append(".cnf");
17
    }
27
    }
18
    if (cnfFile.isEmpty())
28
    if (cnfFile.isEmpty())
19
    {
29
    {
20
        fileName = QFileDialog::getOpenFileName(0, "Select Config File",
30
        fileName = QFileDialog::getOpenFileName(0, "Select Config File",
21
                                                         QDir::currentPath(),
31
                                                         filepath,
22
                                                         "Data (*.cnf);;All (*.*)",
32
                                                         "Data (*.cnf);;All (*.*)",
23
                                                         0,
33
                                                         0,
24
                                                         0
34
                                                         0
25
                                                         );
35
                                                         );
26
    }
36
    }
27
 
37
 
28
    //  No file selected
38
    //  No file selected
29
    //  Just exit
39
    //  Just exit
30
    if (fileName.isEmpty())
40
    if (fileName.isEmpty())
31
    {
41
    {
32
        qDebug("No Confog file selected");
42
        qDebug("No Config file selected");
33
        exit(1);
43
        exit(1);
34
    }
44
    }
35
 
45
 
-
 
46
    //
-
 
47
    //  Setup ffile names
-
 
48
    //
-
 
49
    QFileInfo info (fileName);
-
 
50
    strncpy(filebase, qPrintable(info.baseName()), 8);
-
 
51
    strcpy( datfile, filebase );
-
 
52
    strcat( datfile, ".dat" );
-
 
53
 
-
 
54
    strncpy(filepath, qPrintable(info.absolutePath()), sizeof(filepath)-3);
-
 
55
    strcat(filepath, "/");
-
 
56
    qDebug("FilePath:%s", filepath );
-
 
57
 
36
    if ( !open_read_config() )
58
    if ( !open_read_config() )
37
    {
59
    {
38
        if (QMessageBox::Cancel == QMessageBox::question ( 0,
60
        if (QMessageBox::Cancel == QMessageBox::question ( 0,
39
                                                       "Config Load Error",
61
                                                       "Config Load Error",
40
                                                       "Cannot load or read configuration file.\n"
62
                                                       "Cannot load or read configuration file.\n"
41
                                                       "If you continue a new configuration will be created\n"
63
                                                       "If you continue a new configuration will be created\n"
42
                                                       "If you cancel then the application will terminate.",
64
                                                       "If you cancel then the application will terminate.",
43
                                                       QMessageBox::Ok | QMessageBox::Cancel
65
                                                       QMessageBox::Ok | QMessageBox::Cancel
44
                                                       ) )
66
                                                       ) )
45
        {
67
        {
46
            qDebug("Canel to bad config");
68
            qDebug("Cancel to bad config");
47
            exit(2);
69
            exit(2);
48
        }
70
        }
49
    }
71
    }
50
}
72
}
51
 
73
 
Line 268... Line 290...
268
 *
290
 *
269
 *========================================================================*/
291
 *========================================================================*/
270
 
292
 
271
bool QmConfig::write_config( void )
293
bool QmConfig::write_config( void )
272
{
294
{
-
 
295
    if (fileName.isEmpty())
-
 
296
    {
-
 
297
        qDebug("No Config file selected");
-
 
298
        return(false);
-
 
299
    }
273
    /*
300
    /*
274
     **  Open as a binary file
301
     **  Open as a binary file
275
     */
302
     */
-
 
303
    QFile file;
276
    QFile file(fileName);
304
    file.setFileName(fileName);
277
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate) )
305
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate) )
278
    {
306
    {
-
 
307
        qDebug("File error: %s", qPrintable(file.errorString()));
279
        MainWindow::showMessage("Cannot opne config file");
308
        MainWindow::showMessage("Cannot open config file");
280
        return (false);
309
        return (false);
281
    }
310
    }
282
 
311
 
283
     /*
312
     /*
284
     **  Write out multiple structures
313
     **  Write out multiple structures
Line 315... Line 344...
315
    file.write( (const char *) &config.num_fame, sizeof( config.num_fame ) );
344
    file.write( (const char *) &config.num_fame, sizeof( config.num_fame ) );
316
 
345
 
317
    file.close();
346
    file.close();
318
    return ( TRUE );
347
    return ( TRUE );
319
}
348
}
-
 
349
 
-
 
350
/*========================================================================
-
 
351
 *
-
 
352
 *  Qsort callback: Sort by team
-
 
353
 *
-
 
354
 *  Purpose:
-
 
355
 *      Function used by the team definition sort operation
-
 
356
 *      It will compare two entries of the team def structure and return an
-
 
357
 *      integer for gt eq lt conditions.
-
 
358
 *      Note : If the start is 0 the team entry does exist and is placed at the
-
 
359
 *      end of the sorted list.
-
 
360
 *
-
 
361
 *  Parameters:
-
 
362
 *      a           comparision entry
-
 
363
 *      b           comparision entry
-
 
364
 *
-
 
365
 *  Returns:
-
 
366
 *      gt, eq, lt as required
-
 
367
 *
-
 
368
 *========================================================================*/
-
 
369
 
-
 
370
int f_comp_int( const void *aa, const void *bb )
-
 
371
{
-
 
372
    const ty_t_def *a = (ty_t_def *)aa;
-
 
373
    const ty_t_def *b = (ty_t_def *)bb;
-
 
374
 
-
 
375
    if( a->start == 0 )
-
 
376
        return ( 1 );
-
 
377
    else if( b->start == 0 )
-
 
378
        return ( -1 );
-
 
379
    else
-
 
380
        return ( a->start - b->start );
-
 
381
}
-
 
382
 
-
 
383
/*========================================================================
-
 
384
 *
-
 
385
 *  Compact a string
-
 
386
 *
-
 
387
 *  Purpose:
-
 
388
 *      This function is called remove leading and trailing spaces from
-
 
389
 *      a string. Treats other non-printing characters as leading
-
 
390
 *      spaces. This solves a problem when importing data from a
-
 
391
 *      Microsoft CSV file with empty fields.
-
 
392
 *
-
 
393
 *  Parameters:
-
 
394
 *      str     Address of the string to compact
-
 
395
 *
-
 
396
 *  Returns:
-
 
397
 *      Nothing
-
 
398
 *
-
 
399
 *========================================================================*/
-
 
400
 
-
 
401
void compact( char *str )
-
 
402
{
-
 
403
    char       *ptr;
-
 
404
 
-
 
405
    ptr = str;
-
 
406
    while( *str && ( isspace( *str ) || !isprint( *str ) ) )
-
 
407
        str++;
-
 
408
    strcpy( ptr, str );
-
 
409
}
-
 
410
 
-
 
411
/*========================================================================
-
 
412
 *
-
 
413
 *  Validate a team number
-
 
414
 *
-
 
415
 *  Purpose:
-
 
416
 *      This function is called to validate a team number
-
 
417
 *
-
 
418
 *  Parameters:
-
 
419
 *      x       Number to validate
-
 
420
 *
-
 
421
 *  Returns:
-
 
422
 *      TRUE    : Valid
-
 
423
 *      FALSE   : Not valid
-
 
424
 *
-
 
425
 *========================================================================*/
-
 
426
 
-
 
427
bool valid_field( int x )
-
 
428
{
-
 
429
    int         i;
-
 
430
 
-
 
431
    for( i = 0; i < config.num_teams; i++ )
-
 
432
    {
-
 
433
        if( x <= config.t_def[i].end && x >= config.t_def[i].start )
-
 
434
            return ( TRUE );
-
 
435
        if( x < config.t_def[i].start )
-
 
436
            break;                               /* Because the list is sorted */
-
 
437
    }
-
 
438
    return ( FALSE );
-
 
439
}
-
 
440
 
-
 
441
/*========================================================================
-
 
442
 *
-
 
443
 *  Get a class descriptor from existing text
-
 
444
 *
-
 
445
 *  Purpose:
-
 
446
 *      This function is called to Get a class descriptor
-
 
447
 *
-
 
448
 *  Parameters:
-
 
449
 *      text    - User text to examine
-
 
450
 *      config  - configuration dtaa to use
-
 
451
 *
-
 
452
 *  Returns:
-
 
453
 *      An integer which is the index into the  config.team_class array
-
 
454
 *      The integer is in the range 1 .. num_class
-
 
455
 *      A value fo zero indicates the text was not found.
-
 
456
 *
-
 
457
 *========================================================================*/
-
 
458
 
-
 
459
int lookup_class( const char *text, MARA_CFG * config_ptr )
-
 
460
{
-
 
461
    int         i;
-
 
462
 
-
 
463
    if( config_ptr == NULL )
-
 
464
        config_ptr = &config;
-
 
465
 
-
 
466
    /*
-
 
467
     * Attempt to locate the entered class in the list of defined classes
-
 
468
     */
-
 
469
 
-
 
470
    for( i = 0; i < config_ptr->num_class; i++ )
-
 
471
    {
-
 
472
        if( toupper(text[0]) == toupper(config_ptr->team_class[i].abr[0]) &&
-
 
473
            toupper(text[1]) == toupper(config_ptr->team_class[i].abr[1]) )
-
 
474
            return ( ++i );
-
 
475
    }
-
 
476
    return ( 0 );
-
 
477
}