Skip to content

Commit

Permalink
Merge pull request #50 from Parlane/inifile_tidy
Browse files Browse the repository at this point in the history
Fix IniFile to use string& instead of char*
  • Loading branch information
Parlane committed Feb 13, 2014
2 parents ec0d717 + 3fe05e0 commit 88526be
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 211 deletions.
122 changes: 62 additions & 60 deletions Source/Core/Common/IniFile.cpp
Expand Up @@ -37,7 +37,9 @@ void ParseLine(const std::string& line, std::string* keyOut, std::string* valueO

}

void IniFile::Section::Set(const char* key, const char* newValue)
const std::string& IniFile::NULL_STRING = "";

void IniFile::Section::Set(const std::string& key, const std::string& newValue)
{
auto it = values.find(key);
if (it != values.end())
Expand All @@ -49,39 +51,39 @@ void IniFile::Section::Set(const char* key, const char* newValue)
}
}

void IniFile::Section::Set(const char* key, const std::string& newValue, const std::string& defaultValue)
void IniFile::Section::Set(const std::string& key, const std::string& newValue, const std::string& defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void IniFile::Section::Set(const char* key, const float newValue, const float defaultValue)
void IniFile::Section::Set(const std::string& key, const float newValue, const float defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void IniFile::Section::Set(const char* key, int newValue, int defaultValue)
void IniFile::Section::Set(const std::string& key, int newValue, int defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void IniFile::Section::Set(const char* key, bool newValue, bool defaultValue)
void IniFile::Section::Set(const std::string& key, bool newValue, bool defaultValue)
{
if (newValue != defaultValue)
Set(key, newValue);
else
Delete(key);
}

void IniFile::Section::Set(const char* key, const std::vector<std::string>& newValues)
void IniFile::Section::Set(const std::string& key, const std::vector<std::string>& newValues)
{
std::string temp;
// Join the strings with ,
Expand All @@ -92,18 +94,18 @@ void IniFile::Section::Set(const char* key, const std::vector<std::string>& newV
}
// remove last ,
temp.resize(temp.length() - 1);
Set(key, temp.c_str());
Set(key, temp);
}

bool IniFile::Section::Get(const char* key, std::string* value, const char* defaultValue)
bool IniFile::Section::Get(const std::string& key, std::string* value, const std::string& defaultValue)
{
auto it = values.find(key);
if (it != values.end())
{
*value = it->second;
return true;
}
else if (defaultValue)
else if (&defaultValue != &NULL_STRING)
{
*value = defaultValue;
return true;
Expand All @@ -112,10 +114,10 @@ bool IniFile::Section::Get(const char* key, std::string* value, const char* defa
return false;
}

bool IniFile::Section::Get(const char* key, std::vector<std::string>& out)
bool IniFile::Section::Get(const std::string& key, std::vector<std::string>& out)
{
std::string temp;
bool retval = Get(key, &temp, 0);
bool retval = Get(key, &temp);
if (!retval || temp.empty())
{
return false;
Expand All @@ -138,62 +140,62 @@ bool IniFile::Section::Get(const char* key, std::vector<std::string>& out)
return true;
}

bool IniFile::Section::Get(const char* key, int* value, int defaultValue)
bool IniFile::Section::Get(const std::string& key, int* value, int defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp.c_str(), value))
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}

bool IniFile::Section::Get(const char* key, u32* value, u32 defaultValue)
bool IniFile::Section::Get(const std::string& key, u32* value, u32 defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}

bool IniFile::Section::Get(const char* key, bool* value, bool defaultValue)
bool IniFile::Section::Get(const std::string& key, bool* value, bool defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp.c_str(), value))
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}

bool IniFile::Section::Get(const char* key, float* value, float defaultValue)
bool IniFile::Section::Get(const std::string& key, float* value, float defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp.c_str(), value))
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}

bool IniFile::Section::Get(const char* key, double* value, double defaultValue)
bool IniFile::Section::Get(const std::string& key, double* value, double defaultValue)
{
std::string temp;
bool retval = Get(key, &temp, 0);
if (retval && TryParse(temp.c_str(), value))
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}

bool IniFile::Section::Exists(const char *key) const
bool IniFile::Section::Exists(const std::string& key) const
{
return values.find(key) != values.end();
}

