Subversion Repositories svn1

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
172 - 1
#include "qmconfig.h"
2
#include "mainwindow.h"
3
#include <QFileDialog>
4
#include <QObject>
5
#include <QMessageBox>
174 - 6
#include <QFileInfo>
7
#include <QFile>
199 david 8
#include <QCoreApplication>
320 david 9
#include <QSettings>
172 - 10
 
210 - 11
//#define DISPLAY_STRUCTURES
208 - 12
 
172 - 13
// Global Data
176 - 14
QmConfig    config;
172 - 15
 
320 david 16
// Application Config
17
//  Only use this to store user setting
18
//  Thinks like : 
19
//      Last path for loaded file
20
//      Last entered uploaded leg
21
//      Swim Start: Leg Set up Delta and base
176 - 22
 
320 david 23
QSettings   *appSettings = NULL;
24
 
25
 
174 - 26
/*
27
**  Local definitions
28
*/
29
char        datfile[20];                         /* Name of the data file */
30
char        filebase[20];                        /* Event file name base */
198 - 31
char        filepath[300];
172 - 32
 
320 david 33
/*----------------------------------------------------------------------------
34
** FUNCTION           : QmConfig 
35
**
36
** DESCRIPTION        : Constructor
37
**
38
**
39
** INPUTS             :
40
**
41
----------------------------------------------------------------------------*/
199 david 42
 
320 david 43
QmConfig::QmConfig(void)
44
{
45
    /*
46
    ** Init appSettings - once !
47
    */
48
    if (!appSettings)
49
    {
50
        appSettings = new QSettings(getAddendemFile("mara.ini", true), QSettings::IniFormat); 
51
    }
52
}
53
 
54
/*----------------------------------------------------------------------------
55
** FUNCTION           : ~QmConfig
56
**
57
** DESCRIPTION        : Destructor
58
**
59
**
60
----------------------------------------------------------------------------*/
61
 
62
QmConfig::~QmConfig()
63
{
64
    /*
65
    ** This will force the items to be flushed
66
    */
67
    if (appSettings)
68
    {
69
        delete(appSettings);
70
    }
71
}
72
 
176 - 73
void QmConfig::load(const QString &cnfFile)
172 - 74
{
208 - 75
#ifdef DISPLAY_STRUCTURES
76
    display_structures();
77
#endif
172 - 78
    fileName = cnfFile;
199 david 79
 
172 - 80
    if ( !fileName.endsWith(".cnf",Qt::CaseInsensitive))
81
    {
82
        fileName.append(".cnf");
83
    }
84
    if (cnfFile.isEmpty())
85
    {
86
        fileName = QFileDialog::getOpenFileName(0, "Select Config File",
174 - 87
                                                         filepath,
172 - 88
                                                         "Data (*.cnf);;All (*.*)",
89
                                                         0,
90
 
91
                                                         );
92
    }
93
 
94
    //  No file selected
95
    //  Just exit
96
    if (fileName.isEmpty())
97
    {
174 - 98
        qDebug("No Config file selected");
172 - 99
        exit(1);
100
    }
101
 
174 - 102
    //
227 - 103
    //  Setup file names
174 - 104
    //
105
    QFileInfo info (fileName);
106
    strncpy(filebase, qPrintable(info.baseName()), 8);
107
    strcpy( datfile, filebase );
108
    strcat( datfile, ".dat" );
109
 
110
    strncpy(filepath, qPrintable(info.absolutePath()), sizeof(filepath)-3);
111
    strcat(filepath, "/");
112
    qDebug("FilePath:%s", filepath );
113
 
172 - 114
    if ( !open_read_config() )
115
    {
116
        if (QMessageBox::Cancel == QMessageBox::question ( 0,
117
                                                       "Config Load Error",
118
                                                       "Cannot load or read configuration file.\n"
119
                                                       "If you continue a new configuration will be created\n"
120
                                                       "If you cancel then the application will terminate.",
121
                                                       QMessageBox::Ok | QMessageBox::Cancel
122
                                                       ) )
123
        {
174 - 124
            qDebug("Cancel to bad config");
172 - 125
            exit(2);
126
        }
127
    }
128
}
129
 
