Subversion Repositories svn1-original

Rev

Rev 381 | Details | Compare with Previous | Last modification | View Log | RSS feed

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