|
| 1 | +// Read an INI file into easy-to-access name/value pairs. |
| 2 | + |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +// Copyright (C) 2009-2019, Ben Hoyt |
| 6 | + |
| 7 | +// inih and INIReader are released under the New BSD license (see LICENSE.txt). |
| 8 | +// Go to the project home page for more info: |
| 9 | +// |
| 10 | +// https://github.com/benhoyt/inih |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <cctype> |
| 14 | +#include <cstdlib> |
| 15 | +#include "ini.h" |
| 16 | +#include "INIReader.h" |
| 17 | + |
| 18 | +using std::string; |
| 19 | + |
| 20 | +INIReader::INIReader(const string& filename) |
| 21 | +{ |
| 22 | + _error = ini_parse(filename.c_str(), ValueHandler, this); |
| 23 | +} |
| 24 | + |
| 25 | +int INIReader::ParseError() const |
| 26 | +{ |
| 27 | + return _error; |
| 28 | +} |
| 29 | + |
| 30 | +string INIReader::Get(const string& section, const string& name, const string& default_value) const |
| 31 | +{ |
| 32 | + string key = MakeKey(section, name); |
| 33 | + // Use _values.find() here instead of _values.at() to support pre C++11 compilers |
| 34 | + return _values.count(key) ? _values.find(key)->second : default_value; |
| 35 | +} |
| 36 | + |
| 37 | +string INIReader::GetString(const string& section, const string& name, const string& default_value) const |
| 38 | +{ |
| 39 | + const string str = Get(section, name, ""); |
| 40 | + return str.empty() ? default_value : str; |
| 41 | +} |
| 42 | + |
| 43 | +long INIReader::GetInteger(const string& section, const string& name, long default_value) const |
| 44 | +{ |
| 45 | + string valstr = Get(section, name, ""); |
| 46 | + const char* value = valstr.c_str(); |
| 47 | + char* end; |
| 48 | + // This parses "1234" (decimal) and also "0x4D2" (hex) |
| 49 | + long n = strtol(value, &end, 0); |
| 50 | + return end > value ? n : default_value; |
| 51 | +} |
| 52 | + |
| 53 | +double INIReader::GetReal(const string& section, const string& name, double default_value) const |
| 54 | +{ |
| 55 | + string valstr = Get(section, name, ""); |
| 56 | + const char* value = valstr.c_str(); |
| 57 | + char* end; |
| 58 | + double n = strtod(value, &end); |
| 59 | + return end > value ? n : default_value; |
| 60 | +} |
| 61 | + |
| 62 | +bool INIReader::GetBoolean(const string& section, const string& name, bool default_value) const |
| 63 | +{ |
| 64 | + string valstr = Get(section, name, ""); |
| 65 | + // Convert to lower case to make string comparisons case-insensitive |
| 66 | + std::transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower); |
| 67 | + if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1") |
| 68 | + return true; |
| 69 | + else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0") |
| 70 | + return false; |
| 71 | + else |
| 72 | + return default_value; |
| 73 | +} |
| 74 | + |
| 75 | +bool INIReader::HasSection(const string& section) const |
| 76 | +{ |
| 77 | + const string key = MakeKey(section, ""); |
| 78 | + std::map<string, string>::const_iterator pos = _values.lower_bound(key); |
| 79 | + if (pos == _values.end()) |
| 80 | + return false; |
| 81 | + // Does the key at the lower_bound pos start with "section"? |
| 82 | + return pos->first.compare(0, key.length(), key) == 0; |
| 83 | +} |
| 84 | + |
| 85 | +bool INIReader::HasValue(const string& section, const string& name) const |
| 86 | +{ |
| 87 | + string key = MakeKey(section, name); |
| 88 | + return _values.count(key); |
| 89 | +} |
| 90 | + |
| 91 | +string INIReader::MakeKey(const string& section, const string& name) |
| 92 | +{ |
| 93 | + string key = section + "=" + name; |
| 94 | + // Convert to lower case to make section/name lookups case-insensitive |
| 95 | + std::transform(key.begin(), key.end(), key.begin(), ::tolower); |
| 96 | + return key; |
| 97 | +} |
| 98 | + |
| 99 | +int INIReader::ValueHandler(void* user, const char* section, const char* name, |
| 100 | + const char* value) |
| 101 | +{ |
| 102 | + INIReader* reader = static_cast<INIReader*>(user); |
| 103 | + string key = MakeKey(section, name); |
| 104 | + if (reader->_values[key].size() > 0) |
| 105 | + reader->_values[key] += "\n"; |
| 106 | + reader->_values[key] += value; |
| 107 | + return 1; |
| 108 | +} |
0 commit comments