130
 
131
bool QmConfig::open_read_config( void )
132
{
133
    bool result;
134
    // Open the file
176 - 135
    QFile configFile;
172 - 136
    configFile.setFileName(fileName);
137
    if ( ! configFile.open(QIODevice::ReadOnly) )
138
    {
139
        MainWindow::showMessage("Cannot open config File");
140
        return (false );
141
    }
142
 
176 - 143
    result = read_config(configFile);
172 - 144
    configFile.close();
145
 
146
    if ( result )
147
    {
148
        /*
227 - 149
        **  Post read calculations and fixups
150
        */
176 - 151
        if( datafilename[0] )
172 - 152
        {
176 - 153
            strcpy( datfile, datafilename );
172 - 154
            strcat( datfile, ".dat" );
155
        }
176 - 156
        nonequestrian_class = lookup_class( nonequestrian_class_abr );
227 - 157
 
158
        class_ne_winners_by_class = false;
159
        for( int i = 0; i < MAX_CLASS; i++ )
160
        {
161
           if(class_ne_winners[i])
162
           {
163
               class_ne_winners_by_class = true;
164
               break;
165
           }
166
        }
167
 
168
 
172 - 169
    }
170
    return result;
171
}
172
 
173
/*========================================================================
174
 *
175
 *  Read in the configuration file
176
 *
177
 *  Purpose:
178
 *      This function is called to read in the configuration file
179
 *      NOTE: Must be maintained with the Writer function
180
 *
181
 *  Parameters:
182
 *      fcon        File number of the config file
183
 *
184
 *  Returns:
185
 *      FALSE if an error is encountered
186
 *
187
 *========================================================================*/
188
 
