summaryrefslogtreecommitdiff
path: root/Source/Core/Common/IniFile.cpp
diff options
context:
space:
mode:
authorMatthew Parlane <parlane@gmail.com>2014-02-08 18:50:37 +1300
committerMatthew Parlane <parlane@gmail.com>2014-02-13 17:06:30 +1300
commit3fe05e0a9f6908c1cf9a3caad6bff1d8decf04a3 (patch)
tree129603623669b47f44d89e83437e4f36d194c06e /Source/Core/Common/IniFile.cpp
parent079b1ba93deb8f3585f9211cfd1f73ccf16ae2a7 (diff)
Fix IniFile to use string& instead of char*
Also removes .c_str() usages where found.
Diffstat (limited to 'Source/Core/Common/IniFile.cpp')
-rw-r--r--Source/Core/Common/IniFile.cpp122
1 files changed, 62 insertions, 60 deletions
diff --git a/Source/Core/Common/IniFile.cpp b/Source/Core/Common/IniFile.cpp
index a1a10ab372..b07fb09224 100644
--- a/Source/Core/Common/IniFile.cpp
+++ b/Source/Core/Common/IniFile.cpp
@@ -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())
@@ -49,7 +51,7 @@ 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);
@@ -57,7 +59,7 @@ void IniFile::Section::Set(const char* key, const std::string& newValue, const s
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);
@@ -65,7 +67,7 @@ void IniFile::Section::Set(const char* key, const float newValue, const float de
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);
@@ -73,7 +75,7 @@ void IniFile::Section::Set(const char* key, int newValue, int defaultValue)
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);
@@ -81,7 +83,7 @@ void IniFile::Section::Set(const char* key, bool newValue, bool defaultValue)
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 ,
@@ -92,10 +94,10 @@ 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())
@@ -103,7 +105,7 @@ bool IniFile::Section::Get(const char* key, std::string* value, const char* defa
*value = it->second;
return true;
}
- else if (defaultValue)
+ else if (&defaultValue != &NULL_STRING)
{
*value = defaultValue;
return true;
@@ -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;
@@ -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())
@@ -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)
@@ -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)
@@ -249,7 +251,7 @@ 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)
@@ -257,13 +259,13 @@ bool IniFile::Exists(const char* sectionName, const char* key) const
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)
@@ -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)
@@ -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)
@@ -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;
@@ -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
@@ -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);
}
}
}
@@ -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);
@@ -422,20 +424,7 @@ 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)
@@ -443,7 +432,7 @@ bool IniFile::Get(const char *sectionName, const char* key, std::vector<std::str
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) {
@@ -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) {
@@ -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) {
@@ -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.
/*