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
|
/*
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/**
* Configuration file reader/writer.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <float.h>
#include "base/config_file.h"
#include "base/util_functions.h"
#include "base/string_utilities.h"
#include "base/log.h"
DEFAULT_LOG_DOMAIN(DOMAIN_BASE);
#ifdef WIN32
#define vsnprintf _vsnprintf
#endif
using namespace std;
using namespace base;
// What character separates key and value.
const std::string EqualIndicators = "=:";
typedef struct
{
std::string key;
std::string value;
std::string pre_comment; // A comment before the entry (separate line(s)).
std::string post_comment; // A comment after the entry (same line).
} ConfigEntry;
typedef std::vector<ConfigEntry> EntryList; // Must be a vector instead of a map to preserve order and
// allow multiple values with the same key (includes).
typedef EntryList::iterator EntryListIterator;
typedef struct
{
std::string name;
std::string comment; // A comment placed before a section start.
EntryList keys;
} ConfigSection;
typedef std::vector<ConfigSection> SectionList;
typedef SectionList::iterator SectionListIterator;
/**
* Extracts the next key from the next key/value pair in the line and returns it.
*/
std::string extract_next_word(std::string& line)
{
size_t position = line.find_first_of(EqualIndicators);
std::string word = std::string("");
if (position != std::string::npos)
{
word = line.substr(0, position);
line.erase(0, position + 1);
}
else
{
word = line;
line = "";
}
return base::trim(word);
}
//----------------- ConfigurationFile::Private -----------------------------------------------------
class ConfigurationFile::Private
{
public:
int _flags;
SectionList _sections;
bool _dirty;
std::string _trailing_comments; // Any comments after the last key-value or section entry.
Private(std::string file_name, ConfigFileFlags flags);
ConfigEntry* get_entry_in_section(std::string key, std::string section, bool auto_create);
ConfigSection* get_section(std::string section, bool auto_create);
void clear_includes(const std::string §ion_name);
void add_include(const std::string §ion_name, const std::string &include);
void add_include_dir(const std::string §ion_name, const std::string &include);
std::vector<std::string> get_includes(const std::string §ion_name);
bool set_value(std::string key_name, std::string value, std::string section_name);
void set_dirty();
void clear();
bool create_key(std::string key, std::string value, std::string pre_comment, std::string post_comment,
std::string section);
bool delete_key(std::string key, std::string section_name);
bool create_section(std::string section_name, std::string comment);
bool delete_section(std::string name);
int section_count();
int key_count();
int key_count_for_section(const std::string §ion_name);
bool is_dirty();
std::string make_comment(const std::string &text);
bool load(const std::string &file_name);
bool save(const std::string &file_name);
};
//--------------------------------------------------------------------------------------------------
ConfigurationFile::Private::Private(std::string file_name, ConfigFileFlags flags)
{
_dirty = false;
_flags = flags;
// Always create a default section.
_sections.push_back(ConfigSection());
if (!file_name.empty())
load(file_name);
}
//--------------------------------------------------------------------------------------------------
/**
* Looks up the given key in the given section and returns its entry if found, otherwise NULL.
*/
ConfigEntry* ConfigurationFile::Private::get_entry_in_section(std::string key, std::string section_name,
bool auto_create)
{
ConfigSection* section = get_section(section_name, auto_create && (_flags & AutoCreateSections) != 0);
if (section == NULL)
return NULL;
for (EntryListIterator iterator = section->keys.begin(); iterator != section->keys.end(); iterator++)
{
if (strcasecmp(iterator->key.c_str(), key.c_str()) == 0)
return &(*iterator);
}
if (auto_create)
{
_dirty = true;
ConfigEntry new_entry;
new_entry.key = base::trim(key);
section->keys.push_back(new_entry);
return §ion->keys.back();
}
return NULL;
}
//--------------------------------------------------------------------------------------------------
/**
* Looks up the given section in our list and returns a pointer to it if found.
* If the section could not be found and auto_create is true then it is created and returned.
* Otherwise NULL is returned.
*/
ConfigSection* ConfigurationFile::Private::get_section(std::string section_name, bool auto_create)
{
section_name = base::trim(section_name);
for (SectionListIterator iterator = _sections.begin(); iterator != _sections.end(); iterator++)
{
if (strcasecmp(iterator->name.c_str(), section_name.c_str()) == 0)
return &(*iterator);
}
if (auto_create)
{
create_section(section_name, "");
return &_sections.back();
}
return NULL;
}
//--------------------------------------------------------------------------------------------------
bool is_include(ConfigEntry &entry)
{
std::string key = base::tolower(entry.key);
return key == "!include" || key == "!includedir";
}
void ConfigurationFile::Private::clear_includes(const std::string §ion_name)
{
ConfigSection *section = get_section(section_name, (_flags & AutoCreateSections) != 0);
if (section == NULL)
return;
section->keys.erase(std::remove_if(section->keys.begin(), section->keys.end(), is_include), section->keys.end());
_dirty = true;
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::Private::add_include(const std::string §ion_name, const std::string &include)
{
ConfigSection *section = get_section(section_name, (_flags & AutoCreateSections) != 0);
if (section == NULL)
return;
ConfigEntry* entry = get_entry_in_section("!include", section->name, true);
entry->value = include;
_dirty = true;
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::Private::add_include_dir(const std::string §ion_name, const std::string &include)
{
ConfigSection *section = get_section(section_name, (_flags & AutoCreateSections) != 0);
if (section == NULL)
return;
ConfigEntry* entry = get_entry_in_section("!includedir", section->name, true);
entry->value = include;
_dirty = true;
}
//--------------------------------------------------------------------------------------------------
std::vector<std::string> ConfigurationFile::Private::get_includes(const std::string §ion_name)
{
std::vector<std::string> result;
ConfigSection *section = get_section(section_name, (_flags & AutoCreateSections) != 0);
if (section != NULL)
{
for (EntryListIterator iterator = section->keys.begin(); iterator != section->keys.end(); iterator++)
{
if (is_include(*iterator))
result.push_back(iterator->value);
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* Sets the value for the given key in the given section. If the section does not exist yet and
* the AutoCreateSections flag is set then it is created first. Similarly for the key (and the
* AutoCreateKeys flag).
* Returns true if the value could be set, false otherwise.
*/
bool ConfigurationFile::Private::set_value(std::string key_name, std::string value,
std::string section_name)
{
ConfigEntry* entry = get_entry_in_section(key_name, section_name, (_flags & AutoCreateKeys) != 0);
if (entry == NULL)
return false;
entry->value = base::trim(value);
_dirty = true;
return true;
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::Private::set_dirty()
{
_dirty = true;
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::Private::clear()
{
_dirty = false;
_trailing_comments = "";
_sections.clear();
_sections.push_back(ConfigSection()); // Make sure we always have a default section.
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::Private::delete_section(std::string name)
{
name = base::trim(name);
if (name.empty()) // Don't remove the default section.
return false;
for (SectionListIterator iterator = _sections.begin(); iterator != _sections.end(); iterator++)
{
if (strcasecmp((*iterator).name.c_str(), name.c_str()) == 0)
{
_sections.erase(iterator);
return true;
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
/**
* Removes the given key from the given selection. Returns true if successful, otherwise false.
*/
bool ConfigurationFile::Private::delete_key(std::string key, std::string section_name)
{
ConfigSection* section = get_section(section_name, false);
if (section == NULL)
return false;
key = base::trim(key);
for (EntryListIterator iterator = section->keys.begin(); iterator != section->keys.end(); iterator++)
{
if (strcasecmp(iterator->key.c_str(), key.c_str()) == 0)
{
section->keys.erase(iterator);
return true;
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
/**
* Given a key, a value and a section, this function will attempt to locate the
* Key within the given section, and if it finds it, change the keys value to
* the new value. If it does not locate the key, it will create a new key with
* the proper value and place it in the section requested.
*/
bool ConfigurationFile::Private::create_key(std::string key_name, std::string value,
std::string pre_comment, std::string post_comment, std::string section_name)
{
ConfigEntry* entry = get_entry_in_section(key_name, section_name, true);
if (entry == NULL)
return false;
entry->value = base::trim(value);
entry->pre_comment = pre_comment;
entry->post_comment = post_comment;
_dirty = true;
return true;
}
//--------------------------------------------------------------------------------------------------
/**
* Creates a new section if no section with that name exists already. Returns true if a new section
* has been created, otherwise false.
*/
bool ConfigurationFile::Private::create_section(std::string section_name, std::string comment)
{
// TODO: this should be necessary.
if (get_section(section_name, false) != NULL)
return false;
ConfigSection section;
section.name = base::trim(section_name);
section.comment = comment;
_sections.push_back(section);
_dirty = true;
return true;
}
//--------------------------------------------------------------------------------------------------
/**
* Returns the number of sections.
*/
int ConfigurationFile::Private::section_count()
{
return (int)_sections.size();
}
//--------------------------------------------------------------------------------------------------
/**
* Returns the total number of keys in all sections.
*/
int ConfigurationFile::Private::key_count()
{
int result = 0;
for (SectionListIterator iterator = _sections.begin(); iterator != _sections.end(); iterator++)
result += (int)iterator->keys.size();
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* Returns the number of keys in the given section. Returns 0 if the section could not be found.
*/
int ConfigurationFile::Private::key_count_for_section(const std::string §ion_name)
{
ConfigSection *section = get_section(section_name, false);
if (section != NULL)
return (int)section->keys.size();
return 0;
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::Private::is_dirty()
{
return _dirty;
}
//--------------------------------------------------------------------------------------------------
/**
* Converts the given text to a comment string (prefixing it with the comment character) if it is
* not empty and does not yet have a comment character at the first position.
*/
std::string ConfigurationFile::Private::make_comment(const std::string &text)
{
if (text.size() == 0)
return text;
if (text[0] != '#' && text[0] != ';')
return "# " + text;
return text;
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::Private::load(const std::string& file_name)
{
ifstream file(file_name.c_str());
if (file.is_open())
{
bool done = false;
std::string line;
std::string comment;
ConfigSection* section = get_section("", true); // Start with the default section which must always be there.
while (!done)
{
std::getline(file, line);
line = base::trim(line);
done = file.eof() || file.bad() || file.fail();
// Skip empty lines.
if (line.size() == 0)
continue;
switch (line[0])
{
case '#':
case ';':
if (!comment.empty())
comment += "\n";
comment += line;
break;
case '[':
{
// Start a new section.
size_t closing_bracket_position = line.rfind(']');
if (closing_bracket_position != std::string::npos)
line.erase(closing_bracket_position, 1);
else
log_warning("Unterminated section specifier found in file %s: %s\n", file_name.c_str(), line.c_str());
line.erase(0, 1);
section = get_section(line, true);
section->comment = comment;
comment = "";
}
break;
case '!':
{
std::string post_comment;
std::string::size_type comment_position = line.find('#'); // For post comments only # is allowed.
if (comment_position != std::string::npos)
{
post_comment = line.substr(comment_position, line.size());
line = line.substr(0, comment_position - 1);
}
std::vector<std::string> parts = base::split(line, " ");
if (parts.size() == 2)
{
std::string key = base::tolower(parts[0]);
if (key == "!include" || key == "!includedir")
{
ConfigEntry* entry = get_entry_in_section(key, section->name, true);
entry->value = parts[1];
entry->pre_comment = comment;
entry->post_comment = post_comment;
comment = "";
}
else
log_warning("Invalid include sequence found in file %s: %s\n", file_name.c_str(), line.c_str());
}
else
log_warning("Invalid include sequence found in file %s: %s\n", file_name.c_str(), line.c_str());
}
break;
default:
if (line.size() > 0)
{
// Normal key/value pair or a single key.
std::string post_comment;
std::string::size_type comment_position = line.find('#'); // For post comments only # is allowed.
if (comment_position != std::string::npos)
{
post_comment = line.substr(comment_position, line.size());
line = line.substr(0, comment_position - 1);
}
std::string key = extract_next_word(line);
std::string value = line;
if (key.size() > 0)
{
ConfigEntry* entry = get_entry_in_section(key, section->name, true);
entry->value = base::trim(value);
entry->pre_comment = comment;
entry->post_comment = post_comment;
comment = "";
}
}
break;
}
}
_trailing_comments = comment;
}
else
return false;
file.close();
return true;
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::Private::save(const std::string& file_name)
{
if (file_name.size() == 0)
return false;
fstream output(file_name.c_str(), ios::out | ios::trunc);
if (output.is_open())
{
SectionListIterator section_iterator;
EntryListIterator entry_iterator;
ConfigEntry entry;
bool empty = true; // No output written yet.
// We removed all empty lines on load, so add one for each pre-comment, section comment or section entry
// (except for the very first entry).
for (section_iterator = _sections.begin(); section_iterator != _sections.end(); section_iterator++)
{
if (!empty)
output << '\n';
if (!section_iterator->comment.empty())
{
output << make_comment(section_iterator->comment) << '\n';
empty = false;
}
if (!section_iterator->name.empty())
{
output << "[" << section_iterator->name.c_str() << "]\n";
empty = false;
}
for (entry_iterator = section_iterator->keys.begin(); entry_iterator != section_iterator->keys.end();
entry_iterator++)
{
if (!entry_iterator->pre_comment.empty())
{
if (!empty)
output << '\n';
output << make_comment(entry_iterator->pre_comment) << '\n';
}
empty = false;
if (!entry_iterator->key.empty())
{
if (entry_iterator->key.find("!include") == 0) // Include entry?
output << entry_iterator->key << ' ' << entry_iterator->value;
else
{
if (!entry_iterator->value.empty())
output << entry_iterator->key << ' ' << EqualIndicators[0] << ' ' << entry_iterator->value;
else
output << entry_iterator->key;
}
}
if (!entry_iterator->post_comment.empty())
output << ' ' << make_comment(entry_iterator->post_comment);
output << '\n';
}
}
if (!_trailing_comments.empty())
{
if (!empty)
output << '\n';
output << make_comment(_trailing_comments) << '\n';
}
}
else
return false;
_dirty = false;
output.flush();
output.close();
return true;
}
//----------------- ConfigurationFile --------------------------------------------------------------
ConfigurationFile::ConfigurationFile(std::string file_name, ConfigFileFlags flags)
{
data = new Private(file_name, flags);
}
//--------------------------------------------------------------------------------------------------
ConfigurationFile::ConfigurationFile(ConfigFileFlags flags)
{
data = new Private("", flags);
}
//--------------------------------------------------------------------------------------------------
ConfigurationFile::~ConfigurationFile()
{
delete data;
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::clear_includes(const std::string §ion_name)
{
data->clear_includes(section_name);
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::add_include(const std::string §ion_name, const std::string &include)
{
data->add_include(section_name, include);
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::add_include_dir(const std::string §ion_name, const std::string &include)
{
data->add_include_dir(section_name, include);
}
//--------------------------------------------------------------------------------------------------
std::vector<std::string> ConfigurationFile::get_includes(const std::string §ion_name)
{
return data->get_includes(section_name);
}
//--------------------------------------------------------------------------------------------------
void ConfigurationFile::clear()
{
data->clear();
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::is_dirty()
{
return data->is_dirty();
}
//--------------------------------------------------------------------------------------------------
/**
* Checks if the a key in a given section exists.
*/
bool ConfigurationFile::has_key(const std::string& key, const std::string& section)
{
return data->get_entry_in_section(key, section, false) != NULL;
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::has_section(const std::string& section_name)
{
return data->get_section(section_name, false) != NULL;
}
//--------------------------------------------------------------------------------------------------
/**
* Loads the given file and adds its content to the current content. Comments will be retained.
*/
bool ConfigurationFile::load(const std::string& file_name)
{
return data->load(file_name);
}
//--------------------------------------------------------------------------------------------------
/**
* Saves the content to the given file.
*/
bool ConfigurationFile::save(const std::string& file_name)
{
return data->save(file_name);
}
//--------------------------------------------------------------------------------------------------
/**
* Sets the comment for a given entry. Returns true if the entry was found, otherwise false.
*/
bool ConfigurationFile::set_key_pre_comment(std::string key, std::string comment, std::string section_name)
{
ConfigEntry* entry = data->get_entry_in_section(key, section_name, (data->_flags & AutoCreateKeys) != 0);
if (entry == NULL)
return false;
data->_dirty = true;
entry->pre_comment = comment;
return true;
}
//--------------------------------------------------------------------------------------------------
/**
* Sets the comment for a given section. Returns true if the section was found, otherwise false.
*/
bool ConfigurationFile::set_section_comment(std::string section_name, std::string comment)
{
ConfigSection* section = data->get_section(section_name, (data->_flags & AutoCreateSections) != 0);
if (section == NULL)
return false;
data->set_dirty();
section->comment = comment;
return true;
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::set_value(std::string key_name, std::string value, std::string section_name)
{
return data->set_value(key_name, value, section_name);
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::set_float(std::string key, float value, std::string section)
{
char buffer[64];
snprintf(buffer, 64, "%f", value);
return data->set_value(key, buffer, section);
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::set_int(std::string key, int value, std::string section)
{
char buffer[64];
snprintf(buffer, 64, "%d", value);
return data->set_value(key, buffer, section);
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::set_bool(std::string key, bool value, std::string section)
{
return data->set_value(key, value ? "True" : "False", section);
}
//--------------------------------------------------------------------------------------------------
/**
* Returns the value of the given key as string. An empty string is returned if the key could not be
* found or is empty.
*/
std::string ConfigurationFile::get_value(std::string key, std::string section)
{
ConfigEntry* entry = data->get_entry_in_section(key, section, false);
return (entry == NULL) ? "" : entry->value;
}
//--------------------------------------------------------------------------------------------------
double ConfigurationFile::get_float(std::string key, std::string section)
{
std::string value = base::unquote_identifier(get_value(key, section));
if (value.size() == 0)
return FLT_MIN;
// This conversion handles invalid entries with more than one letter at the end
// (those are simply ignored). The right size computation is done in both cases.
// There's no error handling in case of an overflow.
double factor = 1;
switch (::tolower(value[value.size() - 1]))
{
// All cases fall through.
case 'g':
factor *= 1024;
case 'm':
factor *= 1024;
case 'k':
factor *= 1024;
value[value.size() - 1] = 0;
}
return factor * base::atof<float>(value, 0.0);
}
//--------------------------------------------------------------------------------------------------
int ConfigurationFile::get_int(std::string key, std::string section)
{
std::string value = base::unquote_identifier(get_value(key, section));
if (value.size() == 0)
return INT_MIN;
// Same as for get_float().
// Note: for int overflow can happen much quicker with G suffix or large values.
int factor = 1;
switch (::tolower(value[value.size() - 1]))
{
// All cases fall through.
case 'g':
factor *= 1024;
case 'm':
factor *= 1024;
case 'k':
factor *= 1024;
value[value.size() - 1] = 0;
}
return factor * base::atoi<int>(value, 0);
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::get_bool(std::string key, std::string section)
{
std::string value = base::tolower(base::unquote_identifier(get_value(key, section)));
if (value == "true" || value == "yes")
return true;
if (base::atoi<int>(value, 0) != 0)
return true;
return false;
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::delete_section(std::string name)
{
return data->delete_section(name);
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::delete_key(std::string key, std::string section_name)
{
return data->delete_key(key, section_name);
}
//--------------------------------------------------------------------------------------------------
bool ConfigurationFile::create_key(std::string key, std::string value, std::string pre_comment,
std::string post_comment, std::string section)
{
return data->create_key(key, value, pre_comment, post_comment, section);
}
//--------------------------------------------------------------------------------------------------
/**
* Creates a new section if no section with that name exists already. Returns true if a new section
* has been created, otherwise false.
*/
bool ConfigurationFile::create_section(std::string section_name, std::string comment)
{
return data->create_section(section_name, comment);
}
//--------------------------------------------------------------------------------------------------
int ConfigurationFile::section_count()
{
return data->section_count();
}
//--------------------------------------------------------------------------------------------------
int ConfigurationFile::key_count()
{
return data->key_count();
}
//--------------------------------------------------------------------------------------------------
int ConfigurationFile::key_count_for_secton(const std::string §ion_name)
{
return data->key_count_for_section(section_name);
}
//--------------------------------------------------------------------------------------------------
|