176 - 189
bool QmConfig::read_config( QFile &configFile  )
172 - 190
{
191
    int         len;                            /* Length of data read */
192
    int         fsize;                          /* Length of desired data */
193
 
194
    /*
195
     * Event name
196
     */
210 - 197
//qDebug( "Reading: Event Name" );
176 - 198
    fsize = sizeof( event_name );
199
    len = configFile.read( event_name, fsize );
172 - 200
    if( len != fsize )
201
        return ( FALSE );
202
 
203
    /*
204
     * Leg names
205
     */
210 - 206
//qDebug( "Reading: Leg Names" );
176 - 207
    fsize = sizeof( leg_name );
208
    len = configFile.read( (char *)leg_name, fsize );
172 - 209
    if( len != fsize )
210
        return ( FALSE );
211
 
212
    /*
213
     * Team definitions
214
     */
210 - 215
//qDebug( "Reading: Team Defs" );
176 - 216
    fsize = sizeof( t_def  );
217
    len = configFile.read( (char *)t_def, fsize );
172 - 218
    if( len != fsize )
219
        return ( FALSE );
220
 
221
    /*
222
     * Number of legs
223
     */
210 - 224
//qDebug( "Reading: Leg Nums" );
176 - 225
    fsize = sizeof( num_legs  );
226
    len = configFile.read( (char *)&num_legs, fsize );
172 - 227
    if( len != fsize)
228
        return ( FALSE );
229
 
230
    /*
231
     * Number of team splits
232
     */
210 - 233
//qDebug( "Reading: Team Splits" );
176 - 234
    fsize = sizeof( num_teams  );
235
    len = configFile.read( (char *)&num_teams, fsize );
172 - 236
    if( len != fsize )
237
        return ( FALSE );
238
 
176 - 239
    min_team = t_def[0].start;
240
    max_team = t_def[num_teams - 1].end;
172 - 241
 
242
    /*
243
     * Class information
244
     */
210 - 245
//qDebug( "Reading: Class Data" );
176 - 246
    fsize = sizeof( team_class  );
247
    len = configFile.read( (char *)team_class, fsize );
172 - 248
    if( len != fsize )
249
        return ( FALSE );
176 - 250
    fsize = sizeof( num_class  );
251
    len = configFile.read( (char *)&num_class, fsize);
172 - 252
    if( len != fsize )
253
        return ( FALSE );
254
 
255
    /*
256
     * Country list
257
     */
210 - 258
//qDebug( "Reading: Country Data, Name" );
176 - 259
    fsize = sizeof( country_name  );
260
    len = configFile.read( (char *)country_name, fsize );
172 - 261
    if( len != fsize )
262
        return ( FALSE );
263
 
210 - 264
//qDebug( "Reading: Country Data, Number" );
176 - 265
    fsize = sizeof( num_countries  );
266
    len = configFile.read( (char *)&num_countries, fsize );
172 - 267
    if( len != fsize )
268
        return ( FALSE );
269
 
270
    /*
271
     * Addendum file
272
     */
210 - 273
//qDebug( "Reading: Addendum File" );
176 - 274
    fsize = sizeof( addendum );
275
    len = configFile.read( addendum, fsize );
172 - 276
    if( len != fsize )
277
        return ( configFile.atEnd() );
278
 
279
    /*
280
     * Name of the data file
281
     */
282
 
210 - 283
//qDebug( "Reading: Name of data file" );
176 - 284
    fsize = sizeof( datafilename );
285
    len = configFile.read( datafilename, fsize );
172 - 286
    if( len != fsize )
287
        return ( configFile.atEnd() );
288
 
289
    /*
290
     **  Non-equestrian configuration information
291
     */
210 - 292
//qDebug( "Reading: NonEquest" );
176 - 293
    fsize = sizeof( nonequestrian_class_abr );
294
    len = configFile.read( nonequestrian_class_abr, fsize );
172 - 295
    if( len != fsize )
296
        return ( configFile.atEnd() );
297
 
210 - 298
//qDebug( "Reading: NonEquest-2" );
176 - 299
    fsize = sizeof( equestrian_leg );
300
    len = configFile.read( (char *)&equestrian_leg, fsize );
172 - 301
    if( len != fsize )
302
        return ( FALSE );
303
 
304
    /*
305
    **  .txt file output control. Lines per page and perf-skipping
306
    */
210 - 307
//qDebug( "Reading: Output Control" );
176 - 308
    fsize = sizeof( lines_per_page );
309
    len = configFile.read( (char *)&lines_per_page, fsize );
172 - 310
    if( len != fsize )
311
        return ( configFile.atEnd() );
312
 
210 - 313
//qDebug( "Reading: Output Control-2" );
176 - 314
    fsize = sizeof( perf_skip );
315
    len = configFile.read( (char *)&perf_skip, fsize );
172 - 316
    if( len != fsize )
317
        return ( FALSE );
318
 
210 - 319
//qDebug( "Reading: Winners Info" );
176 - 320
    fsize = sizeof( class_winners );
321
    len = configFile.read( (char *)&class_winners, fsize );
172 - 322
    if( len != fsize )
323
        return ( FALSE );
324
 
210 - 325
//qDebug( "Reading: Hall of Fame Info" );
176 - 326
    fsize = sizeof( hall_fame );
327
    len = configFile.read( (char *)&hall_fame, fsize );
172 - 328
    if( len != fsize )
329
        return ( FALSE );
330
 
210 - 331
//qDebug( "Reading: Hall of Fame Numbers" );
208 - 332
    fsize = sizeof( num_fame );
176 - 333
    len = configFile.read( (char *)&num_fame, fsize );
172 - 334
    if( len != fsize )
335
        return ( configFile.atEnd() );
336
 
227 - 337
//qDebug( "Reading: NE Winners Info" );
320 david 338
    fsize = sizeof( class_ne_winners );
339
    len = configFile.read( (char *)&class_ne_winners, fsize );
340
    if( len != fsize )
341
         return ( configFile.atEnd() );
172 - 342
 
297 david 343
//qDebug( "Reading: Web Import Url" );
320 david 344
    fsize = sizeof( webUrl );
345
    len = configFile.read( (char *)&webUrl, fsize );
346
    if( len != fsize )
347
        return ( configFile.atEnd() );
297 david 348
 
172 - 349
    return ( TRUE );
350
}
351
 
