diff options
| author | John Peterson <jpeterson57@gmail.com> | 2009-01-28 16:09:08 +0000 |
|---|---|---|
| committer | John Peterson <jpeterson57@gmail.com> | 2009-01-28 16:09:08 +0000 |
| commit | 946fb127523d726767a3025327b1ead4d998873d (patch) | |
| tree | b6e9518ba89a39f406069a7129639448ed84de4a /Source/Core/Common/Src/IniFile.cpp | |
| parent | 7bc798b7f1be94407f50560bef2623c2701e0063 (diff) | |
Wiimote: Partial work on movement recording (not working yet)
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2021 8ced0084-cf51-0410-be5f-012b33b47a6e
Diffstat (limited to 'Source/Core/Common/Src/IniFile.cpp')
| -rw-r--r-- | Source/Core/Common/Src/IniFile.cpp | 277 |
1 files changed, 151 insertions, 126 deletions
diff --git a/Source/Core/Common/Src/IniFile.cpp b/Source/Core/Common/Src/IniFile.cpp index 4c5b1e2ba7..957af38925 100644 --- a/Source/Core/Common/Src/IniFile.cpp +++ b/Source/Core/Common/Src/IniFile.cpp @@ -16,6 +16,9 @@ // http://code.google.com/p/dolphin-emu/ // see IniFile.h +////////////////////////////////////////////////////////////////////////////////////// +// Include +// ------------ #include <stdlib.h> #include <stdio.h> @@ -27,15 +30,24 @@ #include "StringUtil.h" #include "IniFile.h" +/////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////// +// Class +// ------------ IniFile::IniFile() {} IniFile::~IniFile() {} +/////////////////////////////// +////////////////////////////////////////////////////////////////////////////////////// +// Section functions used locally only +// ------------ Section::Section() : lines(), name(""), comment("") {} @@ -79,6 +91,7 @@ Section* IniFile::GetOrCreateSection(const char* sectionName) return(section); } +/////////////////////////////// bool IniFile::DeleteSection(const char* sectionName) @@ -158,44 +171,6 @@ std::string* IniFile::GetLine(Section* section, const char* key, std::string* va return 0; } - -void IniFile::Set(const char* sectionName, const char* key, const char* newValue) -{ - Section* section = GetOrCreateSection(sectionName); - std::string value, comment; - std::string* line = GetLine(section, key, &value, &comment); - - if (line) - { - // Change the value - keep the key and comment - *line = StripSpaces(key) + " = " + newValue + comment; - } - else - { - // The key did not already exist in this section - let's add it. - section->lines.push_back(std::string(key) + " = " + newValue); - } -} - - -void IniFile::Set(const char* sectionName, const char* key, u32 newValue) -{ - Set(sectionName, key, StringFromFormat("0x%08x", newValue).c_str()); -} - - -void IniFile::Set(const char* sectionName, const char* key, int newValue) -{ - Set(sectionName, key, StringFromInt(newValue).c_str()); -} - - -void IniFile::Set(const char* sectionName, const char* key, bool newValue) -{ - Set(sectionName, key, StringFromBool(newValue).c_str()); -} - - void IniFile::SetLines(const char* sectionName, const std::vector<std::string> &lines) { Section* section = GetOrCreateSection(sectionName); @@ -206,111 +181,101 @@ void IniFile::SetLines(const char* sectionName, const std::vector<std::string> & section->lines.push_back(*iter); } } -bool IniFile::Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue) + + +bool IniFile::DeleteKey(const char* sectionName, const char* key) { Section* section = GetSection(sectionName); if (!section) { - if (defaultValue) - { - *value = defaultValue; - } - return false; } - std::string* line = GetLine(section, key, value, 0); + std::string* line = GetLine(section, key, 0, 0); - if (!line) + for (std::vector<std::string>::iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter) { - if (defaultValue) + if (line == &(*liter)) { - *value = defaultValue; + section->lines.erase(liter); + return true; } - - return false; } - return true; -} - - -bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue) -{ - std::string temp; - bool retval = Get(sectionName, key, &temp, 0); - - if (retval && TryParseInt(temp.c_str(), value)) - { - return true; - } - - *value = defaultValue; - return false; + return false; //shouldn't happen } -bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defaultValue) +bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) const { - std::string temp; - bool retval = Get(sectionName, key, &temp, 0); + const Section* section = GetSection(sectionName); - if (retval && TryParseUInt(temp.c_str(), value)) + if (!section) { - return true; + return false; } - *value = defaultValue; - return false; -} - - -bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool defaultValue) -{ - std::string temp; - bool retval = Get(sectionName, key, &temp, 0); + keys.clear(); - if (retval && TryParseBool(temp.c_str(), value)) + for (std::vector<std::string>::const_iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter) { - return true; + std::string key; + ParseLine(*liter, &key, 0, 0); + keys.push_back(key); } - *value = defaultValue; - return false; + return true; } -bool IniFile::DeleteKey(const char* sectionName, const char* key) +bool IniFile::GetLines(const char* sectionName, std::vector<std::string>& lines) const { - Section* section = GetSection(sectionName); - + const Section* section = GetSection(sectionName); if (!section) - { return false; - } - - std::string* line = GetLine(section, key, 0, 0); - for (std::vector<std::string>::iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter) + lines.clear(); + for (std::vector<std::string>::const_iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter) { - if (line == &(*liter)) + std::string line = StripSpaces(*iter); + int commentPos = (int)line.find('#'); + if (commentPos == 0) { - section->lines.erase(liter); - return true; + continue; + } + + if (commentPos != (int)std::string::npos) + { + line = StripSpaces(line.substr(0, commentPos)); } + + lines.push_back(line); } - return false; //shouldn't happen + return true; +} + + +void IniFile::SortSections() +{ + std::sort(sections.begin(), sections.end()); } +////////////////////////////////////////////////////////////////////////////////////// +// Load and save file +// ------------ bool IniFile::Load(const char* filename) { + // Maximum number of letters in a line + static const int MAX_BYTES = 1024*10; + sections.clear(); sections.push_back(Section("")); //first section consists of the comments before the first real section + // Open file std::ifstream in; in.open(filename, std::ios::in); @@ -321,8 +286,8 @@ bool IniFile::Load(const char* filename) while (!in.eof()) { - char templine[512]; - in.getline(templine, 512); + char templine[MAX_BYTES]; + in.getline(templine, MAX_BYTES); std::string line = templine; if (in.eof()) @@ -359,7 +324,6 @@ bool IniFile::Load(const char* filename) return true; } - bool IniFile::Save(const char* filename) { std::ofstream out; @@ -389,64 +353,125 @@ bool IniFile::Save(const char* filename) out.close(); return true; } +////////////////////////////////////////// -bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) const +////////////////////////////////////////////////////////////////////////////////////// +// Get and set elements +// ------------ +void IniFile::Set(const char* sectionName, const char* key, const char* newValue) { - const Section* section = GetSection(sectionName); + Section* section = GetOrCreateSection(sectionName); + std::string value, comment; + std::string* line = GetLine(section, key, &value, &comment); - if (!section) + if (line) { - return false; + // Change the value - keep the key and comment + *line = StripSpaces(key) + " = " + newValue + comment; } - - keys.clear(); - - for (std::vector<std::string>::const_iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter) + else { - std::string key; - ParseLine(*liter, &key, 0, 0); - keys.push_back(key); + // The key did not already exist in this section - let's add it. + section->lines.push_back(std::string(key) + " = " + newValue); } +} - return true; + +void IniFile::Set(const char* sectionName, const char* key, u32 newValue) +{ + Set(sectionName, key, StringFromFormat("0x%08x", newValue).c_str()); } -bool IniFile::GetLines(const char* sectionName, std::vector<std::string>& lines) const +void IniFile::Set(const char* sectionName, const char* key, int newValue) { - const Section* section = GetSection(sectionName); - if (!section) - return false; + Set(sectionName, key, StringFromInt(newValue).c_str()); +} - lines.clear(); - for (std::vector<std::string>::const_iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter) + +void IniFile::Set(const char* sectionName, const char* key, bool newValue) +{ + Set(sectionName, key, StringFromBool(newValue).c_str()); +} + +bool IniFile::Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue) +{ + Section* section = GetSection(sectionName); + + if (!section) { - std::string line = StripSpaces(*iter); - int commentPos = (int)line.find('#'); - if (commentPos == 0) + if (defaultValue) { - continue; + *value = defaultValue; } - if (commentPos != (int)std::string::npos) + return false; + } + + std::string* line = GetLine(section, key, value, 0); + + if (!line) + { + if (defaultValue) { - line = StripSpaces(line.substr(0, commentPos)); + *value = defaultValue; } - lines.push_back(line); + return false; } return true; } -void IniFile::SortSections() +bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue) { - std::sort(sections.begin(), sections.end()); + std::string temp; + bool retval = Get(sectionName, key, &temp, 0); + + if (retval && TryParseInt(temp.c_str(), value)) + { + return true; + } + + *value = defaultValue; + return false; } +bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defaultValue) +{ + std::string temp; + bool retval = Get(sectionName, key, &temp, 0); + + if (retval && TryParseUInt(temp.c_str(), value)) + { + return true; + } + + *value = defaultValue; + return false; +} + + +bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool defaultValue) +{ + std::string temp; + bool retval = Get(sectionName, key, &temp, 0); + + if (retval && TryParseBool(temp.c_str(), value)) + { + return true; + } + + *value = defaultValue; + return false; +} +//////////////////////////////////////// + + + /* int main() { |
