Some improvements to priority handling

Fixes issues with some strings that caused issues previously
For example: 4,,1 is now handled correctly
This commit is contained in:
Sude 2015-08-29 16:18:20 +03:00
parent a8e09a7f48
commit 887ac52563
3 changed files with 25 additions and 8 deletions

View File

@ -44,6 +44,7 @@ namespace Util
std::string getHomeDir(); std::string getHomeDir();
std::string getConfigHome(); std::string getConfigHome();
std::string getCacheHome(); std::string getCacheHome();
std::vector<std::string> tokenize(const std::string& str, const std::string& separator = ",");
} }
#endif // UTIL_H #endif // UTIL_H

View File

@ -29,14 +29,11 @@ template<typename T> void set_vm_value(std::map<std::string, bpo::variable_value
// Parse the priority string, making it an array of numeric codes, and override the ORed type if required // Parse the priority string, making it an array of numeric codes, and override the ORed type if required
void handle_priority(const std::string &what, const std::string &priority_string, std::vector<unsigned int> &priority, unsigned int &type) void handle_priority(const std::string &what, const std::string &priority_string, std::vector<unsigned int> &priority, unsigned int &type)
{ {
size_t idx = 0, found; std::vector<std::string> tokens = Util::tokenize(priority_string, ",");
for (std::vector<std::string>::iterator it = tokens.begin(); it != tokens.end(); it++)
while ((found = priority_string.find(',', idx)) != std::string::npos) {
{ priority.push_back(std::stoi(*it));
priority.push_back(std::stoi(priority_string.substr(idx, found - idx))); }
idx = found + 1;
}
priority.push_back(std::stoi(priority_string.substr(idx)));
unsigned int wanted = 0; unsigned int wanted = 0;
#ifdef DEBUG #ifdef DEBUG

View File

@ -405,3 +405,22 @@ std::string Util::getCacheHome()
cacheHome = Util::getHomeDir() + "/.cache"; cacheHome = Util::getHomeDir() + "/.cache";
return cacheHome; return cacheHome;
} }
std::vector<std::string> Util::tokenize(const std::string& str, const std::string& separator)
{
std::vector<std::string> tokens;
std::string token;
size_t idx = 0, found;
while ((found = str.find(separator, idx)) != std::string::npos)
{
token = str.substr(idx, found - idx);
if (!token.empty())
tokens.push_back(token);
idx = found + separator.length();
}
token = str.substr(idx);
if (!token.empty())
tokens.push_back(token);
return tokens;
}