199 david 352
/*----------------------------------------------------------------------------
353
** FUNCTION           : getAddendemFile
354
**
355
** DESCRIPTION        : Returns the full path the the addemdum file
315 david 356
**                      The function will look for the file in a number of
199 david 357
**                      locations
358
**
359
**
360
** INPUTS             : name    - Name of the addenum file
361
**                      create  - True. Allow file to be created
362
**
363
** RETURNS            : NULL    - No addendum name, or file not found
364
**                                If create' is true then the preferred
365
**                                location will be returned.
366
**
367
----------------------------------------------------------------------------*/
172 - 368
 
199 david 369
 
315 david 370
const QString QmConfig::getAddendemFile(const QString &name, bool create )
199 david 371
{
372
    if (name.isEmpty())
373
        return NULL;
374
 
375
    QFile file;
376
    QString addendumFileName;
377
    addendumFileName = filepath;
378
    addendumFileName.append(name);
379
    QString addendumFileNamePreferred(addendumFileName);
380
    file.setFileName(addendumFileName);
200 david 381
    //qDebug("Try:%s", qPrintable(addendumFileName));
199 david 382
    if ( !file.exists())
383
    {
384
        addendumFileName = QCoreApplication::applicationDirPath ();
385
        addendumFileName.append("/");
386
        addendumFileName.append(name);
387
        file.setFileName(addendumFileName);
200 david 388
        //qDebug("Try:%s", qPrintable(addendumFileName));
199 david 389
        if ( !file.exists())
390
        {
391
             addendumFileName = QDir::currentPath ();
392
             addendumFileName.append("/");
393
             addendumFileName.append(name);
394
             file.setFileName(addendumFileName);
200 david 395
             //qDebug("Try:%s", qPrintable(addendumFileName));
199 david 396
             if ( !file.exists())
397
             {
200 david 398
                 //qDebug("Addeddum File not found");
199 david 399
                 if (create)
400
                 {
401
                     addendumFileName = addendumFileNamePreferred;
402
                 }
403
                 else
404
                 {
405
                     return NULL;
406
                 }
407
             }
408
        }
409
    }
315 david 410
    return addendumFileName;
199 david 411
}
412
 
172 - 413
/*========================================================================
414
 *
415
 *  Write out the configuration file
416
 *
417
 *  Purpose:
418
 *      This function is called to write the configuration file
419
 *      NOTE: Must be maintained with the Reader function
420
 *
421
 *  Parameters:
422
 *      None
423
 *
424
 *  Returns:
425
 *      FALSE   : Error encountered
426
 *
427
 *========================================================================*/
428
 
429
bool QmConfig::write_config( void )
430
{
174 - 431
    if (fileName.isEmpty())
432
    {
433
        qDebug("No Config file selected");
434
        return(false);
435
    }
172 - 436
    /*
437
     **  Open as a binary file
438
     */
174 - 439
    QFile file;
440
    file.setFileName(fileName);
172 - 441
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate) )
442
    {
174 - 443
        qDebug("File error: %s", qPrintable(file.errorString()));
218 david 444
        MainWindow::showMessage("Cannot write config file");
172 - 445
        return (false);
446
    }
447
 
448
     /*
449
     **  Write out multiple structures
450
     **     Event name
451
     **     Leg names
452
     **     Team definitions
453
     **     Number of legs
454
     **     Number fo team splits
455
     **     Class information
456
     **     Number of defined classes
457
     **     Country list
458
     **     Number of defined countries
176 - 459
     **     Legend addendum file name
172 - 460
     **     Data file name
461
     */
462
 
176 - 463
    file.write( (const char *) event_name, sizeof( event_name ) );
464
    file.write( (const char *) leg_name, sizeof( leg_name ) );
465
    file.write( (const char *) t_def, sizeof( t_def ) );
