COMMON: Handle invalid INI file characters gracefully
This replaces all the asserts with warning messages
This commit is contained in:
parent
5baa02c750
commit
1dd915ccdf
2 changed files with 67 additions and 24 deletions
|
@ -28,19 +28,13 @@
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
bool INIFile::isValidName(const String &name) {
|
bool INIFile::isValidName(const String &name) const {
|
||||||
const char *p = name.c_str();
|
const char *p = name.c_str();
|
||||||
while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.' || *p == ' '))
|
while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.' || *p == ' '))
|
||||||
p++;
|
p++;
|
||||||
return *p == 0;
|
return *p == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
INIFile::INIFile() {
|
|
||||||
}
|
|
||||||
|
|
||||||
INIFile::~INIFile() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void INIFile::clear() {
|
void INIFile::clear() {
|
||||||
_sections.clear();
|
_sections.clear();
|
||||||
}
|
}
|
||||||
|
@ -125,7 +119,10 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) {
|
||||||
section.comment = comment;
|
section.comment = comment;
|
||||||
comment.clear();
|
comment.clear();
|
||||||
|
|
||||||
assert(isValidName(section.name));
|
if (!isValidName(section.name)) {
|
||||||
|
warning("Invalid section name: %s", section.name.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// This line should be a line with a 'key=value' pair, or an empty one.
|
// This line should be a line with a 'key=value' pair, or an empty one.
|
||||||
|
|
||||||
|
@ -163,7 +160,10 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) {
|
||||||
kv.comment = comment;
|
kv.comment = comment;
|
||||||
comment.clear();
|
comment.clear();
|
||||||
|
|
||||||
assert(isValidName(kv.key));
|
if (!isValidName(kv.key)) {
|
||||||
|
warning("Invalid key name: %s", kv.key.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
section.keys.push_back(kv);
|
section.keys.push_back(kv);
|
||||||
}
|
}
|
||||||
|
@ -240,7 +240,11 @@ void INIFile::addSection(const String §ion) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void INIFile::removeSection(const String §ion) {
|
void INIFile::removeSection(const String §ion) {
|
||||||
assert(isValidName(section));
|
if (!isValidName(section)) {
|
||||||
|
warning("Invalid section name: %s", section.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (List<Section>::iterator i = _sections.begin(); i != _sections.end(); ++i) {
|
for (List<Section>::iterator i = _sections.begin(); i != _sections.end(); ++i) {
|
||||||
if (section.equalsIgnoreCase(i->name)) {
|
if (section.equalsIgnoreCase(i->name)) {
|
||||||
_sections.erase(i);
|
_sections.erase(i);
|
||||||
|
@ -250,14 +254,25 @@ void INIFile::removeSection(const String §ion) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool INIFile::hasSection(const String §ion) const {
|
bool INIFile::hasSection(const String §ion) const {
|
||||||
assert(isValidName(section));
|
if (!isValidName(section)) {
|
||||||
|
warning("Invalid section name: %s", section.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const Section *s = getSection(section);
|
const Section *s = getSection(section);
|
||||||
return s != nullptr;
|
return s != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void INIFile::renameSection(const String &oldName, const String &newName) {
|
void INIFile::renameSection(const String &oldName, const String &newName) {
|
||||||
assert(isValidName(oldName));
|
if (!isValidName(oldName)) {
|
||||||
assert(isValidName(newName));
|
warning("Invalid section name: %s", oldName.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidName(newName)) {
|
||||||
|
warning("Invalid section name: %s", newName.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Section *os = getSection(oldName);
|
Section *os = getSection(oldName);
|
||||||
const Section *ns = getSection(newName);
|
const Section *ns = getSection(newName);
|
||||||
|
@ -278,8 +293,15 @@ void INIFile::renameSection(const String &oldName, const String &newName) {
|
||||||
|
|
||||||
|
|
||||||
bool INIFile::hasKey(const String &key, const String §ion) const {
|
bool INIFile::hasKey(const String &key, const String §ion) const {
|
||||||
assert(isValidName(key));
|
if (!isValidName(key)) {
|
||||||
assert(isValidName(section));
|
warning("Invalid key name: %s", key.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidName(section)) {
|
||||||
|
warning("Invalid section name: %s", section.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const Section *s = getSection(section);
|
const Section *s = getSection(section);
|
||||||
if (!s)
|
if (!s)
|
||||||
|
@ -288,8 +310,15 @@ bool INIFile::hasKey(const String &key, const String §ion) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void INIFile::removeKey(const String &key, const String §ion) {
|
void INIFile::removeKey(const String &key, const String §ion) {
|
||||||
assert(isValidName(key));
|
if (!isValidName(key)) {
|
||||||
assert(isValidName(section));
|
warning("Invalid key name: %s", key.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidName(section)) {
|
||||||
|
warning("Invalid section name: %s", section.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Section *s = getSection(section);
|
Section *s = getSection(section);
|
||||||
if (s)
|
if (s)
|
||||||
|
@ -297,9 +326,15 @@ void INIFile::removeKey(const String &key, const String §ion) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool INIFile::getKey(const String &key, const String §ion, String &value) const {
|
bool INIFile::getKey(const String &key, const String §ion, String &value) const {
|
||||||
assert(isValidName(key));
|
if (!isValidName(key)) {
|
||||||
assert(isValidName(section));
|
warning("Invalid key name: %s", key.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidName(section)) {
|
||||||
|
warning("Invalid section name: %s", section.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
const Section *s = getSection(section);
|
const Section *s = getSection(section);
|
||||||
if (!s)
|
if (!s)
|
||||||
return false;
|
return false;
|
||||||
|
@ -311,8 +346,16 @@ bool INIFile::getKey(const String &key, const String §ion, String &value) co
|
||||||
}
|
}
|
||||||
|
|
||||||
void INIFile::setKey(const String &key, const String §ion, const String &value) {
|
void INIFile::setKey(const String &key, const String §ion, const String &value) {
|
||||||
assert(isValidName(key));
|
if (!isValidName(key)) {
|
||||||
assert(isValidName(section));
|
warning("Invalid key name: %s", key.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidName(section)) {
|
||||||
|
warning("Invalid section name: %s", section.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Verify that value is valid, too. In particular, it shouldn't
|
// TODO: Verify that value is valid, too. In particular, it shouldn't
|
||||||
// contain CR or LF...
|
// contain CR or LF...
|
||||||
|
|
||||||
|
|
|
@ -77,8 +77,8 @@ public:
|
||||||
typedef List<Section> SectionList;
|
typedef List<Section> SectionList;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
INIFile();
|
INIFile() {}
|
||||||
~INIFile();
|
~INIFile() {}
|
||||||
|
|
||||||
// TODO: Maybe add a copy constructor etc.?
|
// TODO: Maybe add a copy constructor etc.?
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ public:
|
||||||
* underscores. In particular, white space and "#", "=", "[", "]"
|
* underscores. In particular, white space and "#", "=", "[", "]"
|
||||||
* are not valid!
|
* are not valid!
|
||||||
*/
|
*/
|
||||||
static bool isValidName(const String &name);
|
bool isValidName(const String &name) const;
|
||||||
|
|
||||||
/** Reset everything stored in this ini file. */
|
/** Reset everything stored in this ini file. */
|
||||||
void clear();
|
void clear();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue