author | zecke <zecke> | 2004-04-08 18:34:28 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-04-08 18:34:28 (UTC) |
commit | e892ef3b71ac582a3ee23e1a2a65e3b43121a40c (patch) (unidiff) | |
tree | ee0a3b858daeca2dcbd24c196c31b18b47616f2e | |
parent | c8481d7f647ffdc08005f630263dfc05cfd7b230 (diff) | |
download | opie-e892ef3b71ac582a3ee23e1a2a65e3b43121a40c.zip opie-e892ef3b71ac582a3ee23e1a2a65e3b43121a40c.tar.gz opie-e892ef3b71ac582a3ee23e1a2a65e3b43121a40c.tar.bz2 |
Be a bit more picky on config files
-rw-r--r-- | library/config.cpp | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/library/config.cpp b/library/config.cpp index 8b60f60..0bfb476 100644 --- a/library/config.cpp +++ b/library/config.cpp | |||
@@ -431,148 +431,150 @@ QStringList Config::readListEntry( const QString &key, const QChar &sep ) | |||
431 | } | 431 | } |
432 | 432 | ||
433 | /*! | 433 | /*! |
434 | Removes all entries from the current group. | 434 | Removes all entries from the current group. |
435 | */ | 435 | */ |
436 | void Config::clearGroup() | 436 | void Config::clearGroup() |
437 | { | 437 | { |
438 | if ( git == groups.end() ) { | 438 | if ( git == groups.end() ) { |
439 | qWarning( "no group set" ); | 439 | qWarning( "no group set" ); |
440 | return; | 440 | return; |
441 | } | 441 | } |
442 | if ( !(*git).isEmpty() ) { | 442 | if ( !(*git).isEmpty() ) { |
443 | ( *git ).clear(); | 443 | ( *git ).clear(); |
444 | changed = TRUE; | 444 | changed = TRUE; |
445 | } | 445 | } |
446 | } | 446 | } |
447 | 447 | ||
448 | /*! | 448 | /*! |
449 | \internal | 449 | \internal |
450 | */ | 450 | */ |
451 | void Config::write( const QString &fn ) | 451 | void Config::write( const QString &fn ) |
452 | { | 452 | { |
453 | QString strNewFile; | 453 | QString strNewFile; |
454 | if ( !fn.isEmpty() ) | 454 | if ( !fn.isEmpty() ) |
455 | filename = fn; | 455 | filename = fn; |
456 | strNewFile = filename + ".new"; | 456 | strNewFile = filename + ".new"; |
457 | 457 | ||
458 | QFile f( strNewFile ); | 458 | QFile f( strNewFile ); |
459 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { | 459 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { |
460 | qWarning( "could not open for writing `%s'", strNewFile.latin1() ); | 460 | qWarning( "could not open for writing `%s'", strNewFile.latin1() ); |
461 | git = groups.end(); | 461 | git = groups.end(); |
462 | return; | 462 | return; |
463 | } | 463 | } |
464 | 464 | ||
465 | QString str; | 465 | QString str; |
466 | QCString cstr; | 466 | QCString cstr; |
467 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); | 467 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); |
468 | 468 | ||
469 | for ( ; g_it != groups.end(); ++g_it ) { | 469 | for ( ; g_it != groups.end(); ++g_it ) { |
470 | str += "[" + g_it.key() + "]\n"; | 470 | str += "[" + g_it.key() + "]\n"; |
471 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); | 471 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); |
472 | for ( ; e_it != ( *g_it ).end(); ++e_it ) | 472 | for ( ; e_it != ( *g_it ).end(); ++e_it ) |
473 | str += e_it.key() + " = " + *e_it + "\n"; | 473 | str += e_it.key() + " = " + *e_it + "\n"; |
474 | } | 474 | } |
475 | cstr = str.utf8(); | 475 | cstr = str.utf8(); |
476 | 476 | ||
477 | int total_length; | 477 | int total_length; |
478 | total_length = f.writeBlock( cstr.data(), cstr.length() ); | 478 | total_length = f.writeBlock( cstr.data(), cstr.length() ); |
479 | if ( total_length != int(cstr.length()) ) { | 479 | if ( total_length != int(cstr.length()) ) { |
480 | QMessageBox::critical( 0, QObject::tr("Out of Space"), | 480 | QMessageBox::critical( 0, QObject::tr("Out of Space"), |
481 | QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); | 481 | QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); |
482 | f.close(); | 482 | f.close(); |
483 | QFile::remove( strNewFile ); | 483 | QFile::remove( strNewFile ); |
484 | return; | 484 | return; |
485 | } | 485 | } |
486 | 486 | ||
487 | f.close(); | 487 | f.close(); |
488 | // now rename the file... | 488 | // now rename the file... |
489 | if ( rename( strNewFile, filename ) < 0 ) { | 489 | if ( rename( strNewFile, filename ) < 0 ) { |
490 | qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), | 490 | qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), |
491 | filename.latin1() ); | 491 | filename.latin1() ); |
492 | QFile::remove( strNewFile ); | 492 | QFile::remove( strNewFile ); |
493 | } | 493 | } |
494 | } | 494 | } |
495 | 495 | ||
496 | /*! | 496 | /*! |
497 | Returns whether the Config is in a valid state. | 497 | Returns whether the Config is in a valid state. |
498 | */ | 498 | */ |
499 | bool Config::isValid() const | 499 | bool Config::isValid() const |
500 | { | 500 | { |
501 | return groups.end() != git; | 501 | return groups.end() != git; |
502 | } | 502 | } |
503 | 503 | ||
504 | /*! | 504 | /*! |
505 | \internal | 505 | \internal |
506 | */ | 506 | */ |
507 | void Config::read() | 507 | void Config::read() |
508 | { | 508 | { |
509 | changed = FALSE; | 509 | changed = FALSE; |
510 | 510 | ||
511 | if ( !QFileInfo( filename ).exists() ) { | 511 | if ( !QFileInfo( filename ).exists() ) { |
512 | git = groups.end(); | 512 | git = groups.end(); |
513 | return; | 513 | return; |
514 | } | 514 | } |
515 | 515 | ||
516 | QFile f( filename ); | 516 | QFile f( filename ); |
517 | if ( !f.open( IO_ReadOnly ) ) { | 517 | if ( !f.open( IO_ReadOnly ) ) { |
518 | git = groups.end(); | 518 | git = groups.end(); |
519 | return; | 519 | return; |
520 | } | 520 | } |
521 | 521 | ||
522 | 522 | ||
523 | // hack to avoid problems if big files are passed to test | 523 | // hack to avoid problems if big files are passed to test |
524 | // if they are valid configs ( like passing a mp3 ... ) | 524 | // if they are valid configs ( like passing a mp3 ... ) |
525 | // I just hope that there are no conf files > 100000 byte | 525 | // I just hope that there are no conf files > 100000 byte |
526 | // not the best solution, find something else later | 526 | // not the best solution, find something else later |
527 | if ( f.size() > 100000 ) { | 527 | if ( f.getch()!='[' ||f.size() > 100000 ) { |
528 | git = groups.end(); | ||
528 | return; | 529 | return; |
529 | } | 530 | } |
531 | f.ungetch('['); | ||
530 | 532 | ||
531 | 533 | ||
532 | QTextStream s( &f ); | 534 | QTextStream s( &f ); |
533 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | 535 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) |
534 | // The below should work, but doesn't in Qt 2.3.0 | 536 | // The below should work, but doesn't in Qt 2.3.0 |
535 | s.setCodec( QTextCodec::codecForMib( 106 ) ); | 537 | s.setCodec( QTextCodec::codecForMib( 106 ) ); |
536 | #else | 538 | #else |
537 | s.setEncoding( QTextStream::UnicodeUTF8 ); | 539 | s.setEncoding( QTextStream::UnicodeUTF8 ); |
538 | #endif | 540 | #endif |
539 | 541 | ||
540 | QStringList list = QStringList::split('\n', s.read() ); | 542 | QStringList list = QStringList::split('\n', s.read() ); |
541 | f.close(); | 543 | f.close(); |
542 | 544 | ||
543 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { | 545 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { |
544 | if ( !parse( *it ) ) { | 546 | if ( !parse( *it ) ) { |
545 | git = groups.end(); | 547 | git = groups.end(); |
546 | return; | 548 | return; |
547 | } | 549 | } |
548 | } | 550 | } |
549 | } | 551 | } |
550 | 552 | ||
551 | /*! | 553 | /*! |
552 | \internal | 554 | \internal |
553 | */ | 555 | */ |
554 | bool Config::parse( const QString &l ) | 556 | bool Config::parse( const QString &l ) |
555 | { | 557 | { |
556 | QString line = l.stripWhiteSpace(); | 558 | QString line = l.stripWhiteSpace(); |
557 | 559 | ||
558 | if ( line [0] == QChar ( '#' )) | 560 | if ( line [0] == QChar ( '#' )) |
559 | return true; // ignore comments | 561 | return true; // ignore comments |
560 | 562 | ||
561 | if ( line[ 0 ] == QChar( '[' ) ) { | 563 | if ( line[ 0 ] == QChar( '[' ) ) { |
562 | QString gname = line; | 564 | QString gname = line; |
563 | gname = gname.remove( 0, 1 ); | 565 | gname = gname.remove( 0, 1 ); |
564 | if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) | 566 | if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) |
565 | gname = gname.remove( gname.length() - 1, 1 ); | 567 | gname = gname.remove( gname.length() - 1, 1 ); |
566 | git = groups.insert( gname, ConfigGroup() ); | 568 | git = groups.insert( gname, ConfigGroup() ); |
567 | } else if ( !line.isEmpty() ) { | 569 | } else if ( !line.isEmpty() ) { |
568 | if ( git == groups.end() ) | 570 | if ( git == groups.end() ) |
569 | return FALSE; | 571 | return FALSE; |
570 | int eq = line.find( '=' ); | 572 | int eq = line.find( '=' ); |
571 | if ( eq == -1 ) | 573 | if ( eq == -1 ) |
572 | return FALSE; | 574 | return FALSE; |
573 | QString key = line.left(eq).stripWhiteSpace(); | 575 | QString key = line.left(eq).stripWhiteSpace(); |
574 | QString value = line.mid(eq+1).stripWhiteSpace(); | 576 | QString value = line.mid(eq+1).stripWhiteSpace(); |
575 | ( *git ).insert( key, value ); | 577 | ( *git ).insert( key, value ); |
576 | } | 578 | } |
577 | return TRUE; | 579 | return TRUE; |
578 | } | 580 | } |