466
    file.write( (const char *) &num_legs, sizeof( num_legs ) );
467
    file.write( (const char *) &num_teams, sizeof( num_teams ) );
468
    file.write( (const char *) team_class, sizeof( team_class ) );
469
    file.write( (const char *) &num_class, sizeof( num_class ) );
470
    file.write( (const char *) country_name, sizeof( country_name ) );
471
    file.write( (const char *) &num_countries, sizeof( num_countries ) );
472
    file.write( (const char *) addendum, sizeof( addendum ) );
473
    file.write( (const char *) datafilename, sizeof( datafilename ) );
474
    file.write( (const char *) nonequestrian_class_abr, sizeof( nonequestrian_class_abr ) );
475
    file.write( (const char *) &equestrian_leg, sizeof( equestrian_leg ) );
476
    file.write( (const char *) &lines_per_page, sizeof( lines_per_page ) );
477
    file.write( (const char *) &perf_skip, sizeof( perf_skip ) );
478
    file.write( (const char *) &class_winners, sizeof( class_winners ) );
479
    file.write( (const char *) &hall_fame, sizeof( hall_fame ) );
480
    file.write( (const char *) &num_fame, sizeof( num_fame ) );
227 - 481
    file.write( (const char *) &class_ne_winners, sizeof( class_ne_winners ) );
297 david 482
    file.write( (const char *) &webUrl, sizeof( webUrl ) );
172 - 483
 
484
    file.close();
485
    return ( TRUE );
486
}
174 - 487
 
488
/*========================================================================
489
 *
490
 *  Qsort callback: Sort by team
491
 *
492
 *  Purpose:
493
 *      Function used by the team definition sort operation
494
 *      It will compare two entries of the team def structure and return an
495
 *      integer for gt eq lt conditions.
496
 *      Note : If the start is 0 the team entry does exist and is placed at the
497
 *      end of the sorted list.
498
 *
499
 *  Parameters:
500
 *      a           comparision entry
501
 *      b           comparision entry
502
 *
503
 *  Returns:
504
 *      gt, eq, lt as required
505
 *
506
 *========================================================================*/
507
 
508
int f_comp_int( const void *aa, const void *bb )
509
{
510
    const ty_t_def *a = (ty_t_def *)aa;
511
    const ty_t_def *b = (ty_t_def *)bb;
512
 
513
    if( a->start == 0 )
514
        return ( 1 );
515
    else if( b->start == 0 )
516
        return ( -1 );
517
    else
518
        return ( a->start - b->start );
519
}
520
 
521
/*========================================================================
522
 *
523
 *  Compact a string
524
 *
525
 *  Purpose:
526
 *      This function is called remove leading and trailing spaces from
527
 *      a string. Treats other non-printing characters as leading
528
 *      spaces. This solves a problem when importing data from a
529
 *      Microsoft CSV file with empty fields.
530
 *
531
 *  Parameters:
532
 *      str     Address of the string to compact
533
 *
534
 *  Returns:
535
 *      Nothing
536
 *
537
 *========================================================================*/
538
 
539
void compact( char *str )
540
{
541
    char       *ptr;
542
 
543
    ptr = str;
544
    while( *str && ( isspace( *str ) || !isprint( *str ) ) )
545
        str++;
546
    strcpy( ptr, str );
547
}
548
 
549
/*========================================================================
550
 *
551
 *  Validate a team number
552
 *
553
 *  Purpose:
554
 *      This function is called to validate a team number
555
 *
556
 *  Parameters:
557
 *      x       Number to validate
558
 *
559
 *  Returns:
560
 *      TRUE    : Valid
561
 *      FALSE   : Not valid
562
 *
563
 *========================================================================*/
564
 
565
bool valid_field( int x )
566
{
567
    int         i;
568
 
569
    for( i = 0; i < config.num_teams; i++ )
570
    {
571
        if( x <= config.t_def[i].end && x >= config.t_def[i].start )
572
            return ( TRUE );
573
        if( x < config.t_def[i].start )
574
            break;                               /* Because the list is sorted */
575
    }
576
    return ( FALSE );
577
}
578
 
