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
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: INIFile.C,v 1.38.18.1 2007/03/25 22:00:17 oliver Exp $
//
#include <BALL/FORMAT/INIFile.h>
#include <fstream>
#include <iostream>
using namespace std;
namespace BALL
{
const String INIFile::UNDEFINED = "[UNDEFINED!]";
const String INIFile::HEADER = "#HEADER!";
// Default constructor
INIFile::INIFile()
: check_duplicate_keys_(false),
valid_(false),
filename_("")
{
appendSection(HEADER);
}
INIFile::INIFile(const String& filename)
: check_duplicate_keys_(false),
valid_(false),
filename_(filename)
{
appendSection(HEADER);
}
const INIFile& INIFile::operator = (const INIFile& file)
{
check_duplicate_keys_ = file.check_duplicate_keys_;
valid_ = file.valid_;
filename_ = file.filename_;
appendSection(HEADER);
return *this;
}
void INIFile::clear()
{
sections_.clear();
section_index_.clear();
valid_ = false;
check_duplicate_keys_ = false;
appendSection(HEADER);
}
INIFile::~INIFile()
{
sections_.clear();
section_index_.clear();
}
bool INIFile::isValid() const
{
return valid_;
}
void INIFile::setFilename(const String& filename)
{
filename_ = filename;
valid_ = false;
}
const String& INIFile::getFilename() const
{
return filename_;
}
bool INIFile::read()
{
// destroy all datastructures - we make a new start
// we only keep the filename...
clear();
// If the filename is empty, there's no point in opening it...
if (filename_ == "")
{
return false;
}
// try to open the file
ifstream infile(filename_.c_str());
// if we couldn't open the file: abort
if (!infile)
{
return false;
}
list<Section>::iterator section_it(sections_.begin());
// read all lines from the file
std::vector<char> buffer(MAX_LINE_LENGTH);
while (infile.getline(&(buffer[0]), MAX_LINE_LENGTH))
{
// remove leading blanks
String line(&(buffer[0]));
line.trimLeft();
// check for comment lines or empty line
if (line.empty() || (line[0] == '!') ||
(line[0] == ';') || (line[0] == '#'))
{
section_it->lines_.push_back(&(buffer[0]));
continue;
}
// check for start of section
if (line[0] == '[')
{
if (!appendSection(line))
{
return false;
}
section_it++;
continue;
}
// this is neither a comment line nor a section start
// line still has to be added
if (!appendLine("", line))
{
return false;
}
}
// close the file
infile.close();
// done.
valid_ = true;
return true;
}
bool INIFile::write()
{
// try to open the file
ofstream out(filename_.c_str(), ios::out);
if (out.bad())
{
return false;
}
LineIterator line_it(getLine(0));
// iterate over all lines and write them
for (; +line_it; ++line_it)
{
out << *line_it << endl;
}
out.close();
valid_ = true;
return true;
}
INIFile::LineIterator INIFile::getLine(Size line_number)
{
if (line_number >= getNumberOfLines())
{
return LineIterator();
}
int line_nr(line_number);
int nr(-1);
LineIterator line_it;
SectionIterator section_it(sections_.begin());
for (; (section_it != sections_.end() && nr != line_nr); ++section_it)
{
if ((int)section_it->lines_.size() + nr < line_nr)
{
nr += section_it->lines_.size();
continue;
}
line_it = getSectionFirstLine(section_it->getName());
nr++;
for (; nr < line_nr; ++line_it)
{
nr++;
}
}
return (line_it);
}
bool INIFile::setLine(LineIterator line_it, const String& line)
{
// section lines cannot be changed with this method
if (!isValid(line_it) || (*line_it)[0] == '[')
{
return false;
}
String new_key(line.before("="));
new_key.trim();
if ((*line_it).hasSubstring("=", 1))
{
// oh, this line had a key :(
String old_key((*line_it).before("="));
old_key.trim();
if (old_key == new_key)
{
line_it.setLine_(line);
return true;
}
// its a new key: delete the old one.
line_it.getSection()->key_map_.remove(old_key);
}
line_it.setLine_(line);
if (line.hasSubstring("=", 1))
{
// oh, the new line has a key :(
line_it.getSection()->key_map_[new_key] = line_it.getPosition();
}
return true;
}
bool INIFile::insertLine(LineIterator line_it, const String& line)
{
if (!isValid(line_it))
{
Log.error() << "In INIFile " << filename_ << " , error while inserting line: "
<< line << " . Illegal iterator!" << endl;
return false;
}
if (line_it.isSectionLastLine())
{
return appendLine(line_it.getSection()->getName(), line);
}
Section& section(*line_it.getSection());
// key?
if (line.hasSubstring("=", 1))
{
String key(line.before("="));
key.trim();
if (section.key_map_.has(key) && check_duplicate_keys_)
{
Log.error() << "In INIFile " << filename_ << " , error while appending line: "
<< line << " . Key '" << key << "' already exists in section." << endl;
return false;
}
line_it.getSectionNextLine();
section.key_map_[key] = section.lines_.insert(line_it.position_, line);
return true;
}
line_it.getSectionNextLine();
section.lines_.insert(line_it.position_, line);
return true;
}
bool INIFile::deleteLine(LineIterator line_it)
{
// test if line exists and if we try to remove a section line
if (!isValid(line_it) || (*line_it)[0] == '[')
{
return false;
}
// falls key, entfernen
if ((*line_it).hasSubstring("=", 1))
{
String key((*line_it).before("="));
key.trim();
line_it.getSection()->key_map_.remove(key);
}
line_it.getSection()->lines_.erase(line_it.position_);
return true;
}
bool INIFile::appendLine(const String& sectionName, const String& line)
{
String section_name(sectionName);
if (section_name == "")
{
SectionIterator it(sections_.end());
it--;
section_name = it->getName();
}
if (!hasSection(section_name) || line[0] == '[')
{
Log.error() << "In INIFile " << filename_ << " , error while appending line: "
<< line << " . Illegal section-name: " << sectionName << endl;
return false;
}
Section& section(*getSection(section_name));
// key?
if (line.hasSubstring("=", 1))
{
String key(line.before("="));
key.trim();
if (section.key_map_.has(key) && check_duplicate_keys_)
{
Log.error() << "In INIFile " << filename_ << " , error while appending line: "
<< line << " . Key '" << key << "' already exists in section." << endl;
return false;
}
section.lines_.push_back(line);
list<String >::iterator line_it(section.lines_.end());
line_it--;
section.key_map_[key] = line_it;
return true;
}
section.lines_.push_back(line);
return true;
}
Size INIFile::getNumberOfLines() const
{
Size number_of_lines(0);
list<Section>::const_iterator it = sections_.begin();
for (; it != sections_.end(); ++it)
{
number_of_lines += it->lines_.size();
}
return number_of_lines;
}
Size INIFile::getNumberOfSections() const
{
// HEADER is not counted
// every inifile has at least one section: the header
// wird sehr gross positiv!
return (Size)sections_.size() - 1;
}
bool INIFile::hasSection(const String& section_name) const
{
return section_index_.has(section_name);
}
INIFile::SectionIterator INIFile::getSection(const String& section_name)
{
if (!section_index_.has(section_name))
{
return SectionIterator(sections_.end());
}
return section_index_[section_name];
}
INIFile::SectionIterator INIFile::getSection(Position pos)
{
if (pos >= sections_.size())
{
return SectionIterator(sections_.end());
}
SectionIterator it = sections_.begin();
for (Position i = 0; i < pos && it != sections_.end(); i++)
{
++it;
}
return it;
}
INIFile::LineIterator INIFile::getSectionFirstLine(const String& section_name)
{
if (!section_index_.has(section_name))
{
return LineIterator();
}
return LineIterator(sections_, getSection(section_name),
getSection(section_name)->lines_.begin());
}
INIFile::LineIterator INIFile::getSectionLastLine(const String& section_name)
{
if (!section_index_.has(section_name))
{
return LineIterator();
}
SectionIterator sit = getSection(section_name);
list<String >::iterator line_it(sit->lines_.end());
if (sit->lines_.size() > 0) line_it--;
return LineIterator(sections_, getSection(section_name), line_it);
}
Size INIFile::getSectionLength(const String& section_name) const
{
if (!section_index_.has(section_name))
{
return INVALID_Size;
}
return (Size)section_index_[section_name]->lines_.size();
}
bool INIFile::hasEntry(const String& section_name, const String& key) const
{
if (!section_index_.has(section_name))
{
return false;
}
// check the hash map for the key
return section_index_[section_name]->key_map_.has(key);
}
bool INIFile::insertValue(const String& section_name, const String& key, const String& value)
{
// does section exists?
if (!section_index_.has(section_name) || hasEntry(section_name, key))
{
return false;
}
String new_line(key + "=" + value);
appendLine(section_name, new_line);
return true;
}
bool INIFile::setValue(const String& section_name, const String& key, const String& new_value)
{
// does section exists?
if (!section_index_.has(section_name) || key.isEmpty()
|| !section_index_[section_name]->key_map_.has(key))
{
return false;
}
String new_line(key + "=" + new_value);
(*section_index_[section_name]->key_map_[key]) = new_line;
return true;
}
String INIFile::getValue(const String& section_name, const String& key) const
{
if (!section_index_.has(section_name) ||
key.isEmpty() ||
!section_index_[section_name]->key_map_.has(key))
{
return UNDEFINED;
}
// get the part of the line behind the "="
String match_name((*section_index_[section_name]->key_map_[key]).after('=', 0));
match_name.trim();
return match_name;
}
bool INIFile::deleteSection(const String& section_name)
{
if (!section_index_.has(section_name))
{
return false;
}
if (section_name == HEADER)
{
while (+getSectionFirstLine(HEADER))
{
deleteLine(getSectionFirstLine(HEADER));
}
return true;
}
sections_.erase(section_index_[section_name]);
section_index_.remove(section_name);
return true;
}
bool INIFile::appendSection(const String& section_name)
{
// strip the bracket to get the name
String line(section_name);
if (line[0] == '[')
{
// remove the leading '['
line.erase(0, 1);
if (!line.has(']'))
{
Log.error() << "INIFile::appendSection " << filename_ << " , while adding section:"
<< "missing bracket." << endl;
return false;
}
line = line.before("]");
}
if (section_index_.has(line))
{
Log.error() << "INIFile::appendSection: " << filename_ << " , while adding section: '"
<< line << "' already exists." << endl;
return false;
}
Section section;
section.name_ = line;
sections_.push_back(section);
SectionIterator section_it(sections_.end());
section_it--;
// remember the current section_name
section_index_[line] = section_it;
if (line == HEADER)
{
return true;
}
// store the line
line = '[' + line +']';
section_it->lines_.push_back(line);
return true;
}
bool INIFile::apply(UnaryProcessor<LineIterator>& processor)
{
if (!processor.start())
{
return false;
}
LineIterator line_it(getLine(0));
for (; +line_it; ++line_it)
{
Processor::Result result = processor(line_it);
if (result <= Processor::BREAK)
{
return (result && processor.finish());
}
}
return (processor.finish());
}
bool INIFile::isValid(const LineIterator& it) const
{
return (it.isValid() && it.getBound_() == §ions_);
}
bool INIFile::operator == (const INIFile& inifile) const
{
return (sections_ == inifile.sections_);
}
bool INIFile::isValid(const SectionIterator& it) const
{
return ((list<Section>::const_iterator)it != sections_.end());
}
void INIFile::setDuplicateKeyCheck(bool mode)
{
check_duplicate_keys_ = mode;
}
bool INIFile::duplicateKeyCheckEnabled() const
{
return check_duplicate_keys_;
}
list<String> INIFile::getContent() const
{
list<String> lines;
LineIterator it = const_cast<INIFile*>(this)->getLine(0);
for (; +it; ++it)
{
lines.push_back(*it);
}
return lines;
}
bool INIFile::setContent(const list<String>& lines)
{
list<String>::const_iterator it = lines.begin();
for (; it != lines.end(); ++it)
{
if (!appendLine(*it)) return false;
}
return true;
}
bool INIFile::appendLine(const String& data)
{
if (data[0] == '[')
{
return appendSection(data);
}
if (sections_.empty()) return false;
return appendLine((*sections_.rbegin()).name_, data);
}
// ===================== IteratorTraits_ ==========================
INIFile::IteratorTraits_::IteratorTraits_()
: bound_(0),
section_(),
position_()
{
}
INIFile::IteratorTraits_::IteratorTraits_(list<INIFile::Section>& list_var,
INIFile::SectionIterator section,
list<String>::iterator line)
: bound_(&list_var),
section_(section),
position_(line)
{
}
INIFile::IteratorTraits_::IteratorTraits_(const INIFile::IteratorTraits_& traits)
: bound_(traits.bound_),
section_(traits.section_),
position_(traits.position_)
{
}
INIFile::IteratorTraits_::~IteratorTraits_()
{
}
const INIFile::IteratorTraits_& INIFile::IteratorTraits_::operator =
(const INIFile::IteratorTraits_ &traits)
{
bound_ = traits.bound_;
section_ = traits.section_;
position_ = traits.position_;
return *this;
}
list<String>::iterator INIFile::IteratorTraits_::getPosition()
{
return position_;
}
INIFile::SectionIterator INIFile::IteratorTraits_::getSection()
{
return section_;
}
const String& INIFile::IteratorTraits_::operator * () const
{
return *position_;
}
INIFile::IteratorTraits_& INIFile::IteratorTraits_::operator ++ ()
{
if (!isValid()) return *this;
if (!isSectionLastLine())
{
position_++;
return *this;
}
section_++;
if (section_ == bound_->end()) return *this;
position_ = section_->lines_.begin();
return *this;
}
INIFile::IteratorTraits_& INIFile::IteratorTraits_::operator -- ()
{
if (!bound_) return *this;
if (section_ == bound_->end()) toLastLine();
if (!isSectionFirstLine())
{
position_--;
return *this;
}
// if we are at the first line in the file, invalidate the iterator
if (section_ == bound_->begin())
{
position_ = section_->lines_.end();
return *this;
}
section_--;
toSectionLastLine();
return *this;
}
INIFile::IteratorTraits_& INIFile::IteratorTraits_::getSectionNextLine()
{
if (!isValid()) return *this;
position_++;
return *this;
}
bool INIFile::IteratorTraits_::operator == (const INIFile::IteratorTraits_& traits) const
{
return (bound_ == traits.bound_ &&
section_ == traits.section_ &&
position_ == traits.position_);
}
bool INIFile::IteratorTraits_::operator != (const INIFile::IteratorTraits_& traits) const
{
return !(*this == traits);
}
bool INIFile::IteratorTraits_::operator + () const
{
return (isValid() &&
position_ != section_->lines_.end());
}
bool INIFile::IteratorTraits_::isValid() const
{
return (bound_ != 0 && section_ != bound_->end());
}
void INIFile::IteratorTraits_::toSectionFirstLine()
{
if (!isValid()) return;
position_ = section_->lines_.begin();
}
void INIFile::IteratorTraits_::toSectionLastLine()
{
if (!isValid()) return;
position_ = section_->lines_.end();
position_--;
}
void INIFile::IteratorTraits_::toSectionEnd()
{
if (!isValid()) return;
position_ = section_->lines_.end();
}
bool INIFile::IteratorTraits_::isSectionFirstLine() const
{
return (isValid() && position_ == section_->lines_.begin());
}
bool INIFile::IteratorTraits_::isSectionLastLine() const
{
if (!isValid()) return false;
list<String>::iterator it = section_->lines_.end();
it--;
return (position_ == it);
}
bool INIFile::IteratorTraits_::isSectionEnd() const
{
if (!isValid()) return false;
return (position_ == section_->lines_.end());
}
void INIFile::IteratorTraits_::toFirstLine()
{
if (!bound_) return;
section_ = bound_->begin();
position_ = section_->lines_.begin();
// avoid problem with empty HEADER
if (position_ == section_->lines_.end()) ++*this;
}
void INIFile::IteratorTraits_::toLastLine()
{
if (!bound_) return;
section_ = bound_->end();
section_--;
position_ = section_->lines_.end();
position_--;
}
void INIFile::IteratorTraits_::toEnd()
{
if (!bound_) return;
section_ = bound_->end();
section_--;
position_ = section_->lines_.end();
}
const list<INIFile::Section>* INIFile::IteratorTraits_::getBound_() const
{
return bound_;
}
void INIFile::IteratorTraits_::setLine_(const String& line)
{
(*position_) = line;
}
bool INIFile::Section::operator < (const Section& section) const
{
return name_ < section.name_;
}
bool INIFile::Section::operator > (const Section& section) const
{
return name_ < section.name_;
}
} // namespace BALL
|