From bec2a115d80f0086da8b6868ea0482b2fa7a3d68 Mon Sep 17 00:00:00 2001 From: nakeee Date: Wed, 25 Feb 2009 10:33:09 +0000 Subject: [PATCH] Added set and get in ini for comma seperated list Also merged the StripSpaces with StripNewline and cleaned it up a bit. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2433 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/Common/Src/FileUtil.cpp | 3 -- Source/Core/Common/Src/IniFile.cpp | 47 ++++++++++++++++++++ Source/Core/Common/Src/StringUtil.cpp | 63 +++++++++++++-------------- Source/Core/Common/Src/StringUtil.h | 1 - 4 files changed, 77 insertions(+), 37 deletions(-) diff --git a/Source/Core/Common/Src/FileUtil.cpp b/Source/Core/Common/Src/FileUtil.cpp index e849c56a15..e649dfaaf9 100644 --- a/Source/Core/Common/Src/FileUtil.cpp +++ b/Source/Core/Common/Src/FileUtil.cpp @@ -30,9 +30,6 @@ #include #include #include -#include -#include -#include #endif #include diff --git a/Source/Core/Common/Src/IniFile.cpp b/Source/Core/Common/Src/IniFile.cpp index 09a3aca415..1d4b112a5d 100644 --- a/Source/Core/Common/Src/IniFile.cpp +++ b/Source/Core/Common/Src/IniFile.cpp @@ -385,6 +385,22 @@ void IniFile::Set(const char* sectionName, const char* key, const char* newValue } } +void IniFile::Set(const char* sectionName, const char* key, const std::vector& newValues) +{ + std::string temp; + + // Join the strings with , + std::vector::const_iterator it; + for (it = newValues.begin(); it != newValues.end(); ++it) { + + temp = (*it) + ","; + } + + // remove last , + temp.resize(temp.length() - 1); + + Set(sectionName, key, temp.c_str()); +} void IniFile::Set(const char* sectionName, const char* key, u32 newValue) { @@ -433,6 +449,37 @@ bool IniFile::Get(const char* sectionName, const char* key, std::string* value, } +bool IniFile::Get(const char* sectionName, const char* key, std::vector& values) +{ + + std::string temp; + bool retval = Get(sectionName, key, &temp, 0); + + if (! retval || temp.empty()) { + return false; + } + + u32 subEnd; + + // ignore starting , if any + u32 subStart = temp.find_first_not_of(","); + + // split by , + while (subStart != std::string::npos) { + + // Find next , + subEnd = temp.find_first_of(",", subStart); + if (subStart != subEnd) + // take from first char until next , + values.push_back(StripSpaces(temp.substr(subStart, subEnd - subStart))); + + // Find the next non , char + subStart = temp.find_first_not_of(",", subEnd); + } + + return true; +} + bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue) { std::string temp; diff --git a/Source/Core/Common/Src/StringUtil.cpp b/Source/Core/Common/Src/StringUtil.cpp index edfbe3a282..e34bbbafcc 100644 --- a/Source/Core/Common/Src/StringUtil.cpp +++ b/Source/Core/Common/Src/StringUtil.cpp @@ -200,33 +200,25 @@ std::string ArrayToString(const u8 *data, u32 size, u32 offset, int line_len, bo // ================ -// Turns " hej " into "hej". Also handles tabs. +// Turns " hej " into "hej". Also handles tabs and newlines. std::string StripSpaces(const std::string &str) { - std::string s = str; - int i; - for (i = 0; i < (int)s.size(); i++) - { - if ((s[i] != ' ') && (s[i] != 9)) - { - break; - } - } + std::string temp = str; - s = s.substr(i); + // Find first non space char + u32 firstChar = str.find_first_not_of(" \t\n"); - for (i = (int)s.size() - 1; i > 0; i--) - { - if ((s[i] != ' ') && (s[i] != 9)) - { - break; - } - } + // Last char which is not whitespace + u32 lastChar = str.find_last_not_of(" \t\n"); - return s.substr(0, i + 1); + if (firstChar != std::string::npos) + temp.resize(firstChar, lastChar); + else + temp = ""; + + return temp; } - // "\"hello\"" is turned to "hello" // This one assumes that the string has already been space stripped in both // ends, as done by StripSpaces above, for example. @@ -238,19 +230,6 @@ std::string StripQuotes(const std::string& s) return s; } -// "\"hello\"" is turned to "hello" -// This one assumes that the string has already been space stripped in both -// ends, as done by StripSpaces above, for example. -std::string StripNewline(const std::string& s) -{ - if (!s.size()) - return s; - else if (s[s.size() - 1] == '\n') - return s.substr(0, s.size() - 1); - else - return s; -} - bool TryParseInt(const char* str, int* outVal) { const char* s = str; @@ -461,3 +440,21 @@ std::string ThS(int Integer, bool Unsigned) } +// Remove trailing whitespaces from begining and end of string +std::string Trim(const std::string& str) +{ + std::string temp = str; + + // Find first non space char + u32 firstChar = str.find_first_not_of(" \t\n"); + + // Last char which is not whitespace + u32 lastChar = str.find_last_not_of(" \t\n"); + + if (firstChar != std::string::npos) + temp.resize(firstChar, lastChar); + else + temp = ""; + + return temp; +} diff --git a/Source/Core/Common/Src/StringUtil.h b/Source/Core/Common/Src/StringUtil.h index 2ecb1e793f..511170df65 100644 --- a/Source/Core/Common/Src/StringUtil.h +++ b/Source/Core/Common/Src/StringUtil.h @@ -49,7 +49,6 @@ inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...) std::string StripSpaces(const std::string &s); std::string StripQuotes(const std::string &s); -std::string StripNewline(const std::string &s); std::string ThS(int a, bool b = true); // thousand separator