579
/*========================================================================
580
 *
581
 *  Get a class descriptor from existing text
582
 *
583
 *  Purpose:
584
 *      This function is called to Get a class descriptor
585
 *
586
 *  Parameters:
587
 *      text    - User text to examine
588
 *      config  - configuration dtaa to use
589
 *
590
 *  Returns:
591
 *      An integer which is the index into the  config.team_class array
592
 *      The integer is in the range 1 .. num_class
593
 *      A value fo zero indicates the text was not found.
594
 *
595
 *========================================================================*/
596
 
176 - 597
int QmConfig::lookup_class( const char *text )
174 - 598
{
599
    int         i;
600
 
176 - 601
//    if( config_ptr == NULL )
602
//        config_ptr = &config;
174 - 603
 
604
    /*
605
     * Attempt to locate the entered class in the list of defined classes
606
     */
607
 
176 - 608
    for( i = 0; i < num_class; i++ )
174 - 609
    {
176 - 610
        if( toupper(text[0]) == toupper(team_class[i].abr[0]) &&
611
            toupper(text[1]) == toupper(team_class[i].abr[1]) )
174 - 612
            return ( ++i );
613
    }
614
    return ( 0 );
615
}
177 - 616
 
617
#ifdef DISPLAY_STRUCTURES
618
/*============================================================================
619
**
620
**  Display structure information
621
**
622
**  Purpose:    Display internal structure information
623
**
624
**  Parameters: Nothing
625
**
626
**  Returns:    Nothing directly
627
**
628
**===========================================================================*/
629
 
630
/*
631
**  esize - A macro to return the size of a structure element
632
**  element - print element information
633
*/
634
#define esize( st, el) ( sizeof(((st *)0)->el))
635
#define element( st, el) \
208 - 636
    printf( "Offset of %-15s :%4d, Size:%d", #el, offsetof( st, el), esize(st, el) );
209 - 637
#define element2( st, el) \
638
    printf( "Size of %-15s :%4d", #el, esize(st, el) );
177 - 639
 
640
void display_structures(void)
641
{
208 - 642
    printf( "Structure: leg_type" );
177 - 643
    element( leg_type, start    );
644
    element( leg_type, end      );
645
    element( leg_type, elapsed  );
646
    element( leg_type, l_place  );
647
    element( leg_type, le_place );
648
    element( leg_type, lc_place );
649
    element( leg_type, lec_place);
650
    element( leg_type, manual   );
208 - 651
    printf( "Sizeof %-18s :%4d", "leg_type", sizeof(leg_type) );
177 - 652
 
653
 
208 - 654
    printf( "" );
655
    printf( "Structure: team_type" );
177 - 656
    element( team_type, numb   );
657
    element( team_type, name   );
658
    element( team_type, leg    );
659
    element( team_type, members);
208 - 660
    element( team_type, teamclass  );
177 - 661
    element( team_type, country);
662
    element( team_type, flags  );
208 - 663
    printf( "Sizeof %-18s :%4d", "team_type", sizeof(team_type) );
177 - 664
 
209 - 665
#if 1
208 - 666
    printf( "" );
667
    printf( "Structure: MARA_CFG" );
209 - 668
    element2( QmConfig, event_name      );
669
    element2( QmConfig, leg_name        );
670
    element2( QmConfig, t_def           );
671
    element2( QmConfig, num_legs        );
672
    element2( QmConfig, num_teams       );
673
    element2( QmConfig, max_team        );
674
    element2( QmConfig, min_team        );
675
    element2( QmConfig, team_class      );
676
    element2( QmConfig, num_class       );
677
    element2( QmConfig, country_name    );
678
    element2( QmConfig, num_countries   );
679
    element2( QmConfig, addendum        );
680
    element2( QmConfig, datafilename    );
681
    printf( "Sizeof %-18s :%4d", "QmConfig", sizeof(QmConfig) );
208 - 682
#endif
177 - 683
}
684
#endif