Fix IniFile to use string& instead of char*

Also removes .c_str() usages where found.
This commit is contained in:
Matthew Parlane
2014-02-08 18:50:37 +13:00
parent 079b1ba93d
commit 3fe05e0a9f
17 changed files with 205 additions and 218 deletions

View File

@ -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))
return true;
*value = defaultValue;
return false;
}
bool IniFile::Section::Get(const char* 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, u32* value, u32 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, 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, double* value, double 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::Exists(const char *key) const
bool IniFile::Section::Get(const std::string& key, double* value, double defaultValue)
{
std::string temp;
bool retval = Get(key, &temp);
if (retval && TryParse(temp, value))
return true;
*value = defaultValue;
return false;
}
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.
/*

View File

@ -32,45 +32,42 @@ public:
Section() {}
Section(const std::string& _name) : name(_name) {}
bool Exists(const char *key) const;
bool Delete(const char *key);
bool Exists(const std::string& key) const;
bool Delete(const std::string& key);
void Set(const char* key, const char* newValue);
void Set(const char* key, const std::string& newValue, const std::string& defaultValue);
void Set(const std::string& key, const std::string& newValue);
void Set(const std::string& key, const std::string& newValue, const std::string& defaultValue);
void Set(const std::string &key, const std::string &value) {
Set(key.c_str(), value.c_str());
}
bool Get(const char* key, std::string* value, const char* defaultValue);
bool Get(const std::string& key, std::string* value, const std::string& defaultValue = NULL_STRING);
void Set(const char* key, u32 newValue) {
Set(key, StringFromFormat("0x%08x", newValue).c_str());
void Set(const std::string& key, u32 newValue) {
Set(key, StringFromFormat("0x%08x", newValue));
}
void Set(const char* key, float newValue) {
Set(key, StringFromFormat("%f", newValue).c_str());
void Set(const std::string& key, float newValue) {
Set(key, StringFromFormat("%f", newValue));
}
void Set(const char* key, const float newValue, const float defaultValue);
void Set(const char* key, double newValue) {
Set(key, StringFromFormat("%f", newValue).c_str());
void Set(const std::string& key, const float newValue, const float defaultValue);
void Set(const std::string& key, double newValue) {
Set(key, StringFromFormat("%f", newValue));
}
void Set(const char* key, int newValue, int defaultValue);
void Set(const char* key, int newValue) {
Set(key, StringFromInt(newValue).c_str());
void Set(const std::string& key, int newValue, int defaultValue);
void Set(const std::string& key, int newValue) {
Set(key, StringFromInt(newValue));
}
void Set(const char* key, bool newValue, bool defaultValue);
void Set(const char* key, bool newValue) {
Set(key, StringFromBool(newValue).c_str());
void Set(const std::string& key, bool newValue, bool defaultValue);
void Set(const std::string& key, bool newValue) {
Set(key, StringFromBool(newValue));
}
void Set(const char* key, const std::vector<std::string>& newValues);
void Set(const std::string& key, const std::vector<std::string>& newValues);
bool Get(const char* key, int* value, int defaultValue = 0);
bool Get(const char* key, u32* value, u32 defaultValue = 0);
bool Get(const char* key, bool* value, bool defaultValue = false);
bool Get(const char* key, float* value, float defaultValue = false);
bool Get(const char* key, double* value, double defaultValue = false);
bool Get(const char* key, std::vector<std::string>& values);
bool Get(const std::string& key, int* value, int defaultValue = 0);
bool Get(const std::string& key, u32* value, u32 defaultValue = 0);
bool Get(const std::string& key, bool* value, bool defaultValue = false);
bool Get(const std::string& key, float* value, float defaultValue = false);
bool Get(const std::string& key, double* value, double defaultValue = false);
bool Get(const std::string& key, std::vector<std::string>& values);
bool operator < (const Section& other) const {
return name < other.name;
@ -92,72 +89,65 @@ public:
* @warning Using any other operations than "Get*" and "Exists" is untested and will behave unexpectedly
* @todo This really is just a hack to support having two levels of gameinis (defaults and user-specified) and should eventually be replaced with a less stupid system.
*/
bool Load(const char* filename, bool keep_current_data = false);
bool Load(const std::string &filename, bool keep_current_data = false) { return Load(filename.c_str(), keep_current_data); }
bool Load(const std::string& filename, bool keep_current_data = false);
bool Save(const char* filename);
bool Save(const std::string &filename) { return Save(filename.c_str()); }
bool Save(const std::string& filename);
// Returns true if key exists in section
bool Exists(const char* sectionName, const char* key) const;
bool Exists(const std::string& sectionName, const std::string& key) const;
// TODO: Get rid of these, in favor of the Section ones.
void Set(const char* sectionName, const char* key, const char* newValue) {
void Set(const std::string& sectionName, const std::string& key, const std::string& newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, const std::string& newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue.c_str());
}
void Set(const char* sectionName, const char* key, int newValue) {
void Set(const std::string& sectionName, const std::string& key, int newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, u32 newValue) {
void Set(const std::string& sectionName, const std::string& key, u32 newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, bool newValue) {
void Set(const std::string& sectionName, const std::string& key, bool newValue) {
GetOrCreateSection(sectionName)->Set(key, newValue);
}
void Set(const char* sectionName, const char* key, const std::vector<std::string>& newValues) {
void Set(const std::string& sectionName, const std::string& key, const std::vector<std::string>& newValues) {
GetOrCreateSection(sectionName)->Set(key, newValues);
}
// TODO: Get rid of these, in favor of the Section ones.
bool Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue = "");
bool Get(const char* sectionName, const char* key, int* value, int defaultValue = 0);
bool Get(const char* sectionName, const char* key, u32* value, u32 defaultValue = 0);
bool Get(const char* sectionName, const char* key, bool* value, bool defaultValue = false);
bool Get(const char* sectionName, const char* key, std::vector<std::string>& values);
bool Get(const std::string& sectionName, const std::string& key, int* value, int defaultValue = 0);
bool Get(const std::string& sectionName, const std::string& key, u32* value, u32 defaultValue = 0);
bool Get(const std::string& sectionName, const std::string& key, bool* value, bool defaultValue = false);
bool Get(const std::string& sectionName, const std::string& key, std::vector<std::string>& values);
bool Get(const std::string& sectionName, const std::string& key, std::string* value, const std::string& defaultValue = NULL_STRING);
template<typename T> bool GetIfExists(const char* sectionName, const char* key, T value)
template<typename T> bool GetIfExists(const std::string& sectionName, const std::string& key, T value)
{
if (Exists(sectionName, key))
return Get(sectionName, key, value);
return false;
}
bool GetKeys(const char* sectionName, std::vector<std::string>& keys) const;
bool GetKeys(const std::string& sectionName, std::vector<std::string>& keys) const;
void SetLines(const char* sectionName, const std::vector<std::string> &lines);
bool GetLines(const char* sectionName, std::vector<std::string>& lines, const bool remove_comments = true) const;
void SetLines(const std::string& sectionName, const std::vector<std::string> &lines);
bool GetLines(const std::string& sectionName, std::vector<std::string>& lines, const bool remove_comments = true) const;
inline bool DeleteKey(const char* sectionName, const std::string& key)
{
return DeleteKey(sectionName, key.c_str());
}
bool DeleteKey(const char* sectionName, const char* key);
bool DeleteSection(const char* sectionName);
bool DeleteKey(const std::string& sectionName, const std::string& key);
bool DeleteSection(const std::string& sectionName);
void SortSections();
Section* GetOrCreateSection(const char* section);
Section* GetOrCreateSection(const std::string& section);
private:
std::vector<Section> sections;
const Section* GetSection(const char* section) const;
Section* GetSection(const char* section);
std::string* GetLine(const char* section, const char* key);
void CreateSection(const char* section);
const Section* GetSection(const std::string& section) const;
Section* GetSection(const std::string& section);
std::string* GetLine(const std::string& section, const std::string& key);
void CreateSection(const std::string& section);
static const std::string& NULL_STRING;
};
#endif // _INIFILE_H_

View File

@ -28,7 +28,7 @@ const u8* SettingsHandler::GetData() const
return m_buffer;
}
const std::string SettingsHandler::GetValue(const std::string key)
const std::string SettingsHandler::GetValue(const std::string& key)
{
std::string delim = std::string("\r\n");
std::string toFind = delim + key + "=";
@ -79,20 +79,16 @@ void SettingsHandler::Reset()
memset(m_buffer, 0, SETTINGS_SIZE);
}
void SettingsHandler::AddSetting(const char *key, const char *value)
void SettingsHandler::AddSetting(const std::string& key, const std::string& value)
{
while (*key != 0)
{
WriteByte(*key);
key++;
for(const char& c : key) {
WriteByte(c);
}
WriteByte('=');
while (*value != 0)
{
WriteByte(*value);
value++;
for(const char& c : value) {
WriteByte(c);
}
WriteByte(13);

View File

@ -21,15 +21,11 @@ public:
// Key used to encrypt/decrypt setting.txt contents
INITIAL_SEED = 0x73B5DBFA
};
inline void AddSetting(const char *key, const std::string& value)
{
AddSetting(key, value.c_str());
}
void AddSetting(const char *key, const char *value);
void AddSetting(const std::string& key, const std::string& value);
const u8 *GetData() const;
const std::string GetValue(const std::string key);
const std::string GetValue(const std::string& key);
void Decrypt();
void Reset();