Skip to content

Commit

Permalink
COMMON: added support for ini files with non english characters
Browse files Browse the repository at this point in the history
  • Loading branch information
voltya committed Aug 19, 2019
1 parent 073d833 commit 62a1a11
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
39 changes: 23 additions & 16 deletions common/ini-file.cpp
Expand Up @@ -28,14 +28,17 @@

namespace Common {

bool INIFile::isValidName(const String &name) {
bool INIFile::isValidName(const String &name, bool allowNonEnglishCharacters) {
if (allowNonEnglishCharacters)
return true;
const char *p = name.c_str();
while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.' || *p == ' '))
p++;
return *p == 0;
}

INIFile::INIFile() {
_allowNonEnglishCharacters = false;
}

INIFile::~INIFile() {
Expand Down Expand Up @@ -108,7 +111,7 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) {
// is, verify that it only consists of alphanumerics,
// periods, dashes and underscores). Mohawk Living Books games
// can have periods in their section names.
while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.' || *p == ' '))
while (*p && ((_allowNonEnglishCharacters && *p != ']') || isAlnum(*p) || *p == '-' || *p == '_' || *p == '.' || *p == ' '))
p++;

if (*p == '\0')
Expand All @@ -125,7 +128,7 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) {
section.comment = comment;
comment.clear();

assert(isValidName(section.name));
assert(isValidName(section.name, _allowNonEnglishCharacters));

This comment has been minimized.

Copy link
@bluegr

bluegr Aug 19, 2019

Member

Why are you passing this as a parameter? This variable is already a global of the INIFile class, and isValidName() is a function in that class (same for all the other calls below)

} else {
// This line should be a line with a 'key=value' pair, or an empty one.

Expand Down Expand Up @@ -160,7 +163,7 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) {
kv.comment = comment;
comment.clear();

assert(isValidName(kv.key));
assert(isValidName(kv.key, _allowNonEnglishCharacters));

section.keys.push_back(kv);
}
Expand Down Expand Up @@ -237,7 +240,7 @@ void INIFile::addSection(const String &section) {
}

void INIFile::removeSection(const String &section) {
assert(isValidName(section));
assert(isValidName(section, _allowNonEnglishCharacters));
for (List<Section>::iterator i = _sections.begin(); i != _sections.end(); ++i) {
if (section.equalsIgnoreCase(i->name)) {
_sections.erase(i);
Expand All @@ -247,14 +250,14 @@ void INIFile::removeSection(const String &section) {
}

bool INIFile::hasSection(const String &section) const {
assert(isValidName(section));
assert(isValidName(section, _allowNonEnglishCharacters));
const Section *s = getSection(section);
return s != nullptr;
}

void INIFile::renameSection(const String &oldName, const String &newName) {
assert(isValidName(oldName));
assert(isValidName(newName));
assert(isValidName(oldName, _allowNonEnglishCharacters));
assert(isValidName(newName, _allowNonEnglishCharacters));

Section *os = getSection(oldName);
const Section *ns = getSection(newName);
Expand All @@ -275,8 +278,8 @@ void INIFile::renameSection(const String &oldName, const String &newName) {


bool INIFile::hasKey(const String &key, const String &section) const {
assert(isValidName(key));
assert(isValidName(section));
assert(isValidName(key, _allowNonEnglishCharacters));
assert(isValidName(section, _allowNonEnglishCharacters));

const Section *s = getSection(section);
if (!s)
Expand All @@ -285,17 +288,17 @@ bool INIFile::hasKey(const String &key, const String &section) const {
}

void INIFile::removeKey(const String &key, const String &section) {
assert(isValidName(key));
assert(isValidName(section));
assert(isValidName(key, _allowNonEnglishCharacters));
assert(isValidName(section, _allowNonEnglishCharacters));

Section *s = getSection(section);
if (s)
s->removeKey(key);
}

bool INIFile::getKey(const String &key, const String &section, String &value) const {
assert(isValidName(key));
assert(isValidName(section));
assert(isValidName(key, _allowNonEnglishCharacters));
assert(isValidName(section, _allowNonEnglishCharacters));

const Section *s = getSection(section);
if (!s)
Expand All @@ -308,8 +311,8 @@ bool INIFile::getKey(const String &key, const String &section, String &value) co
}

void INIFile::setKey(const String &key, const String &section, const String &value) {
assert(isValidName(key));
assert(isValidName(section));
assert(isValidName(key, _allowNonEnglishCharacters));
assert(isValidName(section, _allowNonEnglishCharacters));
// TODO: Verify that value is valid, too. In particular, it shouldn't
// contain CR or LF...

Expand Down Expand Up @@ -389,4 +392,8 @@ void INIFile::Section::removeKey(const String &key) {
}
}

void INIFile::allowNonEnglishCharacters() {
_allowNonEnglishCharacters = true;
}

} // End of namespace Common
5 changes: 4 additions & 1 deletion common/ini-file.h
Expand Up @@ -88,7 +88,7 @@ class INIFile {
* underscores. In particular, white space and "#", "=", "[", "]"
* are not valid!
*/
static bool isValidName(const String &name);
static bool isValidName(const String &name, bool allowNonEnglishCharacters);

/** Reset everything stored in this ini file. */
void clear();
Expand All @@ -115,8 +115,11 @@ class INIFile {

void listKeyValues(StringMap &kv);

void allowNonEnglishCharacters();

private:
SectionList _sections;
bool _allowNonEnglishCharacters;

Section *getSection(const String &section);
const Section *getSection(const String &section) const;
Expand Down

0 comments on commit 62a1a11

Please sign in to comment.