1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
|
/*
FALCON - The Falcon Programming Language
FILE: confparser_mod.cpp
Configuration parser module -- module service classes
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: sab nov 25 2006
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
Configuration parser module -- module service classes
*/
#include "confparser_mod.h"
#include <falcon/fstream.h>
#include <falcon/transcoding.h>
#include <falcon/stdstreams.h>
namespace Falcon {
ConfigFileLine::ConfigFileLine( e_type t, String *original, String *key, String *value, String *comment ):
m_type( t ),
m_original( original ),
m_key( key ),
m_value( value ),
m_comment( comment )
{
}
ConfigFileLine::ConfigFileLine( String *original ):
m_type( t_empty ),
m_original( original ),
m_key( 0 ),
m_value( 0 ),
m_comment( 0 )
{
}
ConfigFileLine::~ConfigFileLine()
{
delete m_key;
delete m_value;
delete m_comment;
delete m_original;
}
bool ConfigFileLine::parseLine()
{
m_type = t_empty;
String tempString;
enum {
comment,
normal,
section,
key,
postkey,
prevalue,
value,
stringvalue,
stringescape,
stringHex,
stringOctal,
postvalue
} state;
state = normal;
uint32 trimValuePos = 0;
for ( uint32 pos = 0; pos < m_original->length(); pos ++ )
{
uint32 chr = m_original->getCharAt( pos );
switch( state ) {
case normal: // normal state
if ( chr == ';' || chr == '#' )
{
//the rest is a comment
if ( pos < m_original->length() - 1 )
m_comment = new String( *m_original, pos + 1 );
m_type = t_comment;
return true;
}
else if ( chr == '=' )
return false;
else if ( chr == '\"' )
return false;
else if ( chr == '[' )
{
m_key = new String;
m_type = t_section;
state = section; // section name
}
else if ( chr != ' ' && chr != '\t' )
{
m_type = t_keyval;
m_key = new String;
m_key->append( chr );
state = key; // read key
}
// else stay in this state;
break;
case section: // read section name
if ( chr != ']' )
m_key->append( chr );
else
state = postvalue; // post value
break;
case key: // read key
if ( chr == '"' )
return false;
else if ( chr == '=' || chr == ':' )
state = prevalue;
else if ( chr == ' ' || chr == '\t' )
state = postkey;
else
m_key->append( chr );
break;
case postkey:
if ( chr == '=' || chr == ':' )
state = prevalue;
else if ( chr != ' ' && chr != '\t' )
return false;
break;
case prevalue:
if ( chr == '"' )
{
state = stringvalue;
m_value = new String;
}
else if ( chr == ';' || chr == '#' )
{
//the rest is a comment
if ( pos < m_original->length() - 1 )
m_comment = new String( *m_original, pos + 1 );
return true;
}
else if ( chr != ' ' && chr != '\t' )
{
state = value;
m_value = new String;
m_value->append( chr );
trimValuePos = m_value->size();
}
break;
case value:
if ( chr == ';' || chr == '#' )
{
//the rest is a comment
if ( pos < m_original->length() - 1 )
m_comment = new String( *m_original, pos + 1 );
m_value->size( trimValuePos );
return true;
}
m_value->append( chr );
if ( chr != ' ' && chr != '\t' )
trimValuePos = m_value->size(); // using directly size instead of length
break;
case stringvalue:
if ( chr == '"' )
state = postvalue;
else if ( chr == '\\' )
{
state = stringescape;
}
else
m_value->append( chr );
break;
case stringescape:
switch( chr )
{
case '\\': m_value->append( '\\' ); state = stringvalue; break;
case '"': m_value->append( '"' ); state = stringvalue; break;
case 'n': m_value->append( '\n' ); state = stringvalue; break;
case 'b': m_value->append( '\b' ); state = stringvalue; break;
case 't': m_value->append( '\t' ); state = stringvalue; break;
case 'r': m_value->append( '\r' ); state = stringvalue; break;
case 'x': case 'X': tempString.size(0); state = stringHex; break;
case '0': tempString.size(0); state = stringOctal; break;
default: m_value->append( chr ); state = stringvalue;
}
break;
case stringHex:
if ( (chr >= '0' && chr <= '9') ||
(chr >= 'a' && chr <= 'f') ||
(chr >= 'A' && chr <= 'F')
)
{
tempString.append( chr );
}
else
{
uint64 retval;
if ( ! tempString.parseHex( retval ) || retval > 0xFFFFFFFF )
return false;
m_value->append( (uint32) retval );
m_value->append( chr );
state = stringvalue;
}
break;
case stringOctal:
if ( (chr >= '0' && chr <= '7') )
{
tempString.append( chr );
}
else
{
uint64 retval;
if ( ! tempString.parseOctal( retval ) || retval > 0xFFFFFFFF )
return false;
m_value->append( (uint32) retval );
m_value->append( chr );
state = stringvalue;
}
break;
case postvalue:
// in postvalue state we wait for a comment.
if ( chr == ';' || chr == '#' )
{
//the rest is a comment
if ( pos < m_original->length() - 1 )
m_comment = new String( *m_original, pos + 1 );
return true;
}
break;
default:
break;
}
}
// cleanup
if ( state == value )
{
// trim the value
m_value->size( trimValuePos );
return true;
}
else if ( state == normal )
{
// empty line
return true;
}
else if ( state == postvalue || state == prevalue )
{
// we are in a coherent status. If we're in prevalue, the key was left without a value, which is ok
// i.e. key = <nothing>
return true;
}
// any other state means we failed parsing
return false;
}
void deletor_ConfigFileLine( void *memory )
{
ConfigFileLine *line = (ConfigFileLine *) memory;
delete line;
}
//=======================================================
// ConfigEntry traits
uint32 ConfigEntryPtrTraits::memSize() const
{
return sizeof( ConfigEntry * );
}
void ConfigEntryPtrTraits::init( void *itemZone ) const
{
ConfigEntry **map = (ConfigEntry **) itemZone;
*map = 0;
}
void ConfigEntryPtrTraits::copy( void *targetZone, const void *sourceZone ) const
{
ConfigEntry **tgt = (ConfigEntry **) targetZone;
ConfigEntry *src = (ConfigEntry *) sourceZone;
*tgt = src;
}
int ConfigEntryPtrTraits::compare( const void *first, const void *second ) const
{
return -1;
}
void ConfigEntryPtrTraits::destroy( void *item ) const
{
ConfigEntry **ptr = (ConfigEntry **) item;
delete *ptr;
}
bool ConfigEntryPtrTraits::owning() const
{
return true;
}
namespace traits
{
ConfigEntryPtrTraits &t_ConfigEntryPtr() { static ConfigEntryPtrTraits td; return td; }
}
//==============================================================
// ConfigSection and traits
//==============================================================
ConfigSection::ConfigSection( const String &name, ListElement *begin, ListElement *ae ):
m_name(name),
m_entries( &traits::t_stringptr(), &traits::t_ConfigEntryPtr() ),
m_sectDecl( begin ),
m_additionPoint( ae )
{
}
uint32 ConfigSectionPtrTraits::memSize() const
{
return sizeof( ConfigSection *);
}
void ConfigSectionPtrTraits::init( void *itemZone ) const
{
ConfigSection **sect = (ConfigSection **) itemZone;
*sect = 0;
}
void ConfigSectionPtrTraits::copy( void *targetZone, const void *sourceZone ) const
{
ConfigSection **tgt = (ConfigSection **) targetZone;
ConfigSection *src = (ConfigSection *) sourceZone;
*tgt = src;
}
int ConfigSectionPtrTraits::compare( const void *first, const void *second ) const
{
return -1;
}
void ConfigSectionPtrTraits::destroy( void *item ) const
{
ConfigSection **ptr = (ConfigSection **) item;
delete *ptr;
}
bool ConfigSectionPtrTraits::owning() const
{
return true;
}
namespace traits
{
ConfigSectionPtrTraits &t_ConfigSectionPtr() { static ConfigSectionPtrTraits dt; return dt; }
}
//==============================================================
// ConfigFile
//==============================================================
ConfigFile::ConfigFile( const String &filename, const String &encoding ):
m_fileName( filename ),
m_lines( deletor_ConfigFileLine ),
m_rootEntry( "root", 0 ),
m_sections( &traits::t_stringptr(), &traits::t_ConfigSectionPtr() ),
m_fsError(0),
m_encoding( encoding ),
m_currentValue( 0 ),
m_errorLine( 0 ),
m_bUseUnixComments( false ),
m_bUseUnixSpecs( false )
{
}
ConfigFile::~ConfigFile()
{}
bool ConfigFile::load()
{
m_fsError = 0;
m_errorMsg = "";
Stream *input = 0;
//===========================================
// Initialization
FileStream stream;
if ( ! stream.open( m_fileName, BaseFileStream::e_omReadOnly, BaseFileStream::e_smShareRead ) )
{
stream.errorDescription( m_errorMsg );
m_fsError = (long) stream.lastError();
return false;
}
// get a good transcoder
if ( m_encoding == "" )
m_encoding = "C";
input = TranscoderFactory( m_encoding, &stream, false );
if ( input == 0 )
{
m_errorMsg = "Invalid encoding '" + m_encoding + "'";
return false;
}
input = AddSystemEOL( input, true );
bool ret = load( input );
delete input;
stream.close();
return ret;
}
bool ConfigFile::load( Stream *input )
{
uint32 chr;
String *currentLine = 0;
uint32 count = 1;
// start putting items in the main section
ConfigSection *current = &m_rootEntry;
//===========================================
// main loop
while( input->get( chr ) )
{
if ( currentLine == 0 )
currentLine = new String;
if ( chr == '\n' )
{
// nextline
ConfigFileLine *line = new ConfigFileLine( currentLine );
if ( line->parseLine() )
{
m_lines.pushBack( line );
if( line->m_type == ConfigFileLine::t_section )
{
// change current key storage
current = new ConfigSection( *line->m_key, m_lines.end(), m_lines.end() );
m_sections.insert( ¤t->m_name, current );
}
else if( line->m_type == ConfigFileLine::t_keyval )
{
ListElement *last = m_lines.end();
// is the item already present?
MapIterator pos;
ConfigEntry *entry;
if ( current->m_entries.find( line->m_key, pos ) )
{
entry = *(ConfigEntry **) pos.currentValue();
}
else {
entry = new ConfigEntry;
entry->m_entry = *line->m_key;
current->m_entries.insert( &entry->m_entry, entry );
}
entry->m_values.pushBack( last ); // save this list element
current->m_additionPoint = m_lines.end();
}
else if ( line->m_type == ConfigFileLine::t_keyval )
{
// be sure to set insertion point after comments.
current->m_additionPoint = m_lines.end();
}
}
else {
m_errorMsg = "Parse failed at line ";
m_errorLine = count;
m_errorMsg.writeNumber( (int64) count );
return false;
}
// the line has been taken by the ConfigFileLine
currentLine = 0;
count ++;
}
else {
currentLine->append( chr );
}
}
//===========================================
// cleanup
if ( input->error() )
goto error;
return true;
//===========================================
// error handling
error:
m_fsError = (long) input->lastError();
input->errorDescription( m_errorMsg );
return false;
}
bool ConfigFile::save()
{
Stream *output = 0;
//===========================================
// Initialization
FileStream stream;
if ( ! stream.create( m_fileName,
FileStream::e_aUserRead | FileStream::e_aReadOnly,
FileStream::e_smShareRead ) )
{
m_fsError = (long) stream.lastError();
stream.errorDescription( m_errorMsg );
return false;
}
// get a good transcoder
if ( m_encoding == "" )
m_encoding = "C";
output = TranscoderFactory( m_encoding, &stream, false );
if ( output == 0 )
{
m_errorMsg = "Invalid encoding '" + m_encoding + "'";
return false;
}
output = AddSystemEOL( output, true );
bool ret = save( output );
delete output;
stream.close();
return ret;
}
bool ConfigFile::save( Stream *output )
{
ListElement *element = m_lines.begin();
while( element != 0 && output->good() )
{
ConfigFileLine *line = (ConfigFileLine *) element->data();
if ( line->m_original != 0 )
{
output->writeString( *line->m_original );
}
else {
if ( line->m_type == ConfigFileLine::t_keyval )
{
output->writeString( *line->m_key );
if( m_bUseUnixSpecs )
output->writeString( ":" );
else
output->writeString( " = " );
String tempValue;
line->m_value->escape( tempValue );
// any escaped char changes the lenght of the string.
// so, if the escaped changed the string...
// We need quotes also if the string contains a comment character.
if ( tempValue.length() != line->m_value->length() ||
line->m_value->find( ";" ) != String::npos ||
line->m_value->find( "#" ) != String::npos
)
tempValue = "\"" + tempValue + "\""; // we need some ""
output->writeString( tempValue );
}
else if ( line->m_type == ConfigFileLine::t_section )
{
output->writeString( "[" );
output->writeString( *line->m_key );
output->writeString( "]" );
}
// if it was an original comment, we would have just re-written in the
// previous if.
if ( line->m_comment != 0 )
{
if ( m_bUseUnixComments )
output->writeString( "\t# " );
else
output->writeString( "\t; " );
output->writeString( *line->m_comment );
}
}
output->writeString( "\n" );
element = element->next();
}
if ( !output->good() )
{
m_fsError = (long) output->lastError();
output->errorDescription( m_errorMsg );
return false;
}
return true;
}
bool ConfigFile::getValue( const String &key, String &value )
{
MapIterator pos;
if ( ! m_rootEntry.m_entries.find( &key, pos ) )
return false;
ConfigEntry *ce = *(ConfigEntry **) pos.currentValue();
// values of entries is a list of ListElements; each ListElement is actually stored in the
// file lines list.
ListElement *le = (ListElement *) ce->m_values.begin()->data();
ConfigFileLine *line = (ConfigFileLine *) le->data();
value = (line->m_value) ? *(line->m_value) : "" ;
m_currentValue = ce->m_values.begin()->next();
return true;
}
bool ConfigFile::getValue( const String §ion, const String &key, String &value )
{
MapIterator pos;
if ( ! m_sections.find( §ion, pos ) )
return false;
ConfigSection *sect = *(ConfigSection **) pos.currentValue();
if ( ! sect->m_entries.find( &key, pos ) )
return false;
ConfigEntry *ce = *(ConfigEntry **) pos.currentValue();
// values of entries is a list of ListElements; each ListElement is actually stored in the
// file lines list.
ListElement *le = (ListElement *) ce->m_values.begin()->data();
ConfigFileLine *line = (ConfigFileLine *) le->data();
value = *line->m_value;
m_currentValue = ce->m_values.begin()->next();
return true;
}
bool ConfigFile::getNextValue( String &value )
{
if ( m_currentValue == 0 )
return false;
ListElement *le = (ListElement *) m_currentValue->data();
ConfigFileLine *line = (ConfigFileLine *) le->data();
value = *line->m_value;
m_currentValue = m_currentValue->next();
return true;
}
bool ConfigFile::getFirstKey_internal( ConfigSection *sect, const String &prefix, String &key )
{
Map *map = §->m_entries;
if ( map->empty() )
return false;
MapIterator pos;
if ( prefix != "" )
{
String catPrefix = prefix + ".";
map->find( &catPrefix, pos );
if ( ! pos.hasCurrent() )
return false;
String *currentKey = *(String **) pos.currentKey();
if ( currentKey->find( catPrefix ) == 0 )
{
m_keysIter = pos;
m_keyMask = catPrefix;
key = *currentKey;
}
else
return false;
}
else {
m_keyMask = "";
m_keysIter = map->begin();
key = **(String **) m_keysIter.currentKey();
}
m_keysIter.next();
return true;
}
bool ConfigFile::getFirstKey( const String §ion, const String &prefix, String &key )
{
MapIterator sectIter;
if ( ! m_sections.find( §ion, sectIter ) )
return false;
ConfigSection *sect = *(ConfigSection **) sectIter.currentValue();
return getFirstKey_internal( sect, prefix, key );
}
bool ConfigFile::getNextKey( String &key )
{
if( ! m_keysIter.hasCurrent() )
return false;
String *currentKey = *(String **) m_keysIter.currentKey();
m_keysIter.next();
if( m_keyMask == "" || currentKey->find( m_keyMask ) == 0 )
{
key = *currentKey;
return true;
}
return false;
}
bool ConfigFile::getFirstSection( String §ion )
{
if( m_sections.empty() )
return false;
m_sectionIter = m_sections.begin();
// sections are string -> map * maps; strings, not string *!
String *sectName = *(String **) m_sectionIter.currentKey();
section = *sectName;
m_sectionIter.next();
return true;
}
bool ConfigFile::getNextSection( String &nextSection )
{
if( m_sectionIter.hasCurrent() )
{
String *sectName = *(String **) m_sectionIter.currentKey();
nextSection = *sectName;
m_sectionIter.next();
return true;
}
return false;
}
void ConfigFile::setValue( const String &key, String &value )
{
setValue_internal( &m_rootEntry, key, value );
}
void ConfigFile::setValue( const String §ion, const String &key, const String &value )
{
MapIterator sectIter;
ConfigSection *sect;
if ( m_sections.find( §ion, sectIter ) )
{
sect = *( ConfigSection **) sectIter.currentValue();
}
else {
// we must add a new section
sect = addSection( section );
}
setValue_internal( sect, key, value );
}
void ConfigFile::addValue( const String &key, const String &value )
{
addValue_internal( &m_rootEntry, key, value );
}
void ConfigFile::addValue( const String §ion, const String &key, String value )
{
MapIterator sectIter;
ConfigSection *sect;
if ( m_sections.find( §ion, sectIter ) )
{
sect = *( ConfigSection **) sectIter.currentValue();
}
else {
// we must add a new section
sect = addSection( section );
}
addValue_internal( sect, key, value );
}
bool ConfigFile::removeValue( const String &key )
{
return removeValue_internal( &m_rootEntry, key );
}
bool ConfigFile::removeValue( const String §ion, const String &key )
{
MapIterator sectIter;
ConfigSection *sect;
if ( ! m_sections.find( §ion, sectIter ) )
{
return false;
}
sect = *( ConfigSection **) m_sectionIter.currentValue();
return removeValue_internal( sect, key );
}
bool ConfigFile::removeCategory_internal( ConfigSection *sect, const String &category )
{
String key;
if( ! getFirstKey_internal( sect, category, key ) )
return false;
String key1 = key;
while( getNextKey( key ) )
{
removeValue_internal( sect, key1 );
key1 = key;
}
removeValue_internal( sect, key1 );
return true;
}
bool ConfigFile::removeCategory( const String &cat )
{
return removeCategory_internal( &m_rootEntry, cat );
}
bool ConfigFile::removeCategory( const String §ion, const String &cat )
{
MapIterator sectIter;
ConfigSection *sect;
if ( ! m_sections.find( &cat, sectIter ) )
{
return false;
}
sect = *( ConfigSection **) m_sectionIter.currentValue();
return removeCategory_internal( sect, cat );
}
ConfigSection *ConfigFile::addSection( const String §ion )
{
// check if the section already exists:
MapIterator sectIter;
if ( m_sections.find( §ion, sectIter ) )
{
return 0;
}
// create the section
ConfigFileLine *sectLine = new ConfigFileLine(
ConfigFileLine::t_section, 0, new String( section ) );
// add a section at the bottom of the file.
m_lines.pushBack( sectLine );
ConfigSection *sect = new ConfigSection( section, m_lines.end(), m_lines.end() );
m_sections.insert( §->m_name, sect );
return sect;
}
void ConfigFile::setValue_internal( ConfigSection *sect, const String &key, const String &value )
{
MapIterator pos;
ConfigEntry *entry;
// we must find the first entry, if present.
if ( sect->m_entries.find( &key, pos ) )
{
entry = *(ConfigEntry **) pos.currentValue();
}
else {
addValue_internal( sect, key, value );
return;
}
// the first value of the entry must be changed; the other must be deletd.
ListElement *valline = entry->m_values.begin();
if ( valline == 0 )
{
// overkill, should never happen.
addValue_internal( sect, key, value );
return;
}
// change the first line value
ListElement *cfline = (ListElement *) valline->data();
ConfigFileLine *line = (ConfigFileLine *) cfline->data();
*line->m_value = value;
// by deleting the original, we signal this line must be recomputed.
delete line->m_original;
line->m_original = 0;
// then remove the other values
valline = valline->next();
while( valline != 0 )
{
ListElement *toBeDeleted = (ListElement *) valline->data();
m_lines.erase( toBeDeleted );
valline = entry->m_values.erase( valline );
}
}
void ConfigFile::addValue_internal( ConfigSection *sect, const String &key, const String &value )
{
MapIterator pos;
ListElement *addPoint;
ConfigEntry *entry = 0;
// If there is already an entry, it's elegant to add the new value below the old ones.
if ( sect->m_entries.find( &key, pos ) )
{
entry = *(ConfigEntry **) pos.currentValue();
addPoint = (ListElement *) entry->m_values.end()->data();
}
else {
// just add the value as the last entry of the section
entry = new ConfigEntry;
entry->m_entry = key;
sect->m_entries.insert( &entry->m_entry, entry );
addPoint = sect->m_additionPoint;
}
ConfigFileLine *line = new ConfigFileLine( ConfigFileLine::t_keyval,
0,
new String( key ),
new String( value ) );
if( addPoint == 0 )
{
m_lines.pushFront( line );
addPoint = m_lines.begin();
sect->m_additionPoint = addPoint;
}
else {
m_lines.insertAfter( addPoint, line );
addPoint = addPoint->next();
}
// we must put the ListElement holding the LINE where the value has been set
entry->m_values.pushBack( addPoint );
}
bool ConfigFile::removeValue_internal( ConfigSection *sect, const String &key )
{
MapIterator pos;
ConfigEntry *entry;
// we must find the first entry, if present.
if ( sect->m_entries.find( &key, pos ) )
{
entry = *(ConfigEntry **) pos.currentValue();
}
else {
return false;
}
// the first value of the entry must be changed; the other must be deletd.
ListElement *valline = entry->m_values.begin();
// then we must remove all the values
while( valline != 0 )
{
ListElement *toBeDeleted = (ListElement *) valline->data();
m_lines.erase( toBeDeleted );
valline = valline->next();
}
// then remove the entry from the map
sect->m_entries.erase( pos );
return true;
}
void ConfigFile::clearMainSection()
{
m_rootEntry.m_entries.clear();
m_rootEntry.m_sectDecl = 0;
m_rootEntry.m_additionPoint = 0;
ListElement *line = m_lines.begin();
while( line != 0 )
{
ConfigFileLine *lineData = (ConfigFileLine *) line->data();
if ( lineData->m_type == ConfigFileLine::t_section )
break;
m_lines.popFront(); // will also destroy the data
line = m_lines.begin();
}
}
bool ConfigFile::removeSection( const String &key )
{
MapIterator pos;
if( ! m_sections.find( &key, pos ) )
return false;
ConfigSection *sect = *(ConfigSection **) pos.currentValue();
ListElement *line = sect->m_sectDecl;
if ( line != 0 )
{
line = m_lines.erase( line );
}
while( line != 0 )
{
ConfigFileLine *lineData = (ConfigFileLine *) line->data();
if ( lineData->m_type == ConfigFileLine::t_section )
break;
line = m_lines.erase( line ); // will also destroy the data
}
m_sections.erase( pos );
return true;
}
}
/* end of confparser_mod.cpp */
|