-rw-r--r-- | library/config.cpp | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/library/config.cpp b/library/config.cpp index e07eecb..1121cd4 100644 --- a/library/config.cpp +++ b/library/config.cpp | |||
@@ -441,114 +441,118 @@ void Config::clearGroup() | |||
441 | void Config::write( const QString &fn ) | 441 | void Config::write( const QString &fn ) |
442 | { | 442 | { |
443 | QString strNewFile; | 443 | QString strNewFile; |
444 | if ( !fn.isEmpty() ) | 444 | if ( !fn.isEmpty() ) |
445 | filename = fn; | 445 | filename = fn; |
446 | strNewFile = filename + ".new"; | 446 | strNewFile = filename + ".new"; |
447 | 447 | ||
448 | QFile f( strNewFile ); | 448 | QFile f( strNewFile ); |
449 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { | 449 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { |
450 | qWarning( "could not open for writing `%s'", strNewFile.latin1() ); | 450 | qWarning( "could not open for writing `%s'", strNewFile.latin1() ); |
451 | git = groups.end(); | 451 | git = groups.end(); |
452 | return; | 452 | return; |
453 | } | 453 | } |
454 | 454 | ||
455 | QString str; | 455 | QString str; |
456 | QCString cstr; | 456 | QCString cstr; |
457 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); | 457 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); |
458 | 458 | ||
459 | for ( ; g_it != groups.end(); ++g_it ) { | 459 | for ( ; g_it != groups.end(); ++g_it ) { |
460 | str += "[" + g_it.key() + "]\n"; | 460 | str += "[" + g_it.key() + "]\n"; |
461 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); | 461 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); |
462 | for ( ; e_it != ( *g_it ).end(); ++e_it ) | 462 | for ( ; e_it != ( *g_it ).end(); ++e_it ) |
463 | str += e_it.key() + " = " + *e_it + "\n"; | 463 | str += e_it.key() + " = " + *e_it + "\n"; |
464 | } | 464 | } |
465 | cstr = str.utf8(); | 465 | cstr = str.utf8(); |
466 | 466 | ||
467 | int total_length; | 467 | int total_length; |
468 | total_length = f.writeBlock( cstr.data(), cstr.length() ); | 468 | total_length = f.writeBlock( cstr.data(), cstr.length() ); |
469 | if ( total_length != int(cstr.length()) ) { | 469 | if ( total_length != int(cstr.length()) ) { |
470 | QMessageBox::critical( 0, QObject::tr("Out of Space"), | 470 | QMessageBox::critical( 0, QObject::tr("Out of Space"), |
471 | QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); | 471 | QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); |
472 | f.close(); | 472 | f.close(); |
473 | QFile::remove( strNewFile ); | 473 | QFile::remove( strNewFile ); |
474 | return; | 474 | return; |
475 | } | 475 | } |
476 | 476 | ||
477 | f.close(); | 477 | f.close(); |
478 | // now rename the file... | 478 | // now rename the file... |
479 | if ( rename( strNewFile, filename ) < 0 ) { | 479 | if ( rename( strNewFile, filename ) < 0 ) { |
480 | qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), | 480 | qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), |
481 | filename.latin1() ); | 481 | filename.latin1() ); |
482 | QFile::remove( strNewFile ); | 482 | QFile::remove( strNewFile ); |
483 | } | 483 | } |
484 | } | 484 | } |
485 | 485 | ||
486 | /*! | 486 | /*! |
487 | Returns whether the Config is in a valid state. | 487 | Returns whether the Config is in a valid state. |
488 | */ | 488 | */ |
489 | bool Config::isValid() const | 489 | bool Config::isValid() const |
490 | { | 490 | { |
491 | return groups.end() != git; | 491 | return groups.end() != git; |
492 | } | 492 | } |
493 | 493 | ||
494 | /*! | 494 | /*! |
495 | \internal | 495 | \internal |
496 | */ | 496 | */ |
497 | void Config::read() | 497 | void Config::read() |
498 | { | 498 | { |
499 | changed = FALSE; | 499 | changed = FALSE; |
500 | 500 | ||
501 | if ( !QFileInfo( filename ).exists() ) { | 501 | if ( !QFileInfo( filename ).exists() ) { |
502 | git = groups.end(); | 502 | git = groups.end(); |
503 | return; | 503 | return; |
504 | } | 504 | } |
505 | 505 | ||
506 | QFile f( filename ); | 506 | QFile f( filename ); |
507 | if ( !f.open( IO_ReadOnly ) ) { | 507 | if ( !f.open( IO_ReadOnly ) ) { |
508 | git = groups.end(); | 508 | git = groups.end(); |
509 | return; | 509 | return; |
510 | } | 510 | } |
511 | 511 | ||
512 | QTextStream s( &f ); | 512 | QTextStream s( &f ); |
513 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | 513 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) |
514 | // The below should work, but doesn't in Qt 2.3.0 | 514 | // The below should work, but doesn't in Qt 2.3.0 |
515 | s.setCodec( QTextCodec::codecForMib( 106 ) ); | 515 | s.setCodec( QTextCodec::codecForMib( 106 ) ); |
516 | #else | 516 | #else |
517 | s.setEncoding( QTextStream::UnicodeUTF8 ); | 517 | s.setEncoding( QTextStream::UnicodeUTF8 ); |
518 | #endif | 518 | #endif |
519 | 519 | ||
520 | QStringList list = QStringList::split('\n', s.read() ); | 520 | QStringList list = QStringList::split('\n', s.read() ); |
521 | f.close(); | 521 | f.close(); |
522 | 522 | ||
523 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { | 523 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { |
524 | if ( !parse( *it ) ) { | 524 | if ( !parse( *it ) ) { |
525 | git = groups.end(); | 525 | git = groups.end(); |
526 | return; | 526 | return; |
527 | } | 527 | } |
528 | } | 528 | } |
529 | } | 529 | } |
530 | 530 | ||
531 | /*! | 531 | /*! |
532 | \internal | 532 | \internal |
533 | */ | 533 | */ |
534 | bool Config::parse( const QString &l ) | 534 | bool Config::parse( const QString &l ) |
535 | { | 535 | { |
536 | QString line = l.stripWhiteSpace(); | 536 | QString line = l.stripWhiteSpace(); |
537 | |||
538 | if ( line [0] == QChar ( '#' )) | ||
539 | return true; // ignore comments | ||
540 | |||
537 | if ( line[ 0 ] == QChar( '[' ) ) { | 541 | if ( line[ 0 ] == QChar( '[' ) ) { |
538 | QString gname = line; | 542 | QString gname = line; |
539 | gname = gname.remove( 0, 1 ); | 543 | gname = gname.remove( 0, 1 ); |
540 | if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) | 544 | if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) |
541 | gname = gname.remove( gname.length() - 1, 1 ); | 545 | gname = gname.remove( gname.length() - 1, 1 ); |
542 | git = groups.insert( gname, ConfigGroup() ); | 546 | git = groups.insert( gname, ConfigGroup() ); |
543 | } else if ( !line.isEmpty() ) { | 547 | } else if ( !line.isEmpty() ) { |
544 | if ( git == groups.end() ) | 548 | if ( git == groups.end() ) |
545 | return FALSE; | 549 | return FALSE; |
546 | int eq = line.find( '=' ); | 550 | int eq = line.find( '=' ); |
547 | if ( eq == -1 ) | 551 | if ( eq == -1 ) |
548 | return FALSE; | 552 | return FALSE; |
549 | QString key = line.left(eq).stripWhiteSpace(); | 553 | QString key = line.left(eq).stripWhiteSpace(); |
550 | QString value = line.mid(eq+1).stripWhiteSpace(); | 554 | QString value = line.mid(eq+1).stripWhiteSpace(); |
551 | ( *git ).insert( key, value ); | 555 | ( *git ).insert( key, value ); |
552 | } | 556 | } |
553 | return TRUE; | 557 | return TRUE; |
554 | } | 558 | } |