bool IniFile::Section::Delete(const char *key)
bool IniFile::Section::Delete(const std::string& key)
{
auto it = values.find(key);
if (it == values.end())
Expand All @@ -206,23 +208,23 @@ bool IniFile::Section::Delete(const char *key)

// IniFile

const IniFile::Section* IniFile::GetSection(const char* sectionName) const
const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const
{
for (const auto& sect : sections)
if (!strcasecmp(sect.name.c_str(), sectionName))
if (!strcasecmp(sect.name.c_str(), sectionName.c_str()))
return (&(sect));
return 0;
}

IniFile::Section* IniFile::GetSection(const char* sectionName)
IniFile::Section* IniFile::GetSection(const std::string& sectionName)
{
for (auto& sect : sections)
if (!strcasecmp(sect.name.c_str(), sectionName))
if (!strcasecmp(sect.name.c_str(), sectionName.c_str()))
return (&(sect));
return 0;
}

IniFile::Section* IniFile::GetOrCreateSection(const char* sectionName)
IniFile::Section* IniFile::GetOrCreateSection(const std::string& sectionName)
{
Section* section = GetSection(sectionName);
if (!section)
Expand All @@ -233,7 +235,7 @@ IniFile::Section* IniFile::GetOrCreateSection(const char* sectionName)
return section;
}

bool IniFile::DeleteSection(const char* sectionName)
bool IniFile::DeleteSection(const std::string& sectionName)
{
Section* s = GetSection(sectionName);
if (!s)
Expand All @@ -249,21 +251,21 @@ bool IniFile::DeleteSection(const char* sectionName)
return false;
}

bool IniFile::Exists(const char* sectionName, const char* key) const
bool IniFile::Exists(const std::string& sectionName, const std::string& key) const
{
const Section* section = GetSection(sectionName);
if (!section)
return false;
return section->Exists(key);
}

void IniFile::SetLines(const char* sectionName, const std::vector<std::string> &lines)
void IniFile::SetLines(const std::string& sectionName, const std::vector<std::string> &lines)
{
Section* section = GetOrCreateSection(sectionName);
section->lines = lines;
}

bool IniFile::DeleteKey(const char* sectionName, const char* key)
bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key)
{
Section* section = GetSection(sectionName);
if (!section)
Expand All @@ -272,7 +274,7 @@ bool IniFile::DeleteKey(const char* sectionName, const char* key)
}

// Return a list of all keys in a section
bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) const
bool IniFile::GetKeys(const std::string& sectionName, std::vector<std::string>& keys) const
{
const Section* section = GetSection(sectionName);
if (!section)
Expand All @@ -282,7 +284,7 @@ bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) c
}

// Return a list of all lines in a section
bool IniFile::GetLines(const char* sectionName, std::vector<std::string>& lines, const bool remove_comments) const
bool IniFile::GetLines(const std::string& sectionName, std::vector<std::string>& lines, const bool remove_comments) const
{
const Section* section = GetSection(sectionName);
if (!section)
Expand Down Expand Up @@ -319,7 +321,7 @@ void IniFile::SortSections()
std::sort(sections.begin(), sections.end());
}

bool IniFile::Load(const char* filename, bool keep_current_data)
bool IniFile::Load(const std::string& filename, bool keep_current_data)
{
// Maximum number of letters in a line
static const int MAX_BYTES = 1024*32;
Expand Down Expand Up @@ -359,7 +361,7 @@ bool IniFile::Load(const char* filename, bool keep_current_data)
{
// New section!
std::string sub = line.substr(1, endpos - 1);
current_section = GetOrCreateSection(sub.c_str());
current_section = GetOrCreateSection(sub);
}
}
else
Expand All @@ -374,9 +376,9 @@ bool IniFile::Load(const char* filename, bool keep_current_data)
// INI is a hack anyway.
if ((key == "" && value == "")
|| (line.size() >= 1 && (line[0] == '$' || line[0] == '+' || line[0] == '*')))
current_section->lines.push_back(line.c_str());
current_section->lines.push_back(line);
else
current_section->Set(key, value.c_str());
current_section->Set(key, value);
}
}
}
Expand All @@ -386,7 +388,7 @@ bool IniFile::Load(const char* filename, bool keep_current_data)
return true;
}

bool IniFile::Save(const char* filename)
bool IniFile::Save(const std::string& filename)
{
std::ofstream out;
std::string temp = File::GetTempFilenameForAtomicWrite(filename);
Expand Down Expand Up @@ -422,28 +424,15 @@ bool IniFile::Save(const char* filename)
return File::RenameSync(temp, filename);
}


bool IniFile::Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue)
{
Section* section = GetSection(sectionName);
if (!section) {
if (defaultValue) {
*value = defaultValue;
}
return false;
}
return section->Get(key, value, defaultValue);
}

bool IniFile::Get(const char *sectionName, const char* key, std::vector<std::string>& values)
bool IniFile::Get(const std::string& sectionName, const std::string& key, std::vector<std::string>& values)
{
Section *section = GetSection(sectionName);
if (!section)
return false;
return section->Get(key, values);
}

bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue)
bool IniFile::Get(const std::string& sectionName, const std::string& key, int* value, int defaultValue)
{
Section *section = GetSection(sectionName);
if (!section) {
Expand All @@ -454,7 +443,7 @@ bool IniFile::Get(const char* sectionName, const char* key, int* value, int defa
}
}

bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defaultValue)
bool IniFile::Get(const std::string& sectionName, const std::string& key, u32* value, u32 defaultValue)
{
Section *section = GetSection(sectionName);
if (!section) {
Expand All @@ -465,7 +454,7 @@ bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defa
}
}

bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool defaultValue)
bool IniFile::Get(const std::string& sectionName, const std::string& key, bool* value, bool defaultValue)
{
Section *section = GetSection(sectionName);
if (!section) {
Expand All @@ -476,6 +465,19 @@ bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool de
}
}

bool IniFile::Get(const std::string& sectionName, const std::string& key, std::string* value, const std::string& defaultValue)
{
Section* section = GetSection(sectionName);
if (!section) {
if (&defaultValue != &NULL_STRING) {
*value = defaultValue;
}
return false;
}
return section->Get(key, value, defaultValue);
}



// Unit test. TODO: Move to the real unit test framework.
/*
Expand Down

0 comments on commit 88526be

Please sign in to comment.